# HG changeset patch # User Ingo Weinzierl # Date 1322495726 0 # Node ID 42d6cf6e10b744c8ab04da7adc7c4a8073b7a099 # Parent 16f19f12a962d7d670d693dbcdc5c7fc0788ec3b Moved code to parse WMS Capabilities to an own class 'CapabilitiesParser' and added code to parse SRS definitions. flys-client/trunk@3325 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r 16f19f12a962 -r 42d6cf6e10b7 flys-client/ChangeLog --- a/flys-client/ChangeLog Wed Nov 23 09:45:22 2011 +0000 +++ b/flys-client/ChangeLog Mon Nov 28 15:55:26 2011 +0000 @@ -1,3 +1,25 @@ +2011-11-28 Ingo Weinzierl + + * src/main/java/de/intevation/flys/client/server/CapabilitiesParser.java: + New. An explicit parser for WMS capabilities documents. Its code has been + moved from GCServiceImpl. In addition to the implementation of + GCServiceImpl, this parser also reads the SRS definition of a layer. + + * src/main/java/de/intevation/flys/client/server/LoggingConfigurator.java: + New. This class is used to initialize logging via Apache Log4j. Its code + has been moved from BaseServlet. + + * src/main/java/de/intevation/flys/client/server/GCServiceImpl.java: + Removed code to fetch and parse WMS Capabilities documents. This work is + done using CapabilitiesParser. + + * src/main/java/de/intevation/flys/client/server/BaseServlet.java: Removed + code to initialize Log4j logging. This work is done using + LoggingConfigurator. + + * src/main/java/de/intevation/flys/client/shared/model/WMSLayer.java: Added + a list of SRS definitions (List). + 2011-11-23 Ingo Weinzierl * src/main/java/de/intevation/flys/client/server/GCServiceImpl.java: diff -r 16f19f12a962 -r 42d6cf6e10b7 flys-client/src/main/java/de/intevation/flys/client/server/BaseServlet.java --- a/flys-client/src/main/java/de/intevation/flys/client/server/BaseServlet.java Wed Nov 23 09:45:22 2011 +0000 +++ b/flys-client/src/main/java/de/intevation/flys/client/server/BaseServlet.java Mon Nov 28 15:55:26 2011 +0000 @@ -4,7 +4,6 @@ import javax.servlet.http.HttpServlet; import org.apache.log4j.Logger; -import org.apache.log4j.PropertyConfigurator; public class BaseServlet extends HttpServlet { @@ -37,15 +36,7 @@ } } - if (log4jProperties != null && log4jProperties.length() > 0) { - PropertyConfigurator.configure(log4jProperties); - logger.info("Log4J logging initialized."); - } - else { - System.out.println("Error while setting up Log4J configuration."); - } - - System.out.println("BaseServlet.init finished"); + LoggingConfigurator.init(log4jProperties); } @@ -56,3 +47,4 @@ getServletContext().setAttribute("server-url", url); } } +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : diff -r 16f19f12a962 -r 42d6cf6e10b7 flys-client/src/main/java/de/intevation/flys/client/server/CapabilitiesParser.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-client/src/main/java/de/intevation/flys/client/server/CapabilitiesParser.java Mon Nov 28 15:55:26 2011 +0000 @@ -0,0 +1,359 @@ +package de.intevation.flys.client.server; + +import java.io.InputStream; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import javax.xml.xpath.XPathConstants; + +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +import org.apache.log4j.Logger; + +import de.intevation.artifacts.common.utils.XMLUtils; + +import de.intevation.flys.client.shared.exceptions.ServerException; +import de.intevation.flys.client.shared.model.Capabilities; +import de.intevation.flys.client.shared.model.ContactInformation; +import de.intevation.flys.client.shared.model.WMSLayer; + + +public class CapabilitiesParser { + + private static final Logger logger = + Logger.getLogger(CapabilitiesParser.class); + + + public static final String ERR_GC_REQUEST_FAILED = + "error_gc_req_failed"; + + public static final String ERR_GC_DOC_NOT_VALID = + "error_gc_doc_not_valid"; + + public static final String ERR_MALFORMED_URL = + "error_malformed_url"; + + + public static final String XPATH_WMS_CAPS = + "/WMS_Capabilities"; + + public static final String XPATH_WMT_CAPS = + "/WMT_MS_Capabilities"; + + public static final String XPATH_TITLE = + "Service/Title/text()"; + + public static final String XPATH_ONLINE_RESOURCE = + "Service/OnlineResource/@href"; + + public static final String XPATH_CONTACT_INFORMATION = + "Service/ContactInformation"; + + public static final String XPATH_CI_PERSON = + "ContactPersonPrimary/ContactPerson/text()"; + + public static final String XPATH_CI_ORGANIZATION = + "ContactPersonPrimary/ContactOrganization/text()"; + + public static final String XPATH_CI_ADDRESS = + "ContactAddress/Address/text()"; + + public static final String XPATH_CI_CITY = + "ContactAddress/City/text()"; + + public static final String XPATH_CI_POSTCODE = + "ContactAddress/PostCode/text()"; + + public static final String XPATH_CI_PHONE = + "ContactVoiceTelephone/text()"; + + public static final String XPATH_CI_EMAIL = + "ContactElectronicMailAddress/text()"; + + public static final String XPATH_FEES = + "Service/Fees/text()"; + + public static final String XPATH_ACCESS_CONSTRAINTS = + "Service/AccessConstraints/text()"; + + public static final String XPATH_LAYERS = + "Capability/Layer"; + + public static final Pattern SRS_PATTERN = Pattern.compile("(EPSG:\\d+)*"); + + + private CapabilitiesParser() { + } + + + public static void main(String[] args) { + logger.info("Do static Capabilities request/parsing."); + + String log4jProperties = System.getenv(BaseServlet.LOG4J_PROPERTIES); + LoggingConfigurator.init(log4jProperties); + + try { + Capabilities caps = getCapabilities( + "http://czech-republic.atlas.intevation.de/cgi-bin/saar-wms?REQUEST=GetCapabilities&SERVICE=WMS&VERSION=1.1.0"); + } + catch (ServerException se) { + se.printStackTrace(); + } + + logger.info("Finished fetching capabiltiies."); + } + + + public static Capabilities getCapabilities(String urlStr) + throws ServerException + { + try { + URL url = new URL(urlStr); + + logger.debug("Open connection to url: " + urlStr); + + URLConnection conn = url.openConnection(); + conn.connect(); + + InputStream is = conn.getInputStream(); + + return parse(is); + } + catch (MalformedURLException mue) { + logger.warn(mue, mue); + throw new ServerException(ERR_MALFORMED_URL); + } + catch (IOException ioe) { + logger.warn(ioe, ioe); + } + + throw new ServerException(ERR_GC_REQUEST_FAILED); + } + + + protected static Capabilities parse(InputStream is) + throws ServerException + { + logger.debug("GCServiceImpl.parseCapabilitiesResponse"); + + Document doc = XMLUtils.parseDocument(is, false); + + if (doc == null) { + throw new ServerException(ERR_GC_DOC_NOT_VALID); + } + + return CapabilitiesParser.parse(doc); + } + + + public static Capabilities parse(Document doc) + throws ServerException + { + Node capabilities = getCapabilitiesNode(doc); + + String title = (String) XMLUtils.xpath( + capabilities, + XPATH_TITLE, + XPathConstants.STRING); + + String onlineResource = (String) XMLUtils.xpath( + capabilities, + XPATH_ONLINE_RESOURCE, + XPathConstants.STRING); + + String fees = (String) XMLUtils.xpath( + capabilities, + XPATH_FEES, + XPathConstants.STRING); + + String accessConstraints = (String) XMLUtils.xpath( + capabilities, + XPATH_ACCESS_CONSTRAINTS, + XPathConstants.STRING); + + Node contactInformation = (Node) XMLUtils.xpath( + capabilities, + XPATH_CONTACT_INFORMATION, + XPathConstants.NODE); + + ContactInformation ci = parseContactInformation(contactInformation); + + logger.debug("Found fees: " + fees); + logger.debug("Found access constraints: " + accessConstraints); + + NodeList layerNodes = (NodeList) XMLUtils.xpath( + capabilities, + XPATH_LAYERS, + XPathConstants.NODESET); + + List layers = parseLayers(layerNodes, onlineResource); + + return new Capabilities( + title, + onlineResource, + ci, + fees, + accessConstraints, + layers); + } + + + protected static Node getCapabilitiesNode(Document doc) + throws ServerException { + Node capabilities = (Node) XMLUtils.xpath( + doc, + XPATH_WMS_CAPS, + XPathConstants.NODE); + + if (capabilities == null) { + logger.info("No '/WMS_Capabilities' node found."); + logger.info("Try to find a '/WMT_MS_Capabilities' node."); + + capabilities = (Node) XMLUtils.xpath( + doc, + XPATH_WMT_CAPS, + XPathConstants.NODE); + } + + if (capabilities == null) { + throw new ServerException(ERR_GC_DOC_NOT_VALID); + } + + return capabilities; + } + + + protected static ContactInformation parseContactInformation(Node node) { + String person = (String) XMLUtils.xpath( + node, + XPATH_CI_PERSON, + XPathConstants.STRING); + + String organization = (String) XMLUtils.xpath( + node, + XPATH_CI_ORGANIZATION, + XPathConstants.STRING); + + String address = (String) XMLUtils.xpath( + node, + XPATH_CI_ADDRESS, + XPathConstants.STRING); + + String postcode = (String) XMLUtils.xpath( + node, + XPATH_CI_POSTCODE, + XPathConstants.STRING); + + String city = (String) XMLUtils.xpath( + node, + XPATH_CI_CITY, + XPathConstants.STRING); + + String phone = (String) XMLUtils.xpath( + node, + XPATH_CI_PHONE, + XPathConstants.STRING); + + String email = (String) XMLUtils.xpath( + node, + XPATH_CI_EMAIL, + XPathConstants.STRING); + + ContactInformation ci = new ContactInformation(); + ci.setPerson(person); + ci.setOrganization(organization); + ci.setAddress(address); + ci.setPostcode(postcode); + ci.setCity(city); + ci.setPhone(phone); + ci.setEmail(email); + + return ci; + } + + + /** + * @param layersNode + * @param onlineResource + * + * @return + */ + protected static List parseLayers( + NodeList layersNode, + String onlineResource + ) { + int len = layersNode != null ? layersNode.getLength() : 0; + + logger.debug("Node has " + len + " layers."); + + List layers = new ArrayList(len); + + for (int i = 0; i < len; i++) { + layers.add(parseLayer(layersNode.item(i), onlineResource)); + } + + return layers; + } + + + protected static WMSLayer parseLayer(Node layerNode, String onlineResource) { + String title = (String) XMLUtils.xpath( + layerNode, + "Title/text()", + XPathConstants.STRING); + + String name = (String) XMLUtils.xpath( + layerNode, + "Name/text()", + XPathConstants.STRING); + + logger.debug("Found layer: " + title + "(" + name + ")"); + + List srs = parseSRS(layerNode); + + NodeList layersNodes = (NodeList) XMLUtils.xpath( + layerNode, + "Layer", + XPathConstants.NODESET); + + List layers = parseLayers(layersNodes, onlineResource); + + return new WMSLayer(onlineResource, title, name, srs, layers); + } + + + protected static List parseSRS(Node layerNode) { + String srsStr = (String) XMLUtils.xpath( + layerNode, + "SRS/text()", + XPathConstants.STRING); + + if (srsStr == null || srsStr.length() == 0) { + logger.debug("No explicit SRS for this layer specified."); + return null; + } + + String[] splittedSrs = srsStr.split(" "); + List srs = new ArrayList(); + + for (String singleSrs: splittedSrs) { + Matcher m = SRS_PATTERN.matcher(singleSrs); + + if (m.matches()) { + logger.debug("Found SRS '" + m.group(1) + "'"); + srs.add(m.group(1)); + } + } + + return srs; + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : diff -r 16f19f12a962 -r 42d6cf6e10b7 flys-client/src/main/java/de/intevation/flys/client/server/GCServiceImpl.java --- a/flys-client/src/main/java/de/intevation/flys/client/server/GCServiceImpl.java Wed Nov 23 09:45:22 2011 +0000 +++ b/flys-client/src/main/java/de/intevation/flys/client/server/GCServiceImpl.java Mon Nov 28 15:55:26 2011 +0000 @@ -1,29 +1,11 @@ package de.intevation.flys.client.server; -import java.io.InputStream; -import java.io.IOException; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.URLConnection; -import java.util.ArrayList; -import java.util.List; - -import javax.xml.xpath.XPathConstants; - import com.google.gwt.user.server.rpc.RemoteServiceServlet; -import org.w3c.dom.Document; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; - import org.apache.log4j.Logger; -import de.intevation.artifacts.common.utils.XMLUtils; - import de.intevation.flys.client.shared.exceptions.ServerException; import de.intevation.flys.client.shared.model.Capabilities; -import de.intevation.flys.client.shared.model.ContactInformation; -import de.intevation.flys.client.shared.model.WMSLayer; import de.intevation.flys.client.client.services.GCService; @@ -34,62 +16,7 @@ extends RemoteServiceServlet implements GCService { - public static final String ERR_GC_REQUEST_FAILED = - "error_gc_req_failed"; - - public static final String ERR_GC_DOC_NOT_VALID = - "error_gc_doc_not_valid"; - - public static final String ERR_MALFORMED_URL = - "error_malformed_url"; - - public static final String XPATH_WMS_CAPS = - "/WMS_Capabilities"; - - public static final String XPATH_WMT_CAPS = - "/WMT_MS_Capabilities"; - - public static final String XPATH_TITLE = - "Service/Title/text()"; - - public static final String XPATH_ONLINE_RESOURCE = - "Service/OnlineResource/@href"; - - public static final String XPATH_CONTACT_INFORMATION = - "Service/ContactInformation"; - - public static final String XPATH_CI_PERSON = - "ContactPersonPrimary/ContactPerson/text()"; - - public static final String XPATH_CI_ORGANIZATION = - "ContactPersonPrimary/ContactOrganization/text()"; - - public static final String XPATH_CI_ADDRESS = - "ContactAddress/Address/text()"; - - public static final String XPATH_CI_CITY = - "ContactAddress/City/text()"; - - public static final String XPATH_CI_POSTCODE = - "ContactAddress/PostCode/text()"; - - public static final String XPATH_CI_PHONE = - "ContactVoiceTelephone/text()"; - - public static final String XPATH_CI_EMAIL = - "ContactElectronicMailAddress/text()"; - - public static final String XPATH_FEES = - "Service/Fees/text()"; - - public static final String XPATH_ACCESS_CONSTRAINTS = - "Service/AccessConstraints/text()"; - - public static final String XPATH_LAYERS = - "Capability/Layer"; - - - private Logger logger = Logger.getLogger(GCServiceImpl.class); + private static Logger logger = Logger.getLogger(GCServiceImpl.class); public Capabilities query(String path) @@ -97,209 +24,7 @@ { logger.info("GCServiceImpl.query"); - try { - URL url = new URL(path); - - logger.debug("Open connection to url: " + path); - - URLConnection conn = url.openConnection(); - conn.connect(); - - InputStream is = conn.getInputStream(); - - return parseCapabilitiesResponse(is); - } - catch (MalformedURLException mue) { - logger.warn(mue, mue); - throw new ServerException(ERR_MALFORMED_URL); - } - catch (IOException ioe) { - logger.warn(ioe, ioe); - } - - throw new ServerException(ERR_GC_REQUEST_FAILED); - } - - - protected Capabilities parseCapabilitiesResponse(InputStream is) - throws ServerException - { - logger.debug("GCServiceImpl.parseCapabilitiesResponse"); - - Document doc = XMLUtils.parseDocument(is, false); - - if (doc == null) { - throw new ServerException(ERR_GC_DOC_NOT_VALID); - } - - Node capabilities = getCapabilitiesNode(doc); - - String title = (String) XMLUtils.xpath( - capabilities, - XPATH_TITLE, - XPathConstants.STRING); - - String onlineResource = (String) XMLUtils.xpath( - capabilities, - XPATH_ONLINE_RESOURCE, - XPathConstants.STRING); - - String fees = (String) XMLUtils.xpath( - capabilities, - XPATH_FEES, - XPathConstants.STRING); - - String accessConstraints = (String) XMLUtils.xpath( - capabilities, - XPATH_ACCESS_CONSTRAINTS, - XPathConstants.STRING); - - Node contactInformation = (Node) XMLUtils.xpath( - capabilities, - XPATH_CONTACT_INFORMATION, - XPathConstants.NODE); - - ContactInformation ci = parseContactInformation(contactInformation); - - logger.debug("Found fees: " + fees); - logger.debug("Found access constraints: " + accessConstraints); - - NodeList layerNodes = (NodeList) XMLUtils.xpath( - capabilities, - XPATH_LAYERS, - XPathConstants.NODESET); - - List layers = parseLayers(layerNodes, onlineResource); - - return new Capabilities( - title, - onlineResource, - ci, - fees, - accessConstraints, - layers); - } - - - protected Node getCapabilitiesNode(Document doc) - throws ServerException { - Node capabilities = (Node) XMLUtils.xpath( - doc, - XPATH_WMS_CAPS, - XPathConstants.NODE); - - if (capabilities == null) { - logger.info("No '/WMS_Capabilities' node found."); - logger.info("Try to find a '/WMT_MS_Capabilities' node."); - - capabilities = (Node) XMLUtils.xpath( - doc, - XPATH_WMT_CAPS, - XPathConstants.NODE); - } - - if (capabilities == null) { - throw new ServerException(ERR_GC_DOC_NOT_VALID); - } - - return capabilities; - } - - - protected ContactInformation parseContactInformation(Node node) { - String person = (String) XMLUtils.xpath( - node, - XPATH_CI_PERSON, - XPathConstants.STRING); - - String organization = (String) XMLUtils.xpath( - node, - XPATH_CI_ORGANIZATION, - XPathConstants.STRING); - - String address = (String) XMLUtils.xpath( - node, - XPATH_CI_ADDRESS, - XPathConstants.STRING); - - String postcode = (String) XMLUtils.xpath( - node, - XPATH_CI_POSTCODE, - XPathConstants.STRING); - - String city = (String) XMLUtils.xpath( - node, - XPATH_CI_CITY, - XPathConstants.STRING); - - String phone = (String) XMLUtils.xpath( - node, - XPATH_CI_PHONE, - XPathConstants.STRING); - - String email = (String) XMLUtils.xpath( - node, - XPATH_CI_EMAIL, - XPathConstants.STRING); - - ContactInformation ci = new ContactInformation(); - ci.setPerson(person); - ci.setOrganization(organization); - ci.setAddress(address); - ci.setPostcode(postcode); - ci.setCity(city); - ci.setPhone(phone); - ci.setEmail(email); - - return ci; - } - - - /** - * @param layersNode - * @param onlineResource - * - * @return - */ - protected List parseLayers( - NodeList layersNode, - String onlineResource - ) { - int len = layersNode != null ? layersNode.getLength() : 0; - - logger.debug("Node has " + len + " layers."); - - List layers = new ArrayList(len); - - for (int i = 0; i < len; i++) { - layers.add(parseLayer(layersNode.item(i), onlineResource)); - } - - return layers; - } - - - protected WMSLayer parseLayer(Node layerNode, String onlineResource) { - String title = (String) XMLUtils.xpath( - layerNode, - "Title/text()", - XPathConstants.STRING); - - String name = (String) XMLUtils.xpath( - layerNode, - "Name/text()", - XPathConstants.STRING); - - logger.debug("Found layers: " + title + "(" + name + ")"); - - NodeList layersNodes = (NodeList) XMLUtils.xpath( - layerNode, - "Layer", - XPathConstants.NODESET); - - List layers = parseLayers(layersNodes, onlineResource); - - return new WMSLayer(onlineResource, title, name, layers); + return CapabilitiesParser.getCapabilities(path); } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : diff -r 16f19f12a962 -r 42d6cf6e10b7 flys-client/src/main/java/de/intevation/flys/client/server/LoggingConfigurator.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-client/src/main/java/de/intevation/flys/client/server/LoggingConfigurator.java Mon Nov 28 15:55:26 2011 +0000 @@ -0,0 +1,28 @@ +package de.intevation.flys.client.server; + +import org.apache.log4j.Logger; +import org.apache.log4j.PropertyConfigurator; + + +public class LoggingConfigurator { + + private static final Logger logger = + Logger.getLogger(LoggingConfigurator.class); + + + private LoggingConfigurator() { + } + + public static void init(String log4jProperties) { + if (log4jProperties != null && log4jProperties.length() > 0) { + PropertyConfigurator.configure(log4jProperties); + logger.info("Log4J logging initialized."); + } + else { + System.out.println("Error while setting up Log4J configuration."); + } + + System.out.println("LoggingConfigurator.init finished"); + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : diff -r 16f19f12a962 -r 42d6cf6e10b7 flys-client/src/main/java/de/intevation/flys/client/shared/model/WMSLayer.java --- a/flys-client/src/main/java/de/intevation/flys/client/shared/model/WMSLayer.java Wed Nov 23 09:45:22 2011 +0000 +++ b/flys-client/src/main/java/de/intevation/flys/client/shared/model/WMSLayer.java Mon Nov 28 15:55:26 2011 +0000 @@ -11,6 +11,7 @@ protected String name; protected String title; + protected List srs; protected List layers; @@ -29,11 +30,13 @@ String server, String title, String name, + List srs, List layers ) { this.server = server; this.title = title; this.name = name; + this.srs = srs; this.layers = layers; } @@ -53,6 +56,11 @@ } + public List getSrs() { + return srs; + } + + public List getLayers() { return layers; }