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

org.efaps.ui.wicket.components.menu.FileRequestTarget 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 - 2011 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: 6474 $
 * Last Changed:    $Date: 2011-05-05 19:34:24 -0500 (Thu, 05 May 2011) $
 * Last Changed By: $Author: [email protected] $
 */

package org.efaps.ui.wicket.components.menu;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.wicket.IRequestTarget;
import org.apache.wicket.RequestCycle;
import org.apache.wicket.protocol.http.WebResponse;
import org.apache.wicket.util.io.Streams;
import org.efaps.ui.wicket.util.MimeTypes;

/**
 * @author The eFaps Team
 * @version $Id: FileRequestTarget.java 6474 2011-05-06 00:34:24Z [email protected] $
 */
public class FileRequestTarget
    implements IRequestTarget
{

    /**
     * Store the file.
     */
    private final File file;

    /**
     * Store the mime type.
     */
    private final MimeTypes mime;

    /**
     * Constructor.
     *
     * @param _file File for this request
     */
    public FileRequestTarget(final File _file)
    {
        this.file = _file;
        this.mime = evaluateMimeType();
    }

    /**
     * Method to evaluate the mime type.
     *
     * @return MimeType
     */
    private MimeTypes evaluateMimeType()
    {
        final String name = this.file.getName();
        final String end = name.substring(name.lastIndexOf(".") + 1);
        final MimeTypes mtype = MimeTypes.getMimeTypeByEnding(end);
        return mtype == null ? MimeTypes.TXT : mtype;
    }

    /**
     * Not needed here.
     *
     * @param _arg RequestCycle
     */
    public void detach(final RequestCycle _arg)
    {
        // not needed here
    }

    /**
     * Respond to the request.
     *
     * @param _requestCycle RequestCycle
     */
    public void respond(final RequestCycle _requestCycle)
    {
        try {
            final InputStream input = new FileInputStream(this.file);
            final WebResponse response = (WebResponse) _requestCycle.getResponse();
            response.setAttachmentHeader(this.file.getName());
            response.setContentType(this.mime.getContentType());
            response.setAjax(false);
            Streams.copy(input, response.getOutputStream());
        } catch (final IOException e) {
            throw new RuntimeException(e);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy