sascha@2488: package de.intevation.flys.client.server; sascha@2488: sascha@2488: import de.intevation.artifacts.common.utils.XMLUtils; sascha@2488: sascha@2488: import de.intevation.artifacts.httpclient.http.HttpClient; sascha@2488: import de.intevation.artifacts.httpclient.http.HttpClientImpl; sascha@2488: sascha@2488: import de.intevation.artifacts.httpclient.exceptions.ConnectionException; sascha@2488: sascha@2488: import de.intevation.artifacts.httpclient.http.response.StreamResponseHandler; sascha@2488: sascha@2488: import java.io.IOException; sascha@2488: import java.io.InputStream; sascha@2488: import java.io.OutputStream; sascha@2488: sascha@2488: import javax.servlet.http.HttpServlet; sascha@2488: import javax.servlet.http.HttpServletRequest; sascha@2488: import javax.servlet.http.HttpServletResponse; sascha@2488: sascha@2488: import org.apache.log4j.Logger; sascha@2488: sascha@2488: import org.w3c.dom.Document; sascha@2488: import org.w3c.dom.Element; sascha@2488: import org.w3c.dom.NodeList; sascha@2488: sascha@2488: public class FixingsKMChartServiceImpl sascha@2488: extends HttpServlet sascha@2488: { sascha@2488: private static final Logger log = sascha@2488: Logger.getLogger(FixingsKMChartServiceImpl.class); sascha@2488: sascha@2488: public static final String SERVICE_NAME = "fixings-km-chart"; sascha@2488: sascha@2488: public FixingsKMChartServiceImpl() { sascha@2488: } sascha@2488: sascha@2488: public void doGet(HttpServletRequest req, HttpServletResponse resp) { sascha@2488: sascha@2488: log.info("FixingsKMChartServiceImpl.doGet"); sascha@2488: sascha@2488: String url = getServletContext().getInitParameter("server-url"); sascha@2488: String locale = req.getParameter("locale"); sascha@2488: String filter = req.getParameter("filter"); sascha@2488: sascha@2488: if (filter == null || filter.length() == 0) { sascha@2488: log.warn("Missing 'filter' parameter."); sascha@2488: return; sascha@2488: } sascha@2488: sascha@2488: if (locale == null || locale.length() == 0) { sascha@2488: locale = "de"; sascha@2488: } sascha@2488: sascha@2488: Document filterDoc = XMLUtils.jsonToXML(filter); sascha@2488: sascha@2488: if (filterDoc == null) { sascha@2488: log.warn("Creating filter document failed."); sascha@2488: return; sascha@2488: } sascha@2488: sascha@2488: InputStream in; sascha@2488: sascha@2488: try { sascha@2493: HttpClient client = new HttpClientImpl(url, locale); sascha@2488: in = (InputStream)client.callService( sascha@2488: url, // XXX: Why? The URL is passed by construction already. sascha@2488: SERVICE_NAME, sascha@2488: filterDoc, sascha@2488: new StreamResponseHandler()); sascha@2488: } sascha@2488: catch (ConnectionException ce) { sascha@2488: log.error(ce); sascha@2488: return; sascha@2488: } sascha@2488: sascha@2493: resp.setHeader("Content-Type", guessMIMEType(filterDoc)); sascha@2493: sascha@2488: try { sascha@2488: OutputStream out = resp.getOutputStream(); sascha@2488: sascha@2488: byte [] buf = new byte[4096]; sascha@2488: int i = -1; sascha@2488: while ((i = in.read(buf)) >= 0) { sascha@2488: out.write(buf, 0, i); sascha@2488: } sascha@2488: out.flush(); sascha@2488: } sascha@2488: catch (IOException ioe) { sascha@2488: log.error(ioe); sascha@2488: } sascha@2488: finally { sascha@2488: try { in.close(); } sascha@2488: catch (IOException ioe) { /* ignored */ } sascha@2488: } sascha@2488: } sascha@2488: sascha@2488: protected static String guessMIMEType(Document document) { sascha@2488: sascha@2488: NodeList formats = document.getElementsByTagName("format"); sascha@2488: sascha@2488: String format = "png"; sascha@2488: sascha@2488: if (formats.getLength() > 0) { sascha@2488: String type = ((Element)formats.item(0)).getAttribute("type"); sascha@2488: if (type.length() > 0) { sascha@2488: format = type; sascha@2488: } sascha@2488: } sascha@2488: sascha@2488: return "image/" + format; sascha@2488: } sascha@2488: } sascha@2488: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :