com.seajas.search.profiler.controller.DownloadController.java Source code

Java tutorial

Introduction

Here is the source code for com.seajas.search.profiler.controller.DownloadController.java

Source

/**
 * Copyright (C) 2013 Seajas, the Netherlands.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3, as
 * published by the Free Software Foundation.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.seajas.search.profiler.controller;

import com.seajas.search.profiler.service.repository.RepositoryContent;
import com.seajas.search.profiler.service.repository.RepositoryService;
import java.io.IOException;
import org.apache.commons.io.IOUtils;
import org.apache.cxf.common.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;

/**
 * Download controller.
 * 
 * @author Jasper van Veghel <jasper@seajas.com>
 */
@RequestMapping("/download")
@Controller
public class DownloadController {
    /**
     * Constants.
     */
    private static final String PARAM_DOCUMENT = "document";

    /**
     * Repository service.
     */
    @Autowired
    private RepositoryService repositoryService;

    /**
     * Handle the request.
     * 
     * @param model
     * @param request
     * @param response
     * @return ModelAndView
     * @throws IOException
     */
    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView handleRequest(final ModelMap model, final WebRequest request,
            final HttpServletResponse response) throws IOException {
        String documentPath = request.getParameter(PARAM_DOCUMENT);

        if (!StringUtils.isEmpty(documentPath)) {
            RepositoryContent content = repositoryService.getResource(documentPath);

            if (content != null && content.getInputStream() != null) {
                try {
                    String filename = documentPath.lastIndexOf("/") != -1
                            ? documentPath.substring(documentPath.lastIndexOf('/') + 1)
                            : documentPath;

                    // Add extensions for some common MIME types

                    if (content.getMimeType() != null) {
                        response.addHeader("Content-Type", content.getMimeType());

                        if (content.getMimeType().equals("text/html")
                                || content.getMimeType().equals("application/xhtml+xml"))
                            filename += ".html";
                        else if (content.getMimeType().equals("application/pdf"))
                            filename += ".pdf";
                        else if (content.getMimeType().equals("application/msword"))
                            filename += ".doc";
                        else if (content.getMimeType().equals("application/vnd.ms-excel"))
                            filename += ".xls";
                        else if (content.getMimeType().equals("application/vnd.ms-powerpoint"))
                            filename += ".ppt";
                        else if (content.getMimeType()
                                .equals("application/vnd.openxmlformats-officedocument.wordprocessingml.document"))
                            filename += ".docx";
                        else if (content.getMimeType()
                                .equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"))
                            filename += ".xlsx";
                        else if (content.getMimeType().equals(
                                "application/vnd.openxmlformats-officedocument.presentationml.presentation"))
                            filename += ".pptx";
                        else if (content.getMimeType().equals("application/vnd.oasis.opendocument.text"))
                            filename += ".odt";
                        else if (content.getMimeType().equals("application/vnd.oasis.opendocument.spreadsheet"))
                            filename += ".ods";
                        else if (content.getMimeType().equals("application/vnd.oasis.opendocument.presentation"))
                            filename += ".odp";
                    }

                    // And then add the disposition header

                    response.addHeader("Content-Disposition", "attachment; filename=" + filename);

                    // Copy the input stream content over to the servlet's output stream

                    IOUtils.copy(content.getInputStream(), response.getOutputStream());

                } finally {
                    IOUtils.closeQuietly(content.getInputStream());
                }
            } else
                response.getWriter().print("The requested document could not be retrieved for downloading.");
        } else
            response.getWriter().print("No document was specified for downloading.");

        return null;
    }
}