raimund@2494: package de.intevation.flys.client.server;
raimund@2494: 
sascha@2497: import de.intevation.artifacts.common.utils.XMLUtils;
christian@2954: import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator;
sascha@2497: import de.intevation.artifacts.httpclient.exceptions.ConnectionException;
sascha@2497: import de.intevation.artifacts.httpclient.http.HttpClient;
sascha@2497: import de.intevation.artifacts.httpclient.http.HttpClientImpl;
sascha@2497: 
sascha@2497: import java.io.ByteArrayOutputStream;
sascha@2497: import java.io.InputStream;
raimund@2494: 
raimund@2494: import javax.servlet.http.HttpServlet;
raimund@2494: import javax.servlet.http.HttpServletRequest;
raimund@2494: import javax.servlet.http.HttpServletResponse;
raimund@2494: 
sascha@2497: import org.apache.commons.codec.binary.Base64;
sascha@2497: import org.apache.commons.fileupload.FileItemIterator;
sascha@2497: import org.apache.commons.fileupload.FileItemStream;
sascha@2497: import org.apache.commons.fileupload.servlet.ServletFileUpload;
sascha@2497: import org.apache.log4j.Logger;
sascha@2497: import org.w3c.dom.Document;
sascha@2497: import org.w3c.dom.Element;
raimund@2494: 
raimund@2494: public class FileUploadServiceImpl
raimund@2494: extends      HttpServlet
raimund@2494: {
raimund@2494:     private static final Logger logger = Logger.getLogger(FileUploadServiceImpl.class);
raimund@2494: 
raimund@2494:     @Override
raimund@2494:     protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
raimund@2494:         processPost(req, resp);
raimund@2494:     }
raimund@2494: 
raimund@2494: 
raimund@2494:     protected void processPost(HttpServletRequest req, HttpServletResponse resp) {
raimund@2494:         logger.debug("handling post request.");
raimund@2494: 
raimund@2494:         String url  = getServletContext().getInitParameter("server-url");
raimund@2494: 
christian@4632:         Document request = createFileXML(req);
raimund@2494: 
raimund@2494:         if (request == null) {
raimund@2494:             return;
raimund@2494:         }
raimund@2494:         HttpClient client = new HttpClientImpl(url);
raimund@2494: 
raimund@2494:         try {
raimund@2494:             Document result = client.callService(url, "fileupload", request);
raimund@2494: 
raimund@2494:             if (result == null) {
raimund@2494:                 logger.warn("FileUpload service returned no result.");
raimund@2494:             }
raimund@2494: 
raimund@2494:             return;
raimund@2494:         }
raimund@2494:         catch (ConnectionException ce) {
raimund@2494:             logger.error(ce, ce);
raimund@2494:         }
raimund@2494:     }
raimund@2494: 
raimund@2494: 
raimund@2494:     protected Document createFileXML(HttpServletRequest req) {
raimund@2494:            ServletFileUpload upload = new ServletFileUpload();
raimund@2494: 
raimund@2494:         try{
raimund@2494:             FileItemIterator iter = upload.getItemIterator(req);
raimund@2494: 
raimund@2494:             while (iter.hasNext()) {
raimund@2494:                 FileItemStream item = iter.next();
raimund@2494: 
raimund@2494:                 String name = item.getFieldName();
raimund@2494:                 InputStream stream = item.openStream();
raimund@2494: 
raimund@2494:                 // Process the input stream
raimund@2494:                 ByteArrayOutputStream out = new ByteArrayOutputStream();
raimund@2494:                 int len;
raimund@2494:                 byte[] buffer = new byte[stream.available()];
raimund@2494:                 while ((len = stream.read(buffer, 0, buffer.length)) != -1) {
raimund@2494:                     out.write(buffer, 0, len);
raimund@2494:                 }
raimund@2494: 
christian@2954:                 buffer = Base64.encodeBase64(buffer);
christian@2954:                 String b64File = new String(buffer);
raimund@2494: 
raimund@2494:                 Document fileDoc = XMLUtils.newDocument();
raimund@2494: 
raimund@2494:                 ElementCreator ec = new ElementCreator(fileDoc, null, null);
raimund@2494:                 Element root = ec.create("upload");
raimund@2494:                 Element id = ec.create("artifact-uuid");
raimund@2494:                 id.setTextContent(req.getParameter("uuid"));
raimund@2494: 
raimund@2494:                 Element data = ec.create("data");
raimund@2494:                 data.setTextContent(b64File);
raimund@2494: 
raimund@2494:                 fileDoc.appendChild(root);
raimund@2494:                 root.appendChild(id);
raimund@2494:                 root.appendChild(data);
raimund@2494: 
raimund@2494:                 return fileDoc;
raimund@2494:             }
raimund@2494:         }
raimund@2494:         catch(Exception e){
raimund@2494:             logger.debug("Failed to create xml document containing the file.");
raimund@2494:             logger.debug(e, e);
raimund@2494:         }
raimund@2494:         return null;
raimund@2494:     }
raimund@2494: }