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