Mercurial > dive4elements > river
view flys-client/src/main/java/de/intevation/flys/client/server/GCServiceImpl.java @ 1414:3484cbfaf369
Create new Artifacts for selected WMS layers of the ExternalWMSWindow.
flys-client/trunk@3304 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Tue, 22 Nov 2011 20:13:16 +0000 |
parents | 63be3137abac |
children | 16f19f12a962 |
line wrap: on
line source
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; /** * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> */ public class GCServiceImpl 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_TITLE = "/WMS_Capabilities/Service/Title/text()"; public static final String XPATH_ONLINE_RESOURCE = "/WMS_Capabilities/Service/OnlineResource/@href"; public static final String XPATH_CONTACT_INFORMATION = "/WMS_Capabilities/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 = "/WMS_Capabilities/Service/Fees/text()"; public static final String XPATH_ACCESS_CONSTRAINTS = "/WMS_Capabilities/Service/AccessConstraints/text()"; public static final String XPATH_LAYERS = "/WMS_Capabilities/Capability/Layer"; private Logger logger = Logger.getLogger(GCServiceImpl.class); public Capabilities query(String path) throws ServerException { 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); } String title = (String) XMLUtils.xpath( doc, XPATH_TITLE, XPathConstants.STRING); String onlineResource = (String) XMLUtils.xpath( doc, XPATH_ONLINE_RESOURCE, XPathConstants.STRING); String fees = (String) XMLUtils.xpath( doc, XPATH_FEES, XPathConstants.STRING); String accessConstraints = (String) XMLUtils.xpath( doc, XPATH_ACCESS_CONSTRAINTS, XPathConstants.STRING); Node contactInformation = (Node) XMLUtils.xpath( doc, 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( doc, XPATH_LAYERS, XPathConstants.NODESET); List<WMSLayer> layers = parseLayers(layerNodes, onlineResource); return new Capabilities( title, onlineResource, ci, fees, accessConstraints, layers); } 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<WMSLayer> parseLayers( NodeList layersNode, String onlineResource ) { int len = layersNode != null ? layersNode.getLength() : 0; logger.debug("Node has " + len + " layers."); List<WMSLayer> layers = new ArrayList<WMSLayer>(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<WMSLayer> layers = parseLayers(layersNodes, onlineResource); return new WMSLayer(onlineResource, title, name, layers); } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :