@@ -235,14 +235,9 @@ private void connectAndCheckHeaders(
235235 Resource target ,
236236 WMSLayer .HttpRequestMode httpRequestMode )
237237 throws GeoWebCacheException {
238-
239- ClassicHttpResponse method = null ;
240- final int responseCode ;
241- int responseLength = 0 ;
242-
243- try {
244- method = executeRequest (wmsBackendUrl , wmsParams , backendTimeout , httpRequestMode );
245- responseCode = method .getCode ();
238+ try (ClassicHttpResponse method = executeRequest (wmsBackendUrl , wmsParams , backendTimeout , httpRequestMode )) {
239+ final int responseCode = method .getCode ();
240+ int responseLength = 0 ;
246241 if (responseCode == 200 ) {
247242 if (method .getFirstHeader ("length" ) != null ) {
248243 responseLength =
@@ -257,89 +252,91 @@ private void connectAndCheckHeaders(
257252 }
258253 }
259254 // Do not set error at this stage
260- } catch (IOException ce ) {
261- if (log .isLoggable (Level .FINE )) {
262- String message = "Error forwarding request " + wmsBackendUrl .toString ();
263- log .log (Level .FINE , message , ce );
255+
256+ // Check that the response code is okay
257+ tileRespRecv .setStatus (responseCode );
258+ if (responseCode != 200 && responseCode != 204 ) {
259+ tileRespRecv .setError ();
260+ throw new ServiceException (
261+ "Unexpected response code from backend: " + responseCode + " for " + wmsBackendUrl .toString ());
264262 }
265- throw new GeoWebCacheException (ce );
266- }
267- // Check that the response code is okay
268- tileRespRecv .setStatus (responseCode );
269- if (responseCode != 200 && responseCode != 204 ) {
270- tileRespRecv .setError ();
271- throw new ServiceException (
272- "Unexpected response code from backend: " + responseCode + " for " + wmsBackendUrl .toString ());
273- }
274263
275- // Check that we're not getting an error MIME back.
276- String responseMime = method .getFirstHeader ("Content-Type" ).getValue ();
277- if (responseCode != 204 && responseMime != null && !requestMimeType .isCompatible (responseMime )) {
278- String message = null ;
279- if (responseMime .equalsIgnoreCase (ErrorMime .vnd_ogc_se_inimage .getFormat ())) {
280- // TODO: revisit: I don't understand why it's trying to create a String message
281- // out of an ogc_se_inimage response?
282-
283- try (InputStream stream = method .getEntity ().getContent ()) {
284- byte [] error = IOUtils .toByteArray (stream );
285- message = new String (error );
286- } catch (IOException ioe ) {
287- // Do nothing
288- }
289- } else if (responseMime != null && responseMime .toLowerCase ().startsWith ("application/vnd.ogc.se_xml" )) {
290- try (InputStream stream = method .getEntity ().getContent ()) {
291- message = IOUtils .toString (stream , StandardCharsets .UTF_8 );
292- } catch (IOException e ) {
293- //
264+ // Check that we're not getting an error MIME back.
265+ String responseMime = method .getFirstHeader ("Content-Type" ).getValue ();
266+ if (responseCode != 204 && responseMime != null && !requestMimeType .isCompatible (responseMime )) {
267+ String message = null ;
268+ if (responseMime .equalsIgnoreCase (ErrorMime .vnd_ogc_se_inimage .getFormat ())) {
269+ // TODO: revisit: I don't understand why it's trying to create a String message
270+ // out of an ogc_se_inimage response?
271+
272+ try (InputStream stream = method .getEntity ().getContent ()) {
273+ byte [] error = IOUtils .toByteArray (stream );
274+ message = new String (error );
275+ } catch (IOException ioe ) {
276+ // Do nothing
277+ }
278+ } else if (responseMime != null
279+ && responseMime .toLowerCase ().startsWith ("application/vnd.ogc.se_xml" )) {
280+ try (InputStream stream = method .getEntity ().getContent ()) {
281+ message = IOUtils .toString (stream , StandardCharsets .UTF_8 );
282+ } catch (IOException e ) {
283+ //
284+ }
294285 }
286+ String msg = "MimeType mismatch, expected "
287+ + requestMimeType
288+ + " but got "
289+ + responseMime
290+ + " from "
291+ + wmsBackendUrl .toString ()
292+ + (message == null ? "" : (":\n " + message ));
293+ tileRespRecv .setError ();
294+ tileRespRecv .setErrorMessage (msg );
295+ log .warning (msg );
295296 }
296- String msg = "MimeType mismatch, expected "
297- + requestMimeType
298- + " but got "
299- + responseMime
300- + " from "
301- + wmsBackendUrl .toString ()
302- + (message == null ? "" : (":\n " + message ));
303- tileRespRecv .setError ();
304- tileRespRecv .setErrorMessage (msg );
305- log .warning (msg );
306- }
307297
308- // Everything looks okay, try to save expiration
309- if (tileRespRecv .getExpiresHeader () == GWCVars .CACHE_USE_WMS_BACKEND_VALUE ) {
310- String expireValue = method .getFirstHeader ("Expires" ).getValue ();
311- long expire = ServletUtils .parseExpiresHeader (expireValue );
312- if (expire != -1 ) {
313- tileRespRecv .setExpiresHeader (expire / 1000 );
298+ // Everything looks okay, try to save expiration
299+ if (tileRespRecv .getExpiresHeader () == GWCVars .CACHE_USE_WMS_BACKEND_VALUE ) {
300+ String expireValue = method .getFirstHeader ("Expires" ).getValue ();
301+ long expire = ServletUtils .parseExpiresHeader (expireValue );
302+ if (expire != -1 ) {
303+ tileRespRecv .setExpiresHeader (expire / 1000 );
304+ }
314305 }
315- }
316306
317- // Read the actual data
318- if (responseCode != 204 ) {
319- try (InputStream inStream = method .getEntity ().getContent ()) {
320- if (inStream == null ) {
321- log .severe ("No response for " + method );
322- } else {
323- try (ReadableByteChannel channel = Channels .newChannel (inStream )) {
324- target .transferFrom (channel );
307+ // Read the actual data
308+ if (responseCode != 204 ) {
309+ try (InputStream inStream = method .getEntity ().getContent ()) {
310+ if (inStream == null ) {
311+ log .severe ("No response for " + method );
312+ } else {
313+ try (ReadableByteChannel channel = Channels .newChannel (inStream )) {
314+ target .transferFrom (channel );
315+ }
325316 }
326- }
327- if ( responseLength > 0 ) {
328- int readAccu = ( int ) target . getSize ();
329- if ( readAccu != responseLength ) {
330- tileRespRecv . setError ();
331- throw new GeoWebCacheException ( "Responseheader advertised "
332- + responseLength
333- + " bytes, but only received "
334- + readAccu
335- + " from "
336- + wmsBackendUrl . toString ());
317+ if ( responseLength > 0 ) {
318+ int readAccu = ( int ) target . getSize ();
319+ if ( readAccu != responseLength ) {
320+ tileRespRecv . setError ();
321+ throw new GeoWebCacheException ( "Responseheader advertised "
322+ + responseLength
323+ + " bytes, but only received "
324+ + readAccu
325+ + " from "
326+ + wmsBackendUrl . toString ());
327+ }
337328 }
329+ } catch (IOException ioe ) {
330+ tileRespRecv .setError ();
331+ log .severe ("Caught IO exception, " + wmsBackendUrl .toString () + " " + ioe .getMessage ());
338332 }
339- } catch (IOException ioe ) {
340- tileRespRecv .setError ();
341- log .severe ("Caught IO exception, " + wmsBackendUrl .toString () + " " + ioe .getMessage ());
342333 }
334+ } catch (IOException ce ) {
335+ if (log .isLoggable (Level .FINE )) {
336+ String message = "Error forwarding request " + wmsBackendUrl .toString ();
337+ log .log (Level .FINE , message , ce );
338+ }
339+ throw new GeoWebCacheException (ce );
343340 }
344341 }
345342
0 commit comments