HttpEngine: throw exception instead of null return in conn.read()

- convert OkHttps FileNotFound to some more useful exception
This commit is contained in:
Hannes Janetzek 2014-09-11 20:53:06 +02:00
parent 47b2a55b11
commit 691a18873d
3 changed files with 29 additions and 32 deletions

View File

@ -252,7 +252,6 @@ public class LwHttp implements HttpEngine {
byte[] buf = buffer; byte[] buf = buffer;
boolean first = true; boolean first = true;
boolean ok = true;
boolean gzip = false; boolean gzip = false;
int read = 0; int read = 0;
@ -272,7 +271,7 @@ public class LwHttp implements HttpEngine {
end++; end++;
if (end == BUFFER_SIZE) { if (end == BUFFER_SIZE) {
return null; throw new IOException("Header too large!");
} }
if (buf[end] != '\n') if (buf[end] != '\n')
@ -284,25 +283,24 @@ public class LwHttp implements HttpEngine {
break; break;
} }
if (ok) { if (first) {
if (first) { first = false;
first = false; /* check only for OK ("HTTP/1.? ".length == 9) */
/* check only for OK ("HTTP/1.? ".length == 9) */ if (!check(HEADER_HTTP_OK, buf, pos + 9, end)) {
if (!check(HEADER_HTTP_OK, buf, pos + 9, end)) throw new IOException("HTTP Error: "
ok = false; + new String(buf, pos, end - pos - 1));
} else if (check(HEADER_CONTENT_LENGTH, buf, pos, end)) {
/* parse Content-Length */
contentLength = parseInt(buf, pos +
HEADER_CONTENT_LENGTH.length + 2, end - 1);
} else if (check(HEADER_ENCODING_GZIP, buf, pos, end)) {
gzip = true;
} else if (check(HEADER_CONNECTION_CLOSE, buf, pos, end)) {
mMustCloseConnection = true;
} }
} else if (check(HEADER_CONTENT_LENGTH, buf, pos, end)) {
/* parse Content-Length */
contentLength = parseInt(buf, pos +
HEADER_CONTENT_LENGTH.length + 2, end - 1);
} else if (check(HEADER_ENCODING_GZIP, buf, pos, end)) {
gzip = true;
} else if (check(HEADER_CONNECTION_CLOSE, buf, pos, end)) {
mMustCloseConnection = true;
} }
if (!ok || dbg) { if (dbg) {
String line = new String(buf, pos, end - pos - 1); String line = new String(buf, pos, end - pos - 1);
log.debug("> {} <", line); log.debug("> {} <", line);
} }
@ -311,9 +309,6 @@ public class LwHttp implements HttpEngine {
end = pos; end = pos;
} }
if (!ok)
return null;
/* back to start of content */ /* back to start of content */
is.reset(); is.reset();
is.mark(0); is.mark(0);

View File

@ -17,6 +17,7 @@
*/ */
package org.oscim.tiling.source; package org.oscim.tiling.source;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
@ -69,7 +70,12 @@ public class OkHttpEngine implements HttpEngine {
for (Entry<String, String> opt : mTileSource.getRequestHeader().entrySet()) for (Entry<String, String> opt : mTileSource.getRequestHeader().entrySet())
conn.addRequestProperty(opt.getKey(), opt.getValue()); conn.addRequestProperty(opt.getKey(), opt.getValue());
inputStream = conn.getInputStream(); try {
inputStream = conn.getInputStream();
} catch (FileNotFoundException e) {
throw new IOException("ERROR " + conn.getResponseCode()
+ ": " + conn.getResponseMessage());
}
} }
@Override @Override

View File

@ -76,23 +76,19 @@ public class UrlTileDataSource implements ITileDataSource {
try { try {
mConn.sendRequest(tile); mConn.sendRequest(tile);
InputStream is = mConn.read(); InputStream is = mConn.read();
if (is == null) { if (mUseCache) {
log.debug("{} Network Error", tile); cacheWriter = cache.writeTile(tile);
} else { mConn.setCache(cacheWriter.getOutputStream());
if (mUseCache) {
cacheWriter = cache.writeTile(tile);
mConn.setCache(cacheWriter.getOutputStream());
}
success = mTileDecoder.decode(tile, sink, is);
} }
success = mTileDecoder.decode(tile, sink, is);
} catch (SocketException e) { } catch (SocketException e) {
log.debug("{} Socket exception: {}", tile, e.getMessage()); log.debug("{} Socket Error: {}", tile, e.getMessage());
} catch (SocketTimeoutException e) { } catch (SocketTimeoutException e) {
log.debug("{} Socket Timeout", tile); log.debug("{} Socket Timeout", tile);
} catch (UnknownHostException e) { } catch (UnknownHostException e) {
log.debug("{} Unknown host: {}", tile, e.getMessage()); log.debug("{} Unknown host: {}", tile, e.getMessage());
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); log.debug("{} Network Error: {}", tile, e.getMessage());
} finally { } finally {
success = mConn.requestCompleted(success); success = mConn.requestCompleted(success);
if (cacheWriter != null) if (cacheWriter != null)