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