> entry : connection.getHeaderFields().entrySet())
- {
- if ("set-cookie".equalsIgnoreCase(entry.getKey()) || "set-cookie2".equalsIgnoreCase(entry.getKey()))
- {
- for (final String value : entry.getValue())
- {
- for (final HttpCookie cookie : HttpCookie.parse(value))
- {
- if (cookie.getName().equals(sessionCookieName))
- {
- sessionCookie = cookie;
- break c;
- }
- }
- }
- }
- }
- }
-
- return is;
- }
- else if (responseCode == HttpURLConnection.HTTP_BAD_REQUEST || responseCode == HttpURLConnection.HTTP_UNAUTHORIZED
- || responseCode == HttpURLConnection.HTTP_FORBIDDEN || responseCode == HttpURLConnection.HTTP_NOT_ACCEPTABLE
- || responseCode == HttpURLConnection.HTTP_UNAVAILABLE)
- {
- throw new BlockedException(url, new InputStreamReader(connection.getErrorStream(), requestEncoding));
- }
- else if (responseCode == HttpURLConnection.HTTP_NOT_FOUND)
- {
- throw new NotFoundException(url, new InputStreamReader(connection.getErrorStream(), requestEncoding));
- }
- else if (responseCode == HttpURLConnection.HTTP_MOVED_PERM || responseCode == HttpURLConnection.HTTP_MOVED_TEMP)
- {
- throw new UnexpectedRedirectException(url, connection.getURL());
- }
- else if (responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR)
- {
- throw new InternalErrorException(url, new InputStreamReader(connection.getErrorStream(), requestEncoding));
- }
- else
- {
- final String message = "got response: " + responseCode + " " + connection.getResponseMessage();
- if (tries-- > 0)
- log.info("{}, retrying...", message);
- else
- throw new IOException(message + ": " + url);
- }
- }
- }
-
- private static InputStream wrapGzip(final InputStream is) throws IOException
- {
- is.mark(2);
- final int byte0 = is.read();
- final int byte1 = is.read();
- is.reset();
-
- // check for gzip header
- if (byte0 == 0x1f && byte1 == 0x8b)
- {
- final BufferedInputStream is2 = new BufferedInputStream(new GZIPInputStream(is));
- is2.mark(2);
- final int byte0_2 = is2.read();
- final int byte1_2 = is2.read();
- is2.reset();
-
- // check for gzip header again
- if (byte0_2 == 0x1f && byte1_2 == 0x8b)
- {
- // double gzipped
- return new BufferedInputStream(new GZIPInputStream(is2));
- }
- else
- {
- // gzipped
- return is2;
- }
- }
- else
- {
- // uncompressed
- return is;
- }
- }
-
- public static String peekFirstChars(final InputStream is) throws IOException
- {
- is.mark(SCRAPE_PEEK_SIZE);
- final byte[] firstBytes = new byte[SCRAPE_PEEK_SIZE];
- final int read = is.read(firstBytes);
- if (read == -1)
- return "";
- is.reset();
- return new String(firstBytes, 0, read).replaceAll("\\p{C}", "");
- }
-
- private static final Pattern P_REDIRECT_HTTP_EQUIV = Pattern.compile(" \\s*(?:window.location|location.href)\\s*=\\s*\"([^\"]+)\"",
- Pattern.CASE_INSENSITIVE);
-
- public static URL testRedirect(final URL context, final String content) throws MalformedURLException
- {
- // check for redirect by http-equiv meta tag header
- final Matcher mHttpEquiv = P_REDIRECT_HTTP_EQUIV.matcher(content);
- if (mHttpEquiv.find())
- return new URL(context, mHttpEquiv.group(1));
-
- // check for redirect by window.location javascript
- final Matcher mScript = P_REDIRECT_SCRIPT.matcher(content);
- if (mScript.find())
- return new URL(context, mScript.group(1));
-
- 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_INTERNAL_ERROR = Pattern
- .compile(">\\s*(Internal Error|Server ein Fehler aufgetreten|Internal error in gateway|VRN - Keine Verbindung zum Server m.glich)\\s*<");
-
- public static boolean testInternalError(final String content)
- {
- // check for internal error
- final Matcher m = P_INTERNAL_ERROR.matcher(content);
- if (m.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/HttpClientTest.java
similarity index 94%
rename from enabler/test/de/schildbach/pte/util/ParserUtilsTest.java
rename to enabler/test/de/schildbach/pte/util/HttpClientTest.java
index 00203ea1..1d1584d2 100644
--- a/enabler/test/de/schildbach/pte/util/ParserUtilsTest.java
+++ b/enabler/test/de/schildbach/pte/util/HttpClientTest.java
@@ -29,7 +29,7 @@ import org.junit.Test;
/**
* @author Andreas Schildbach
*/
-public class ParserUtilsTest
+public class HttpClientTest
{
private URL context;
@@ -42,7 +42,7 @@ public class ParserUtilsTest
@Test
public void vodafoneRedirect() throws Exception
{
- final URL url = ParserUtils
+ final URL url = HttpClient
.testRedirect(
context,
"Vodafone Center Sie werden weitergeleitet ... Sollten Sie nicht weitergeleitet werden, klicken Sie bitte window.location = \"http://www.hotspot.kabeldeutschland.de/portal/?RequestedURI=http%3A%2F%2Fwww.fahrinfo-berlin.de%2FFahrinfo%2Fbin%2Fajax-getstop.bin%2Fdny%3Fgetstop%3D1%26REQ0JourneyStopsS0A%3D255%26REQ0JourneyStopsS0G%3Dgneisenustra%25DFe%3F%26js%3Dtrue&RedirectReason=Policy&RedirectAqpId=100&DiscardAqpId=100&SubscriberId=4fa432d4a653e5f8b2acb27aa862f98d&SubscriberType=ESM&ClientIP=10.136.25.241&SystemId=10.143.181.2-1%2F2&GroupId=1&PartitionId=2&Application=Unknown&ApplicationGroup=Unknown\" ");
@@ -63,7 +63,7 @@ public class ParserUtilsTest
@Test
public void tplinkRedirect() throws Exception
{
- final URL url = ParserUtils.testRedirect(context,
+ final URL url = HttpClient.testRedirect(context,
"");
assertNotNull(url);
assertEquals("tplinkextender.net", url.getHost());
@@ -72,7 +72,7 @@ public class ParserUtilsTest
@Test
public void mshtmlRedirect() throws Exception
{
- final URL url = ParserUtils
+ final URL url = HttpClient
.testRedirect(
context,
"HTML Redirection If your browser can not redirect you to home page automatically. Please click here . ");
@@ -83,41 +83,41 @@ public class ParserUtilsTest
@Test
public void efaExpired() throws Exception
{
- assertTrue(ParserUtils
+ assertTrue(HttpClient
.testExpired("Efa9 Internal Error Internal Error
Your session has expired.
"));
}
@Test
public void tflExpired() throws Exception
{
- assertTrue(ParserUtils
+ assertTrue(HttpClient
.testExpired("Session Expired
Session Expire"));
}
@Test
public void nvbwExpired() throws Exception
{
- assertTrue(ParserUtils.testExpired("Ihre Verbindungskennung ist nicht mehr gültig. "));
+ assertTrue(HttpClient.testExpired("Ihre Verbindungskennung ist nicht mehr gültig. "));
}
@Test
public void internalError() throws Exception
{
- assertTrue(ParserUtils
+ assertTrue(HttpClient
.testInternalError(" Internal error in gateway Internal error in gateway "));
}
@Test
public void vgnInternalError() throws Exception
{
- assertTrue(ParserUtils
+ assertTrue(HttpClient
.testInternalError("Efa9 Internal Error Internal Error
.\\EfaHttpServer.cpp
Line: 2507
"));
}
@Test
public void vrnInternalError() throws Exception
{
- assertTrue(ParserUtils
+ assertTrue(HttpClient
.testInternalError("VRN - Keine Verbindung zum Server möglich "));
}
}