All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.efaps.ui.servlet.FileServlet Maven / Gradle / Ivy

Go to download

eFaps WebApp provides a web interface as the User Interface for eFaps which can be easily expanded and altered.

There is a newer version: 3.2.0
Show newest version
/*
 * Copyright 2003 - 2012 The eFaps Team
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Revision:        $Rev: 7505 $
 * Last Changed:    $Date: 2012-05-11 13:14:52 -0500 (Fri, 11 May 2012) $
 * Last Changed By: $Author: [email protected] $
 */

package org.efaps.ui.servlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.NumberFormat;

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

import org.apache.commons.io.IOUtils;
import org.efaps.admin.user.Person;
import org.efaps.db.Context;
import org.efaps.util.EFapsException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Servlet used to give access to files for a user.
 *
 * @author The eFaps Team
 * @version $Id: FileServlet.java 7505 2012-05-11 18:14:52Z [email protected] $
 */
public class FileServlet
    extends HttpServlet
{

    /**
     * Name of the temp folder.
     */
    public static final String TMPFOLDERNAME = "eFapsUserDepTemp";

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    /**
     * Logging instance used in this class.
     */
    private static final Logger LOG = LoggerFactory.getLogger(ImageServlet.class);

    /**
     * Search for the requested file in the folder corresponding to the user of the context.
     *
     * @param _req request variable
     * @param _res response variable
     * @throws ServletException on error
     */
    @Override
    protected void doGet(final HttpServletRequest _req,
                         final HttpServletResponse _resp)
        throws ServletException, IOException
    {
        String fileName = _req.getRequestURI();
        fileName = fileName.substring(fileName.lastIndexOf('/') + 1);
        try {
            final Person pers = Context.getThreadContext().getPerson();
            if (pers != null) {
                final File file = getFile(pers.getId(), fileName);
                if (file != null && file.exists()) {
                    _resp.setContentType(getServletContext().getMimeType(file.getName()));
                    _resp.setContentLength((int) file.length());
                    _resp.setDateHeader("Last-Modified", System.currentTimeMillis());
                    _resp.setDateHeader("Expires", System.currentTimeMillis());
                    _resp.addHeader("Content-Disposition", "inline; filename=\"" + file.getName() + "\"");
                    _resp.setHeader("Cache-Control", "max-age=10");
                    final FileInputStream input = new FileInputStream(file);
                    IOUtils.copy(input, _resp.getOutputStream());
                }
            }
        } catch (final EFapsException e) {
            FileServlet.LOG.error("EFapsException", e);
            throw new ServletException(e);
        }
    }

    private File getFile(final Long _personId,
                         final String _fileName)
        throws IOException
    {
        final File temp = File.createTempFile("eFaps", ".tmp");
        final File tmpfld = temp.getParentFile();
        temp.delete();
        final File storeFolder = new File(tmpfld, FileServlet.TMPFOLDERNAME);
        final NumberFormat formater = NumberFormat.getInstance();
        formater.setMinimumIntegerDigits(8);
        formater.setGroupingUsed(false);
        final File userFolder = new File(storeFolder, formater.format(_personId));
        if (!userFolder.exists()) {
            userFolder.mkdirs();
        }
        return new File(userFolder, _fileName);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy