ingo@782: package de.intevation.flys.client.server;
ingo@782: 
ingo@782: import java.io.InputStream;
ingo@782: import java.io.IOException;
ingo@782: import java.io.OutputStream;
ingo@782: 
ingo@782: import org.w3c.dom.Document;
ingo@782: import org.w3c.dom.Element;
ingo@782: 
ingo@1367: import org.apache.log4j.Logger;
ingo@1367: 
ingo@782: import javax.servlet.http.HttpServlet;
ingo@782: import javax.servlet.http.HttpServletRequest;
ingo@782: import javax.servlet.http.HttpServletResponse;
ingo@782: 
ingo@782: import de.intevation.artifacts.common.ArtifactNamespaceContext;
ingo@782: import de.intevation.artifacts.common.utils.XMLUtils;
ingo@782: 
ingo@782: import de.intevation.artifacts.httpclient.exceptions.ConnectionException;
ingo@782: import de.intevation.artifacts.httpclient.http.HttpClient;
ingo@782: import de.intevation.artifacts.httpclient.http.HttpClientImpl;
ingo@782: import de.intevation.artifacts.httpclient.http.response.StreamResponseHandler;
ingo@782: 
ingo@782: 
ingo@782: /**
ingo@782:  * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
ingo@782:  */
ingo@782: public class DistanceInfoXML
ingo@782: extends      HttpServlet
ingo@782: {
ingo@1367:     private static final Logger logger = Logger.getLogger(DistanceInfoXML.class);
ingo@1367: 
ingo@1367: 
ingo@782:     public static final String ERROR_NO_DISTANCEINFO_FOUND =
ingo@782:         "error_no_distanceinfo_found";
ingo@782: 
ingo@782: 
ingo@782:     public void doGet(HttpServletRequest req, HttpServletResponse resp) {
ingo@1367:         logger.info("DistanceInfoXML.doGet");
ingo@782: 
raimund@1425:         String url  = getServletContext().getInitParameter("server-url");
raimund@1425: 
ingo@782:         String river  = req.getParameter("river");
ingo@782:         String filter = req.getParameter("filter");
ingo@782: 
ingo@782:         Document doc = XMLUtils.newDocument();
ingo@782: 
ingo@782:         XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator(
ingo@782:             doc,
ingo@782:             ArtifactNamespaceContext.NAMESPACE_URI,
ingo@782:             ArtifactNamespaceContext.NAMESPACE_PREFIX);
ingo@782: 
ingo@782:         Element riverEl = ec.create("river");
ingo@782:         riverEl.setTextContent(river);
ingo@782: 
ingo@782:         doc.appendChild(riverEl);
ingo@782: 
ingo@782:         if (filter != null && filter.length() > 0) {
ingo@782:             Element typeEl = ec.create("filter");
ingo@782:             typeEl.setTextContent(filter);
ingo@782: 
ingo@782:             riverEl.appendChild(typeEl);
ingo@782:         }
ingo@782: 
ingo@782:         HttpClient client = new HttpClientImpl(url);
ingo@782: 
ingo@782:         try {
ingo@782:             InputStream in = (InputStream) client.callService(
ingo@782:                 url, "distanceinfo", doc, new StreamResponseHandler());
ingo@782: 
ingo@782:             OutputStream out = resp.getOutputStream();
ingo@782: 
ingo@782:             byte[] b = new byte[4096];
ingo@782:             int i;
ingo@782:             while ((i = in.read(b)) >= 0) {
ingo@782:                 out.write(b, 0, i);
ingo@782:             }
ingo@782: 
ingo@782:             out.flush();
ingo@782:             out.close();
ingo@782:         }
ingo@782:         catch (ConnectionException ce) {
ingo@1367:             logger.error(ce, ce);
ingo@782:         }
ingo@782:         catch (IOException ioe) {
ingo@1367:             logger.error(ioe, ioe);
ingo@782:         }
ingo@782:     }
ingo@782: }
ingo@782: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :