Skip to content

Commit

Permalink
GEOS-8827: some changes following up andrea's feedbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
mbarto committed Aug 2, 2018
1 parent b5da1de commit 465d3ee
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 81 deletions.
1 change: 0 additions & 1 deletion src/community/geogig/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,6 @@
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.5</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
2 changes: 0 additions & 2 deletions src/community/onelogin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,11 @@
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.5</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
15 changes: 5 additions & 10 deletions src/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -738,16 +738,6 @@
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.5</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient-cache</artifactId>
<version>4.5.5</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
Expand Down Expand Up @@ -803,6 +793,11 @@
<artifactId>httpclient</artifactId>
<version>4.5.5</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient-cache</artifactId>
<version>4.5.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ WMSAdminPage.disableFeaturesReproject = Disable the reprojection of GetFeatureIn
WMSAdminPage.remoteStylesCache = Remote Styles Cache
WMSAdminPage.cacheEnabled = Enabled
WMSAdminPage.cacheEntries = Max entries
WMSAdminPage.cacheEntrySize = Max entry size
WMSAdminPage.cacheEntrySize = Max entry size (Kb)

MimeTypesFormComponent.selectedHeader =Allowed MIME types
MimeTypesFormComponent.availableHeader =Available MIME types
Expand Down
24 changes: 23 additions & 1 deletion src/wms/src/main/java/org/geoserver/wms/CacheConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* @author maurobartolomeoli@gmail.com
*/
public class CacheConfiguration {
public class CacheConfiguration implements Cloneable {

private boolean enabled;
private int maxEntries = 1000;
Expand All @@ -25,6 +25,13 @@ public CacheConfiguration(boolean enabled) {
this.enabled = enabled;
}

public CacheConfiguration(boolean enabled, int maxEntries, long maxEntrySize) {
super();
this.enabled = enabled;
this.maxEntries = maxEntries;
this.maxEntrySize = maxEntrySize;
}

public boolean isEnabled() {
return enabled;
}
Expand Down Expand Up @@ -63,4 +70,19 @@ public long getMaxEntrySize() {
public void setMaxEntrySize(long maxEntrySize) {
this.maxEntrySize = maxEntrySize;
}

@Override
public boolean equals(Object obj) {
if (obj instanceof CacheConfiguration) {
CacheConfiguration other = (CacheConfiguration) obj;
return other.enabled == enabled
&& other.maxEntries == maxEntries
&& other.maxEntrySize == maxEntrySize;
}
return false;
}

public Object clone() {
return new CacheConfiguration(enabled, maxEntries, maxEntrySize);
}
}
2 changes: 0 additions & 2 deletions src/wms/src/main/java/org/geoserver/wms/WMS.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,6 @@ public class WMS implements ApplicationContextAware {

public static final String FEATURES_REPROJECTION_DISABLED = "featuresReprojectionDisabled";

public static final String CACHE_REMOTE_STYLES = "cacheRemoteStyles";

static final Logger LOGGER = Logging.getLogger(WMS.class);

public static final String WEB_CONTAINER_KEY = "WMS";
Expand Down
189 changes: 125 additions & 64 deletions src/wms/src/main/java/org/geoserver/wms/map/GetMapKvpRequestReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
import org.geoserver.catalog.Styles;
import org.geoserver.catalog.WMSLayerInfo;
import org.geoserver.catalog.util.ReaderDimensionsAccessor;
import org.geoserver.config.ConfigurationListenerAdapter;
import org.geoserver.config.ServiceInfo;
import org.geoserver.ows.HttpServletRequestAware;
import org.geoserver.ows.KvpRequestReader;
import org.geoserver.ows.LocalHttpServletRequest;
Expand All @@ -55,6 +57,7 @@
import org.geoserver.wms.MapLayerInfo;
import org.geoserver.wms.WMS;
import org.geoserver.wms.WMSErrorCode;
import org.geoserver.wms.WMSInfo;
import org.geotools.coverage.grid.io.GridCoverage2DReader;
import org.geotools.data.DataStore;
import org.geotools.data.simple.SimpleFeatureSource;
Expand All @@ -77,12 +80,14 @@
import org.opengis.filter.expression.PropertyName;
import org.opengis.filter.sort.SortBy;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.springframework.beans.factory.DisposableBean;
import org.vfny.geoserver.util.Requests;
import org.vfny.geoserver.util.SLDValidator;
import org.xml.sax.EntityResolver;
import org.xml.sax.SAXException;

public class GetMapKvpRequestReader extends KvpRequestReader implements HttpServletRequestAware {
public class GetMapKvpRequestReader extends KvpRequestReader
implements HttpServletRequestAware, DisposableBean {

private static Map<String, Integer> interpolationMethods;

Expand Down Expand Up @@ -110,6 +115,9 @@ public class GetMapKvpRequestReader extends KvpRequestReader implements HttpServ

/** HTTP client used to fetch remote styles * */
CloseableHttpClient httpClient;

/** Current cache configuration. */
private CacheConfiguration cacheCfg;
/**
* This flags allows the kvp reader to go beyond the SLD library mode specification and match
* the first style that can be applied to a given layer. This is for backwards compatibility
Expand All @@ -122,20 +130,62 @@ public GetMapKvpRequestReader(WMS wms) {

public GetMapKvpRequestReader(WMS wms, HttpClientConnectionManager manager) {
super(GetMapRequest.class);
// configure the http client used to fetch remote styles
RequestConfig requestConfig = RequestConfig.DEFAULT;

wms.getGeoServer()
.addListener(
new ConfigurationListenerAdapter() {

@Override
public void handleServiceChange(
ServiceInfo service,
List<String> propertyNames,
List<Object> oldValues,
List<Object> newValues) {
if (service instanceof WMSInfo) {
WMSInfo info = (WMSInfo) service;
CacheConfiguration newCacheCfg = info.getCacheConfiguration();
if (!newCacheCfg.equals(cacheCfg)) {
createHttpClient(
wms,
manager,
requestConfig,
(CacheConfiguration) newCacheCfg.clone());
}
}
}
});
this.wms = wms;
this.entityResolverProvider = new EntityResolverProvider(wms.getGeoServer());
createHttpClient(
wms,
manager,
requestConfig,
(CacheConfiguration) wms.getRemoteResourcesCacheConfiguration().clone());
}

// configure the http client used to fetch remote styles
RequestConfig requestConfig = RequestConfig.custom().build();

CacheConfiguration cacheCfg = wms.getRemoteResourcesCacheConfiguration();
if (cacheCfg != null && cacheCfg.isEnabled()) {
private synchronized void createHttpClient(
WMS wms,
HttpClientConnectionManager manager,
RequestConfig requestConfig,
CacheConfiguration cfg) {
this.cacheCfg = cfg;
if (cfg != null && cfg.isEnabled()) {
CacheConfig cacheConfig =
CacheConfig.custom()
.setMaxCacheEntries(cacheCfg.getMaxEntries())
.setMaxObjectSize(cacheCfg.getMaxEntrySize())
.build();

if (httpClient != null) {
try {
httpClient.close();
} catch (IOException e) {
if (LOGGER.isLoggable(Level.SEVERE)) {
LOGGER.log(Level.SEVERE, "Error closing HTTPClient", e);
}
}
}
this.httpClient =
CachingHttpClientBuilder.create()
.setCacheConfig(cacheConfig)
Expand Down Expand Up @@ -367,63 +417,7 @@ public GetMapRequest read(Object request, Map kvp, Map rawKvp) throws Exception

InputStream input = null;
if (styleUrl.getProtocol().toLowerCase().indexOf("http") == 0) {
HttpCacheContext cacheContext = HttpCacheContext.create();
CloseableHttpResponse response = null;
try {
HttpGet httpget = new HttpGet(styleUrl.toExternalForm());
response = httpClient.execute(httpget, cacheContext);

if (cacheContext != null) {
CacheResponseStatus responseStatus = cacheContext.getCacheResponseStatus();
if (responseStatus != null) {
switch (responseStatus) {
case CACHE_HIT:
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.fine(
"A response was generated from the cache with "
+ "no requests sent upstream");
}
break;
case CACHE_MODULE_RESPONSE:
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.fine(
"The response was generated directly by the "
+ "caching module");
}
break;
case CACHE_MISS:
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.fine("The response came from an upstream server");
}
break;
case VALIDATED:
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.fine(
"The response was generated from the cache "
+ "after validating the entry with the origin server");
}
break;
}
}
}
input = response.getEntity().getContent();
ByteArrayInputStream styleData =
new ByteArrayInputStream(IOUtils.toByteArray(input));
input.close();
input = styleData;
input.reset();
} catch (Exception ex) {
LOGGER.log(Level.WARNING, "Exception while getting SLD.", ex);
// KMS: Replace with a generic exception so it can't be used to port scan the
// local
// network.
throw new ServiceException("Error while getting SLD.");
} finally {
if (response != null) {
response.close();
}
}

input = getHttpInputStream(styleUrl);
} else {
try {
input = Requests.getInputStream(styleUrl);
Expand Down Expand Up @@ -694,6 +688,66 @@ public GetMapRequest read(Object request, Map kvp, Map rawKvp) throws Exception
return getMap;
}

private InputStream getHttpInputStream(URL styleUrl) throws IOException {
InputStream input = null;
HttpCacheContext cacheContext = HttpCacheContext.create();
CloseableHttpResponse response = null;
try {
HttpGet httpget = new HttpGet(styleUrl.toExternalForm());
response = httpClient.execute(httpget, cacheContext);

if (cacheContext != null) {
CacheResponseStatus responseStatus = cacheContext.getCacheResponseStatus();
if (responseStatus != null) {
switch (responseStatus) {
case CACHE_HIT:
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.fine(
"A response was generated from the cache with "
+ "no requests sent upstream");
}
break;
case CACHE_MODULE_RESPONSE:
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.fine(
"The response was generated directly by the "
+ "caching module");
}
break;
case CACHE_MISS:
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.fine("The response came from an upstream server");
}
break;
case VALIDATED:
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.fine(
"The response was generated from the cache "
+ "after validating the entry with the origin server");
}
break;
}
}
}
input = response.getEntity().getContent();
ByteArrayInputStream styleData = new ByteArrayInputStream(IOUtils.toByteArray(input));
input.close();
input = styleData;
input.reset();
return input;
} catch (Exception ex) {
LOGGER.log(Level.WARNING, "Exception while getting SLD.", ex);
// KMS: Replace with a generic exception so it can't be used to port scan the
// local
// network.
throw new ServiceException("Error while getting SLD.");
} finally {
if (response != null) {
response.close();
}
}
}

private List<Interpolation> parseInterpolations(
List<Object> requestedLayers, List<String> interpolationList) {
List<Interpolation> interpolations = new ArrayList<Interpolation>();
Expand Down Expand Up @@ -1408,4 +1462,11 @@ public boolean isLaxStyleMatchAllowed() {
public void setLaxStyleMatchAllowed(boolean laxStyleMatchAllowed) {
this.laxStyleMatchAllowed = laxStyleMatchAllowed;
}

@Override
public void destroy() throws Exception {
if (httpClient != null) {
httpClient.close();
}
}
}

0 comments on commit 465d3ee

Please sign in to comment.