com.gantzgulch.sharing.controller.SharedFileDownloadController.java Source code

Java tutorial

Introduction

Here is the source code for com.gantzgulch.sharing.controller.SharedFileDownloadController.java

Source

/*
 * Copyright 2011 GantzGulch, Inc.
 * 
 * This file is part of GantzFileSharing.
 * 
 * GantzFileSharing 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.
 * 
 * GantzFileSharing 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 GantzFileSharing.  If not, see <http://www.gnu.org/licenses/>. 
 */
package com.gantzgulch.sharing.controller;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

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

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.gantzgulch.sharing.domain.SharedFile;
import com.gantzgulch.sharing.service.SharedFileManager;
import com.gantzgulch.sharing.service.UserManager;

@Controller
@RequestMapping("/secure/fileDownload.htm")
public class SharedFileDownloadController {

    private final SharedFileManager sharedFileManager;
    private final UserManager userManager;

    @Autowired
    public SharedFileDownloadController(SharedFileManager sharedFileManager, UserManager userManager) {
        this.sharedFileManager = sharedFileManager;
        this.userManager = userManager;
    }

    @RequestMapping(method = RequestMethod.GET)
    public void showUploadForm(@RequestParam(required = true) String id, final HttpServletResponse response,
            final HttpServletRequest request) {

        SharedFile sharedFile = sharedFileManager.findById(id);

        InputStream is = null;
        OutputStream os = null;

        try {
            is = sharedFileManager.download(id, userManager.getCurrentUser());
            os = response.getOutputStream();

            response.setHeader("Content-Disposition", "attachment; filename=\"" + sharedFile.getFilename() + "\"");
            response.setHeader("Content-Type", "application/force-download");

            if (sharedFile.getSize() < Integer.MAX_VALUE) {
                response.setContentLength((int) sharedFile.getSize());
            }

            IOUtils.copyLarge(is, os);

            os.flush();
        } catch (IOException ioe) {
            throw new RuntimeException(ioe);
        } finally {
            IOUtils.closeQuietly(is);
        }
    }

}