Clover icon

Magnolia Resources Module 2.4.2

  1. Project Clover database Fri Nov 6 2015 16:15:26 CET
  2. Package info.magnolia.module.resources.setup

File InstallResourceTask.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart6.png
64% of files have more coverage

Code metrics

10
63
13
1
211
131
19
0.3
4.85
13
1.46

Classes

Class Line # Actions
InstallResourceTask 60 63 0% 19 34
0.6046511560.5%
 

Contributing tests

This file is covered by 4 tests. .

Source view

1    /**
2    * This file Copyright (c) 2009-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.module.resources.setup;
35   
36    import info.magnolia.cms.beans.runtime.FileProperties;
37    import info.magnolia.cms.core.Content;
38    import info.magnolia.cms.core.HierarchyManager;
39    import info.magnolia.cms.core.ItemType;
40    import info.magnolia.cms.core.NodeData;
41    import info.magnolia.cms.util.ClasspathResourcesUtil;
42    import info.magnolia.cms.util.ContentUtil;
43    import info.magnolia.cms.util.NodeDataUtil;
44    import info.magnolia.module.InstallContext;
45    import info.magnolia.module.delta.AbstractTask;
46    import info.magnolia.module.delta.TaskExecutionException;
47   
48    import java.io.InputStream;
49   
50    import org.apache.commons.io.IOUtils;
51    import org.apache.commons.io.input.CountingInputStream;
52    import org.apache.commons.lang3.StringUtils;
53    import org.devlib.schmidt.imageinfo.ImageInfo;
54   
55    /**
56    * Task to load single resource to the repository.
57    *
58    * @deprecated since 2.2.1 use {@link InstallBinaryResourceTask} or {@link InstallTextResourceTask} instead.
59    */
 
