diff --git a/enabler/src/de/schildbach/pte/AbstractEfaProvider.java b/enabler/src/de/schildbach/pte/AbstractEfaProvider.java index 151fbbd0..32ab7123 100644 --- a/enabler/src/de/schildbach/pte/AbstractEfaProvider.java +++ b/enabler/src/de/schildbach/pte/AbstractEfaProvider.java @@ -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"); diff --git a/enabler/src/de/schildbach/pte/exception/ProtocolException.java b/enabler/src/de/schildbach/pte/exception/ProtocolException.java deleted file mode 100644 index df8fd2d5..00000000 --- a/enabler/src/de/schildbach/pte/exception/ProtocolException.java +++ /dev/null @@ -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 . - */ - -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); - } -} diff --git a/enabler/src/de/schildbach/pte/util/ParserUtils.java b/enabler/src/de/schildbach/pte/util/ParserUtils.java index 988ae9f5..044209b6 100644 --- a/enabler/src/de/schildbach/pte/util/ParserUtils.java +++ b/enabler/src/de/schildbach/pte/util/ParserUtils.java @@ -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> 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("", Pattern.DOTALL | Pattern.CASE_INSENSITIVE); private static final Pattern P_HTML_LIST_ITEM = Pattern.compile("
  • (.*?)
  • ", Pattern.DOTALL | Pattern.CASE_INSENSITIVE); private static final Pattern P_HTML_BREAKS = Pattern.compile("()+", Pattern.DOTALL | Pattern.CASE_INSENSITIVE); diff --git a/enabler/test/de/schildbach/pte/util/ParserUtilsTest.java b/enabler/test/de/schildbach/pte/util/ParserUtilsTest.java index b0cd70d9..ad9b43a8 100644 --- a/enabler/test/de/schildbach/pte/util/ParserUtilsTest.java +++ b/enabler/test/de/schildbach/pte/util/ParserUtilsTest.java @@ -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("Efa9 Internal Error
    Internal Error
    Your session has expired.
    ")); + } + + @Test + public void tflExpired() throws Exception + { + assertTrue(ParserUtils + .testExpired("Session Expired
     \"\"
     Session Expire")); + } + + @Test + public void nvbwExpired() throws Exception + { + assertTrue(ParserUtils.testExpired("

    Ihre Verbindungskennung ist nicht mehr gültig.

    ")); + } }