org.geowe.server.upload.FileUploadZipServlet.java Source code

Java tutorial

Introduction

Here is the source code for org.geowe.server.upload.FileUploadZipServlet.java

Source

/*
 * #%L
 * GeoWE Project
 * %%
 * Copyright (C) 2015 - 2016 GeoWE.org
 * %%
 * This file is part of GeoWE.org.
 * 
 * GeoWE is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * GeoWE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with GeoWE.  If not, see <http://www.gnu.org/licenses/>.
 * #L%
 */
package org.geowe.server.upload;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.http.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FileUploadZipServlet extends HttpServlet {

    private static final long serialVersionUID = 1296384085403737411L;

    private static final long MAX_FILE_SIZE = 1024 * 1024 * 2; // 2MB
    private static final Logger LOG = LoggerFactory.getLogger(FileUploadZipServlet.class.getName());
    private static final String PRJ_FILE_NAME = "geowe-project.prj";
    private static final String EMPTY = "empty";
    private static final String BAD_FORMAT = "bad format";

    @Override
    public void doPost(final HttpServletRequest request, final HttpServletResponse response)
            throws ServletException, IOException {
        final ServletFileUpload upload = new ServletFileUpload();
        response.setContentType("text/plain; charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        request.setCharacterEncoding("UTF-8");
        upload.setFileSizeMax(MAX_FILE_SIZE);
        upload.setSizeMax(MAX_FILE_SIZE);

        try {
            final FileItemIterator iter = upload.getItemIterator(request);

            while (iter.hasNext()) {
                final FileItemStream item = iter.next();

                ZipFile zipFile = createZipFile(item);

                final String content = readZipFile(zipFile);

                if (EMPTY.equals(content)) {
                    response.setStatus(HttpStatus.SC_NO_CONTENT);
                    response.getWriter().printf(HttpStatus.SC_NO_CONTENT + ":" + content);
                } else if (BAD_FORMAT.equals(content)) {
                    response.setStatus(HttpStatus.SC_NOT_ACCEPTABLE);
                    response.getWriter().printf(HttpStatus.SC_NOT_ACCEPTABLE + ":" + content);
                } else {
                    response.setStatus(HttpStatus.SC_OK);
                    response.getWriter().printf(content);
                }

            }
        } catch (SizeLimitExceededException e) {
            response.setStatus(HttpStatus.SC_REQUEST_TOO_LONG);
            response.getWriter().printf(HttpStatus.SC_REQUEST_TOO_LONG + ":" + e.getMessage());
            LOG.error(e.getMessage());
        } catch (Exception e) {
            response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            response.getWriter().printf(HttpStatus.SC_INTERNAL_SERVER_ERROR + ": ups! something went wrong.");
            LOG.error(e.getMessage());
        }
    }

    private ZipFile createZipFile(FileItemStream item) {
        ZipFile zipFile = null;
        try {
            File f = new File("zipFile");
            InputStream inputStream = item.openStream();
            OutputStream outputStream = new FileOutputStream(f);
            int len;
            byte[] buffer = new byte[1000000];
            while ((len = inputStream.read(buffer, 0, buffer.length)) != -1) {
                outputStream.write(buffer, 0, len);
            }
            outputStream.close();
            inputStream.close();

            zipFile = new ZipFile(f);

        } catch (Exception e) {
            LOG.error(e.getMessage());
        }

        return zipFile;
    }

    private String readZipFile(ZipFile zipFile) {
        String content = EMPTY;

        try {

            Enumeration<?> enu = zipFile.entries();
            if (enu.hasMoreElements()) {
                ZipEntry zipEntry = (ZipEntry) enu.nextElement();
                if (zipEntry.isDirectory()) {
                    content = BAD_FORMAT;
                } else if (!(zipEntry.getName().equals(PRJ_FILE_NAME))) {
                    content = BAD_FORMAT;
                } else {
                    InputStream is = zipFile.getInputStream(zipEntry);
                    content = new java.util.Scanner(is).useDelimiter("\\A").next();
                }

                // String name = zipEntry.getName();
                // long size = zipEntry.getSize();
                // long compressedSize = zipEntry.getCompressedSize();
                // LOG.info("name: " + name + " | size: " + size
                // + " | compressed size: " + compressedSize);

            }
            zipFile.close();

        } catch (IOException e) {
            LOG.error("Se produce error en ZipFile: " + e.getMessage());
            e.printStackTrace();
        }

        return content;
    }
}