view flys-client/src/main/java/de/intevation/flys/client/server/GFIServiceImpl.java @ 5622:b28a6d05e969

Add a new mechanism in mapfish print call to add arbitary data maps Data properties are identified by starting with mapfish-data and they are then split in info value pairs where info can be the description of the information and value the value of the information to be transported in the data map.
author Andre Heinecke <aheinecke@intevation.de>
date Tue, 09 Apr 2013 19:04:32 +0200
parents 30c7f8f9f1b7
children af2aa716152f
line wrap: on
line source
package de.intevation.flys.client.server;

import java.io.InputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

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

import org.apache.log4j.Logger;

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

import de.intevation.artifacts.common.utils.XMLUtils;

import de.intevation.flys.client.shared.exceptions.ServerException;
import de.intevation.flys.client.shared.model.AttributedTheme;
import de.intevation.flys.client.shared.model.FeatureInfo;
import de.intevation.flys.client.shared.model.Theme;

import de.intevation.flys.client.client.services.GFIService;


/**
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public class GFIServiceImpl
extends      RemoteServiceServlet
implements   GFIService
{
    public static final String ERR_NO_VALID_GFI_URL =
        "error_no_valid_gfi_url";

    public static final String ERR_GFI_REQUEST_FAILED =
        "error_gfi_req_failed";

    public static final String ERR_PARSING_RESPONSE_FAILED =
        "error_gfi_parsing_failed";


    private static final Logger logger =
        Logger.getLogger(GFIServiceImpl.class);


    /**
     * @param themes
     * @param format
     * @param bbox
     * @param height
     * @param width
     * @param x
     * @param y
     *
     * @return
     */
    public List<FeatureInfo> query(
        List<Theme> themes,
        String      format,
        String      bbox,
        String      projection,
        int         height,
        int         width,
        int         x,
        int         y
    ) throws ServerException
    {
        logger.info("GFIServiceImpl.query");

        String path = createGetFeautureInfoURL(
            themes, format, bbox, projection, height, width, x, y);

        logger.debug("URL=" + path);

        try {
            URL url = new URL(path);

            URLConnection conn = url.openConnection();
            conn.connect();

            InputStream is = conn.getInputStream();

            return parseResponse(is);

        }
        catch (IOException ioe) {
            logger.warn(ioe, ioe);
        }

        throw new ServerException(ERR_GFI_REQUEST_FAILED);
    }


    /**
     * @param map
     * @param themes
     * @param format
     * @param x
     * @param y
     *
     * @return
     */
    protected String createGetFeautureInfoURL(
        List<Theme> themes,
        String      infoFormat,
        String      bbox,
        String      projection,
        int         height,
        int         width,
        int         x,
        int         y
    ) throws ServerException
    {
        String url = getUrl(themes);

        if (url == null || url.length() == 0) {
            throw new ServerException(ERR_NO_VALID_GFI_URL);
        }

        String layers = createLayersString(themes);

        StringBuilder sb = new StringBuilder();
        sb.append(url);

        if (url.indexOf("?") < 0) {
            sb.append("?SERVICE=WMS");
        }
        else {
            sb.append("&SERVICE=WMS");
        }

        sb.append("&VERSION=1.1.1");
        sb.append("&REQUEST=GetFeatureInfo");
        sb.append("&LAYERS=" + layers);
        sb.append("&QUERY_LAYERS=" + layers);
        sb.append("&BBOX=" + bbox);
        sb.append("&HEIGHT=" + height);
        sb.append("&WIDTH=" + width);
        sb.append("&FORMAT=image/png");
        sb.append("&INFO_FORMAT=" + infoFormat);
        sb.append("&SRS=" + projection);
        sb.append("&X=" + String.valueOf(x));
        sb.append("&Y=" + String.valueOf(y));

        return sb.toString();
    }


    protected String getUrl(List<Theme> themes) {
        for (Theme t: themes) {
            AttributedTheme attr = (AttributedTheme) t;

            if (attr.getAttrAsBoolean("queryable")) {
                return attr.getAttr("url");
            }
        }

        return null;
    }


    protected String createLayersString(List<Theme> themes) {
        StringBuilder sb = new StringBuilder();
        boolean first = true;

        for (Theme theme: themes) {
            AttributedTheme layer = (AttributedTheme) theme;
            if (layer.getAttrAsBoolean("queryable")) {
                if (!first) {
                    sb.append(",");
                }

                sb.append(layer.getAttr("layers"));
                first = false;
            }
        }

        return sb.toString();
    }


    protected List<FeatureInfo> parseResponse(InputStream is) {
        logger.debug("GFIServiceImpl.parseResponse");

        Document response = XMLUtils.parseDocument(is);

        List<FeatureInfo> features = new ArrayList<FeatureInfo>();

        parseFeatureInfos(response, features);

        return features;
    }


    protected void parseFeatureInfos(Node node, List<FeatureInfo> features) {
        logger.debug("GFIServiceImpl.parseFeatureInfos");

        String name = node.getNodeName();

        if (name.endsWith("_layer")) {
            features.add(parseFeature(node));

            return;
        }

        NodeList children = node.getChildNodes();

        if (children != null && children.getLength() > 0) {
            for (int i = 0, n = children.getLength(); i < n; i++) {
                parseFeatureInfos(children.item(i), features);
            }
        }
    }


    protected FeatureInfo parseFeature(Node node) {
        logger.debug("GFIServiceImpl.parseFeature");

        String layername = node.getNodeName();

        FeatureInfo f = new FeatureInfo(layername);

        NodeList children = node.getChildNodes();
        int numChildren   = children != null ? children.getLength() : 0;

        logger.debug("Feature '" + layername + "' has " + numChildren + " nodes.");

        for (int i = 0; i < numChildren; i++) {
            Node  tmp       = children.item(i);
            String nodeName = tmp.getNodeName();

            logger.debug("   node name: '" + nodeName + "'");

            if (nodeName.equals("gml:name")) {
                logger.debug("NAME node has child: " + tmp.getFirstChild().getNodeValue());
                f.setLayername(tmp.getFirstChild().getNodeValue());
            }
            else if (nodeName.endsWith("_feature")) {
                parseFeatureAttributes(tmp, f);
            }
        }

        return f;
    }


    protected void parseFeatureAttributes(Node node, FeatureInfo f) {
        logger.debug("GFIServiceImpl.parseFeatureAttributes");

        NodeList children = node.getChildNodes();
        int numChildren   = children != null ? children.getLength() : 0;

        logger.debug("Has " + numChildren + " attributes.");

        for (int i = 0; i < numChildren; i++) {
            Node   tmp  = children.item(i);
            String name = tmp.getNodeName();

            logger.debug("  tmp attribute name: '" + name + "'");

            if (name.equals("gml:boundedBy")) {
                // TODO
            }
            else {
                Node child = tmp.getFirstChild();
                if (child != null) {
                    f.addAttr(name, child.getNodeValue());
                }
            }
        }
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org