Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -235,14 +235,9 @@ private void connectAndCheckHeaders(
Resource target,
WMSLayer.HttpRequestMode httpRequestMode)
throws GeoWebCacheException {

ClassicHttpResponse method = null;
final int responseCode;
int responseLength = 0;

try {
method = executeRequest(wmsBackendUrl, wmsParams, backendTimeout, httpRequestMode);
responseCode = method.getCode();
try (ClassicHttpResponse method = executeRequest(wmsBackendUrl, wmsParams, backendTimeout, httpRequestMode)) {
final int responseCode = method.getCode();
int responseLength = 0;
if (responseCode == 200) {
if (method.getFirstHeader("length") != null) {
responseLength =
Expand All @@ -257,89 +252,91 @@ private void connectAndCheckHeaders(
}
}
// Do not set error at this stage
} catch (IOException ce) {
if (log.isLoggable(Level.FINE)) {
String message = "Error forwarding request " + wmsBackendUrl.toString();
log.log(Level.FINE, message, 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());
}
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 = method.getFirstHeader("Content-Type").getValue();
if (responseCode != 204 && responseMime != null && !requestMimeType.isCompatible(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?

try (InputStream stream = method.getEntity().getContent()) {
byte[] error = IOUtils.toByteArray(stream);
message = new String(error);
} catch (IOException ioe) {
// Do nothing
}
} else if (responseMime != null && responseMime.toLowerCase().startsWith("application/vnd.ogc.se_xml")) {
try (InputStream stream = method.getEntity().getContent()) {
message = IOUtils.toString(stream, StandardCharsets.UTF_8);
} catch (IOException e) {
//
// Check that we're not getting an error MIME back.
String responseMime = method.getFirstHeader("Content-Type").getValue();
if (responseCode != 204 && responseMime != null && !requestMimeType.isCompatible(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?

try (InputStream stream = method.getEntity().getContent()) {
byte[] error = IOUtils.toByteArray(stream);
message = new String(error);
} catch (IOException ioe) {
// Do nothing
}
} else if (responseMime != null
&& responseMime.toLowerCase().startsWith("application/vnd.ogc.se_xml")) {
try (InputStream stream = method.getEntity().getContent()) {
message = IOUtils.toString(stream, StandardCharsets.UTF_8);
} catch (IOException e) {
//
}
}
String msg = "MimeType mismatch, expected "
+ requestMimeType
+ " but got "
+ responseMime
+ " from "
+ wmsBackendUrl.toString()
+ (message == null ? "" : (":\n" + message));
tileRespRecv.setError();
tileRespRecv.setErrorMessage(msg);
log.warning(msg);
}
String msg = "MimeType mismatch, expected "
+ requestMimeType
+ " but got "
+ responseMime
+ " from "
+ wmsBackendUrl.toString()
+ (message == null ? "" : (":\n" + message));
tileRespRecv.setError();
tileRespRecv.setErrorMessage(msg);
log.warning(msg);
}

// Everything looks okay, try to save expiration
if (tileRespRecv.getExpiresHeader() == GWCVars.CACHE_USE_WMS_BACKEND_VALUE) {
String expireValue = method.getFirstHeader("Expires").getValue();
long expire = ServletUtils.parseExpiresHeader(expireValue);
if (expire != -1) {
tileRespRecv.setExpiresHeader(expire / 1000);
// Everything looks okay, try to save expiration
if (tileRespRecv.getExpiresHeader() == GWCVars.CACHE_USE_WMS_BACKEND_VALUE) {
String expireValue = method.getFirstHeader("Expires").getValue();
long expire = ServletUtils.parseExpiresHeader(expireValue);
if (expire != -1) {
tileRespRecv.setExpiresHeader(expire / 1000);
}
}
}

// Read the actual data
if (responseCode != 204) {
try (InputStream inStream = method.getEntity().getContent()) {
if (inStream == null) {
log.severe("No response for " + method);
} else {
try (ReadableByteChannel channel = Channels.newChannel(inStream)) {
target.transferFrom(channel);
// Read the actual data
if (responseCode != 204) {
try (InputStream inStream = method.getEntity().getContent()) {
if (inStream == null) {
log.severe("No response for " + method);
} else {
try (ReadableByteChannel channel = Channels.newChannel(inStream)) {
target.transferFrom(channel);
}
}
}
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());
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) {
tileRespRecv.setError();
log.severe("Caught IO exception, " + wmsBackendUrl.toString() + " " + ioe.getMessage());
}
} catch (IOException ioe) {
tileRespRecv.setError();
log.severe("Caught IO exception, " + wmsBackendUrl.toString() + " " + ioe.getMessage());
}
} catch (IOException ce) {
if (log.isLoggable(Level.FINE)) {
String message = "Error forwarding request " + wmsBackendUrl.toString();
log.log(Level.FINE, message, ce);
}
throw new GeoWebCacheException(ce);
}
}

Expand Down
Loading