/** * */ package org.gcube.spatial.data.geoutility.wms; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.SocketTimeoutException; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import org.apache.commons.io.IOUtils; import org.apache.log4j.Logger; import org.gcube.spatial.data.geoutility.bean.LayerZAxis; import org.gcube.spatial.data.geoutility.bean.NcWmsLayerMetadata; import org.gcube.spatial.data.geoutility.bean.NcWmsLayerMetadata.METADATA; import org.gcube.spatial.data.geoutility.bean.WmsParameters; import org.gcube.spatial.data.geoutility.util.UrlEncoderUtil; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; /** * The Class NcWmsGetMetadata. * * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it * Dec 18, 2015 */ public class NcWmsGetMetadata { /** 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. * * @param wmsServerUri the wms server uri * @param layerName the layer name * @return the metadata * @throws Exception the exception */ public static NcWmsLayerMetadata getMetadata(String wmsServerUri, String layerName) throws Exception { return getMetadata(wmsServerUri, layerName, CONNECTION_TIMEOUT, METADATA.values()); } /** * Gets the metadata. * * @param wmsServerUri the wms server uri * @param layerName the layer name * @param connectionTimeout the connection timeout * @param meta the meta * @return the metadata * @throws Exception the exception */ public static NcWmsLayerMetadata getMetadata(String wmsServerUri, String layerName, int connectionTimeout, NcWmsLayerMetadata.METADATA...meta) throws Exception { if(wmsServerUri==null || wmsServerUri.isEmpty()) throw new Exception("Invalid wms server uri"); HashMap parameters = new HashMap(); parameters.put(WmsParameters.REQUEST.getParameter(), "GetMetadata"); parameters.put("layerName", layerName); parameters.put("item", "layerDetails"); parameters.put(WmsParameters.SERVICE.getParameter(), "WMS"); try { String query = UrlEncoderUtil.encodeQuery(parameters); return openConnectionGetMetadata(wmsServerUri, query, connectionTimeout, meta); }catch (Exception e) { logger.error("Error Exception with url " + wmsServerUri); return null; } } /** * 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. * @param meta the meta * @return the nc wms layer metadata */ private static NcWmsLayerMetadata openConnectionGetMetadata(String urlConn, String query, int connectionTimeout, NcWmsLayerMetadata.METADATA...meta) { URL url; NcWmsLayerMetadata metadata = null; try { url = new URL(urlConn + "?" + query); URLConnection connection = url.openConnection(); if(connectionTimeout<0) connectionTimeout = CONNECTION_TIMEOUT; connection.setConnectTimeout(connectionTimeout); connection.setReadTimeout(connectionTimeout + connectionTimeout); logger.trace("openConnectionGetMetadata on: " + url); // Cast to a HttpURLConnection if (connection instanceof HttpURLConnection) { HttpURLConnection httpConnection = (HttpURLConnection) connection; httpConnection.setRequestMethod("GET"); int code = httpConnection.getResponseCode(); if (code == 200) { metadata = getMetadata(httpConnection, meta); metadata.setResponseCode(code); }else{ logger.warn("openConnectionGetMetadata error, code = " + code +", returning"); } httpConnection.disconnect(); } else { logger.error("error - not a http request!"); } return metadata; } catch (SocketTimeoutException e) { logger.error("Error SocketTimeoutException with url " + urlConn); return metadata; } catch (MalformedURLException e) { logger.error("Error MalformedURLException with url " + urlConn); return metadata; } catch (IOException e) { logger.error("Error IOException with url " + urlConn); return metadata; } catch (Exception e) { logger.error("Error Exception with url " + urlConn); return metadata; } } /** * Gets the metadata. * * @param httpConnection the http connection * @param meta the meta * @return the metadata * @throws IOException Signals that an I/O exception has occurred. */ private static NcWmsLayerMetadata getMetadata(HttpURLConnection httpConnection, METADATA[] meta) throws IOException{ InputStream source = null; NcWmsLayerMetadata metadata = new NcWmsLayerMetadata(); try{ source = httpConnection.getInputStream(); String jsonTxt = IOUtils.toString(source); metadata.setRawJson(jsonTxt); if(meta==null) meta = METADATA.values(); List listMeta = Arrays.asList(meta); JSONObject json = new JSONObject(jsonTxt); if(listMeta.contains(NcWmsLayerMetadata.METADATA.SUPPORTEDSTYLES)){ JSONArray supportedStyles = json.getJSONArray(NcWmsLayerMetadata.METADATA.SUPPORTEDSTYLES.getKey()); if(supportedStyles!=null){ List s = new ArrayList(supportedStyles.length()); for (int i=0; i s = new ArrayList(palettes.length()); for (int i=0; i s = new ArrayList(values.length()); for (int i=0; i