From e60f3a936f9d2254a4b03e17d9ca722f1e08c2d7 Mon Sep 17 00:00:00 2001 From: "andreas.schildbach@gmail.com" Date: Tue, 21 Jun 2011 10:27:43 +0000 Subject: [PATCH] exception for unexpected redirects git-svn-id: https://public-transport-enabler.googlecode.com/svn/trunk@712 0924bc21-9374-b0fa-ee44-9ff1593b38f0 --- src/de/schildbach/pte/BvgProvider.java | 7 +-- .../UnexpectedRedirectException.java | 54 +++++++++++++++++++ src/de/schildbach/pte/util/ParserUtils.java | 16 ++++-- 3 files changed, 70 insertions(+), 7 deletions(-) create mode 100644 src/de/schildbach/pte/exception/UnexpectedRedirectException.java diff --git a/src/de/schildbach/pte/BvgProvider.java b/src/de/schildbach/pte/BvgProvider.java index 7380a8ac..f515a26b 100644 --- a/src/de/schildbach/pte/BvgProvider.java +++ b/src/de/schildbach/pte/BvgProvider.java @@ -43,6 +43,7 @@ import de.schildbach.pte.dto.QueryDeparturesResult.Status; import de.schildbach.pte.dto.StationDepartures; import de.schildbach.pte.dto.Stop; import de.schildbach.pte.exception.SessionExpiredException; +import de.schildbach.pte.exception.UnexpectedRedirectException; import de.schildbach.pte.util.Color; import de.schildbach.pte.util.ParserUtils; @@ -264,7 +265,7 @@ public final class BvgProvider extends AbstractHafasProvider if (mError.group(2) != null) return new QueryDeparturesResult(Status.SERVICE_DOWN); if (mError.group(3) != null) - throw new IOException("connected to private wlan"); + throw new UnexpectedRedirectException(); } // parse page @@ -360,7 +361,7 @@ public final class BvgProvider extends AbstractHafasProvider if (mError.group(2) != null) return new QueryDeparturesResult(Status.SERVICE_DOWN); if (mError.group(3) != null) - throw new IOException("connected to private wlan"); + throw new UnexpectedRedirectException(); } // parse page @@ -667,7 +668,7 @@ public final class BvgProvider extends AbstractHafasProvider if (mError.group(5) != null) throw new SessionExpiredException(); if (mError.group(6) != null) - throw new IOException("connected to private wlan"); + throw new UnexpectedRedirectException(); } final Matcher mAllDetailsAction = P_CONNECTIONS_ALL_DETAILS.matcher(firstPage); diff --git a/src/de/schildbach/pte/exception/UnexpectedRedirectException.java b/src/de/schildbach/pte/exception/UnexpectedRedirectException.java new file mode 100644 index 00000000..a05ff113 --- /dev/null +++ b/src/de/schildbach/pte/exception/UnexpectedRedirectException.java @@ -0,0 +1,54 @@ +/* + * Copyright 2010, 2011 the original author or authors. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package de.schildbach.pte.exception; + +import java.io.IOException; +import java.net.URL; + +/** + * @author Andreas Schildbach + */ +public class UnexpectedRedirectException extends IOException +{ + private final URL originalUrl; + private final URL redirectedUrl; + + public UnexpectedRedirectException() + { + this.originalUrl = null; + this.redirectedUrl = null; + } + + public UnexpectedRedirectException(final URL originalUrl, final URL redirectedUrl) + { + super(originalUrl + " -> " + redirectedUrl); + + this.originalUrl = originalUrl; + this.redirectedUrl = redirectedUrl; + } + + public URL getUrl() + { + return originalUrl; + } + + public URL getRedirectedUrl() + { + return redirectedUrl; + } +} diff --git a/src/de/schildbach/pte/util/ParserUtils.java b/src/de/schildbach/pte/util/ParserUtils.java index ca887673..9c08a242 100644 --- a/src/de/schildbach/pte/util/ParserUtils.java +++ b/src/de/schildbach/pte/util/ParserUtils.java @@ -38,6 +38,8 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.zip.GZIPInputStream; +import de.schildbach.pte.exception.UnexpectedRedirectException; + /** * @author Andreas Schildbach */ @@ -69,7 +71,7 @@ public final class ParserUtils return scrape(url, isPost, request, encoding, sessionCookieName, 3); } - public static final CharSequence scrape(final String url, final boolean isPost, final String request, String encoding, + public static final CharSequence scrape(final String urlStr, final boolean isPost, final String request, String encoding, final String sessionCookieName, int tries) throws IOException { if (encoding == null) @@ -80,7 +82,8 @@ public final class ParserUtils try { final StringBuilder buffer = new StringBuilder(SCRAPE_INITIAL_CAPACITY); - final HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); + final URL url = new URL(urlStr); + final HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true); connection.setDoOutput(request != null); @@ -109,6 +112,8 @@ public final class ParserUtils } final Reader pageReader = new InputStreamReader(connection.getInputStream(), encoding); + if (!url.equals(connection.getURL())) + throw new UnexpectedRedirectException(url, connection.getURL()); copy(pageReader, buffer); pageReader.close(); @@ -170,12 +175,13 @@ public final class ParserUtils return scrapeInputStream(url, null, null, 3); } - public static final InputStream scrapeInputStream(final String url, final String postRequest, final String sessionCookieName, int tries) + public static final InputStream scrapeInputStream(final String urlStr, final String postRequest, final String sessionCookieName, int tries) throws IOException { while (true) { - final HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); + final URL url = new URL(urlStr); + final HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true); connection.setDoOutput(postRequest != null); @@ -205,6 +211,8 @@ public final class ParserUtils { final String contentEncoding = connection.getContentEncoding(); final InputStream is = connection.getInputStream(); + if (!url.equals(connection.getURL())) + throw new UnexpectedRedirectException(url, connection.getURL()); if (sessionCookieName != null) {