view gwt-client/src/main/java/org/dive4elements/river/client/server/ServerInfoServiceImpl.java @ 8203:238fc722f87a

sed 's/logger/log/g' src/**/*.java
author Sascha L. Teichmann <teichmann@intevation.de>
date Fri, 05 Sep 2014 13:19:22 +0200
parents b0b0ba6e7bb0
children a805211690f7
line wrap: on
line source
package org.dive4elements.river.client.server;

import java.util.HashMap;
import java.util.Map;

import javax.xml.xpath.XPathConstants;

import org.apache.log4j.Logger;
import org.dive4elements.artifacts.common.ArtifactNamespaceContext;
import org.dive4elements.artifacts.common.utils.XMLUtils;
import org.dive4elements.artifacts.httpclient.http.HttpClient;
import org.dive4elements.artifacts.httpclient.http.HttpClientImpl;
import org.dive4elements.river.client.client.services.ServerInfoService;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class ServerInfoServiceImpl extends RemoteServiceServlet implements
        ServerInfoService {

    // This works only because currently there is only one info transmitted
    private static final String XPATH_INFO = "/art:server/art:info";

    private final Logger log = Logger.getLogger(ServerInfoServiceImpl.class);

    @Override
    public Map<String, String> getConfig(String locale) {
        Map<String, String> infoMap = new HashMap<String, String>();
        String url = getServletContext().getInitParameter("server-url");

        Document doc = XMLUtils.newDocument();

        XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator(
            doc,
            ArtifactNamespaceContext.NAMESPACE_URI,
            ArtifactNamespaceContext.NAMESPACE_PREFIX);

        doc.appendChild(ec.create("action"));

        HttpClient client = new HttpClientImpl(url, locale);

        try {
            Document res = client.callService(url, "server-info", doc);

            NodeList info = (NodeList) XMLUtils.xpath(res,
                    XPATH_INFO,
                    XPathConstants.NODESET,
                    ArtifactNamespaceContext.INSTANCE);

            for (int n = 0; n < info.getLength(); n++) {
                Element el = (Element)info.item(n);
                String key = el.getAttributeNS(
                        ArtifactNamespaceContext.NAMESPACE_URI, "key");
                String val = el.getAttributeNS(
                        ArtifactNamespaceContext.NAMESPACE_URI, "value");
                infoMap.put(key, val);

                log.debug("ServerInfoServiceImpl: " + key + "=" + val);
            }
        }
        catch (Exception ex) {
            log.error(ex, ex);
        }

        return infoMap;
    }


}

http://dive4elements.wald.intevation.org