From 0999b3ba7f925c23d14fb04c0d468a7fc040cb48 Mon Sep 17 00:00:00 2001 From: Andreas Schildbach Date: Sun, 13 Jul 2014 13:40:18 +0200 Subject: [PATCH] Extract AbstractHttpException from HTTP related exceptions and provide access to error content. --- .../pte/exception/AbstractHttpException.java | 69 +++++++++++++++++++ .../pte/exception/BlockedException.java | 22 ++---- .../pte/exception/InternalErrorException.java | 22 ++---- .../UnexpectedRedirectException.java | 19 +---- .../de/schildbach/pte/util/ParserUtils.java | 8 +-- 5 files changed, 83 insertions(+), 57 deletions(-) create mode 100644 enabler/src/de/schildbach/pte/exception/AbstractHttpException.java diff --git a/enabler/src/de/schildbach/pte/exception/AbstractHttpException.java b/enabler/src/de/schildbach/pte/exception/AbstractHttpException.java new file mode 100644 index 00000000..8eca510d --- /dev/null +++ b/enabler/src/de/schildbach/pte/exception/AbstractHttpException.java @@ -0,0 +1,69 @@ +/* + * Copyright 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; +import java.io.Reader; +import java.net.URL; + +import de.schildbach.pte.util.ParserUtils; + +/** + * @author Andreas Schildbach + */ +public abstract class AbstractHttpException extends ParserException +{ + private final URL url; + private final Reader errorReader; + + public AbstractHttpException(final URL url, final Reader errorReader) + { + super(); + this.url = url; + this.errorReader = errorReader; + } + + public AbstractHttpException(final URL url, final String message) + { + super(message); + this.url = url; + this.errorReader = null; + } + + public URL getUrl() + { + return url; + } + + public Reader getErrorReader() + { + return errorReader; + } + + public CharSequence scrapeErrorStream() throws IOException + { + if (errorReader == null) + return null; + + final StringBuilder error = new StringBuilder(ParserUtils.SCRAPE_INITIAL_CAPACITY); + ParserUtils.copy(errorReader, error); + errorReader.close(); + + return error; + } +} diff --git a/enabler/src/de/schildbach/pte/exception/BlockedException.java b/enabler/src/de/schildbach/pte/exception/BlockedException.java index 8ccef72d..5bdeb52b 100644 --- a/enabler/src/de/schildbach/pte/exception/BlockedException.java +++ b/enabler/src/de/schildbach/pte/exception/BlockedException.java @@ -17,30 +17,16 @@ package de.schildbach.pte.exception; -import java.io.IOException; +import java.io.Reader; import java.net.URL; /** * @author Andreas Schildbach */ -public class BlockedException extends IOException +public class BlockedException extends AbstractHttpException { - private final URL url; - - public BlockedException() + public BlockedException(final URL url, final Reader errorReader) { - this.url = null; - } - - public BlockedException(final URL url) - { - super(url.toString()); - - this.url = url; - } - - public URL getUrl() - { - return url; + super(url, errorReader); } } diff --git a/enabler/src/de/schildbach/pte/exception/InternalErrorException.java b/enabler/src/de/schildbach/pte/exception/InternalErrorException.java index d0c7d08b..7bfce1f4 100644 --- a/enabler/src/de/schildbach/pte/exception/InternalErrorException.java +++ b/enabler/src/de/schildbach/pte/exception/InternalErrorException.java @@ -17,30 +17,16 @@ package de.schildbach.pte.exception; -import java.io.IOException; +import java.io.Reader; import java.net.URL; /** * @author Andreas Schildbach */ -public class InternalErrorException extends IOException +public class InternalErrorException extends AbstractHttpException { - private final URL url; - - public InternalErrorException() + public InternalErrorException(final URL url, final Reader errorReader) { - this.url = null; - } - - public InternalErrorException(final URL url) - { - super(url.toString()); - - this.url = url; - } - - public URL getUrl() - { - return url; + super(url, errorReader); } } diff --git a/enabler/src/de/schildbach/pte/exception/UnexpectedRedirectException.java b/enabler/src/de/schildbach/pte/exception/UnexpectedRedirectException.java index f0f500e2..69d5d10a 100644 --- a/enabler/src/de/schildbach/pte/exception/UnexpectedRedirectException.java +++ b/enabler/src/de/schildbach/pte/exception/UnexpectedRedirectException.java @@ -17,36 +17,21 @@ package de.schildbach.pte.exception; -import java.io.IOException; import java.net.URL; /** * @author Andreas Schildbach */ -public class UnexpectedRedirectException extends IOException +public class UnexpectedRedirectException extends AbstractHttpException { - 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; + super(originalUrl, originalUrl + " -> " + redirectedUrl); this.redirectedUrl = redirectedUrl; } - public URL getUrl() - { - return originalUrl; - } - public URL getRedirectedUrl() { return redirectedUrl; diff --git a/enabler/src/de/schildbach/pte/util/ParserUtils.java b/enabler/src/de/schildbach/pte/util/ParserUtils.java index bd8319e7..441f3af4 100644 --- a/enabler/src/de/schildbach/pte/util/ParserUtils.java +++ b/enabler/src/de/schildbach/pte/util/ParserUtils.java @@ -52,7 +52,7 @@ public final class ParserUtils { private static final String SCRAPE_USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0"; private static final String SCRAPE_ACCEPT = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; - private static final int SCRAPE_INITIAL_CAPACITY = 4096; + public static final int SCRAPE_INITIAL_CAPACITY = 4096; private static final int SCRAPE_CONNECT_TIMEOUT = 5000; private static final int SCRAPE_READ_TIMEOUT = 15000; private static final Charset SCRAPE_DEFAULT_ENCODING = Charset.forName("ISO-8859-1"); @@ -89,7 +89,7 @@ public final class ParserUtils return buffer; } - private static final long copy(final Reader reader, final StringBuilder builder) throws IOException + public static final long copy(final Reader reader, final StringBuilder builder) throws IOException { final char[] buffer = new char[SCRAPE_INITIAL_CAPACITY]; long count = 0; @@ -193,7 +193,7 @@ public final class ParserUtils || responseCode == HttpURLConnection.HTTP_FORBIDDEN || responseCode == HttpURLConnection.HTTP_NOT_ACCEPTABLE || responseCode == HttpURLConnection.HTTP_UNAVAILABLE) { - throw new BlockedException(url); + throw new BlockedException(url, new InputStreamReader(connection.getErrorStream(), requestEncoding)); } else if (responseCode == HttpURLConnection.HTTP_NOT_FOUND) { @@ -205,7 +205,7 @@ public final class ParserUtils } else if (responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR) { - throw new InternalErrorException(url); + throw new InternalErrorException(url, new InputStreamReader(connection.getErrorStream(), requestEncoding)); } else {