view flys-client/src/main/java/de/intevation/flys/client/server/ChartOutputServiceImpl.java @ 1344:51d4b51a51ed

Added icons to export chart as PNG, PDF and SVG to the ChartToolbar. flys-client/trunk@2998 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Mon, 17 Oct 2011 17:33:27 +0000
parents e74bf6bfeeb6
children ab8eb2f544f2
line wrap: on
line source
package de.intevation.flys.client.server;

import java.io.OutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.w3c.dom.Document;

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

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

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

            prepareHeader(req, resp);

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

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

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


    protected Map<String, String> prepareChartAttributes(HttpServletRequest req) {
        Map<String, String> attr = new HashMap<String, String>();

        Map params = req.getParameterMap();

        attr.put("width", req.getParameter("width"));
        attr.put("height", req.getParameter("height"));
        attr.put("minx", req.getParameter("minx"));
        attr.put("maxx", req.getParameter("maxx"));
        attr.put("miny", req.getParameter("miny"));
        attr.put("maxy", req.getParameter("maxy"));
        attr.put("format", req.getParameter("format"));

        return attr;
    }


    protected void prepareHeader(
        HttpServletRequest  req,
        HttpServletResponse resp
    ) {
        String export = req.getParameter("export");

        if (export != null && export.equals("true")) {
            String format = req.getParameter("format");

            if (format == null || format.length() == 0) {
                format = "png";
            }

            String fn = "chart_export" + getFileExtension(format);

            resp.setHeader("Content-Disposition", "attachment;filename=" + fn);
            resp.setHeader("Content-Type", getMimeType(format));
        }
    }


    protected String getFileExtension(String format) {
        if (format.equals("png")) {
            return ".png";
        }
        else if (format.equals("pdf")) {
            return ".pdf";
        }
        else if (format.equals("svg")) {
            return ".svg";
        }

        return ".png";
    }


    protected String getMimeType(String format) {
        if (format.equals("png")) {
            return "image/png";
        }
        else if (format.equals("pdf")) {
            return "application/pdf";
        }
        else if (format.equals("svg")) {
            return "svg+xml";
        }

        return "image/png";
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org