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; ingo@274: teichmann@7486: import java.io.InputStreamReader; ingo@274: import java.io.OutputStream; ingo@274: import java.io.IOException; teichmann@7486: import java.io.OutputStreamWriter; ingo@274: ingo@274: import org.w3c.dom.Document; raimund@3499: import org.w3c.dom.Element; ingo@274: ingo@1367: import org.apache.log4j.Logger; ingo@1367: ingo@274: import javax.servlet.http.HttpServlet; ingo@274: import javax.servlet.http.HttpServletRequest; ingo@274: import javax.servlet.http.HttpServletResponse; ingo@274: teichmann@5835: import org.dive4elements.artifacts.common.utils.ClientProtocolUtils; teichmann@5835: import org.dive4elements.artifacts.common.utils.XMLUtils; ingo@274: teichmann@5835: import org.dive4elements.artifacts.httpclient.http.HttpClient; teichmann@5835: import org.dive4elements.artifacts.httpclient.http.HttpClientImpl; ingo@274: ingo@274: ingo@274: /** ingo@274: * This service is used to request a data export from the artifact server. The ingo@274: * response is directed directly to the output stream, so that a file dialog is ingo@274: * opened. ingo@274: * ingo@274: * @author Ingo Weinzierl ingo@274: */ ingo@274: public class ExportServiceImpl ingo@274: extends HttpServlet ingo@274: { teichmann@8203: private static final Logger log = ingo@1367: Logger.getLogger(ExportServiceImpl.class); ingo@1367: ingo@1367: ingo@274: public void doGet(HttpServletRequest req, HttpServletResponse resp) { teichmann@8203: log.info("ExportServiceImpl.doGet"); ingo@274: ingo@274: try { teichmann@7486: final OutputStream out = resp.getOutputStream(); ingo@274: sascha@3500: String url = getServletContext().getInitParameter("server-url"); sascha@3500: String uuid = req.getParameter("uuid"); rrenkert@4912: String name = req.getParameter("name"); sascha@3500: String mode = req.getParameter("mode"); sascha@3500: String type = req.getParameter("type"); sascha@3500: String locale = req.getParameter("locale"); sascha@3500: String km = req.getParameter("km"); rrenkert@4912: String fn = name + "." + type; teichmann@7486: final String enc = req.getParameter("encoding"); ingo@274: ingo@274: resp.setHeader("Content-Disposition", "attachment;filename=" + fn); ingo@274: teichmann@8203: if (log.isDebugEnabled()) { teichmann@8203: log.debug("Request " + type + " export."); sascha@3500: } raimund@3499: raimund@3499: Document attr = null; raimund@3499: if (km != null && km.length() > 0) { raimund@3499: attr = XMLUtils.newDocument(); raimund@3499: XMLUtils.ElementCreator ec = raimund@3499: new XMLUtils.ElementCreator(attr, null, null); raimund@3499: Element e = ec.create("km"); raimund@3499: e.setTextContent(km); raimund@3499: attr.appendChild(e); raimund@3499: } ingo@274: Document request = ClientProtocolUtils.newOutCollectionDocument( raimund@3499: uuid, mode, type, attr); raimund@1425: HttpClient client = new HttpClientImpl(url, locale); felix@7139: felix@7139: if (enc != null) { teichmann@7486: InputStreamReader in = new InputStreamReader( teichmann@7486: client.collectionOut(request, uuid, mode), teichmann@7486: "UTF-8"); felix@7139: try { teichmann@7486: OutputStreamWriter encOut = new OutputStreamWriter(out, enc); teichmann@7486: char buf [] = new char[4096]; teichmann@7486: int c; teichmann@7486: while ((c = in.read(buf, 0, buf.length)) >= 0) { teichmann@7486: encOut.write(buf, 0, c); teichmann@7486: } aheinecke@7633: encOut.flush(); felix@7139: } teichmann@7486: finally { teichmann@7486: in.close(); felix@7139: } felix@7139: } teichmann@7486: else { // Just copy thru. teichmann@7486: client.collectionOut(request, uuid, mode, out); teichmann@7486: out.flush(); teichmann@7486: } ingo@274: } ingo@274: catch (IOException ioe) { teichmann@8203: log.error(ioe, ioe); ingo@274: } ingo@274: } ingo@274: } ingo@274: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :