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 |
|
@deprecated@link@link |
59 |
|
|
|
|
| 60.5% |
Uncovered Elements: 34 (86) |
Complexity: 19 |
Complexity Density: 0.3 |
|
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@link |
72 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
73 |
0 |
public InstallResourceTask(String resource, String template) {... |
74 |
0 |
this(resource, template, false, false); |
75 |
|
} |
76 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
77 |
3 |
public InstallResourceTask(String resource, String template, boolean binary) {... |
78 |
3 |
this(resource, template, binary, null, null, null, false); |
79 |
|
} |
80 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
81 |
0 |
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@link |
87 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
88 |
0 |
public InstallResourceTask(String resource, String template, String modelClass) {... |
89 |
0 |
this(resource, template, false, modelClass); |
90 |
|
} |
91 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
92 |
0 |
public InstallResourceTask(String resource, String template, boolean binary, String modelClass) {... |
93 |
0 |
this(resource, template, binary, null, null, null, modelClass); |
94 |
|
} |
95 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
96 |
1 |
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@link |
102 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
103 |
0 |
public InstallResourceTask(String resource, String template, String version, String source, String rights) {... |
104 |
0 |
this(resource, template, false, version, source, rights); |
105 |
|
} |
106 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
107 |
0 |
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
111 |
3 |
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@link |
117 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
118 |
0 |
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 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
122 |
0 |
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
|
126 |
4 |
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 |
|
|
|
|
| 66% |
Uncovered Elements: 18 (53) |
Complexity: 7 |
Complexity Density: 0.16 |
|
138 |
4 |
@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 |
|
|
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 |
|
|
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 |
|
|
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 |
|
} |