All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.vfny.geoserver.sld.servlets.PutStyles Maven / Gradle / Ivy

/* Copyright (c) 2001 - 2007 TOPP - www.openplans.org.  All rights reserved.
 * This code is licensed under the GPL 2.0 license, availible at the root
 * application directory.
 */
package org.vfny.geoserver.sld.servlets;

import org.geoserver.ows.util.XmlCharsetDetector;
import org.geoserver.platform.ServiceException;
import org.geotools.styling.SLDParser;
import org.geotools.styling.StyleFactory;
import org.geotools.styling.StyleFactoryFinder;
import org.geotools.styling.StyledLayerDescriptor;

import org.vfny.geoserver.Response;

import org.vfny.geoserver.config.ConfigRequests;
import org.vfny.geoserver.config.DataConfig;
import org.vfny.geoserver.config.FeatureTypeConfig;
import org.vfny.geoserver.config.StyleConfig;
import org.vfny.geoserver.global.ConfigurationException;
import org.vfny.geoserver.global.GeoserverDataDirectory;
import org.vfny.geoserver.global.WMS;
import org.vfny.geoserver.servlets.AbstractService;
import org.vfny.geoserver.sld.SldException;
import org.vfny.geoserver.sld.requests.PutStylesKvpReader;
import org.vfny.geoserver.sld.requests.PutStylesRequest;
import org.vfny.geoserver.sld.responses.PutStylesResponse;
import org.vfny.geoserver.util.SLDValidator;
import org.vfny.geoserver.util.requests.readers.KvpRequestReader;
import org.vfny.geoserver.util.requests.readers.XmlRequestReader;
import org.vfny.geoserver.wms.WmsException;
import org.vfny.geoserver.wms.requests.GetMapXmlReader;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.CharArrayReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.math.BigDecimal;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.ParserConfigurationException;


public class PutStyles extends AbstractService {
    private static Logger LOGGER = org.geotools.util.logging.Logging.getLogger("org.vfny.geoserver.sld.servlets");
    public final String success_mime_type = "application/vnd.ogc.success+xml";
    private static final StyleFactory styleFactory = StyleFactoryFinder.createStyleFactory();

    public PutStyles( WMS wms ) {
        super("WMS", "PutStyles", wms);
    }

    protected boolean isServiceEnabled(HttpServletRequest req) {
        return true;
    }

    protected Response getResponseHandler() {
        return new PutStylesResponse();
    }

    protected KvpRequestReader getKvpReader(Map params) {
        return new PutStylesKvpReader(params,(WMS) getServiceRef());
    }

    protected XmlRequestReader getXmlRequestReader() {
        /**
        * @todo Implement this org.vfny.geoserver.servlets.AbstractService
        *       abstract method
        */
        throw new java.lang.UnsupportedOperationException(
            "Method getXmlRequestReader() not yet implemented.");
    }

    /**
     * doGet:
     *
     *
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        LOGGER.info("PutStyles.doGet()");

        Map requestParams = new HashMap();
        String paramName;
        String paramValue;

        // gather the parameters
        for (Enumeration pnames = request.getParameterNames(); pnames.hasMoreElements();) {
            paramName = (String) pnames.nextElement();
            paramValue = request.getParameter(paramName);
            requestParams.put(paramName.toUpperCase(), paramValue);
        }

        PutStylesKvpReader requestReader = new PutStylesKvpReader(requestParams, (WMS) getServiceRef());

        PutStylesRequest serviceRequest; // the request object we will deal with

        try {
            serviceRequest = (PutStylesRequest) requestReader.getRequest(request);
        } catch (ServiceException e) {
            e.printStackTrace();

            return;
        }

        ServletContext context = request.getSession().getServletContext();

        try {
            processSLD(serviceRequest, request, response, context);
        } catch (SldException e) {
            throw new ServletException(e);
        } catch (IOException e) {
            throw new ServletException(e);
        }
    }

    /**
     * doPost:
     *
     *
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response, Reader requestXml)
        throws ServletException, IOException {
        LOGGER.fine("PutStyles POST");

        if (requestXml == null) {
            requestXml = new BufferedReader(XmlCharsetDetector.getCharsetAwareReader(
                        request.getInputStream()));
        }

        File temp = File.createTempFile("putStylesPost", "xml");
        temp.deleteOnExit();

        FileOutputStream fos = new FileOutputStream(temp);
        BufferedOutputStream out = new BufferedOutputStream(fos);
        StringBuffer sb = new StringBuffer();

        if (requestXml == null) {
            throw new NullPointerException();
        }

        int c;

        while (-1 != (c = requestXml.read())) {
            char chr = (char) c;
            out.write(c);
            sb.append(chr);
        }

        requestXml.close();
        out.flush();
        out.close();
        requestXml = new BufferedReader(new FileReader(temp)); // pretend like nothing has happened

        PutStylesRequest serviceRequest = new PutStylesRequest((WMS) getServiceRef());
        serviceRequest.setSldBody(sb.toString()); // save the SLD body in the request object

        ServletContext context = request.getSession().getServletContext();

        try {
            processSLD(serviceRequest, request, response, context);
        } catch (SldException e) {
            throw new ServletException(e);
        } catch (IOException e) {
            throw new ServletException(e);
        }
    }

    private Node generateDOM(Reader reader) {
        javax.xml.parsers.DocumentBuilderFactory dbf = javax.xml.parsers.DocumentBuilderFactory
            .newInstance();

        dbf.setExpandEntityReferences(false);
        dbf.setValidating(false);
        dbf.setNamespaceAware(true);

        javax.xml.parsers.DocumentBuilder db;
        Node rootNode = null;

        try {
            db = dbf.newDocumentBuilder();

            InputSource input = new InputSource(reader);
            org.w3c.dom.Document dom = db.parse(input);

            rootNode = dom.getDocumentElement();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return rootNode;
    }

    /**
    * Give a node and the name of a child of that node, return it. This doesnt
    * do anything complex.
    *
    * @param parentNode
    * @param wantedChildName
    *
    * @return
    */
    public Node getNode(Node parentNode, String wantedChildName) {
        NodeList children = parentNode.getChildNodes();

        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);

            if ((child == null) || (child.getNodeType() != Node.ELEMENT_NODE)) {
                continue;
            }

            String childName = child.getLocalName();

            if (childName == null) {
                childName = child.getNodeName();
            }

