From fd1134e56b63f97577e49d570cfd08c4272a65f3 Mon Sep 17 00:00:00 2001 From: "francesco.mangiacrapa" Date: Tue, 26 Jan 2016 13:18:03 +0000 Subject: [PATCH] 2054: Geo-Utility: an utility class to work with geo-spatial data Task-Url: https://support.d4science.org/issues/2054 Updated classes git-svn-id: https://svn.research-infrastructures.eu/d4science/gcube/trunk/spatial-data/geo-utility@122518 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../data/geoutility/GeoGetStylesUtility.java | 14 +++ .../data/geoutility/GeoWmsServiceUtility.java | 63 ++++++++++++ .../data/geoutility/wms/WmsGetStyles.java | 18 +++- .../data/geoutility/wms/WmsUrlValidator.java | 98 ++++++++++--------- 4 files changed, 144 insertions(+), 49 deletions(-) create mode 100644 src/main/java/org/gcube/spatial/data/geoutility/GeoWmsServiceUtility.java 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 e8eb6d0..a21f90e 100644 --- a/src/main/java/org/gcube/spatial/data/geoutility/GeoGetStylesUtility.java +++ b/src/main/java/org/gcube/spatial/data/geoutility/GeoGetStylesUtility.java @@ -46,6 +46,20 @@ public class GeoGetStylesUtility { loadStyles(); } + + + /** + * Instantiates a new geo get styles utility. + * + * @param wmsRequest the wms request + * @param validator the validator + * @throws Exception the exception + */ + public GeoGetStylesUtility(String wmsRequest, WmsUrlValidator validator) throws Exception{ + this.validator = validator; + loadStyles(); + } + /** * Load styles. */ diff --git a/src/main/java/org/gcube/spatial/data/geoutility/GeoWmsServiceUtility.java b/src/main/java/org/gcube/spatial/data/geoutility/GeoWmsServiceUtility.java new file mode 100644 index 0000000..c128999 --- /dev/null +++ b/src/main/java/org/gcube/spatial/data/geoutility/GeoWmsServiceUtility.java @@ -0,0 +1,63 @@ +/** + * + */ +package org.gcube.spatial.data.geoutility; + + + +/** + * The Class GeoWmsServiceUtility. + * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * Jan 25, 2016 + */ +public class GeoWmsServiceUtility { + + public static final String OWS = "ows"; + public static final String SERVICE_WMS = "service=wms"; + + + /** + * Instantiates a new geo service wms utility. + */ + public GeoWmsServiceUtility() { + } + + + /** + * Checks if is WMS service. + * + * @param wmsRequest the wms request + * @return true, if is WMS service + * @throws Exception the exception + */ + public static boolean isWMSService(String wmsRequest) throws Exception{ + validateWmsRequest(wmsRequest); + return wmsRequest.toLowerCase().lastIndexOf(SERVICE_WMS)>-1; + } + + + /** + * Validate wms request. + * + * @param wmsRequest the wms request + * @throws Exception the exception + */ + private static void validateWmsRequest(String wmsRequest) throws Exception{ + if(wmsRequest==null || wmsRequest.isEmpty()) + throw new Exception("Wms Request is null or empty!"); + } + + + /** + * Checks if is OWS serice. + * + * @param wmsRequest the wms request + * @return true, if is OWS serice + * @throws Exception the exception + */ + public static boolean isOWSSerice(String wmsRequest) throws Exception{ + validateWmsRequest(wmsRequest); + return wmsRequest.toLowerCase().contains(OWS); + } +} 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 a055a3b..2a32d20 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 @@ -83,11 +83,22 @@ public class WmsGetStyles { if(query==null || query.isEmpty()) return new ArrayList(); - logger.trace("tentative get styles..with query:" + geosever.getScope()); + logger.trace("tentative get styles.. with query:" + query); styles = openConnectionGetStyles(urlConn, query); + if(styles==null || styles.isEmpty()){ + if(geosever.getScope()!=null && !geosever.getScope().isEmpty()){ + logger.trace("trying to get styles with scope: "+geosever.getScope()); + String newUrlConn = urlConn.replace("/"+geosever.getScope(), ""); + logger.trace("new tentative get styles as base url without scope"); + styles = openConnectionGetStyles(newUrlConn, query); + } + } + logger.info("WMS GetStyles returning : "+styles.toString()); + return styles; - if(geosever.getScope()!=null && geosever.getScope().length()>0){ + + /*if(geosever.getScope()!=null && !geosever.getScope().isEmpty()){ logger.trace("tentative get styles with scope " + geosever.getScope()); styles = openConnectionGetStyles(urlConn, query); @@ -102,7 +113,8 @@ public class WmsGetStyles { return styles; } else - return openConnectionGetStyles(urlConn, query); + return styles;*/ +// return openConnectionGetStyles(urlConn, query); }catch (Exception e) { logger.error("Error Exception with url " + urlConn); diff --git a/src/main/java/org/gcube/spatial/data/geoutility/wms/WmsUrlValidator.java b/src/main/java/org/gcube/spatial/data/geoutility/wms/WmsUrlValidator.java index ae7cc38..a3d98a0 100644 --- a/src/main/java/org/gcube/spatial/data/geoutility/wms/WmsUrlValidator.java +++ b/src/main/java/org/gcube/spatial/data/geoutility/wms/WmsUrlValidator.java @@ -15,15 +15,15 @@ import org.gcube.spatial.data.geoutility.bean.WmsParameters; * The Class WmsUrlValidator. * * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it - * Dec 4, 2015 + * Jan 25, 2016 */ public class WmsUrlValidator { - private HashMap parametersMap = new HashMap(); + private HashMap mapWmsParameters = new HashMap(); private String wmsRequest; private String baseWmsServiceUrl; private String wmsParameters; - private String wmsNotStandardParameters = ""; + private String wmsNoStandardParameters = ""; private Map mapWmsNoStandardParams; public static Logger logger = Logger.getLogger(WmsUrlValidator.class); /** @@ -50,8 +50,8 @@ public class WmsUrlValidator { * * @param returnEmptyParameter the return empty parameter * @param fillEmptyParameterAsDefaultValue the fill empty parameter as default - * @return - * @throws Exception + * @return the wms request uri builded + * @throws Exception the exception */ public String parseWmsRequest(boolean returnEmptyParameter, boolean fillEmptyParameterAsDefaultValue) throws Exception{ @@ -65,57 +65,57 @@ public class WmsUrlValidator { if(wmsParam.equals(WmsParameters.BBOX)){ String value = validateValueOfParameter(WmsParameters.BBOX, wmsParameters, fillEmptyParameterAsDefaultValue); - parametersMap.put(wmsParam.getParameter(), value); + mapWmsParameters.put(wmsParam.getParameter(), value); } if(wmsParam.equals(WmsParameters.FORMAT)){ String value = validateValueOfParameter(WmsParameters.FORMAT, wmsParameters, fillEmptyParameterAsDefaultValue); - parametersMap.put(wmsParam.getParameter(), value); + mapWmsParameters.put(wmsParam.getParameter(), value); } if(wmsParam.equals(WmsParameters.HEIGHT)){ String value = validateValueOfParameter( WmsParameters.HEIGHT, wmsParameters, fillEmptyParameterAsDefaultValue); - parametersMap.put(wmsParam.getParameter(), value); + mapWmsParameters.put(wmsParam.getParameter(), value); } if(wmsParam.equals(WmsParameters.CRS)){ String crs = validateValueOfParameter(WmsParameters.CRS, wmsParameters, fillEmptyParameterAsDefaultValue); - parametersMap.put(wmsParam.getParameter(), crs); + mapWmsParameters.put(wmsParam.getParameter(), crs); } if(wmsParam.equals(WmsParameters.WIDTH)){ String value = validateValueOfParameter(WmsParameters.WIDTH, wmsParameters, fillEmptyParameterAsDefaultValue); - parametersMap.put(wmsParam.getParameter(), value); + mapWmsParameters.put(wmsParam.getParameter(), value); } if(wmsParam.equals(WmsParameters.REQUEST)){ String value = validateValueOfParameter(WmsParameters.REQUEST, wmsParameters, fillEmptyParameterAsDefaultValue); - parametersMap.put(wmsParam.getParameter(), value); + mapWmsParameters.put(wmsParam.getParameter(), value); } if(wmsParam.equals(WmsParameters.SERVICE)){ String value = validateValueOfParameter(WmsParameters.SERVICE, wmsParameters,fillEmptyParameterAsDefaultValue); - parametersMap.put(wmsParam.getParameter(), value); + mapWmsParameters.put(wmsParam.getParameter(), value); } if(wmsParam.equals(WmsParameters.SRS)){ String value = validateValueOfParameter(WmsParameters.SRS, wmsParameters, fillEmptyParameterAsDefaultValue); - parametersMap.put(wmsParam.getParameter(), value); + mapWmsParameters.put(wmsParam.getParameter(), value); } if(wmsParam.equals(WmsParameters.STYLES)){ String styles = validateValueOfParameter(WmsParameters.STYLES, wmsParameters, fillEmptyParameterAsDefaultValue); - parametersMap.put(wmsParam.getParameter(), styles); + mapWmsParameters.put(wmsParam.getParameter(), styles); } if(wmsParam.equals(WmsParameters.VERSION)){ String version = validateValueOfParameter(WmsParameters.VERSION, wmsParameters, fillEmptyParameterAsDefaultValue); - parametersMap.put(wmsParam.getParameter(), version); + mapWmsParameters.put(wmsParam.getParameter(), version); } if(wmsParam.equals(WmsParameters.LAYERS)){ String layers = validateValueOfParameter(WmsParameters.LAYERS, wmsParameters, fillEmptyParameterAsDefaultValue); - parametersMap.put(wmsParam.getParameter(), layers); + mapWmsParameters.put(wmsParam.getParameter(), layers); } } @@ -134,9 +134,9 @@ public class WmsUrlValidator { } //CREATE WMS REQUEST - for (String key : parametersMap.keySet()) { + for (String key : mapWmsParameters.keySet()) { - String value = parametersMap.get(key); + String value = mapWmsParameters.get(key); if(returnEmptyParameter || !(value==null) && !value.isEmpty()){ parsedWmsRequest+=key+"="+value; parsedWmsRequest+="&"; @@ -147,26 +147,26 @@ public class WmsUrlValidator { fullWmsRequest+="&"; }*/ - String exist = parametersMap.get(key); + String exist = mapWmsParameters.get(key); if(exist!=null) mapWmsNoStandardParams.remove(key); //REMOVE WMS STANDARD PARAMETER FROM MAP } for (String key : mapWmsNoStandardParams.keySet()) { - wmsNotStandardParameters+=key+"="+mapWmsNoStandardParams.get(key) + "&"; + wmsNoStandardParameters+=key+"="+mapWmsNoStandardParams.get(key) + "&"; } - if(wmsNotStandardParameters.length()>0) - wmsNotStandardParameters = wmsNotStandardParameters.substring(0, wmsNotStandardParameters.length()-1); //REMOVE LAST & + if(wmsNoStandardParameters.length()>0) + wmsNoStandardParameters = wmsNoStandardParameters.substring(0, wmsNoStandardParameters.length()-1); //REMOVE LAST & - logger.trace("wmsNotStandardParameters: "+wmsNotStandardParameters); + logger.trace("wmsNotStandardParameters: "+wmsNoStandardParameters); String fullWmsUrlBuilded; - if(!wmsNotStandardParameters.isEmpty()){ - fullWmsUrlBuilded = parsedWmsRequest + wmsNotStandardParameters; //remove last & - logger.trace("full wms url builded + not wms standard parameters: "+fullWmsUrlBuilded); + if(!wmsNoStandardParameters.isEmpty()){ + fullWmsUrlBuilded = parsedWmsRequest + wmsNoStandardParameters; //remove last & + logger.trace("full wms url builded + wms no standard parameters: "+fullWmsUrlBuilded); }else{ fullWmsUrlBuilded = parsedWmsRequest.substring(0, parsedWmsRequest.length()-1); //remove last & logger.trace("full wms url builded: "+fullWmsUrlBuilded); @@ -175,15 +175,20 @@ public class WmsUrlValidator { return fullWmsUrlBuilded; } - /** - * @return the parametersMap - */ - public HashMap getParametersMap() { - return parametersMap; + /** + * Gets the map wms parameters. + * + * @return the map wms parameters + */ + public HashMap getMapWmsParameters() { + + return mapWmsParameters; } /** + * Gets the wms request. + * * @return the wmsRequest */ public String getWmsRequest() { @@ -191,6 +196,8 @@ public class WmsUrlValidator { } /** + * Gets the base wms service url. + * * @return the baseWmsServiceUrl */ public String getBaseWmsServiceUrl() { @@ -198,15 +205,14 @@ public class WmsUrlValidator { } /** - * Gets the wms not standard parameters. + * Gets the wms no standard parameters. * - * @return the wms not standard parameters + * @return the wms no standard parameters like a query string (param1=value1¶m2=value2&...) */ - public String getWmsNotStandardParameters() { - return wmsNotStandardParameters; + public String getWmsNoStandardParameters() { + return wmsNoStandardParameters; } - /** * Gets the value of parsed wms parameter. * @@ -214,10 +220,9 @@ public class WmsUrlValidator { * @return the value of parsed wms parameter parsed from wms request. */ public String getValueOfParsedWMSParameter(WmsParameters parameter){ - return parametersMap.get(parameter.getParameter()); + return mapWmsParameters.get(parameter.getParameter()); } - /** * Validate value of parameter. * @@ -233,7 +238,7 @@ public class WmsUrlValidator { String value = getValueOfParameter(wmsParam, valueOfParameter); if(fillEmptyParameterAsDefaultValue && (value==null || value.isEmpty())){ - logger.trace("setting empty value to wms parameter: "+wmsParam.getParameter() +", as default value: "+wmsParam.getValue()); + logger.trace("setting empty value for parameter: "+wmsParam.getParameter() +", as default value: "+wmsParam.getValue()); value = wmsParam.getValue(); } return value; @@ -336,8 +341,8 @@ public class WmsUrlValidator { public String toString() { StringBuilder builder = new StringBuilder(); - builder.append("WmsUrlValidator [parametersMap="); - builder.append(parametersMap); + builder.append("WmsUrlValidator [mapWmsParameters="); + builder.append(mapWmsParameters); builder.append(", wmsRequest="); builder.append(wmsRequest); builder.append(", baseWmsServiceUrl="); @@ -345,7 +350,7 @@ public class WmsUrlValidator { builder.append(", wmsParameters="); builder.append(wmsParameters); builder.append(", wmsNotStandardParameters="); - builder.append(wmsNotStandardParameters); + builder.append(wmsNoStandardParameters); builder.append(", mapWmsNoStandardParams="); builder.append(mapWmsNoStandardParams); builder.append("]"); @@ -357,7 +362,7 @@ public class WmsUrlValidator { * * @param args the arguments */ - /*public static void main(String[] args) { + public static void main(String[] args) { // String baseGeoserverUrl = "http://repoigg.services.iit.cnr.it:8080/geoserver/IGG/ows"; // String baseGeoserverUrl = "http://www.fao.org/figis/geoserver/species"; @@ -382,9 +387,10 @@ public class WmsUrlValidator { // } // } // -// String fullPath = "http://thredds-d-d4s.d4science.org/thredds/wms/public/netcdf/test20.nc?service=wms&version=1.3.0&request=GetMap&layers=analyzed_field&styles=&width=640&height=480&srs=EPSG:4326&CRS=EPSG:4326&format=image/png&COLORSCALERANGE=auto&bbox=-85.0,-180.0,85.0,180.0"; -// fullPath = WmsUrlValidator.setValueOfParameter(WmsParameters.STYLES, fullPath, "123", true); + String fullPath = "http://www.fao.org/figis/geoserver/species/ows?CRS=EPSG:4326&BBOX=-180,-90,180,90&VERSION=1.1.0&FORMAT=image/png&SERVICE=WMS&HEIGHT=230&LAYERS=&REQUEST=GetMap&STYLES=&SRS=EPSG:4326&WIDTH=676"; + fullPath = WmsUrlValidator.setValueOfParameter(WmsParameters.LAYERS, fullPath, "123", true); + System.out.println(fullPath); // - }*/ + } }