view flys-client/src/main/java/de/intevation/flys/client/server/GCServiceImpl.java @ 1409:ec6e4dad1279

Read and display all FLYS relevant information of Capabilities documents. flys-client/trunk@3296 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Mon, 21 Nov 2011 18:15:44 +0000
parents 5b5a20e4c4e5
children 63be3137abac
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 javax.xml.xpath.XPathConstants;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

import org.w3c.dom.Document;
import org.w3c.dom.Node;

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.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()";


    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);

        // TODO PARSE LAYERS

        return new Capabilities(
            title,
            onlineResource,
            ci,
            fees,
            accessConstraints,
            null);
    }


    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;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org