            if (childName.equalsIgnoreCase(wantedChildName)) {
                return child;
            }
        }

        return null;
    }

    /**
     * Convenience method to get the value from the specified node.
     *
     * @param node
     * @return
     */
    public String getNodeValue(Node node) {
        return node.getChildNodes().item(0).getNodeValue();
    }

    /**
     * Give a node and the name of a child of that node, find its (string)
     * value. This doesnt do anything complex.
     *
     * @param parentNode
     * @param wantedChildName
     *
     * @return
     */
    public String getNodeChildValue(Node parentNode, String wantedChildName) {
        NodeList children = parentNode.getChildNodes();

        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);

            if ((child == null) || (child.getNodeType() != Node.ELEMENT_NODE)) {
                continue;
            }

            String childName = child.getLocalName();

            if (childName == null) {
                childName = child.getNodeName();
            }

            if (childName.equalsIgnoreCase(wantedChildName)) {
                return child.getChildNodes().item(0).getNodeValue();
            }
        }

        return null;
    }

    /**
     * returns true if this node is named "name".  Ignores case and namespaces.
     *
     * @param n
     * @param name
     *
     * @return
     */
    public boolean nodeNameEqual(Node n, String name) {
        if (n.getNodeName().equalsIgnoreCase(name)) {
            return true;
        }

        String nname = n.getNodeName();
        int idx = nname.indexOf(':');

        if (idx == -1) {
            return false;
        }

        if (nname.substring(idx + 1).equalsIgnoreCase(name)) {
            return true;
        }

        return false;
    }

    /**
     * processSLD:
     *
     * Makes the SLD into a DOM object and validates it.
     * It will then get the layer names and update for each layer.
     *
     * @param sld
     * @param rootNode the root node of the DOM document for parsing
     * @param response
     * @throws IOException
     * @throws WmsException
     */
    private void processSLD(PutStylesRequest serviceRequest, HttpServletRequest request,
        HttpServletResponse response, ServletContext context)
        throws IOException, SldException {
        LOGGER.info("Processing SLD");

        String sld_remote = serviceRequest.getSLD();

        if ((sld_remote != null) && !sld_remote.equals("")) {
            throw new java.lang.UnsupportedOperationException(
                "SLD= param not yet implemented. Use SLD_BODY=");
        }

        String sld_body = serviceRequest.getSldBody(); // the actual SLD body

        if ((sld_body == null) || (sld_body == "")) {
            throw new IllegalArgumentException("The body of the SLD cannot be empty!");
        }

        // write out SLD so we can read it in and validate it
        File temp = File.createTempFile("putStyles", "xml");
        temp.deleteOnExit();

        FileOutputStream fos = new FileOutputStream(temp);
        BufferedOutputStream tempOut = new BufferedOutputStream(fos);

        byte[] bytes = sld_body.getBytes();

        for (int i = 0; i < bytes.length; i++) {
            tempOut.write(bytes[i]);
        }

        tempOut.flush();
        tempOut.close();

        BufferedInputStream fs = new BufferedInputStream(new FileInputStream(temp));

        // finish making our tempory file stream (for SLD validation)
        CharArrayReader xml = new CharArrayReader(sld_body.toCharArray()); // put the xml into a 'Reader'

        Node rootNode = generateDOM(xml);

        // validate the SLD
        SLDValidator validator = new SLDValidator();
        List errors = validator.validateSLD(fs, context);

        if (errors.size() != 0) {
            throw new SldException(SLDValidator.getErrorMessage(xml, errors));
        }

        Node n_namedLayer = getNode(rootNode, "NamedLayer");
        Node n_layerName = getNode(n_namedLayer, "Name");
        Node n_userStyle = getNode(n_namedLayer, "UserStyle");
        Node n_styleName = getNode(n_userStyle, "Name");

        // "ftname_styleLayerName": ignore "_style"
        String layerName = getNodeValue(n_layerName); //.split("_styleLayerName")[0];
        String styleName = getNodeValue(n_styleName);
        LOGGER.info("PutStyles SLD:\nLayer: " + layerName + ", style: " + styleName);

        // store the SLD
        StyleConfig style = new StyleConfig();
        style.setId(styleName);

        // make the SLD file in the data_dir/styles directory
        File data_dir = GeoserverDataDirectory.getGeoserverDataDirectory();
        File style_dir;

        try {
            style_dir = GeoserverDataDirectory.findConfigDir(data_dir, "styles");
        } catch (ConfigurationException cfe) {
            LOGGER.warning("no style dir found, creating new one");
            //if for some bizarre reason we don't fine the dir, make a new one.
            style_dir = new File(data_dir, "styles");
        }

        File styleFile = new File(style_dir, styleName + ".sld"); // styleName.sld
                                                                  //styleFile.createNewFile();

        LOGGER.info("Saving new SLD file to " + styleFile.getPath());

        // populate it with the style code
        StringBuffer sldText = new StringBuffer();
        sldText.append("\n");
        sldText.append("\n");

        FileOutputStream style_fos = new FileOutputStream(styleFile); // save the sld to a file

        String sldBody = serviceRequest.getSldBody();
        int start = sldBody.indexOf("");
        int end = sldBody.indexOf("");

        sldText.append(sldBody.substring(start, end));
        sldText.append("\n");
        sldText.append("");
        style_fos.write(sldText.toString().getBytes());
        style_fos.flush();
        style_fos.close();

        style.setFilename(styleFile);

        // update the data config to tell it about our new style
        DataConfig dataConfig = ConfigRequests.getDataConfig(request);
        dataConfig.addStyle(styleName, style);

        // SLD is set up now, so tell the feature type to use it

        // get our featureType by the layerName
        List keys = dataConfig.getFeatureTypeConfigKeys();
        Iterator it = keys.iterator();
        layerName = null;

        while (it.hasNext()) // get the full featureType name that has the datastore prefix
         {
            String o = it.next().toString();
            String[] os = o.split(":");

            if (os[1].equalsIgnoreCase(layerName)) {
                layerName = o;

                break;
            }
        }

        // get the feature type and save the style for it, if the feature type exists yet
        // If there is no FT there that may mean that the user is just creating it.
        if (layerName != null) {
            FeatureTypeConfig featureTypeConfig = dataConfig.getFeatureTypeConfig(layerName);
            featureTypeConfig.setDefaultStyle(styleName);
        }

        // if successful, return "success"
        //response.setContentType(success_mime_type);
        LOGGER.info("sending back result");

        String message = "\n"
            + "success";
        BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
        byte[] msg = message.getBytes();
        out.write(msg);
        out.flush();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy