view flys-client/src/main/java/de/intevation/flys/client/server/FileUploadServiceImpl.java @ 5200:42bb6ff78d1b 2.9.11

Directly set the connectionInitSqls on the datasource Somehow the factory fails to set the connectionInitSqls if we add it to the dbcpProperties. So we now set it directly
author Andre Heinecke <aheinecke@intevation.de>
date Fri, 08 Mar 2013 11:48:33 +0100
parents 04ccec566689
children
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.IOException;
import java.io.InputStream;
import java.io.PrintWriter;

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) {
        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);

            PrintWriter respWriter = resp.getWriter();
            respWriter.write("<html><link href='FLYS.css' rel='stylesheet' type='text/css'>");
            respWriter.write("<body><div style='font-face: Arial,Verdana,sans-serif; font-size: 11px'>");

            if (result == null) {
                logger.warn("FileUpload service returned no result.");
                respWriter.write("FileUpload service returned no result");
            }
            else {
                String status = result.getElementsByTagName("status")
                        .item(0).getTextContent();
                respWriter.write(status);
            }

            respWriter.write("</div></body></html>");
            respWriter.flush();

            return;
        }
        catch (ConnectionException ce) {
            logger.error(ce, ce);
        }
        catch (IOException e) {
            logger.error(e, e);
        }
    }


    protected Document createFileXML(HttpServletRequest req) {
           ServletFileUpload upload = new ServletFileUpload();

        try{
            FileItemIterator iter = upload.getItemIterator(req);

            while (iter.hasNext()) {
                FileItemStream item = iter.next();
                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;
    }
}

http://dive4elements.wald.intevation.org