} catch (IOException ce) {
if (log.isDebugEnabled()) {
String message = "Error forwarding request " + wmsBackendUrl.toString();
log.debug(message, ce);
}
throw new GeoWebCacheException(ce);
}
// Check that the response code is okay
tileRespRecv.setStatus(responseCode);
if (responseCode != 200 && responseCode != 204) {
tileRespRecv.setError();
throw new ServiceException("Unexpected response code from backend: " + responseCode
+ " for " + wmsBackendUrl.toString());
}
// Check that we're not getting an error MIME back.
String responseMime = getMethod.getResponseHeader("Content-Type").getValue();
if (responseCode != 204 && responseMime != null
&& !mimeStringCheck(requestMime, responseMime)) {
String message = null;
if (responseMime.equalsIgnoreCase(ErrorMime.vnd_ogc_se_inimage.getFormat())) {
// TODO: revisit: I don't understand why it's trying to create a String message
// out of an ogc_se_inimage response?
InputStream stream = null;
try {
stream = getMethod.getResponseBodyAsStream();
byte[] error = IOUtils.toByteArray(stream);
message = new String(error);
} catch (IOException ioe) {
// Do nothing
} finally {
IOUtils.closeQuietly(stream);
}
} else if (responseMime != null
&& responseMime.toLowerCase().startsWith("application/vnd.ogc.se_xml")) {
InputStream stream = null;
try {
stream = getMethod.getResponseBodyAsStream();
message = IOUtils.toString(stream);
} catch (IOException e) {
//
} finally {
IOUtils.closeQuietly(stream);
}
}
String msg = "MimeType mismatch, expected " + requestMime + " but got "
+ responseMime + " from " + wmsBackendUrl.toString()
+ (message == null ? "" : (":\n" + message));
tileRespRecv.setError();
tileRespRecv.setErrorMessage(msg);
log.warn(msg);
}
// Everything looks okay, try to save expiration
if (tileRespRecv.getExpiresHeader() == GWCVars.CACHE_USE_WMS_BACKEND_VALUE) {
String expireValue = getMethod.getResponseHeader("Expires").getValue();
long expire = ServletUtils.parseExpiresHeader(expireValue);
if (expire != -1) {
tileRespRecv.setExpiresHeader(expire / 1000);
}
}
// Read the actual data
if (responseCode != 204) {
try {
InputStream inStream = getMethod.getResponseBodyAsStream();
ReadableByteChannel channel = Channels.newChannel(inStream);
try {
target.transferFrom(channel);
} finally {
channel.close();
}
if (responseLength > 0) {
int readAccu = (int) target.getSize();
if (readAccu != responseLength) {
tileRespRecv.setError();
throw new GeoWebCacheException("Responseheader advertised "
+ responseLength + " bytes, but only received " + readAccu
+ " from " + wmsBackendUrl.toString());
}
}
} catch (IOException ioe) {