Clover icon

Google Sitemap Module 2.4.3

  1. Project Clover database Thu May 11 2017 16:52:32 CEST
  2. Package info.magnolia.module.googlesitemap.app.actions

File ExportSiteMapToXMLAction.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart0.png
68% of files have more coverage

Code metrics

2
32
7
2
156
94
13
0.41
4.57
3.5
1.86

Classes

Class Line # Actions
ExportSiteMapToXMLAction 66 27 0% 9 32
0.00%
ExportSiteMapToXMLAction.DeleteOnCloseFileInputStream 139 5 0% 4 9
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /**
2    * This file Copyright (c) 2013-2017 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.module.googlesitemap.app.actions;
35   
36    import info.magnolia.cms.core.Path;
37    import info.magnolia.i18nsystem.SimpleTranslator;
38    import info.magnolia.module.googlesitemap.service.SiteMapXMLUtil;
39    import info.magnolia.ui.api.action.AbstractAction;
40    import info.magnolia.ui.api.action.ActionExecutionException;
41    import info.magnolia.ui.api.context.UiContext;
42    import info.magnolia.ui.vaadin.integration.jcr.JcrNodeAdapter;
43    import info.magnolia.ui.vaadin.overlay.MessageStyleTypeEnum;
44   
45    import java.io.File;
46    import java.io.FileInputStream;
47    import java.io.FileNotFoundException;
48    import java.io.FileOutputStream;
49    import java.io.IOException;
50    import java.io.InputStream;
51   
52    import javax.inject.Inject;
53    import javax.jcr.RepositoryException;
54    import javax.xml.bind.JAXBException;
55   
56    import org.slf4j.Logger;
57    import org.slf4j.LoggerFactory;
58   
59    import com.vaadin.server.Page;
60    import com.vaadin.server.StreamResource;
61   
62    /**
63    * Exports sitemap to xml via JAXB using according to <a href = "http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"></a>.
64    * @param <T> exact definition type.
65    */
 
66    public class ExportSiteMapToXMLAction<T extends ExportSiteMapToXMLActionDefinition> extends AbstractAction<T> {
67   
68    public static final String XML_EXTENSION = ".xml";
69   
70    private Logger log = LoggerFactory.getLogger(getClass());
71   
72    private FileOutputStream fileOutputStream;
73   
74    private SimpleTranslator translator;
75   
76    private JcrNodeAdapter siteMapNode;
77   
78    private SiteMapXMLUtil xmlUtil;
79   
80    private UiContext uiContext;
81   
82    private File fileOutput;
83   
 
84  0 toggle @Inject
85    public ExportSiteMapToXMLAction(T definition, JcrNodeAdapter siteMapNode, UiContext uiContext, SimpleTranslator translator, SiteMapXMLUtil xmlUtil) {
86  0 super(definition);
87  0 this.siteMapNode = siteMapNode;
88  0 this.uiContext = uiContext;
89  0 this.translator = translator;
90  0 this.xmlUtil = xmlUtil;
91    }
92   
 
93  0 toggle @Override
94    public void execute() throws ActionExecutionException {
95  0 try {
96  0 String sw = xmlUtil.generateSiteMapXML(siteMapNode.getJcrItem());
97  0 fileOutput = File.createTempFile(siteMapNode.getItemId().toString(), XML_EXTENSION, Path.getTempDirectory());
98  0 fileOutputStream = new FileOutputStream(fileOutput);
99  0 fileOutputStream.write(sw.toString().getBytes());
100  0 openFileInBlankWindow(siteMapNode.getNodeName() + XML_EXTENSION, "application/octet-stream");
101    } catch (RepositoryException e) {
102  0 onError(e);
103    } catch (JAXBException e) {
104  0 onError(e);
105    } catch (IOException e) {
106  0 onError(e);
107    }
108    }
109   
 
110  0 toggle protected void onError(Exception e) {
111  0 log.error("Error occurred during site map export", e);
112  0 String message = translator.translate("siteMaps.export.failed");
113  0 uiContext.openNotification(MessageStyleTypeEnum.ERROR, true, message + ":" + e.getMessage());
114    }
115   
 
116  0 toggle protected void openFileInBlankWindow(String fileName, String mimeType) {
117  0 StreamResource.StreamSource source = new StreamResource.StreamSource() {
 
118  0 toggle @Override
119    public InputStream getStream() {
120  0 try {
121  0 return new DeleteOnCloseFileInputStream(fileOutput);
122    } catch (IOException e) {
123  0 return null;
124    }
125    }
126    };
127  0 StreamResource resource = new StreamResource(source, fileName);
128  0 resource.setCacheTime(-1);
129  0 resource.getStream().setParameter("Content-Disposition", "attachment; filename=" + fileName + "\"");
130  0 resource.setMIMEType(mimeType);
131  0 resource.setCacheTime(0);
132  0 Page.getCurrent().open(resource, "", true);
133    }
134   
135    /**
136    * Implementation of {@link java.io.FileInputStream} that ensure that the {@link File} <br>
137    * used to construct this class is deleted on close() call.
138    */
 
139    private class DeleteOnCloseFileInputStream extends FileInputStream {
140    private File file;
141    private final Logger log = LoggerFactory.getLogger(DeleteOnCloseFileInputStream.class);
142   
 
143  0 toggle public DeleteOnCloseFileInputStream(File file) throws FileNotFoundException {
144  0 super(file);
145  0 this.file = file;
146    }
147   
 
148  0 toggle @Override
149    public void close() throws IOException {
150  0 super.close();
151  0 if (file.exists() && !file.delete()) {
152  0 log.warn("Could not delete temporary export file {}", file.getAbsolutePath());
153    }
154    }
155    }
156    }