Mercurial > dive4elements > river
view flys-client/src/main/java/de/intevation/flys/client/server/FileUploadServiceImpl.java @ 2879:41478952e16d
Corrected image path to add_wms.png
flys-client/trunk@4533 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Sun, 27 May 2012 12:50:38 +0000 |
parents | a6c6f305546c |
children | b9433322fcaf |
line wrap: on
line source
package de.intevation.flys.client.server; import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator; import de.intevation.artifacts.common.utils.XMLUtils; 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); } Base64 encoder = new Base64(); String b64File = Base64.encodeBase64String(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; } }