mirror of
https://gitlab.com/oeffi/public-transport-enabler.git
synced 2025-07-19 16:59:51 +00:00
Extract AbstractHttpException from HTTP related exceptions and provide access to error content.
This commit is contained in:
parent
1643e12e24
commit
0999b3ba7f
5 changed files with 83 additions and 57 deletions
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -17,30 +17,16 @@
|
||||||
|
|
||||||
package de.schildbach.pte.exception;
|
package de.schildbach.pte.exception;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.Reader;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Andreas Schildbach
|
* @author Andreas Schildbach
|
||||||
*/
|
*/
|
||||||
public class BlockedException extends IOException
|
public class BlockedException extends AbstractHttpException
|
||||||
{
|
{
|
||||||
private final URL url;
|
public BlockedException(final URL url, final Reader errorReader)
|
||||||
|
|
||||||
public BlockedException()
|
|
||||||
{
|
{
|
||||||
this.url = null;
|
super(url, errorReader);
|
||||||
}
|
|
||||||
|
|
||||||
public BlockedException(final URL url)
|
|
||||||
{
|
|
||||||
super(url.toString());
|
|
||||||
|
|
||||||
this.url = url;
|
|
||||||
}
|
|
||||||
|
|
||||||
public URL getUrl()
|
|
||||||
{
|
|
||||||
return url;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,30 +17,16 @@
|
||||||
|
|
||||||
package de.schildbach.pte.exception;
|
package de.schildbach.pte.exception;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.Reader;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Andreas Schildbach
|
* @author Andreas Schildbach
|
||||||
*/
|
*/
|
||||||
public class InternalErrorException extends IOException
|
public class InternalErrorException extends AbstractHttpException
|
||||||
{
|
{
|
||||||
private final URL url;
|
public InternalErrorException(final URL url, final Reader errorReader)
|
||||||
|
|
||||||
public InternalErrorException()
|
|
||||||
{
|
{
|
||||||
this.url = null;
|
super(url, errorReader);
|
||||||
}
|
|
||||||
|
|
||||||
public InternalErrorException(final URL url)
|
|
||||||
{
|
|
||||||
super(url.toString());
|
|
||||||
|
|
||||||
this.url = url;
|
|
||||||
}
|
|
||||||
|
|
||||||
public URL getUrl()
|
|
||||||
{
|
|
||||||
return url;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,36 +17,21 @@
|
||||||
|
|
||||||
package de.schildbach.pte.exception;
|
package de.schildbach.pte.exception;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Andreas Schildbach
|
* @author Andreas Schildbach
|
||||||
*/
|
*/
|
||||||
public class UnexpectedRedirectException extends IOException
|
public class UnexpectedRedirectException extends AbstractHttpException
|
||||||
{
|
{
|
||||||
private final URL originalUrl;
|
|
||||||
private final URL redirectedUrl;
|
private final URL redirectedUrl;
|
||||||
|
|
||||||
public UnexpectedRedirectException()
|
|
||||||
{
|
|
||||||
this.originalUrl = null;
|
|
||||||
this.redirectedUrl = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public UnexpectedRedirectException(final URL originalUrl, final URL redirectedUrl)
|
public UnexpectedRedirectException(final URL originalUrl, final URL redirectedUrl)
|
||||||
{
|
{
|
||||||
super(originalUrl + " -> " + redirectedUrl);
|
super(originalUrl, originalUrl + " -> " + redirectedUrl);
|
||||||
|
|
||||||
this.originalUrl = originalUrl;
|
|
||||||
this.redirectedUrl = redirectedUrl;
|
this.redirectedUrl = redirectedUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
public URL getUrl()
|
|
||||||
{
|
|
||||||
return originalUrl;
|
|
||||||
}
|
|
||||||
|
|
||||||
public URL getRedirectedUrl()
|
public URL getRedirectedUrl()
|
||||||
{
|
{
|
||||||
return redirectedUrl;
|
return redirectedUrl;
|
||||||
|
|
|
@ -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_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 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_CONNECT_TIMEOUT = 5000;
|
||||||
private static final int SCRAPE_READ_TIMEOUT = 15000;
|
private static final int SCRAPE_READ_TIMEOUT = 15000;
|
||||||
private static final Charset SCRAPE_DEFAULT_ENCODING = Charset.forName("ISO-8859-1");
|
private static final Charset SCRAPE_DEFAULT_ENCODING = Charset.forName("ISO-8859-1");
|
||||||
|
@ -89,7 +89,7 @@ public final class ParserUtils
|
||||||
return buffer;
|
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];
|
final char[] buffer = new char[SCRAPE_INITIAL_CAPACITY];
|
||||||
long count = 0;
|
long count = 0;
|
||||||
|
@ -193,7 +193,7 @@ public final class ParserUtils
|
||||||
|| responseCode == HttpURLConnection.HTTP_FORBIDDEN || responseCode == HttpURLConnection.HTTP_NOT_ACCEPTABLE
|
|| responseCode == HttpURLConnection.HTTP_FORBIDDEN || responseCode == HttpURLConnection.HTTP_NOT_ACCEPTABLE
|
||||||
|| responseCode == HttpURLConnection.HTTP_UNAVAILABLE)
|
|| responseCode == HttpURLConnection.HTTP_UNAVAILABLE)
|
||||||
{
|
{
|
||||||
throw new BlockedException(url);
|
throw new BlockedException(url, new InputStreamReader(connection.getErrorStream(), requestEncoding));
|
||||||
}
|
}
|
||||||
else if (responseCode == HttpURLConnection.HTTP_NOT_FOUND)
|
else if (responseCode == HttpURLConnection.HTTP_NOT_FOUND)
|
||||||
{
|
{
|
||||||
|
@ -205,7 +205,7 @@ public final class ParserUtils
|
||||||
}
|
}
|
||||||
else if (responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR)
|
else if (responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR)
|
||||||
{
|
{
|
||||||
throw new InternalErrorException(url);
|
throw new InternalErrorException(url, new InputStreamReader(connection.getErrorStream(), requestEncoding));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue