Mercurial > dive4elements > river
view flys-client/src/main/java/de/intevation/flys/client/server/FileUploadServiceImpl.java @ 4586:319565a48182
Add the csv export to the GaugeDischargeCurveArtifact
Add the csv export to show the calculation results in the flys client.
author | Björn Ricks <bjoern.ricks@intevation.de> |
---|---|
date | Tue, 27 Nov 2012 17:43:19 +0100 |
parents | b9433322fcaf |
children | 02cf2b1dff84 |
line wrap: on
line source
package de.intevation.flys.client.server; import de.intevation.artifacts.common.utils.XMLUtils; import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator; import de.intevation.artifacts.httpclient.exceptions.ConnectionException; import de.intevation.artifacts.httpclient.http.HttpClient; import de.intevation.artifacts.httpclient.http.HttpClientImpl; import java.io.ByteArrayOutputStream; import java.io.InputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.codec.binary.Base64; import org.apache.commons.fileupload.FileItemIterator; import org.apache.commons.fileupload.FileItemStream; import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.apache.log4j.Logger; import org.w3c.dom.Document; import org.w3c.dom.Element; public class FileUploadServiceImpl extends HttpServlet { private static final Logger logger = Logger.getLogger(FileUploadServiceImpl.class); @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) { processPost(req, resp); } protected void processPost(HttpServletRequest req, HttpServletResponse resp) { logger.debug("handling post request."); String url = getServletContext().getInitParameter("server-url"); Document request = createFileXML(req);; if (request == null) { return; } HttpClient client = new HttpClientImpl(url); try { Document result = client.callService(url, "fileupload", request); if (result == null) { logger.warn("FileUpload service returned no result."); } return; } catch (ConnectionException ce) { logger.error(ce, ce); } } protected Document createFileXML(HttpServletRequest req) { ServletFileUpload upload = new ServletFileUpload(); try{ FileItemIterator iter = upload.getItemIterator(req); while (iter.hasNext()) { FileItemStream item = iter.next(); String name = item.getFieldName(); InputStream stream = item.openStream(); // Process the input stream ByteArrayOutputStream out = new ByteArrayOutputStream(); int len; byte[] buffer = new byte[stream.available()]; while ((len = stream.read(buffer, 0, buffer.length)) != -1) { out.write(buffer, 0, len); } buffer = Base64.encodeBase64(buffer); String b64File = new String(buffer); Document fileDoc = XMLUtils.newDocument(); ElementCreator ec = new ElementCreator(fileDoc, null, null); Element root = ec.create("upload"); Element id = ec.create("artifact-uuid"); id.setTextContent(req.getParameter("uuid")); Element data = ec.create("data"); data.setTextContent(b64File); fileDoc.appendChild(root); root.appendChild(id); root.appendChild(data); return fileDoc; } } catch(Exception e){ logger.debug("Failed to create xml document containing the file."); logger.debug(e, e); } return null; } }