View Javadoc
1   /**
2    * This file Copyright (c) 2013-2015 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 info.magnolia.commands.CommandsManager;
37  import info.magnolia.commands.impl.ExportCommand;
38  import info.magnolia.i18nsystem.SimpleTranslator;
39  import info.magnolia.ui.api.action.ActionExecutionException;
40  import info.magnolia.ui.api.context.UiContext;
41  import info.magnolia.ui.framework.util.TempFileStreamResource;
42  import info.magnolia.ui.vaadin.integration.jcr.JcrItemAdapter;
43  
44  import java.io.IOException;
45  import java.util.Map;
46  
47  import javax.inject.Inject;
48  import javax.jcr.Item;
49  
50  import com.vaadin.server.Page;
51  
52  /**
53   * Action for exporting a node to XML format. Uses the export command to perform the serialization to XML then sends the
54   * result to the client browser.
55   *
56   * @see ExportActionDefinition
57   */
58  public class ExportAction extends AbstractCommandAction<ExportActionDefinition> {
59  
60      private TempFileStreamResource tempFileStreamResource;
61  
62      @Inject
63      public ExportAction(ExportActionDefinition definition, JcrItemAdapter item, CommandsManager commandsManager, UiContext uiContext, SimpleTranslator i18n) throws ActionExecutionException {
64          super(definition, item, commandsManager, uiContext, i18n);
65      }
66  
67      @Override
68      protected void onPreExecute() throws Exception {
69          tempFileStreamResource = new TempFileStreamResource();
70          tempFileStreamResource.setTempFileName(getCurrentItem().getItemId().getUuid());
71          tempFileStreamResource.setTempFileExtension("xml");
72  
73          super.onPreExecute();
74      }
75  
76      /**
77       * After command execution we push the created XML to the client browser.<br>
78       * The created data is put in the temporary file 'fileOutput' linked to 'fileOutputStream' sent to the export command.<br>
79       * This temporary file is the used to create a {@code FileInputStream} that ensure that this temporary file is removed once the <br>
80       * fileInputStream is closed by Vaadin resource component.
81       * Directs the created XML file to the user.
82       */
83      @Override
84      protected void onPostExecute() throws Exception {
85          final ExportCommand exportCommand = (ExportCommand) getCommand();
86          tempFileStreamResource.setFilename(exportCommand.getFileName());
87          tempFileStreamResource.setMIMEType(exportCommand.getMimeExtension());
88          // Opens the resource for download
89          Page.getCurrent().open(tempFileStreamResource, "", true);
90      }
91  
92      @Override
93      protected Map<String, Object> buildParams(final Item jcrItem) {
94          Map<String, Object> params = super.buildParams(jcrItem);
95          params.put(ExportCommand.EXPORT_EXTENSION, ".xml");
96          params.put(ExportCommand.EXPORT_FORMAT, Boolean.TRUE);
97          params.put(ExportCommand.EXPORT_KEEP_HISTORY, Boolean.FALSE);
98          try {
99              params.put(ExportCommand.EXPORT_OUTPUT_STREAM, tempFileStreamResource.getTempFileOutputStream());
100         } catch (IOException e) {
101             throw new IllegalStateException("Failed to bind command to temp file output stream: ", e);
102         }
103         return params;
104     }
105 }