001/**
002 * Copyright 2005-2018 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krad.demo.uif.controller;
017
018import org.kuali.rice.krad.file.FileMeta;
019import org.kuali.rice.krad.file.FileMetaBlob;
020import org.kuali.rice.krad.web.form.UifFormBase;
021import org.kuali.rice.krad.web.service.impl.FileControllerServiceImpl;
022import org.springframework.util.FileCopyUtils;
023
024import javax.servlet.http.HttpServletResponse;
025import java.io.InputStream;
026import java.util.List;
027
028/**
029 * Demo controller service for sending back file contents.
030 *
031 * @author Kuali Rice Team (rice.collab@kuali.org)
032 */
033public class DemoFileControllerServiceImpl extends FileControllerServiceImpl {
034
035    /**
036     * {@inheritDoc}
037     */
038    @Override
039    protected void sendFileFromLineResponse(UifFormBase form, HttpServletResponse response, List<FileMeta> collection,
040            FileMeta fileLine) {
041        if (!(fileLine instanceof FileMetaBlob)) {
042            return;
043        }
044
045        try {
046            InputStream is = ((FileMetaBlob) fileLine).getBlob().getBinaryStream();
047            response.setContentType(fileLine.getContentType());
048            response.setHeader("Content-Disposition", "attachment; filename=" + fileLine.getName());
049
050            // copy it to response's OutputStream
051            FileCopyUtils.copy(is, response.getOutputStream());
052
053            response.flushBuffer();
054        } catch (Exception e) {
055            throw new RuntimeException("Unable to get file contents", e);
056        }
057    }
058}