ingo@78: package de.intevation.flys.client.server; ingo@78: ingo@78: import java.io.OutputStream; ingo@78: import java.io.IOException; ingo@78: ingo@78: import org.w3c.dom.Document; ingo@280: import org.w3c.dom.Element; ingo@78: ingo@78: import javax.servlet.http.HttpServlet; ingo@78: import javax.servlet.http.HttpServletRequest; ingo@78: import javax.servlet.http.HttpServletResponse; ingo@78: ingo@280: import de.intevation.artifacts.common.ArtifactNamespaceContext; ingo@213: import de.intevation.artifacts.common.utils.ClientProtocolUtils; ingo@280: import de.intevation.artifacts.common.utils.XMLUtils; ingo@280: import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator; 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@280: /** The default chart width if no value is specified in the request.*/ ingo@280: public static final int DEFAULT_CHART_WIDTH = 600; ingo@280: ingo@280: /** The default chart height if no value is specified in the request.*/ ingo@280: public static final int DEFAULT_CHART_HEIGHT = 400; ingo@280: ingo@280: ingo@78: public void doGet(HttpServletRequest req, HttpServletResponse resp) { ingo@78: System.out.println("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@213: Document request = ClientProtocolUtils.newOutCollectionDocument( ingo@432: uuid, type, type, getChartAttributes(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@78: System.err.println(ioe.getMessage()); ingo@78: } ingo@280: catch (Exception e) { ingo@280: System.err.println(e.getMessage()); ingo@280: } ingo@280: } ingo@280: ingo@280: ingo@280: /** ingo@280: * This method returns a document which might contain parameters to adjust ingo@280: * chart settings. The document is created using the information that are ingo@280: * contained in the request object. ingo@280: * ingo@280: * @param req The request document. ingo@280: * ingo@280: * @return a document to adjust chart settings. ingo@280: */ ingo@280: protected Document getChartAttributes(HttpServletRequest req) { ingo@280: Document doc = XMLUtils.newDocument(); ingo@280: ingo@280: ElementCreator ec = new ElementCreator( ingo@280: doc, ingo@280: ArtifactNamespaceContext.NAMESPACE_URI, ingo@280: ArtifactNamespaceContext.NAMESPACE_PREFIX); ingo@280: ingo@542: Element attributes = ec.create("attributes"); ingo@542: ingo@542: appendChartSize(req, attributes, ec); ingo@542: appendXRange(req, attributes, ec); ingo@542: appendYRange(req, attributes, ec); ingo@542: ingo@542: doc.appendChild(attributes); ingo@280: ingo@280: return doc; ingo@280: } ingo@280: ingo@280: ingo@280: /** ingo@280: * This method extracts the size (width/height) of a chart from request ingo@280: * object and append those values - if they exist - to the attribute ingo@280: * document used to adjust chart settings. ingo@280: * ingo@280: * @param req The request object that might contain the chart size. ingo@542: * @param attributes The attributes element used to adjust chart settings. ingo@280: * @param ec The ElementCreator that might be used to create new Elements. ingo@280: */ ingo@280: protected void appendChartSize( ingo@280: HttpServletRequest req, ingo@542: Element attributes, ingo@280: ElementCreator ec) ingo@280: { ingo@280: Element size = ec.create("size"); ingo@280: ingo@280: String width = req.getParameter("width"); ingo@280: String height = req.getParameter("height"); ingo@280: ingo@280: if (width == null || height == null) { ingo@280: width = Integer.toString(DEFAULT_CHART_WIDTH); ingo@280: height = Integer.toString(DEFAULT_CHART_HEIGHT); ingo@280: } ingo@280: ingo@280: ec.addAttr(size, "width", width, true); ingo@280: ec.addAttr(size, "height", height, true); ingo@280: ingo@542: attributes.appendChild(size); ingo@542: } ingo@542: ingo@542: ingo@542: /** ingo@542: * This method extracts the x range for the chart from request object and ingo@542: * appends those range - if it exists - to the attribute document used to ingo@542: * adjust the chart settings. ingo@542: * ingo@542: * @param req The request object that might contain the chart size. ingo@542: * @param doc The attribute document used to adjust chart settings. ingo@542: * @param ec The ElementCreator that might be used to create new Elements. ingo@542: */ ingo@542: protected void appendXRange( ingo@542: HttpServletRequest req, ingo@542: Element attributes, ingo@542: ElementCreator ec) ingo@542: { ingo@542: Element range = ec.create("xrange"); ingo@542: ingo@542: String from = req.getParameter("minx"); ingo@542: String to = req.getParameter("maxx"); ingo@542: ingo@542: if (from != null && to != null) { ingo@542: ec.addAttr(range, "from", from, true); ingo@542: ec.addAttr(range, "to", to, true); ingo@542: ingo@542: attributes.appendChild(range); ingo@542: } ingo@542: } ingo@542: ingo@542: ingo@542: /** ingo@542: * This method extracts the x range for the chart from request object and ingo@542: * appends those range - if it exists - to the attribute document used to ingo@542: * adjust the chart settings. ingo@542: * ingo@542: * @param req The request object that might contain the chart size. ingo@542: * @param doc The attribute document used to adjust chart settings. ingo@542: * @param ec The ElementCreator that might be used to create new Elements. ingo@542: */ ingo@542: protected void appendYRange( ingo@542: HttpServletRequest req, ingo@542: Element attributes, ingo@542: ElementCreator ec) ingo@542: { ingo@542: Element range = ec.create("yrange"); ingo@542: ingo@542: String from = req.getParameter("miny"); ingo@542: String to = req.getParameter("maxy"); ingo@542: ingo@542: if (from != null && to != null) { ingo@542: ec.addAttr(range, "from", from, true); ingo@542: ec.addAttr(range, "to", to, true); ingo@542: ingo@542: attributes.appendChild(range); ingo@542: } ingo@78: } ingo@78: } ingo@280: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :