teichmann@5861: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5861: * Software engineering by Intevation GmbH teichmann@5861: * teichmann@5993: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5861: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5993: * documentation coming with Dive4Elements River for details. teichmann@5861: */ teichmann@5861: teichmann@5835: package org.dive4elements.river.client.server; raimund@1600: raimund@1600: import java.io.InputStream; raimund@1600: import java.io.IOException; raimund@1600: import java.io.OutputStream; raimund@1600: raimund@1600: import org.w3c.dom.Document; raimund@1600: import org.w3c.dom.Element; raimund@1600: raimund@1600: import org.apache.log4j.Logger; raimund@1600: raimund@1600: import javax.servlet.http.HttpServlet; raimund@1600: import javax.servlet.http.HttpServletRequest; raimund@1600: import javax.servlet.http.HttpServletResponse; raimund@1600: teichmann@5835: import org.dive4elements.artifacts.common.ArtifactNamespaceContext; teichmann@5835: import org.dive4elements.artifacts.common.utils.XMLUtils; raimund@1600: 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; teichmann@5835: import org.dive4elements.artifacts.httpclient.http.response.StreamResponseHandler; raimund@1600: raimund@1600: raimund@1600: /** raimund@1600: * @author Raimund Renkert raimund@1600: */ raimund@1600: public class DischargeInfoXML raimund@1600: extends HttpServlet raimund@1600: { teichmann@8203: private static final Logger log = Logger.getLogger(DischargeInfoXML.class); raimund@1600: raimund@1600: raimund@1600: public static final String ERROR_NO_DISTANCEINFO_FOUND = raimund@1600: "error_no_dischargeinfo_found"; raimund@1600: raimund@1600: raimund@1600: public void doGet(HttpServletRequest req, HttpServletResponse resp) { teichmann@8203: log.info("DischargeInfoXML.doGet"); raimund@1600: raimund@1600: String url = getServletContext().getInitParameter("server-url"); raimund@1600: raimund@1600: String gauge = req.getParameter("gauge"); raimund@1600: aheinecke@6837: String river = req.getParameter("river"); aheinecke@6837: raimund@1600: Document doc = XMLUtils.newDocument(); raimund@1600: raimund@1600: XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator( raimund@1600: doc, raimund@1600: ArtifactNamespaceContext.NAMESPACE_URI, raimund@1600: ArtifactNamespaceContext.NAMESPACE_PREFIX); raimund@1600: raimund@1600: Element gaugeEl = ec.create("gauge"); raimund@1600: gaugeEl.setTextContent(gauge); raimund@1600: aheinecke@6837: if (river != null && !river.isEmpty()) { aheinecke@6842: Element riverEl = ec.create("river"); aheinecke@6837: riverEl.setTextContent(river); aheinecke@6837: gaugeEl.appendChild(riverEl); aheinecke@6837: } aheinecke@6837: raimund@1600: doc.appendChild(gaugeEl); raimund@1600: raimund@1600: HttpClient client = new HttpClientImpl(url); raimund@1600: raimund@1600: try { raimund@1600: InputStream in = (InputStream) client.callService( raimund@1600: url, "dischargeinfo", doc, new StreamResponseHandler()); raimund@1600: raimund@1600: OutputStream out = resp.getOutputStream(); raimund@1600: raimund@1600: byte[] b = new byte[4096]; raimund@1600: int i; raimund@1600: while ((i = in.read(b)) >= 0) { raimund@1600: out.write(b, 0, i); raimund@1600: } raimund@1600: raimund@1600: out.flush(); raimund@1600: out.close(); raimund@1600: } raimund@1600: catch (ConnectionException ce) { teichmann@8203: log.error(ce, ce); raimund@1600: } raimund@1600: catch (IOException ioe) { teichmann@8203: log.error(ioe, ioe); raimund@1600: } raimund@1600: } raimund@1600: } raimund@1600: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :