view flys-client/src/main/java/de/intevation/flys/client/server/ChartOutputServiceImpl.java @ 516:ba606e575663

ISSUE-85 (part II/III) Repaired broken location/range panel. flys-client/trunk@1987 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 24 May 2011 10:52:00 +0000
parents 8ea213bd8fba
children 7c57149e8715
line wrap: on
line source
package de.intevation.flys.client.server;

import java.io.OutputStream;
import java.io.IOException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import de.intevation.artifacts.common.ArtifactNamespaceContext;
import de.intevation.artifacts.common.utils.ClientProtocolUtils;
import de.intevation.artifacts.common.utils.XMLUtils;
import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator;

import de.intevation.artifacts.httpclient.http.HttpClient;
import de.intevation.artifacts.httpclient.http.HttpClientImpl;


/**
 * This service is used to request a chart from the artifact server. The
 * response is directed directly to the output stream, so the image that is
 * retrieved is displayed in the UI afterwards.
 *
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public class ChartOutputServiceImpl
extends      HttpServlet
{
    /** The default chart width if no value is specified in the request.*/
    public static final int DEFAULT_CHART_WIDTH  = 600;

    /** The default chart height if no value is specified in the request.*/
    public static final int DEFAULT_CHART_HEIGHT = 400;


    public void doGet(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("ChartOutputServiceImpl.doGet");

        try {
            OutputStream out = resp.getOutputStream();

            String serverUrl = req.getParameter("server");
            String uuid      = req.getParameter("uuid");
            String type      = req.getParameter("type");
            String locale    = req.getParameter("locale");

            Document request = ClientProtocolUtils.newOutCollectionDocument(
                uuid, type, type, getChartAttributes(req));

            HttpClient client = new HttpClientImpl(serverUrl, locale);
            client.collectionOut(request, uuid, "chart", out);

            out.close();
            out.flush();
        }
        catch (IOException ioe) {
            System.err.println(ioe.getMessage());
        }
        catch (Exception e) {
            System.err.println(e.getMessage());
        }
    }


    /**
     * This method returns a document which might contain parameters to adjust
     * chart settings. The document is created using the information that are
     * contained in the request object.
     *
     * @param req The request document.
     *
     * @return a document to adjust chart settings.
     */
    protected Document getChartAttributes(HttpServletRequest req) {
        Document doc = XMLUtils.newDocument();

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

        appendChartSize(req, doc, ec);

        return doc;
    }


    /**
     * This method extracts the size (width/height) of a chart from request
     * object and append those values - if they exist - to the attribute
     * document used to adjust chart settings.
     *
     * @param req The request object that might contain the chart size.
     * @param doc The attribute document used to adjust chart settings.
     * @param ec The ElementCreator that might be used to create new Elements.
     */
    protected void appendChartSize(
        HttpServletRequest req,
        Document           doc,
        ElementCreator     ec)
    {
        Element size = ec.create("size");

        String width  = req.getParameter("width");
        String height = req.getParameter("height");

        if (width == null || height == null) {
            width  = Integer.toString(DEFAULT_CHART_WIDTH);
            height = Integer.toString(DEFAULT_CHART_HEIGHT);
        }

        ec.addAttr(size, "width", width, true);
        ec.addAttr(size, "height", height, true);

        doc.appendChild(size);
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org