1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
57
58
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
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
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
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
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
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
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
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 }