Move test for expired session into ParserUtils

This commit is contained in:
Andreas Schildbach 2014-06-17 18:29:25 +02:00
parent cbf19ab96e
commit f268772820
4 changed files with 42 additions and 91 deletions

View file

@ -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");

View file

@ -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);
}
}

View file

@ -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);

View file

@ -19,6 +19,7 @@ package de.schildbach.pte.util;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.net.URL;
@ -78,4 +79,24 @@ public class ParserUtilsTest
assertNotNull(url);
assertEquals("example.com", url.getHost());
}
@Test
public void efaExpired() throws Exception
{
assertTrue(ParserUtils
.testExpired("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"><html><head><meta http-equiv=\"Content-Type\" content=\"text/html; \"/><meta http-equiv=\"Expires\" content=\"0\"/><title>Efa9 Internal Error</title><style>.BOLD {font: bold large Arial;}.NORMAL {font: normal x-small Arial;}</style></head><body><div class=\"BOLD\">Internal Error</div><div class=\"NORMAL\">Your session has expired.</div><!--<p>&nbsp;</p><div class=\"NORMAL\">.\\EfaHttpServer.cpp</div><div class=\"NORMAL\">Line: 2043</div>--></body></html>"));
}
@Test
public void tflExpired() throws Exception
{
assertTrue(ParserUtils
.testExpired("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"><html><head><title>Session Expired</title><style type=\"text/css\">body{ font-family:Verdana, Arial, Helvetica, sans-serif}</style></head><body bgcolor=\"#FFFFFF\" leftmargin=\"0\" topmargin=\"0\" rightmargin=\"0\" bottommargin=\"0\" marginwidth=\"0\" marginheight=\"0\"><!--Logo--><table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\"><tr> <td width=\"100%\" height=\"40\" valign=\"top\" class=\"fenster\"><table width=\"389\" height=\"40\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\"><tr> <td width=\"93\" valign=\"top\"><span>&nbsp;</span></td><td width=\"296\" valign=\"top\"><img src=\"images/logo.gif\" alt=\"\" width=\"372\" height=\"86\" border=\"0\"></td></tr></table></td></tr></table><!--/ Logo--><!--Content--><span><!--Headline--><table cellspacing=\"0\" cellpadding=\"0\" border=\"0\"><tr><td width=\"18\" valign=\"top\"><span>&nbsp;</span></td><td width=\"566\" valign=\"top\"><span class=\"headline\"><b>Session Expire"));
}
@Test
public void nvbwExpired() throws Exception
{
assertTrue(ParserUtils.testExpired("<h2>Ihre Verbindungskennung ist nicht mehr gültig.</h2>"));
}
}