View Javadoc
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      public InstallResourceTask(String resource, String template) {
74          this(resource, template, false, false);
75      }
76  
77      public InstallResourceTask(String resource, String template, boolean binary) {
78          this(resource, template, binary, null, null, null, false);
79      }
80  
81      public InstallResourceTask(String resource, String template, boolean binary, boolean preserveResourceParameters) {
82          this(resource, template, binary, null, null, null, preserveResourceParameters);
83      }
84  
85      /**
86       * @deprecated Use {@link #InstallResourceTask(String,String,boolean,String)} instead.
87       */
88      public InstallResourceTask(String resource, String template, String modelClass) {
89          this(resource, template, false, modelClass);
90      }
91  
92      public InstallResourceTask(String resource, String template, boolean binary, String modelClass) {
93          this(resource, template, binary, null, null, null, modelClass);
94      }
95  
96      public InstallResourceTask(String resource, String template, boolean binary, String modelClass, boolean preserveResourceParameters) {
97          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     public InstallResourceTask(String resource, String template, String version, String source, String rights) {
104         this(resource, template, false, version, source, rights);
105     }
106 
107     public InstallResourceTask(String resource, String template, boolean binary, String version, String source, String rights) {
108         this(resource, template, binary, version, source, rights, null);
109     }
110 
111     public InstallResourceTask(String resource, String template, boolean binary, String version, String source, String rights, boolean preserveResourceParameters) {
112         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     public InstallResourceTask(String resource, String template, String version, String source, String rights, String modelClass) {
119         this(resource, template, false, version, source, rights);
120     }
121 
122     public InstallResourceTask(String resource, String template, boolean binary, String version, String source, String rights, String modelClass) {
123         this(resource, template, binary, version, source, rights, modelClass, false);
124     }
125 
126     public InstallResourceTask(String resource, String template, boolean binary, String version, String source, String rights, String modelClass, boolean preserveResourceParameters) {
127         super("Install resource", "Copies the " + resource + " file to the resources workspace.");
128         this.resource = resource;
129         this.template = template;
130         this.binary = binary;
131         this.version = StringUtils.defaultString(version);
132         this.source = StringUtils.defaultString(source);
133         this.rights = StringUtils.defaultString(rights);
134         this.modelClass = StringUtils.defaultString(modelClass);
135         this.preserveResourceParameters = preserveResourceParameters;
136     }
137 
138     @Override
139     public void execute(InstallContext installContext) throws TaskExecutionException {
140 
141         if (ClasspathResourcesUtil.getResource(resource) == null) {
142             throw new TaskExecutionException("Can't find resource [" + resource + "] to install.");
143         }
144 
145         try {
146             String name = StringUtils.substringAfterLast(resource, "/");
147             name = StringUtils.substringBeforeLast(name, ".");
148             String extension = StringUtils.substringAfterLast(resource, ".");
149             String path = StringUtils.substringBeforeLast(resource, "/");
150 
151             HierarchyManager hm = installContext.getHierarchyManager("resources");
152             Content parent = ContentUtil.createPath(hm, path, ItemType.FOLDER);
153             Content node = ContentUtil.getOrCreateContent(parent, name, ItemType.CONTENT);
154 
155             if (this.preserveResourceParameters) {
156                 this.template = node.getMetaData().getTemplate();
157                 this.version = NodeDataUtil.getString(node, "version", this.version);
158                 this.source = NodeDataUtil.getString(node, "source", this.source);
159                 this.rights = NodeDataUtil.getString(node, "rights", this.rights);
160                 this.modelClass = NodeDataUtil.getString(node, "modelClass", this.modelClass);
161                 this.binary = NodeDataUtil.getString(node, "binary", null) == null ? false : true;
162             }
163             node.getMetaData().setTemplate(template);
164 
165             // set meta information
166             NodeDataUtil.getOrCreate(node, "extension").setValue(extension);
167             NodeDataUtil.getOrCreate(node, "version").setValue(version);
168             NodeDataUtil.getOrCreate(node, "source").setValue(source);
169             NodeDataUtil.getOrCreate(node, "rights").setValue(rights);
170             NodeDataUtil.getOrCreate(node, "modelClass").setValue(modelClass);
171 
172             // write content (binary or text)
173             InputStream stream = ClasspathResourcesUtil.getStream(resource);
174 
175             try{
176                 if (binary) {
177                     CountingInputStream countingStream = new CountingInputStream(stream);
178                     NodeData binaryNode = node.setNodeData("binary", countingStream);
179                     binaryNode.setAttribute(FileProperties.PROPERTY_FILENAME, name);
180                     binaryNode.setAttribute(FileProperties.PROPERTY_EXTENSION, extension);
181                     binaryNode.setAttribute(FileProperties.PROPERTY_SIZE, Long.toString(countingStream.getByteCount()));
182 
183                     // we have to reread the stream to get image information
184                     InputStream imageStream = ClasspathResourcesUtil.getStream(resource);
185                     try{
186                         ImageInfo ii = new ImageInfo();
187                         ii.setInput(imageStream);
188                         if (ii.check()) {
189                             binaryNode.setAttribute(FileProperties.PROPERTY_WIDTH, Long.toString(ii.getWidth()));
190                             binaryNode.setAttribute(FileProperties.PROPERTY_HEIGHT, Long.toString(ii.getHeight()));
191                         }
192                     }
193                     finally{
194                         IOUtils.closeQuietly(imageStream);
195                     }
196                 }
197                 else {
198                     String content = IOUtils.toString(stream, "UTF-8");
199                     NodeDataUtil.getOrCreate(node, "text").setValue(content);
200                 }
201             }
202             finally{
203                 IOUtils.closeQuietly(stream);
204             }
205         } catch (Exception e) {
206             throw new TaskExecutionException("Can't install resource [" + resource + "]", e);
207         }
208 
209     }
210 
211 }