60    public class InstallResourceTask extends AbstractTask {
61    private final String resource;
62    private String template;
63    private boolean binary;
64    private String version;
65    private String source;
66    private String rights;
67    private String modelClass;
68    private final boolean preserveResourceParameters;
69   
70    /**
71    * @deprecated Use {@link #InstallResourceTask(String,String,boolean)} instead.
72    */
 
73  0 toggle public InstallResourceTask(String resource, String template) {
74  0 this(resource, template, false, false);
75    }
76   
 
77  3 toggle public InstallResourceTask(String resource, String template, boolean binary) {
78  3 this(resource, template, binary, null, null, null, false);
79    }
80   
 
81  0 toggle public InstallResourceTask(String resource, String template, boolean binary, boolean preserveResourceParameters) {
82  0 this(resource, template, binary, null, null, null, preserveResourceParameters);
83    }
84   
85    /**
86    * @deprecated Use {@link #InstallResourceTask(String,String,boolean,String)} instead.
87    */
 
88  0 toggle public InstallResourceTask(String resource, String template, String modelClass) {
89  0 this(resource, template, false, modelClass);
90    }
91   
 
92  0 toggle public InstallResourceTask(String resource, String template, boolean binary, String modelClass) {
93  0 this(resource, template, binary, null, null, null, modelClass);
94    }
95   
 
96  1 toggle public InstallResourceTask(String resource, String template, boolean binary, String modelClass, boolean preserveResourceParameters) {
97  1 this(resource, template, binary, null, null, null, modelClass, preserveResourceParameters);
98    }
99   
100    /**
101    * @deprecated Use {@link #InstallResourceTask(String,String,boolean,String,String,String)} instead.
102    */
 
103  0 toggle public InstallResourceTask(String resource, String template, String version, String source, String rights) {
104  0 this(resource, template, false, version, source, rights);
105    }
106   
 
107  0 toggle public InstallResourceTask(String resource, String template, boolean binary, String version, String source, String rights) {
108  0 this(resource, template, binary, version, source, rights, null);
109    }
110   
 
111  3 toggle public InstallResourceTask(String resource, String template, boolean binary, String version, String source, String rights, boolean preserveResourceParameters) {
112  3 this(resource, template, binary, version, source, rights, null, preserveResourceParameters);
113    }
114   
115    /**
116    * @deprecated Use {@link #InstallResourceTask(String, String, boolean, String, String, String, String)} instead.
117    */
 
118  0 toggle public InstallResourceTask(String resource, String template, String version, String source, String rights, String modelClass) {
119  0 this(resource, template, false, version, source, rights);
120    }
121   
 
122  0 toggle public InstallResourceTask(String resource, String template, boolean binary, String version, String source, String rights, String modelClass) {
123  0 this(resource, template, binary, version, source, rights, modelClass, false);
124    }
125   
 
126  4 toggle public InstallResourceTask(String resource, String template, boolean binary, String version, String source, String rights, String modelClass, boolean preserveResourceParameters) {
127  4 super("Install resource", "Copies the " + resource + " file to the resources workspace.");
128  4 this.resource = resource;
129  4 this.template = template;
130  4 this.binary = binary;
131  4 this.version = StringUtils.defaultString(version);
132  4 this.source = StringUtils.defaultString(source);
133  4 this.rights = StringUtils.defaultString(rights);
134  4 this.modelClass = StringUtils.defaultString(modelClass);
135  4 this.preserveResourceParameters = preserveResourceParameters;
136    }
137   
 
138  4 toggle @Override
139    public void execute(InstallContext installContext) throws TaskExecutionException {
140   
141  4 if (ClasspathResourcesUtil.getResource(resource) == null) {
142  1 throw new TaskExecutionException("Can't find resource [" + resource + "] to install.");
143    }
144   
145  3 try {
146  3 String name = StringUtils.substringAfterLast(resource, "/");
147  3 name = StringUtils.substringBeforeLast(name, ".");
148  3 String extension = StringUtils.substringAfterLast(resource, ".");
149  3 String path = StringUtils.substringBeforeLast(resource, "/");
150   
151  3 HierarchyManager hm = installContext.getHierarchyManager("resources");
152  3 Content parent = ContentUtil.createPath(hm, path, ItemType.FOLDER);
153  3 Content node = ContentUtil.getOrCreateContent(parent, name, ItemType.CONTENT);
154   
155  3 if (this.preserveResourceParameters) {
156  1 this.template = node.getMetaData().getTemplate();
157  1 this.version = NodeDataUtil.getString(node, "version", this.version);
158  1 this.source = NodeDataUtil.getString(node, "source", this.source);
159  1 this.rights = NodeDataUtil.getString(node, "rights", this.rights);
160  1 this.modelClass = NodeDataUtil.getString(node, "modelClass", this.modelClass);
161  1 this.binary = NodeDataUtil.getString(node, "binary", null) == null ? false : true;
162    }
163  3 node.getMetaData().setTemplate(template);
164   
165    // set meta information
166  3 NodeDataUtil.getOrCreate(node, "extension").setValue(extension);
167  3 NodeDataUtil.getOrCreate(node, "version").setValue(version);
168  3 NodeDataUtil.getOrCreate(node, "source").setValue(source);
169  3 NodeDataUtil.getOrCreate(node, "rights").setValue(rights);
170  3 NodeDataUtil.getOrCreate(node, "modelClass").setValue(modelClass);
171   
172    // write content (binary or text)
173  3 InputStream stream = ClasspathResourcesUtil.getStream(resource);
174   
175  3 try{
176  3 if (binary) {
177  0 CountingInputStream countingStream = new CountingInputStream(stream);
178  0 NodeData binaryNode = node.setNodeData("binary", countingStream);
179  0 binaryNode.setAttribute(FileProperties.PROPERTY_FILENAME, name);
180  0 binaryNode.setAttribute(FileProperties.PROPERTY_EXTENSION, extension);
181  0 binaryNode.setAttribute(FileProperties.PROPERTY_SIZE, Long.toString(countingStream.getByteCount()));
182   
183    // we have to reread the stream to get image information
184  0 InputStream imageStream = ClasspathResourcesUtil.getStream(resource);
185  0 try{
186  0 ImageInfo ii = new ImageInfo();
187  0 ii.setInput(imageStream);
188  0 if (ii.check()) {
189  0 binaryNode.setAttribute(FileProperties.PROPERTY_WIDTH, Long.toString(ii.getWidth()));
190  0 binaryNode.setAttribute(FileProperties.PROPERTY_HEIGHT, Long.toString(ii.getHeight()));
191    }
192    }
193    finally{
194  0 IOUtils.closeQuietly(imageStream);
195    }
196    }
197    else {
198  3 String content = IOUtils.toString(stream, "UTF-8");
199  3 NodeDataUtil.getOrCreate(node, "text").setValue(content);
200    }
201    }
202    finally{
203  3 IOUtils.closeQuietly(stream);
204    }
205    } catch (Exception e) {
206  0 throw new TaskExecutionException("Can't install resource [" + resource + "]", e);
207    }
208   
209    }
210   
211    }