ingo@78: package de.intevation.flys.client.server; ingo@78: ingo@78: import java.io.OutputStream; ingo@78: import java.io.IOException; ingo@549: import java.util.HashMap; ingo@549: import java.util.Map; ingo@78: ingo@78: import org.w3c.dom.Document; ingo@78: ingo@1367: import org.apache.log4j.Logger; ingo@1367: ingo@78: import javax.servlet.http.HttpServlet; ingo@78: import javax.servlet.http.HttpServletRequest; ingo@78: import javax.servlet.http.HttpServletResponse; ingo@78: ingo@213: import de.intevation.artifacts.common.utils.ClientProtocolUtils; ingo@213: ingo@78: import de.intevation.artifacts.httpclient.http.HttpClient; ingo@78: import de.intevation.artifacts.httpclient.http.HttpClientImpl; ingo@78: ingo@78: ingo@78: /** ingo@78: * This service is used to request a chart from the artifact server. The ingo@78: * response is directed directly to the output stream, so the image that is ingo@78: * retrieved is displayed in the UI afterwards. ingo@78: * ingo@78: * @author Ingo Weinzierl ingo@78: */ ingo@78: public class ChartOutputServiceImpl ingo@78: extends HttpServlet ingo@78: { ingo@1367: private static final Logger logger = ingo@1367: Logger.getLogger(ChartOutputServiceImpl.class); ingo@1367: ingo@1367: ingo@78: public void doGet(HttpServletRequest req, HttpServletResponse resp) { ingo@1367: logger.info("ChartOutputServiceImpl.doGet"); ingo@78: ingo@78: try { ingo@78: OutputStream out = resp.getOutputStream(); ingo@78: ingo@78: String serverUrl = req.getParameter("server"); ingo@78: String uuid = req.getParameter("uuid"); ingo@213: String type = req.getParameter("type"); ingo@254: String locale = req.getParameter("locale"); ingo@78: ingo@1344: prepareHeader(req, resp); ingo@1344: ingo@213: Document request = ClientProtocolUtils.newOutCollectionDocument( ingo@549: uuid, type, type, ingo@549: ChartServiceHelper.getChartAttributes(prepareChartAttributes(req))); ingo@78: ingo@254: HttpClient client = new HttpClientImpl(serverUrl, locale); ingo@213: client.collectionOut(request, uuid, "chart", out); ingo@78: ingo@78: out.close(); ingo@78: out.flush(); ingo@78: } ingo@78: catch (IOException ioe) { ingo@1367: logger.error(ioe, ioe); ingo@78: } ingo@280: catch (Exception e) { ingo@1367: logger.error(e, e); ingo@280: } ingo@280: } ingo@280: ingo@280: ingo@549: protected Map prepareChartAttributes(HttpServletRequest req) { ingo@549: Map attr = new HashMap(); ingo@280: ingo@549: Map params = req.getParameterMap(); ingo@542: ingo@549: attr.put("width", req.getParameter("width")); ingo@549: attr.put("height", req.getParameter("height")); ingo@549: attr.put("minx", req.getParameter("minx")); ingo@549: attr.put("maxx", req.getParameter("maxx")); ingo@549: attr.put("miny", req.getParameter("miny")); ingo@549: attr.put("maxy", req.getParameter("maxy")); ingo@1344: attr.put("format", req.getParameter("format")); ingo@542: ingo@549: return attr; ingo@78: } ingo@1344: ingo@1344: ingo@1344: protected void prepareHeader( ingo@1344: HttpServletRequest req, ingo@1344: HttpServletResponse resp ingo@1344: ) { ingo@1344: String export = req.getParameter("export"); ingo@1344: ingo@1344: if (export != null && export.equals("true")) { ingo@1344: String format = req.getParameter("format"); ingo@1344: ingo@1344: if (format == null || format.length() == 0) { ingo@1344: format = "png"; ingo@1344: } ingo@1344: ingo@1344: String fn = "chart_export" + getFileExtension(format); ingo@1344: ingo@1344: resp.setHeader("Content-Disposition", "attachment;filename=" + fn); ingo@1344: resp.setHeader("Content-Type", getMimeType(format)); ingo@1344: } ingo@1344: } ingo@1344: ingo@1344: ingo@1344: protected String getFileExtension(String format) { ingo@1344: if (format.equals("png")) { ingo@1344: return ".png"; ingo@1344: } ingo@1344: else if (format.equals("pdf")) { ingo@1344: return ".pdf"; ingo@1344: } ingo@1344: else if (format.equals("svg")) { ingo@1344: return ".svg"; ingo@1344: } ingo@1344: ingo@1344: return ".png"; ingo@1344: } ingo@1344: ingo@1344: ingo@1344: protected String getMimeType(String format) { ingo@1344: if (format.equals("png")) { ingo@1344: return "image/png"; ingo@1344: } ingo@1344: else if (format.equals("pdf")) { ingo@1344: return "application/pdf"; ingo@1344: } ingo@1344: else if (format.equals("svg")) { ingo@1344: return "svg+xml"; ingo@1344: } ingo@1344: ingo@1344: return "image/png"; ingo@1344: } ingo@78: } ingo@280: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :