View Javadoc
1   /**
2    * This file Copyright (c) 2014-2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.ui.framework.action;
35  
36  import com.vaadin.util.EncodeUtil;
37  import info.magnolia.ui.api.action.AbstractAction;
38  import info.magnolia.ui.api.action.ActionExecutionException;
39  import info.magnolia.ui.vaadin.integration.jcr.JcrItemAdapter;
40  import info.magnolia.ui.vaadin.integration.jcr.JcrNodeAdapter;
41  import info.magnolia.ui.vaadin.server.DownloadStreamResource;
42  
43  import java.io.InputStream;
44  
45  import javax.inject.Inject;
46  import javax.jcr.Binary;
47  import javax.jcr.Node;
48  import javax.jcr.RepositoryException;
49  
50  import com.vaadin.server.Page;
51  import com.vaadin.server.StreamResource;
52  
53  /**
54   * Action for downloading a binary.
55   *
56   * @param <D> action definition
57   */
58  public class DownloadBinaryAction<D extends DownloadBinaryActionDefinition> extends AbstractAction<D> {
59  
60      private static final String CONTENT_TYPE = "application/octet-stream";
61      private final DownloadBinaryActionDefinition definition;
62      private final JcrItemAdapter item;
63  
64      @Inject
65      public DownloadBinaryAction(D definition, JcrItemAdapter item) {
66          super(definition);
67          this.definition = definition;
68          this.item = item;
69      }
70  
71      @Override
72      public void execute() throws ActionExecutionException {
73          if (item instanceof JcrNodeAdapter) {
74              final Node node = (Node) item.getJcrItem();
75              final InputStream inputStream;
76              Node binaryNode;
77              String fileName;
78              StreamResource streamResource;
79              try {
80                  binaryNode = getBinaryNode(node);
81                  fileName = getFileName(binaryNode);
82                  inputStream = getInputStream(binaryNode);
83                  streamResource = getStreamResource(inputStream, fileName);
84  
85                  Page.getCurrent().open(streamResource, null, false);
86              } catch (RepositoryException e) {
87                  throw new ActionExecutionException(String.format("Error getting binary data from node [%s] to download.", node), e);
88              }
89          }
90      }
91  
92      /**
93       * Returns a downloadable {@link DownloadStreamResource} created from the supplied {@link InputStream}.
94       *
95       * @see com.vaadin.server.DownloadStream#DEFAULT_CACHETIME
96       * @see StreamResource
97       */
98      protected DownloadStreamResource getStreamResource(final InputStream inputStream, String fileName) {
99          final DownloadStreamResource resource = new DownloadStreamResource(new StreamResource.StreamSource() {
100             @Override
101             public InputStream getStream() {
102                 return inputStream;
103             }
104         }, fileName);
105         // Accessing the DownloadStream via getStream() will set its cacheTime to whatever is set in the parent
106         // StreamResource. By default it is set to 1000 * 60 * 60 * 24, thus we have to override it beforehand.
107         // A negative value or zero will disable caching of this stream.
108         resource.setCacheTime(-1);
109         String encodedFilename = EncodeUtil.rfc5987Encode(fileName);
110         resource.getStream().setParameter("Content-Disposition", String.format("attachment;filename=\"%s\"; filename*=utf-8''%s",
111                 encodedFilename, encodedFilename));
112         resource.setMIMEType(CONTENT_TYPE);
113         return resource;
114     }
115 
116     protected InputStream getInputStream(Node binaryNode) throws RepositoryException {
117         Binary binary = binaryNode.getProperty(definition.getDataProperty()).getBinary();
118         return binary.getStream();
119     }
120 
121     protected String getFileName(Node binaryNode) throws RepositoryException {
122         String fileName = binaryNode.getProperty(definition.getFileNameProperty()).getString();
123         String extension = "." + binaryNode.getProperty(definition.getExtensionProperty()).getString();
124         return fileName.endsWith(extension) ? fileName : fileName + extension;
125     }
126 
127     protected Node getBinaryNode(Node node) throws RepositoryException {
128         return node.getNode(definition.getBinaryNodeName());
129     }
130 
131 }