diff --git a/src/main/java/org/gcube/spatial/data/geoutility/GeoGetStylesUtility.java b/src/main/java/org/gcube/spatial/data/geoutility/GeoGetStylesUtility.java index a21f90e..78e62af 100644 --- a/src/main/java/org/gcube/spatial/data/geoutility/GeoGetStylesUtility.java +++ b/src/main/java/org/gcube/spatial/data/geoutility/GeoGetStylesUtility.java @@ -32,38 +32,65 @@ public class GeoGetStylesUtility { private List geoStyles; private Map mapNcWmsStyles; private WmsUrlValidator validator; - - /** - * Instantiates a new geo get styles. - * - * @param wmsRequest the wms request - * @throws Exception the exception - */ - public GeoGetStylesUtility(String wmsRequest) throws Exception{ - this.wmsRequest = wmsRequest; - this.validator = new WmsUrlValidator(wmsRequest); - validator.parseWmsRequest(true, false); - loadStyles(); - } - + /** The Constant CONNECTION_TIMEOUT. */ + public static final int CONNECTION_TIMEOUT = 1000; //DEFAULT CONNECTION TIMEOUT /** * Instantiates a new geo get styles utility. * * @param wmsRequest the wms request + * @throws Exception the exception + */ + public GeoGetStylesUtility(String wmsRequest) throws Exception{ + this(wmsRequest, CONNECTION_TIMEOUT); + } + + + /** + * Instantiates a new geo get styles utility. + * + * @param wmsRequest the wms request + * @param connectionTimeout the connection timeout sets a specified timeout value, in milliseconds, to be used when opening URLConnection. + * @throws Exception the exception + */ + public GeoGetStylesUtility(String wmsRequest, int connectionTimeout) throws Exception{ + this.wmsRequest = wmsRequest; + this.validator = new WmsUrlValidator(wmsRequest); + validator.parseWmsRequest(true, false); + loadStyles(connectionTimeout); + } + + + /** + * Instantiates a new geo get styles utility. + * * @param validator the validator * @throws Exception the exception */ - public GeoGetStylesUtility(String wmsRequest, WmsUrlValidator validator) throws Exception{ + public GeoGetStylesUtility(WmsUrlValidator validator) throws Exception{ + this(validator, CONNECTION_TIMEOUT); + } + + + /** + * Instantiates a new geo get styles utility. + * + * @param validator the validator + * @param connectionTimeout the connection timeout sets a specified timeout value, in milliseconds, to be used when opening URLConnection. + * @throws Exception the exception + */ + public GeoGetStylesUtility(WmsUrlValidator validator, int connectionTimeout) throws Exception{ + this.wmsRequest = validator.getWmsRequest(); this.validator = validator; - loadStyles(); + loadStyles(connectionTimeout); } /** * Load styles. + * @param connectionTimeout the connection timeout sets a specified timeout value, in milliseconds, to be used when opening URLConnection. */ - private void loadStyles(){ + private void loadStyles(int connectionTimeout){ String uriWMSService = validator.getBaseWmsServiceUrl(); String layerName = validator.getValueOfParsedWMSParameter(WmsParameters.LAYERS); @@ -137,7 +164,7 @@ public class GeoGetStylesUtility { if(stylesRead==null || stylesRead.isEmpty()){ logger.trace("styles are empty or null - Trying to get styles by 'WMS Get Style'"); - WmsGetStyles wmsGetStyles = new WmsGetStyles(); + WmsGetStyles wmsGetStyles = new WmsGetStyles(connectionTimeout); styles = wmsGetStyles.getStylesFromWms(uriWMSService, layerName); if(wmsGetStyles.getResponseCode()==200){ @@ -146,7 +173,7 @@ public class GeoGetStylesUtility { logger.debug("Wms GetStyles not found, Trying to get styles by 'NcWmsGetMetadata'"); isNcWms = true; try{ - NcWmsLayerMetadata ncMetadata = NcWmsGetMetadata.getMetadata(uriWMSService, layerName); + NcWmsLayerMetadata ncMetadata = NcWmsGetMetadata.getMetadata(uriWMSService, layerName, connectionTimeout); if(ncMetadata!=null && ncMetadata.getResponseCode()==200){ if(ncMetadata.getSupportedStyles().size()>0){ @@ -224,6 +251,7 @@ public class GeoGetStylesUtility { return mapNcWmsStyles == null?new HashMap(1):mapNcWmsStyles; } + /* (non-Javadoc) * @see java.lang.Object#toString() */ diff --git a/src/main/java/org/gcube/spatial/data/geoutility/wms/NcWmsGetMetadata.java b/src/main/java/org/gcube/spatial/data/geoutility/wms/NcWmsGetMetadata.java index 523bd27..364c510 100644 --- a/src/main/java/org/gcube/spatial/data/geoutility/wms/NcWmsGetMetadata.java +++ b/src/main/java/org/gcube/spatial/data/geoutility/wms/NcWmsGetMetadata.java @@ -31,10 +31,10 @@ import org.json.JSONObject; */ public class NcWmsGetMetadata { - protected static final int CONNECTION_TIMEOUT = 1000; + /** The Constant CONNECTION_TIMEOUT. */ + protected static final int CONNECTION_TIMEOUT = 1000; //DEFAULT CONNECTION TIMEOUT public static Logger logger = Logger.getLogger(NcWmsGetMetadata.class); - /** * Gets the metadata. * @@ -44,6 +44,19 @@ public class NcWmsGetMetadata { * @throws Exception the exception */ public static NcWmsLayerMetadata getMetadata(String wmsServerUri, String layerName) throws Exception { + return getMetadata(wmsServerUri, layerName, CONNECTION_TIMEOUT); + } + + /** + * Gets the metadata. + * + * @param wmsServerUri the wms server uri + * @param layerName the layer name + * @param connectionTimeout the connection timeout sets a specified timeout value, in milliseconds, to be used when opening URLConnection. + * @return the metadata + * @throws Exception the exception + */ + public static NcWmsLayerMetadata getMetadata(String wmsServerUri, String layerName, int connectionTimeout) throws Exception { if(wmsServerUri==null || wmsServerUri.isEmpty()) throw new Exception("Invalid wms server uri"); @@ -57,7 +70,7 @@ public class NcWmsGetMetadata { try { String query = UrlEncoderUtil.encodeQuery(parameters); - return openConnectionGetMetadata(wmsServerUri, query); + return openConnectionGetMetadata(wmsServerUri, query, connectionTimeout); }catch (Exception e) { logger.error("Error Exception with url " + wmsServerUri); @@ -65,14 +78,17 @@ public class NcWmsGetMetadata { } } + + /** * Open connection get metadata. * * @param urlConn the url conn * @param query the query + * @param connectionTimeout the connection timeout sets a specified timeout value, in milliseconds, to be used when opening URLConnection. * @return the nc wms layer metadata */ - private static NcWmsLayerMetadata openConnectionGetMetadata(String urlConn, String query) { + private static NcWmsLayerMetadata openConnectionGetMetadata(String urlConn, String query, int connectionTimeout) { URL url; NcWmsLayerMetadata metadata = null; @@ -81,8 +97,11 @@ public class NcWmsGetMetadata { url = new URL(urlConn + "?" + query); URLConnection connection = url.openConnection(); - connection.setConnectTimeout(CONNECTION_TIMEOUT); - connection.setReadTimeout(CONNECTION_TIMEOUT + CONNECTION_TIMEOUT); + if(connectionTimeout<0) + connectionTimeout = CONNECTION_TIMEOUT; + + connection.setConnectTimeout(connectionTimeout); + connection.setReadTimeout(connectionTimeout + connectionTimeout); logger.trace("openConnectionGetMetadata on: " + url); diff --git a/src/main/java/org/gcube/spatial/data/geoutility/wms/WmsGetStyles.java b/src/main/java/org/gcube/spatial/data/geoutility/wms/WmsGetStyles.java index 2a32d20..706362b 100644 --- a/src/main/java/org/gcube/spatial/data/geoutility/wms/WmsGetStyles.java +++ b/src/main/java/org/gcube/spatial/data/geoutility/wms/WmsGetStyles.java @@ -37,7 +37,7 @@ import org.gcube.spatial.data.geoutility.util.XpathParserUtil; */ public class WmsGetStyles { - protected static final int CONNECTION_TIMEOUT = 1000; + private int CONNECTION_TIMEOUT = 1000; //DEFAULT CONNECTION TIMEOUT public static Logger logger = Logger.getLogger(WmsGetStyles.class); protected HashMap mappings; protected NamespaceContextMap context; @@ -58,6 +58,17 @@ public class WmsGetStyles { context = new NamespaceContextMap(mappings); } + /** + * Instantiates a new wms get styles. + * + * @param connectionTimeout the connection timeout sets a specified timeout value, in milliseconds, to be used when opening WMS GetStyles request. + */ + public WmsGetStyles(int connectionTimeout) { + this(); + if(connectionTimeout>0) + CONNECTION_TIMEOUT = connectionTimeout; + } + /** * Gets the styles from wms. * @@ -97,25 +108,6 @@ public class WmsGetStyles { logger.info("WMS GetStyles returning : "+styles.toString()); return styles; - - /*if(geosever.getScope()!=null && !geosever.getScope().isEmpty()){ - logger.trace("tentative get styles with scope " + geosever.getScope()); - styles = openConnectionGetStyles(urlConn, query); - - if(styles.size()==0){ - - logger.trace("tentative get styles with scope: "+geosever.getScope()+", has returned empty styles"); - String newUrlConn = urlConn.replace("/"+geosever.getScope(), ""); - logger.trace("new tentative get styles as base url without scope"); - return openConnectionGetStyles(newUrlConn, query); - - }else - return styles; - } - else - return styles;*/ -// return openConnectionGetStyles(urlConn, query); - }catch (Exception e) { logger.error("Error Exception with url " + urlConn); return styles;