View Javadoc
1   /**
2    * Copyright 2005-2016 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.demo.uif.controller;
17  
18  import org.kuali.rice.krad.file.FileMeta;
19  import org.kuali.rice.krad.file.FileMetaBlob;
20  import org.kuali.rice.krad.web.form.UifFormBase;
21  import org.kuali.rice.krad.web.service.impl.FileControllerServiceImpl;
22  import org.springframework.util.FileCopyUtils;
23  
24  import javax.servlet.http.HttpServletResponse;
25  import java.io.InputStream;
26  import java.util.List;
27  
28  /**
29   * Demo controller service for sending back file contents.
30   *
31   * @author Kuali Rice Team (rice.collab@kuali.org)
32   */
33  public class DemoFileControllerServiceImpl extends FileControllerServiceImpl {
34  
35      /**
36       * {@inheritDoc}
37       */
38      @Override
39      protected void sendFileFromLineResponse(UifFormBase form, HttpServletResponse response, List<FileMeta> collection,
40              FileMeta fileLine) {
41          if (!(fileLine instanceof FileMetaBlob)) {
42              return;
43          }
44  
45          try {
46              InputStream is = ((FileMetaBlob) fileLine).getBlob().getBinaryStream();
47              response.setContentType(fileLine.getContentType());
48              response.setHeader("Content-Disposition", "attachment; filename=" + fileLine.getName());
49  
50              // copy it to response's OutputStream
51              FileCopyUtils.copy(is, response.getOutputStream());
52  
53              response.flushBuffer();
54          } catch (Exception e) {
55              throw new RuntimeException("Unable to get file contents", e);
56          }
57      }
58  }