diff --git a/geowebcache/core/src/main/java/org/geowebcache/GeoWebCacheEnvironment.java b/geowebcache/core/src/main/java/org/geowebcache/GeoWebCacheEnvironment.java index e76f632c5..e9ed17fc6 100644 --- a/geowebcache/core/src/main/java/org/geowebcache/GeoWebCacheEnvironment.java +++ b/geowebcache/core/src/main/java/org/geowebcache/GeoWebCacheEnvironment.java @@ -21,7 +21,6 @@ import org.geotools.util.logging.Logging; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.PlaceholderConfigurerSupport; -import org.springframework.core.Constants; import org.springframework.util.PropertyPlaceholderHelper; /** @@ -47,8 +46,6 @@ public class GeoWebCacheEnvironment { /** logger */ public static final Logger LOGGER = Logging.getLogger(GeoWebCacheEnvironment.class.getName()); - private static final Constants constants = new Constants(PlaceholderConfigurerSupport.class); - /** * Constant set via System Environment in order to instruct GeoWebCache to make use or not of the config * placeholders translation. @@ -65,9 +62,10 @@ public class GeoWebCacheEnvironment { private static final String nullValue = "null"; private final PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper( - constants.asString("DEFAULT_PLACEHOLDER_PREFIX"), - constants.asString("DEFAULT_PLACEHOLDER_SUFFIX"), - constants.asString("DEFAULT_VALUE_SEPARATOR"), + PlaceholderConfigurerSupport.DEFAULT_PLACEHOLDER_PREFIX, + PlaceholderConfigurerSupport.DEFAULT_PLACEHOLDER_SUFFIX, + PlaceholderConfigurerSupport.DEFAULT_VALUE_SEPARATOR, + null, true); private Properties props; diff --git a/geowebcache/core/src/main/java/org/geowebcache/filter/request/WMSRasterFilter.java b/geowebcache/core/src/main/java/org/geowebcache/filter/request/WMSRasterFilter.java index 5e886ef23..a2a742a5d 100644 --- a/geowebcache/core/src/main/java/org/geowebcache/filter/request/WMSRasterFilter.java +++ b/geowebcache/core/src/main/java/org/geowebcache/filter/request/WMSRasterFilter.java @@ -117,44 +117,45 @@ protected BufferedImage loadMatrix(TileLayer tlayer, String gridSetId, int z) backendTimeout = 120; } - ClassicHttpResponse httpResponse = null; BufferedImage img = null; - httpResponse = srcHelper.executeRequest(wmsUrl, requestParams, backendTimeout, WMSLayer.HttpRequestMode.Get); + try (ClassicHttpResponse httpResponse = + srcHelper.executeRequest(wmsUrl, requestParams, backendTimeout, WMSLayer.HttpRequestMode.Get)) { - int statusCode = httpResponse.getCode(); - if (statusCode != 200) { - throw new GeoWebCacheException("Received response code " + statusCode + "\n"); - } + int statusCode = httpResponse.getCode(); + if (statusCode != 200) { + throw new GeoWebCacheException("Received response code " + statusCode + "\n"); + } - if (!httpResponse.getFirstHeader("Content-Type").getValue().startsWith("image/")) { - throw new GeoWebCacheException("Unexpected response content type " - + httpResponse.getFirstHeader("Content-Type").getValue() - + " , request was " - + urlStr - + "\n"); - } + if (!httpResponse.getFirstHeader("Content-Type").getValue().startsWith("image/")) { + throw new GeoWebCacheException("Unexpected response content type " + + httpResponse.getFirstHeader("Content-Type").getValue() + + " , request was " + + urlStr + + "\n"); + } - byte[] ret = ServletUtils.readStream(httpResponse.getEntity().getContent(), 16384, 2048); + byte[] ret = ServletUtils.readStream(httpResponse.getEntity().getContent(), 16384, 2048); - InputStream is = new ByteArrayInputStream(ret); + InputStream is = new ByteArrayInputStream(ret); - img = ImageIO.read(is); + img = ImageIO.read(is); - if (img.getWidth() != widthHeight[0] || img.getHeight() != widthHeight[1]) { - String msg = "WMS raster filter has dimensions " - + img.getWidth() - + "," - + img.getHeight() - + ", expected " - + widthHeight[0] - + "," - + widthHeight[1] - + "\n"; - throw new GeoWebCacheException(msg); - } + if (img.getWidth() != widthHeight[0] || img.getHeight() != widthHeight[1]) { + String msg = "WMS raster filter has dimensions " + + img.getWidth() + + "," + + img.getHeight() + + ", expected " + + widthHeight[0] + + "," + + widthHeight[1] + + "\n"; + throw new GeoWebCacheException(msg); + } - return img; + return img; + } } /** Generates the URL used to create the lookup raster */ diff --git a/geowebcache/core/src/main/java/org/geowebcache/layer/wms/WMSHttpHelper.java b/geowebcache/core/src/main/java/org/geowebcache/layer/wms/WMSHttpHelper.java index 628d8a7ef..c67958d08 100644 --- a/geowebcache/core/src/main/java/org/geowebcache/layer/wms/WMSHttpHelper.java +++ b/geowebcache/core/src/main/java/org/geowebcache/layer/wms/WMSHttpHelper.java @@ -34,7 +34,6 @@ import org.apache.hc.client5.http.classic.methods.HttpUriRequestBase; import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; import org.apache.hc.core5.http.ClassicHttpResponse; -import org.apache.hc.core5.http.HttpEntity; import org.apache.hc.core5.http.HttpException; import org.apache.hc.core5.http.NameValuePair; import org.apache.hc.core5.http.io.entity.StringEntity; @@ -378,7 +377,8 @@ public ClassicHttpResponse executeRequest( if (httpRequestMode == WMSLayer.HttpRequestMode.FormPost) { HttpPost pm = new HttpPost(urlString); if (queryParams != null && !queryParams.isEmpty()) { - HttpEntity requestEntity = new StringEntity(processRequestParameters(queryParams)); + @SuppressWarnings("PMD.CloseResource") + StringEntity requestEntity = new StringEntity(processRequestParameters(queryParams)); pm.setEntity(requestEntity); } method = pm; diff --git a/geowebcache/core/src/main/java/org/geowebcache/layer/wms/WMSLayer.java b/geowebcache/core/src/main/java/org/geowebcache/layer/wms/WMSLayer.java index 854c3fa41..32cb857db 100644 --- a/geowebcache/core/src/main/java/org/geowebcache/layer/wms/WMSLayer.java +++ b/geowebcache/core/src/main/java/org/geowebcache/layer/wms/WMSLayer.java @@ -780,7 +780,6 @@ public void proxyRequest(ConveyorTile tile) throws GeoWebCacheException { String queryStr = tile.servletReq.getQueryString(); String serverStr = getWMSurl()[0]; - ClassicHttpResponse httpResponse = null; try { URL url; if (serverStr.contains("?")) { @@ -794,27 +793,28 @@ public void proxyRequest(ConveyorTile tile) throws GeoWebCacheException { throw new GeoWebCacheException("Can only proxy if WMS Layer is backed by an HTTP backend"); } - httpResponse = - ((WMSHttpHelper) helper).executeRequest(url, null, getBackendTimeout(), getHttpRequestMode()); - HttpEntity entity = httpResponse.getEntity(); - try (InputStream is = entity.getContent()) { - HttpServletResponse response = tile.servletResp; - org.apache.hc.core5.http.Header contentType = httpResponse.getFirstHeader("Content-Type"); - if (contentType != null) { - response.setContentType(contentType.getValue()); - String contentEncoding = entity.getContentEncoding(); - if (!MimeType.isBinary(contentType.getValue())) { - response.setCharacterEncoding(contentEncoding); + try (ClassicHttpResponse httpResponse = + ((WMSHttpHelper) helper).executeRequest(url, null, getBackendTimeout(), getHttpRequestMode())) { + HttpEntity entity = httpResponse.getEntity(); + try (InputStream is = entity.getContent()) { + HttpServletResponse response = tile.servletResp; + org.apache.hc.core5.http.Header contentType = httpResponse.getFirstHeader("Content-Type"); + if (contentType != null) { + response.setContentType(contentType.getValue()); + String contentEncoding = entity.getContentEncoding(); + if (!MimeType.isBinary(contentType.getValue())) { + response.setCharacterEncoding(contentEncoding); + } } - } - int read = 0; - byte[] data = new byte[1024]; + int read = 0; + byte[] data = new byte[1024]; - while (read > -1) { - read = is.read(data); - if (read > -1) { - response.getOutputStream().write(data, 0, read); + while (read > -1) { + read = is.read(data); + if (read > -1) { + response.getOutputStream().write(data, 0, read); + } } } } diff --git a/geowebcache/core/src/main/java/org/geowebcache/storage/MetastoreRemover.java b/geowebcache/core/src/main/java/org/geowebcache/storage/MetastoreRemover.java index 5e638bb7a..265e2a8cf 100644 --- a/geowebcache/core/src/main/java/org/geowebcache/storage/MetastoreRemover.java +++ b/geowebcache/core/src/main/java/org/geowebcache/storage/MetastoreRemover.java @@ -57,6 +57,7 @@ public MetastoreRemover(DefaultStorageFinder finder) throws Exception { try (Connection conn = getMetaStoreConnection(root)) { if (conn != null) { log.info("Migrating the old metastore to filesystem storage"); + @SuppressWarnings("PMD.CloseResource") SingleConnectionDataSource ds = new SingleConnectionDataSource(conn, false); JdbcTemplate template = new JdbcTemplate(ds); diff --git a/geowebcache/core/src/main/java/org/geowebcache/util/HttpClientBuilder.java b/geowebcache/core/src/main/java/org/geowebcache/util/HttpClientBuilder.java index 6750556cd..47623d07f 100644 --- a/geowebcache/core/src/main/java/org/geowebcache/util/HttpClientBuilder.java +++ b/geowebcache/core/src/main/java/org/geowebcache/util/HttpClientBuilder.java @@ -18,6 +18,7 @@ import java.util.logging.Logger; import org.apache.hc.client5.http.auth.AuthScope; import org.apache.hc.client5.http.auth.UsernamePasswordCredentials; +import org.apache.hc.client5.http.config.ConnectionConfig; import org.apache.hc.client5.http.config.RequestConfig; import org.apache.hc.client5.http.cookie.StandardCookieSpec; import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider; @@ -66,16 +67,22 @@ public HttpClientBuilder( .setCookieSpec(StandardCookieSpec.RELAXED) .setExpectContinueEnabled(true) .setResponseTimeout(backendTimeoutMillis, TimeUnit.MILLISECONDS) - .setConnectTimeout(backendTimeoutMillis, TimeUnit.MILLISECONDS) .setRedirectsEnabled(true) .build()); + ConnectionConfig connectionConfig = ConnectionConfig.custom() + .setConnectTimeout(backendTimeoutMillis, TimeUnit.MILLISECONDS) + .build(); + + @SuppressWarnings("PMD.CloseResource") PoolingHttpClientConnectionManager connectionManager = PoolingHttpClientConnectionManagerBuilder.create() .setMaxConnTotal(concurrency) + .setDefaultConnectionConfig(connectionConfig) .build(); clientBuilder = HttpClients.custom(); clientBuilder.useSystemProperties(); + clientBuilder.setDefaultRequestConfig(this.connectionConfig); clientBuilder.setConnectionManager(connectionManager); } diff --git a/geowebcache/core/src/main/java/org/geowebcache/util/ResponseUtils.java b/geowebcache/core/src/main/java/org/geowebcache/util/ResponseUtils.java index 437627115..41a42ee13 100644 --- a/geowebcache/core/src/main/java/org/geowebcache/util/ResponseUtils.java +++ b/geowebcache/core/src/main/java/org/geowebcache/util/ResponseUtils.java @@ -142,13 +142,13 @@ private static void writeData(ConveyorTile tile, RuntimeStats runtimeStats) thro // (e.g. 'Sun, 06 Nov 1994 08:49:37 GMT'). See // http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3.1 - final String lastModified = DateUtils.formatDate(new Date(tileTimeStamp)); + final String lastModified = DateUtils.formatStandardDate(new Date(tileTimeStamp).toInstant()); servletResp.setHeader("Last-Modified", lastModified); final Date ifModifiedSince; if (ifModSinceHeader != null && ifModSinceHeader.length() > 0) { - ifModifiedSince = DateUtils.parseDate(ifModSinceHeader); + ifModifiedSince = Date.from(DateUtils.parseStandardDate(ifModSinceHeader)); // the HTTP header has second precision long ifModSinceSeconds = 1000 * (ifModifiedSince.getTime() / 1000); long tileTimeStampSeconds = 1000 * (tileTimeStamp / 1000); diff --git a/geowebcache/core/src/test/java/org/geowebcache/layer/wms/WMSLayerTest.java b/geowebcache/core/src/test/java/org/geowebcache/layer/wms/WMSLayerTest.java index 3162bbdf3..70333d50f 100644 --- a/geowebcache/core/src/test/java/org/geowebcache/layer/wms/WMSLayerTest.java +++ b/geowebcache/core/src/test/java/org/geowebcache/layer/wms/WMSLayerTest.java @@ -108,6 +108,7 @@ * @author Gabriel Roldan (OpenGeo) * @version $Id$ */ +@SuppressWarnings("PMD.CloseResource") public class WMSLayerTest extends TileLayerTest { private final GridSetBroker gridSetBroker = diff --git a/geowebcache/diskquota/jdbc/src/main/java/org/geowebcache/diskquota/jdbc/JDBCQuotaStore.java b/geowebcache/diskquota/jdbc/src/main/java/org/geowebcache/diskquota/jdbc/JDBCQuotaStore.java index c7d4ea62a..7d5054cb0 100644 --- a/geowebcache/diskquota/jdbc/src/main/java/org/geowebcache/diskquota/jdbc/JDBCQuotaStore.java +++ b/geowebcache/diskquota/jdbc/src/main/java/org/geowebcache/diskquota/jdbc/JDBCQuotaStore.java @@ -46,7 +46,7 @@ import org.geowebcache.util.SuppressFBWarnings; import org.springframework.dao.ConcurrencyFailureException; import org.springframework.dao.DataAccessException; -import org.springframework.dao.DeadlockLoserDataAccessException; +import org.springframework.dao.PessimisticLockingFailureException; import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.transaction.TransactionStatus; @@ -506,7 +506,7 @@ private void upsertTilePageFillFactor(PageStatsPayload payload) { modified = createNewPageStats(stats, page); } - } catch (DeadlockLoserDataAccessException e) { + } catch (PessimisticLockingFailureException e) { if (log.isLoggable(Level.FINE)) { log.log(Level.FINE, "Deadlock while updating page stats, will retry", e); } @@ -871,7 +871,7 @@ private PageStats upsertTilePageHitAccessTime(PageStatsPayload payload) { updatePageStats(payload, page, stats); modified = createNewPageStats(stats, page); } - } catch (DeadlockLoserDataAccessException e) { + } catch (PessimisticLockingFailureException e) { if (log.isLoggable(Level.FINE)) { log.log(Level.FINE, "Deadlock while updating page stats, will retry", e); } diff --git a/geowebcache/georss/src/main/java/org/geowebcache/georss/GeoRSSReaderFactory.java b/geowebcache/georss/src/main/java/org/geowebcache/georss/GeoRSSReaderFactory.java index bc9316636..766d999db 100644 --- a/geowebcache/georss/src/main/java/org/geowebcache/georss/GeoRSSReaderFactory.java +++ b/geowebcache/georss/src/main/java/org/geowebcache/georss/GeoRSSReaderFactory.java @@ -45,31 +45,31 @@ public GeoRSSReader createReader(final URL url, final String username, final Str builder.setHttpCredentials(username, password, url); builder.setBackendTimeout(120); - CloseableHttpClient httpClient = builder.buildClient(); + try (CloseableHttpClient httpClient = builder.buildClient()) { - HttpGet getMethod = new HttpGet(url.toString()); - - if (log.isLoggable(Level.FINE)) { - log.fine("Executing HTTP GET requesr for feed URL " + url.toExternalForm()); - } - - try { - ClassicHttpResponse response = httpClient.executeOpen(determineHost(getMethod), getMethod, null); + HttpGet getMethod = new HttpGet(url.toString()); if (log.isLoggable(Level.FINE)) { - log.fine("Building GeoRSS reader out of URL response"); - } - String contentEncoding = response.getEntity().getContentEncoding(); - if (contentEncoding == null) { - contentEncoding = "UTF-8"; + log.fine("Executing HTTP GET requesr for feed URL " + url.toExternalForm()); } - Reader reader = new BufferedReader( - new InputStreamReader(response.getEntity().getContent(), contentEncoding)); - if (log.isLoggable(Level.FINE)) { - log.fine("GeoRSS reader created, returning."); + try (ClassicHttpResponse response = httpClient.executeOpen(determineHost(getMethod), getMethod, null)) { + + if (log.isLoggable(Level.FINE)) { + log.fine("Building GeoRSS reader out of URL response"); + } + String contentEncoding = response.getEntity().getContentEncoding(); + if (contentEncoding == null) { + contentEncoding = "UTF-8"; + } + + Reader reader = new BufferedReader( + new InputStreamReader(response.getEntity().getContent(), contentEncoding)); + if (log.isLoggable(Level.FINE)) { + log.fine("GeoRSS reader created, returning."); + } + return createReader(reader); } - return createReader(reader); } catch (HttpException e) { throw new IOException(e); } diff --git a/geowebcache/web/src/test/java/org/geowebcache/jetty/RestIntegrationTest.java b/geowebcache/web/src/test/java/org/geowebcache/jetty/RestIntegrationTest.java index 17b27ab19..568891b3c 100644 --- a/geowebcache/web/src/test/java/org/geowebcache/jetty/RestIntegrationTest.java +++ b/geowebcache/web/src/test/java/org/geowebcache/jetty/RestIntegrationTest.java @@ -15,6 +15,7 @@ */ package org.geowebcache.jetty; +import static org.apache.hc.client5.http.routing.RoutingSupport.determineHost; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.describedAs; @@ -30,7 +31,6 @@ import java.io.StringWriter; import java.net.URI; import java.nio.charset.Charset; -import java.nio.charset.StandardCharsets; import java.util.Arrays; import org.apache.commons.io.IOUtils; import org.apache.hc.client5.http.classic.methods.HttpDelete; @@ -39,8 +39,11 @@ import org.apache.hc.client5.http.classic.methods.HttpPut; import org.apache.hc.client5.http.classic.methods.HttpUriRequest; import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; -import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse; +import org.apache.hc.core5.http.ClassicHttpResponse; import org.apache.hc.core5.http.ContentType; +import org.apache.hc.core5.http.HttpException; +import org.apache.hc.core5.http.io.HttpClientResponseHandler; +import org.apache.hc.core5.http.io.entity.EntityUtils; import org.apache.hc.core5.http.io.entity.StringEntity; import org.apache.hc.core5.http.message.StatusLine; import org.custommonkey.xmlunit.XMLUnit; @@ -130,9 +133,8 @@ public void setUp() throws Exception { 120 """; - try (CloseableHttpResponse response = + try (ClassicHttpResponse response = handlePut(URI.create("/geowebcache/rest/global"), admin.getClient(), globalUpdate)) { - System.out.println(response.getEntity().toString()); assertEquals(200, response.getCode()); } } @@ -149,7 +151,7 @@ Matcher hasXPath(final String xpathExpr) { @Test public void testGetLogo() throws Exception { - try (CloseableHttpResponse response = + try (ClassicHttpResponse response = handleGet(URI.create("/geowebcache/rest/web/geowebcache_logo.png"), anonymous.getClient())) { assertEquals(200, response.getCode()); } @@ -157,7 +159,7 @@ public void testGetLogo() throws Exception { @Test public void testGetCss() throws Exception { - try (CloseableHttpResponse response = + try (ClassicHttpResponse response = handleGet(URI.create("/geowebcache/rest/web/gwc.css"), anonymous.getClient())) { assertEquals(200, response.getCode()); } @@ -165,7 +167,7 @@ public void testGetCss() throws Exception { @Test public void testGetBadWebResource() throws Exception { - try (CloseableHttpResponse response = + try (ClassicHttpResponse response = handleGet(URI.create("/geowebcache/rest/web/thisDoesNotExist"), anonymous.getClient())) { assertEquals(404, response.getCode()); } @@ -175,8 +177,7 @@ public void testGetBadWebResource() throws Exception { @Test public void testGetGlobal() throws Exception { - try (CloseableHttpResponse response = - handleGet(URI.create("/geowebcache/rest/global.xml"), admin.getClient())) { + try (ClassicHttpResponse response = handleGet(URI.create("/geowebcache/rest/global.xml"), admin.getClient())) { assertEquals(200, response.getCode()); Document doc = getResponseEntityAsXML(response); @@ -245,13 +246,12 @@ public void testPutGlobal() throws Exception { testGetGlobal(); - try (CloseableHttpResponse response = + try (ClassicHttpResponse response = handlePut(URI.create("/geowebcache/rest/global"), admin.getClient(), globalUpdate)) { assertEquals(200, response.getCode()); } - try (CloseableHttpResponse response = - handleGet(URI.create("/geowebcache/rest/global.xml"), admin.getClient())) { + try (ClassicHttpResponse response = handleGet(URI.create("/geowebcache/rest/global.xml"), admin.getClient())) { assertEquals(200, response.getCode()); Document doc = getResponseEntityAsXML(response); @@ -283,14 +283,13 @@ public void testPutGlobalRoundTrip() throws Exception { testGetGlobal(); final String globalUpdate; - try (CloseableHttpResponse response = - handleGet(URI.create("/geowebcache/rest/global.xml"), admin.getClient())) { + try (ClassicHttpResponse response = handleGet(URI.create("/geowebcache/rest/global.xml"), admin.getClient())) { assertEquals(200, response.getCode()); globalUpdate = getResponseEntity(response); } - try (CloseableHttpResponse response = + try (ClassicHttpResponse response = handlePut(URI.create("/geowebcache/rest/global"), admin.getClient(), globalUpdate)) { assertEquals(200, response.getCode()); response.close(); @@ -308,13 +307,12 @@ public void testPutGlobalPartial() throws Exception { testGetGlobal(); - try (CloseableHttpResponse response = + try (ClassicHttpResponse response = handlePut(URI.create("/geowebcache/rest/global"), admin.getClient(), globalUpdate)) { assertEquals(200, response.getCode()); } - try (CloseableHttpResponse response = - handleGet(URI.create("/geowebcache/rest/global.xml"), admin.getClient())) { + try (ClassicHttpResponse response = handleGet(URI.create("/geowebcache/rest/global.xml"), admin.getClient())) { assertEquals(200, response.getCode()); Document doc = getResponseEntityAsXML(response); @@ -348,8 +346,7 @@ public void testPutGlobalLock() throws Exception { final String globalUpdate = "nioLock"; - try (CloseableHttpResponse response = - handleGet(URI.create("/geowebcache/rest/global.xml"), admin.getClient())) { + try (ClassicHttpResponse response = handleGet(URI.create("/geowebcache/rest/global.xml"), admin.getClient())) { assertEquals(200, response.getCode()); Document doc = getResponseEntityAsXML(response); @@ -359,13 +356,12 @@ public void testPutGlobalLock() throws Exception { assertThat(doc, hasXPath("count(/global/lockProvider)", equalTo("0"))); } - try (CloseableHttpResponse response = + try (ClassicHttpResponse response = handlePut(URI.create("/geowebcache/rest/global"), admin.getClient(), globalUpdate)) { assertEquals(200, response.getCode()); } - try (CloseableHttpResponse response = - handleGet(URI.create("/geowebcache/rest/global.xml"), admin.getClient())) { + try (ClassicHttpResponse response = handleGet(URI.create("/geowebcache/rest/global.xml"), admin.getClient())) { assertEquals(200, response.getCode()); Document doc = getResponseEntityAsXML(response); @@ -379,7 +375,7 @@ public void testPutGlobalReadOnly() throws Exception { // PUT a value that is read-only final String globalUpdate = "foobar"; - try (CloseableHttpResponse response = + try (ClassicHttpResponse response = handlePut(URI.create("/geowebcache/rest/global"), admin.getClient(), globalUpdate)) { assertEquals(400, response.getCode()); } @@ -436,9 +432,7 @@ public void testCreateUpdateDelete() throws Exception { + layers + "", ContentType.APPLICATION_XML)); - try (CloseableHttpResponse response = admin.getClient().execute(request)) { - assertThat(new StatusLine(response), hasProperty("statusCode", equalTo(200))); - } + admin.getClient().execute(request, new StatusCheckHandler(200)); doGetXML("rest/layers.xml", admin.getClient(), equalTo(200), doc -> { assertThat( @@ -468,9 +462,7 @@ public void testCreateUpdateDelete() throws Exception { + layers + "", ContentType.APPLICATION_XML)); - try (CloseableHttpResponse response = admin.getClient().execute(request)) { - assertThat(new StatusLine(response), hasProperty("statusCode", equalTo(200))); - } + admin.getClient().execute(request, new StatusCheckHandler(200)); doGetXML("rest/layers/" + layerName + ".xml", admin.getClient(), equalTo(200), doc -> { assertThat(doc, hasXPath("/wmsLayer/name", equalTo(layerName))); assertThat(doc, hasXPath("/wmsLayer/wmsUrl/string", equalTo(url2))); @@ -489,9 +481,7 @@ public void testCreateUpdateDelete() throws Exception { { final HttpDelete request = new HttpDelete(jetty.getUri().resolve("rest/layers/").resolve(layerName + ".xml")); - try (CloseableHttpResponse response = admin.getClient().execute(request)) { - assertThat(new StatusLine(response), hasProperty("statusCode", equalTo(200))); - } + admin.getClient().execute(request, new StatusCheckHandler(200)); doGetXML("rest/layers.xml", admin.getClient(), equalTo(200), doc -> { assertThat(doc, not(hasXPath("/layers/layer[name/text()='" + layerName + "']"))); @@ -499,9 +489,7 @@ public void testCreateUpdateDelete() throws Exception { final HttpGet request2 = new HttpGet(jetty.getUri().resolve("rest/layers/").resolve(layerName + ".xml")); - try (CloseableHttpResponse response = admin.getClient().execute(request2)) { - assertEquals(404, response.getCode()); - } + admin.getClient().execute(request2, new StatusCheckHandler(404)); } // GetCap { @@ -592,14 +580,14 @@ client, equalTo(401), doc -> { protected void testSecured(HttpUriRequest request, Matcher authenticatedStatus) throws Exception { { CloseableHttpClient client = admin.getClient(); - try (CloseableHttpResponse response = client.execute(request); + try (ClassicHttpResponse response = client.executeOpen(determineHost(request), request, null); InputStream in = response.getEntity().getContent()) { assertThat(new StatusLine(response), hasProperty("statusCode", authenticatedStatus)); } } for (CloseableHttpClient client : Arrays.asList(anonymous.getClient(), notAUser.getClient(), badPassword.getClient())) { - try (CloseableHttpResponse response = client.execute(request); + try (ClassicHttpResponse response = client.executeOpen(determineHost(request), request, null); InputStream in = response.getEntity().getContent()) { final int code = 401; assertThat( @@ -628,7 +616,7 @@ public void testAddLayer() throws Exception { @Test public void testGetBlobStoresXML() throws Exception { - try (CloseableHttpResponse response = + try (ClassicHttpResponse response = handleGet(URI.create("/geowebcache/rest/blobstores.xml"), admin.getClient())) { assertEquals(200, response.getCode()); @@ -647,7 +635,7 @@ public void testGetBlobStoresXML() throws Exception { @Test public void testGetBlobStoresJSON() throws Exception { - try (CloseableHttpResponse response = + try (ClassicHttpResponse response = handleGet(URI.create("/geowebcache/rest/blobstores.json"), admin.getClient())) { assertEquals(200, response.getCode()); @@ -659,7 +647,7 @@ public void testGetBlobStoresJSON() throws Exception { @Test public void testGetBlobStoreXML() throws Exception { - try (CloseableHttpResponse response = + try (ClassicHttpResponse response = handleGet(URI.create("/geowebcache/rest/blobstores/defaultCache.xml"), admin.getClient())) { assertEquals(200, response.getCode()); @@ -674,7 +662,7 @@ public void testGetBlobStoreXML() throws Exception { @Test public void testGetBlobStoreJSON() throws Exception { - try (CloseableHttpResponse response = + try (ClassicHttpResponse response = handleGet(URI.create("/geowebcache/rest/blobstores/defaultCache.json"), admin.getClient())) { assertEquals(200, response.getCode()); @@ -699,17 +687,17 @@ public void testPutBlobStoreCreateModifyDelete() throws Exception { """; // Make it sure doesn't exist - try (CloseableHttpResponse response = + try (ClassicHttpResponse response = handleGet(URI.create("/geowebcache/rest/blobstores/newCache.xml"), admin.getClient())) { assertEquals(404, response.getCode()); } - try (CloseableHttpResponse response = + try (ClassicHttpResponse response = handlePut(URI.create("/geowebcache/rest/blobstores/newCache"), admin.getClient(), blobStore)) { assertEquals(201, response.getCode()); } - try (CloseableHttpResponse response = + try (ClassicHttpResponse response = handleGet(URI.create("/geowebcache/rest/blobstores/newCache.xml"), admin.getClient())) { assertEquals(200, response.getCode()); @@ -730,12 +718,12 @@ public void testPutBlobStoreCreateModifyDelete() throws Exception { 2048 """; - try (CloseableHttpResponse response = + try (ClassicHttpResponse response = handlePut(URI.create("/geowebcache/rest/blobstores/newCache"), admin.getClient(), blobStoreUpdate)) { assertEquals(200, response.getCode()); } - try (CloseableHttpResponse response = + try (ClassicHttpResponse response = handleGet(URI.create("/geowebcache/rest/blobstores/newCache.xml"), admin.getClient())) { assertEquals(200, response.getCode()); @@ -747,12 +735,12 @@ public void testPutBlobStoreCreateModifyDelete() throws Exception { assertThat(doc, hasXPath("//fileSystemBlockSize", equalTo("2048"))); } - try (CloseableHttpResponse response = + try (ClassicHttpResponse response = handleDelete(URI.create("/geowebcache/rest/blobstores/newCache.xml"), admin.getClient())) { assertEquals(200, response.getCode()); } - try (CloseableHttpResponse response = + try (ClassicHttpResponse response = handleGet(URI.create("/geowebcache/rest/blobstores/newCache.xml"), admin.getClient())) { assertEquals(404, response.getCode()); } @@ -762,7 +750,7 @@ public void testPutBlobStoreCreateModifyDelete() throws Exception { @Test public void testGetGridSetsXML() throws Exception { - try (CloseableHttpResponse response = + try (ClassicHttpResponse response = handleGet(URI.create("/geowebcache/rest/gridsets.xml"), admin.getClient())) { assertEquals(200, response.getCode()); @@ -784,7 +772,7 @@ public void testGetGridSetsXML() throws Exception { @Test public void testGetGridSetsJSON() throws Exception { - try (CloseableHttpResponse response = + try (ClassicHttpResponse response = handleGet(URI.create("/geowebcache/rest/gridsets.json"), admin.getClient())) { assertEquals(200, response.getCode()); @@ -796,7 +784,7 @@ public void testGetGridSetsJSON() throws Exception { @Test public void testGetGridSetXML() throws Exception { - try (CloseableHttpResponse response = + try (ClassicHttpResponse response = handleGet(URI.create("/geowebcache/rest/gridsets/EPSG:2163.xml"), admin.getClient())) { assertEquals(200, response.getCode()); @@ -819,7 +807,7 @@ public void testGetGridSetXML() throws Exception { @Test public void testGetGridSetJSON() throws Exception { - try (CloseableHttpResponse response = + try (ClassicHttpResponse response = handleGet(URI.create("/geowebcache/rest/gridsets/EPSG:2163.json"), admin.getClient())) { assertEquals(200, response.getCode()); @@ -873,17 +861,17 @@ public void testPutGridSetCreateModifyDelete() throws Exception { """; // Make it sure doesn't exist - try (CloseableHttpResponse response = + try (ClassicHttpResponse response = handleGet(URI.create("/geowebcache/rest/gridsets/testGridset.xml"), admin.getClient())) { assertEquals(404, response.getCode()); } - try (CloseableHttpResponse response = + try (ClassicHttpResponse response = handlePut(URI.create("/geowebcache/rest/gridsets/testGridset.xml"), admin.getClient(), gridSet)) { assertEquals(201, response.getCode()); } - try (CloseableHttpResponse response = + try (ClassicHttpResponse response = handleGet(URI.create("/geowebcache/rest/gridsets/testGridset.xml"), admin.getClient())) { assertEquals(200, response.getCode()); @@ -935,12 +923,12 @@ public void testPutGridSetCreateModifyDelete() throws Exception { false """; - try (CloseableHttpResponse response = + try (ClassicHttpResponse response = handlePut(URI.create("/geowebcache/rest/gridsets/testGridset"), admin.getClient(), gridSetUpdate)) { assertEquals(200, response.getCode()); } - try (CloseableHttpResponse response = + try (ClassicHttpResponse response = handleGet(URI.create("/geowebcache/rest/gridsets/testGridset.xml"), admin.getClient())) { assertEquals(200, response.getCode()); @@ -955,12 +943,12 @@ public void testPutGridSetCreateModifyDelete() throws Exception { assertThat(doc, hasXPath("//tileHeight", equalTo("200"))); } - try (CloseableHttpResponse response = + try (ClassicHttpResponse response = handleDelete(URI.create("/geowebcache/rest/gridsets/testGridset.xml"), admin.getClient())) { assertEquals(200, response.getCode()); } - try (CloseableHttpResponse response = + try (ClassicHttpResponse response = handleGet(URI.create("/geowebcache/rest/gridsets/testGridset.xml"), admin.getClient())) { assertEquals(404, response.getCode()); } @@ -970,7 +958,7 @@ public void testPutGridSetCreateModifyDelete() throws Exception { @Test public void testDiskQuotaXML() throws Exception { - try (CloseableHttpResponse response = + try (ClassicHttpResponse response = handleGet(URI.create("/geowebcache/rest/diskquota.xml"), admin.getClient())) { assertEquals(200, response.getCode()); if (response.getCode() == 200) { @@ -988,7 +976,7 @@ public void testDiskQuotaXML() throws Exception { @Test public void testDiskQuotaJson() throws Exception { - try (CloseableHttpResponse response = + try (ClassicHttpResponse response = handleGet(URI.create("/geowebcache/rest/diskquota.json"), admin.getClient())) { assertEquals(200, response.getCode()); if (response.getCode() == 200) { @@ -1036,7 +1024,7 @@ public void testSeedPost() throws Exception { + // ""; - try (CloseableHttpResponse response = + try (ClassicHttpResponse response = handlePost(URI.create("/geowebcache/rest/seed/states.xml"), admin.getClient(), seedLayer)) { assertEquals(200, response.getCode()); } @@ -1044,37 +1032,35 @@ public void testSeedPost() throws Exception { @Test public void testSeedGet() throws Exception { - try (CloseableHttpResponse response = - handleGet(URI.create("/geowebcache/rest/seed/states"), admin.getClient())) { + try (ClassicHttpResponse response = handleGet(URI.create("/geowebcache/rest/seed/states"), admin.getClient())) { assertEquals(200, response.getCode()); } } @Test public void testSeedGetNoLayer() throws Exception { - try (CloseableHttpResponse response = handleGet(URI.create("/geowebcache/rest/seed"), admin.getClient())) { + try (ClassicHttpResponse response = handleGet(URI.create("/geowebcache/rest/seed"), admin.getClient())) { assertEquals(405, response.getCode()); } } @Test public void testSeedGetSeedForm() throws Exception { - try (CloseableHttpResponse response = - handleGet(URI.create("/geowebcache/rest/seed/states"), admin.getClient())) { + try (ClassicHttpResponse response = handleGet(URI.create("/geowebcache/rest/seed/states"), admin.getClient())) { assertEquals(200, response.getCode()); } } @Test public void testSeedGetJson() throws Exception { - try (CloseableHttpResponse response = handleGet(URI.create("/geowebcache/rest/seed.json"), admin.getClient())) { + try (ClassicHttpResponse response = handleGet(URI.create("/geowebcache/rest/seed.json"), admin.getClient())) { assertEquals(200, response.getCode()); } } @Test public void testSeedGetLayerJson() throws Exception { - try (CloseableHttpResponse response = + try (ClassicHttpResponse response = handleGet(URI.create("/geowebcache/rest/seed/states.json"), admin.getClient())) { assertEquals(200, response.getCode()); } @@ -1082,7 +1068,7 @@ public void testSeedGetLayerJson() throws Exception { @Test public void testSeedGetLayerXml() throws Exception { - try (CloseableHttpResponse response = + try (ClassicHttpResponse response = handleGet(URI.create("/geowebcache/rest/seed/states.xml"), admin.getClient())) { assertEquals(200, response.getCode()); } @@ -1091,7 +1077,7 @@ public void testSeedGetLayerXml() throws Exception { @Test public void testKillAll() throws Exception { String killCommand = "kill_all=all"; - try (CloseableHttpResponse response = + try (ClassicHttpResponse response = handlePost(URI.create("/geowebcache/rest/seed"), admin.getClient(), killCommand)) { assertEquals(200, response.getCode()); } @@ -1100,7 +1086,7 @@ public void testKillAll() throws Exception { @Test public void testLayerKillAll() throws Exception { String killCommand = "kill_all=all"; - try (CloseableHttpResponse response = + try (ClassicHttpResponse response = handlePost(URI.create("/geowebcache/rest/seed/states"), admin.getClient(), killCommand)) { assertEquals(200, response.getCode()); } @@ -1146,54 +1132,53 @@ public void testNewFileBlobstoreDontDeleteExistingContent() throws Exception { String layer2truncate = "" + layer2name + ""; // Make it sure the blobstore doesn't exist already as that would invalidate the test - try (CloseableHttpResponse response = + try (ClassicHttpResponse response = handleGet(URI.create("/geowebcache/rest/blobstores/maliciousCache.xml"), admin.getClient())) { - System.out.println(response); assertEquals(404, response.getCode()); } // Create a store - try (CloseableHttpResponse response = + try (ClassicHttpResponse response = handlePut(URI.create("/geowebcache/rest/blobstores/maliciousCache"), admin.getClient(), blobStore)) { } catch (Exception e) { // Ignore and keep going } // Create layers with the names of entries in that directory - try (CloseableHttpResponse response = + try (ClassicHttpResponse response = handlePut(URI.create("/geowebcache/rest/layers/" + layer1name), admin.getClient(), layer1)) { } catch (Exception e) { // Ignore and keep going } - try (CloseableHttpResponse response = + try (ClassicHttpResponse response = handlePut(URI.create("/geowebcache/rest/layers/" + layer2name), admin.getClient(), layer2)) { } catch (Exception e) { // Ignore and keep going } // Mass Truncate those layers - try (CloseableHttpResponse response = + try (ClassicHttpResponse response = handlePost(URI.create("/geowebcache/rest/masstruncate"), admin.getClient(), layer1truncate)) {} - try (CloseableHttpResponse response = + try (ClassicHttpResponse response = handlePost(URI.create("/geowebcache/rest/masstruncate"), admin.getClient(), layer2truncate)) { } catch (Exception e) { // Ignore and keep going } // Delete the layers - try (CloseableHttpResponse response = + try (ClassicHttpResponse response = handleDelete(URI.create("/geowebcache/rest/layers/" + layer1name), admin.getClient())) { } catch (Exception e) { // Ignore and keep going } - try (CloseableHttpResponse response = + try (ClassicHttpResponse response = handleDelete(URI.create("/geowebcache/rest/layers/" + layer2name), admin.getClient())) { } catch (Exception e) { // Ignore and keep going } // Delete the store - try (CloseableHttpResponse response = + try (ClassicHttpResponse response = handleDelete(URI.create("/geowebcache/rest/blobstores/maliciousCache"), admin.getClient())) { } catch (Exception e) { // Ignore and keep going @@ -1207,7 +1192,7 @@ public void testNewFileBlobstoreDontDeleteExistingContent() throws Exception { /* Utility methods ***************************************************************************/ - private Document getResponseEntityAsXML(CloseableHttpResponse response) throws Exception { + private Document getResponseEntityAsXML(ClassicHttpResponse response) throws Exception { Document doc = XMLUnit.buildTestDocument(new InputSource(response.getEntity().getContent())); @@ -1216,17 +1201,17 @@ private Document getResponseEntityAsXML(CloseableHttpResponse response) throws E return doc; } - private JSONObject getResponseEntityAsJSONObject(CloseableHttpResponse response) throws Exception { + private JSONObject getResponseEntityAsJSONObject(ClassicHttpResponse response) throws Exception { JSONObject jsonObject = new JSONObject(getResponseEntity(response)); return jsonObject; } - private JSONArray getResponseEntityAsJSONArray(CloseableHttpResponse response) throws Exception { + private JSONArray getResponseEntityAsJSONArray(ClassicHttpResponse response) throws Exception { JSONArray jsonObject = new JSONArray(getResponseEntity(response)); return jsonObject; } - private String getResponseEntity(CloseableHttpResponse response) { + private String getResponseEntity(ClassicHttpResponse response) { String doc; try { StringWriter writer = new StringWriter(); @@ -1239,10 +1224,9 @@ private String getResponseEntity(CloseableHttpResponse response) { return doc; } - private CloseableHttpResponse handleGet(URI uri, CloseableHttpClient client) throws Exception { + private ClassicHttpResponse handleGet(URI uri, CloseableHttpClient client) throws Exception { HttpGet request = new HttpGet(jetty.getUri().resolve(uri)); - CloseableHttpResponse response = client.execute(request); - return response; + return client.executeOpen(determineHost(request), request, null); } interface Assertions { @@ -1258,37 +1242,50 @@ void doGetXML(URI uri, CloseableHttpClient client, Matcher statusMatche throws Exception { final HttpGet request = new HttpGet(jetty.getUri().resolve(uri)); final Document doc; - try (CloseableHttpResponse response = client.execute(request); + try (ClassicHttpResponse response = client.executeOpen(determineHost(request), request, null); InputStream in = response.getEntity().getContent()) { if (response.getCode() != 401) { doc = XMLUnit.buildTestDocument(new InputSource(in)); body.accept(doc); - } else { - System.out.println(IOUtils.toString(in, StandardCharsets.UTF_8)); } assertThat(new StatusLine(response), hasProperty("statusCode", statusMatcher)); } } - private CloseableHttpResponse handleDelete(URI uri, CloseableHttpClient client) throws Exception { + private ClassicHttpResponse handleDelete(URI uri, CloseableHttpClient client) throws Exception { HttpDelete request = new HttpDelete(jetty.getUri().resolve(uri)); - CloseableHttpResponse response = client.execute(request); - return response; + return client.executeOpen(determineHost(request), request, null); } - private CloseableHttpResponse handlePut(URI uri, CloseableHttpClient client, String data) throws Exception { + @SuppressWarnings("PMD.CloseResource") + private ClassicHttpResponse handlePut(URI uri, CloseableHttpClient client, String data) throws Exception { HttpPut request = new HttpPut(jetty.getUri().resolve(uri)); StringEntity entity = new StringEntity(data, ContentType.TEXT_XML); request.setEntity(entity); - CloseableHttpResponse response = client.execute(request); - return response; + return client.executeOpen(determineHost(request), request, null); } - private CloseableHttpResponse handlePost(URI uri, CloseableHttpClient client, String data) throws Exception { + @SuppressWarnings("PMD.CloseResource") + private ClassicHttpResponse handlePost(URI uri, CloseableHttpClient client, String data) throws Exception { HttpPost request = new HttpPost(jetty.getUri().resolve(uri)); StringEntity entity = new StringEntity(data, ContentType.TEXT_XML); request.setEntity(entity); - CloseableHttpResponse response = client.execute(request); - return response; + return client.executeOpen(determineHost(request), request, null); + } + + private static class StatusCheckHandler implements HttpClientResponseHandler { + + private final int expectedStatus; + + public StatusCheckHandler(int expectedStatus) { + this.expectedStatus = expectedStatus; + } + + @Override + public Object handleResponse(ClassicHttpResponse response) throws HttpException, IOException { + assertEquals(expectedStatus, response.getCode()); + EntityUtils.consumeQuietly(response.getEntity()); + return null; + } } }