mirror of
https://gitlab.com/oeffi/public-transport-enabler.git
synced 2025-07-20 17:29:51 +00:00
Move test for expired session into ParserUtils
This commit is contained in:
parent
cbf19ab96e
commit
f268772820
4 changed files with 42 additions and 91 deletions
|
@ -17,11 +17,9 @@
|
|||
|
||||
package de.schildbach.pte;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Serializable;
|
||||
import java.nio.charset.Charset;
|
||||
import java.text.DateFormat;
|
||||
|
@ -71,7 +69,6 @@ import de.schildbach.pte.dto.Stop;
|
|||
import de.schildbach.pte.dto.Trip;
|
||||
import de.schildbach.pte.exception.InvalidDataException;
|
||||
import de.schildbach.pte.exception.ParserException;
|
||||
import de.schildbach.pte.exception.ProtocolException;
|
||||
import de.schildbach.pte.exception.SessionExpiredException;
|
||||
import de.schildbach.pte.util.ParserUtils;
|
||||
import de.schildbach.pte.util.XmlPullUtil;
|
||||
|
@ -2233,8 +2230,6 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider
|
|||
}
|
||||
}
|
||||
|
||||
private static final Pattern P_SESSION_EXPIRED = Pattern.compile("Your session has expired");
|
||||
|
||||
public QueryTripsResult queryMoreTrips(final QueryTripsContext contextObj, final boolean later) throws IOException
|
||||
{
|
||||
final Context context = (Context) contextObj;
|
||||
|
@ -2249,7 +2244,6 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider
|
|||
{
|
||||
is = ParserUtils.scrapeInputStream(uri.toString(), null, null, httpRefererTrip, "NSC_", 3);
|
||||
firstChars = ParserUtils.peekFirstChars(is);
|
||||
is.mark(512);
|
||||
|
||||
return queryTrips(uri.toString(), is);
|
||||
}
|
||||
|
@ -2261,18 +2255,6 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider
|
|||
{
|
||||
throw new SessionExpiredException();
|
||||
}
|
||||
catch (final ProtocolException x) // must be html content
|
||||
{
|
||||
is.reset();
|
||||
final BufferedReader reader = new BufferedReader(new InputStreamReader(is));
|
||||
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null)
|
||||
if (P_SESSION_EXPIRED.matcher(line).find())
|
||||
throw new SessionExpiredException();
|
||||
|
||||
throw x;
|
||||
}
|
||||
catch (final RuntimeException x)
|
||||
{
|
||||
throw new RuntimeException("uncategorized problem while processing " + uri, x);
|
||||
|
@ -2306,18 +2288,6 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider
|
|||
{
|
||||
throw new ParserException("cannot parse xml: " + firstChars, x);
|
||||
}
|
||||
catch (final ProtocolException x) // must be html content
|
||||
{
|
||||
is.reset();
|
||||
final BufferedReader reader = new BufferedReader(new InputStreamReader(is));
|
||||
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null)
|
||||
if (P_SESSION_EXPIRED.matcher(line).find())
|
||||
throw new SessionExpiredException();
|
||||
|
||||
throw x;
|
||||
}
|
||||
catch (final RuntimeException x)
|
||||
{
|
||||
throw new RuntimeException("uncategorized problem while processing " + uri, x);
|
||||
|
@ -3360,22 +3330,11 @@ public abstract class AbstractEfaProvider extends AbstractNetworkProvider
|
|||
if (pp.getEventType() != XmlPullParser.START_DOCUMENT)
|
||||
throw new IllegalStateException("start of document expected");
|
||||
|
||||
try
|
||||
{
|
||||
pp.next();
|
||||
}
|
||||
catch (final XmlPullParserException x)
|
||||
{
|
||||
if (x.getMessage().startsWith("Expected a quoted string"))
|
||||
throw new ProtocolException("html");
|
||||
}
|
||||
pp.next();
|
||||
|
||||
if (pp.getEventType() == XmlPullParser.DOCDECL)
|
||||
pp.next();
|
||||
|
||||
if (XmlPullUtil.test(pp, "html"))
|
||||
throw new ProtocolException("html");
|
||||
|
||||
XmlPullUtil.require(pp, "itdRequest");
|
||||
|
||||
final String serverVersion = XmlPullUtil.attr(pp, "version");
|
||||
|
|
|
@ -1,48 +0,0 @@
|
|||
/*
|
||||
* Copyright 2010-2014 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;
|
||||
|
||||
/**
|
||||
* @author Andreas Schildbach
|
||||
*/
|
||||
public class ProtocolException extends IOException
|
||||
{
|
||||
public ProtocolException()
|
||||
{
|
||||
super();
|
||||
}
|
||||
|
||||
public ProtocolException(final String message)
|
||||
{
|
||||
super(message);
|
||||
}
|
||||
|
||||
public ProtocolException(final String message, final Throwable cause)
|
||||
{
|
||||
super(message);
|
||||
super.initCause(cause);
|
||||
}
|
||||
|
||||
public ProtocolException(final Throwable cause)
|
||||
{
|
||||
super(cause == null ? null : cause.toString());
|
||||
super.initCause(cause);
|
||||
}
|
||||
}
|
|
@ -42,6 +42,7 @@ import java.util.zip.GZIPInputStream;
|
|||
|
||||
import de.schildbach.pte.exception.BlockedException;
|
||||
import de.schildbach.pte.exception.InternalErrorException;
|
||||
import de.schildbach.pte.exception.SessionExpiredException;
|
||||
import de.schildbach.pte.exception.UnexpectedRedirectException;
|
||||
|
||||
/**
|
||||
|
@ -160,10 +161,15 @@ public final class ParserUtils
|
|||
if (!url.getHost().equals(connection.getURL().getHost()))
|
||||
throw new UnexpectedRedirectException(url, connection.getURL());
|
||||
|
||||
final URL redirectUrl = testRedirect(url, peekFirstChars(is));
|
||||
final String firstChars = peekFirstChars(is);
|
||||
|
||||
final URL redirectUrl = testRedirect(url, firstChars);
|
||||
if (redirectUrl != null)
|
||||
throw new UnexpectedRedirectException(url, redirectUrl);
|
||||
|
||||
if (testExpired(firstChars))
|
||||
throw new SessionExpiredException();
|
||||
|
||||
if (sessionCookieName != null)
|
||||
{
|
||||
for (final Map.Entry<String, List<String>> entry : connection.getHeaderFields().entrySet())
|
||||
|
@ -280,6 +286,19 @@ public final class ParserUtils
|
|||
return null;
|
||||
}
|
||||
|
||||
private static final Pattern P_EXPIRED = Pattern
|
||||
.compile(">\\s*(Your session has expired\\.|Session Expired|Ihre Verbindungskennung ist nicht mehr g.ltig\\.)\\s*<");
|
||||
|
||||
public static boolean testExpired(final String content)
|
||||
{
|
||||
// check for expired session
|
||||
final Matcher mSessionExpired = P_EXPIRED.matcher(content);
|
||||
if (mSessionExpired.find())
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static final Pattern P_HTML_UNORDERED_LIST = Pattern.compile("<ul>(.*?)</ul>", Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
|
||||
private static final Pattern P_HTML_LIST_ITEM = Pattern.compile("<li>(.*?)</li>", Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
|
||||
private static final Pattern P_HTML_BREAKS = Pattern.compile("(<br\\s*/>)+", Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue