mirror of
https://gitlab.com/oeffi/public-transport-enabler.git
synced 2025-07-14 00:30:31 +00:00
exception for unexpected redirects
git-svn-id: https://public-transport-enabler.googlecode.com/svn/trunk@712 0924bc21-9374-b0fa-ee44-9ff1593b38f0
This commit is contained in:
parent
a2b5775b8f
commit
e60f3a936f
3 changed files with 70 additions and 7 deletions
|
@ -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);
|
||||
|
|
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -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)
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue