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.resources.app; |
35 |
|
|
36 |
|
import info.magnolia.cms.beans.runtime.FileProperties; |
37 |
|
import info.magnolia.cms.core.Path; |
38 |
|
import info.magnolia.context.Context; |
39 |
|
import info.magnolia.context.MgnlContext; |
40 |
|
import info.magnolia.jcr.util.NodeTypes; |
41 |
|
import info.magnolia.jcr.util.NodeUtil; |
42 |
|
import info.magnolia.resourceloader.Resource; |
43 |
|
import info.magnolia.resourceloader.ResourceOrigin; |
44 |
|
import info.magnolia.resourceloader.jcr.JcrResourceOrigin; |
45 |
|
import info.magnolia.resourceloader.layered.LayeredResource; |
46 |
|
import info.magnolia.resources.app.workbench.ResourcesContainer; |
47 |
|
import info.magnolia.ui.vaadin.integration.contentconnector.ContentConnector; |
48 |
|
|
49 |
|
import java.io.IOException; |
50 |
|
import java.util.Calendar; |
51 |
|
|
52 |
|
import javax.inject.Inject; |
53 |
|
import javax.jcr.Binary; |
54 |
|
import javax.jcr.Node; |
55 |
|
import javax.jcr.RepositoryException; |
56 |
|
import javax.jcr.Session; |
57 |
|
|
58 |
|
import org.apache.commons.io.IOUtils; |
59 |
|
import org.apache.jackrabbit.JcrConstants; |
60 |
|
import org.slf4j.Logger; |
61 |
|
import org.slf4j.LoggerFactory; |
62 |
|
|
63 |
|
import com.google.common.net.MediaType; |
64 |
|
import com.vaadin.data.Item; |
65 |
|
import com.vaadin.data.Property; |
66 |
|
|
67 |
|
|
68 |
|
@link |
69 |
|
|
70 |
|
@link@link |
71 |
|
@link@link |
72 |
|
|
|
|
| 75% |
Uncovered Elements: 26 (104) |
Complexity: 28 |
Complexity Density: 0.44 |
|
73 |
|
public class ResourcesContentConnector implements ContentConnector { |
74 |
|
|
75 |
|
private static final Logger log = LoggerFactory.getLogger(ResourcesContentConnector.class); |
76 |
|
|
77 |
|
private final ResourceOrigin<LayeredResource> origin; |
78 |
|
|
79 |
|
private Context context; |
80 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
81 |
18 |
@Inject... |
82 |
|
@SuppressWarnings("unchecked") |
83 |
|
public ResourcesContentConnector(ResourceOrigin resourceOrigin, Context context) { |
84 |
18 |
this.context = context; |
85 |
18 |
this.origin = (ResourceOrigin<LayeredResource>) resourceOrigin; |
86 |
|
} |
87 |
|
|
88 |
|
|
89 |
|
@deprecated@link |
90 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
91 |
0 |
@Deprecated... |
92 |
|
public ResourcesContentConnector(ResourceOrigin resourceOrigin) { |
93 |
0 |
this((ResourceOrigin<LayeredResource>) resourceOrigin, MgnlContext.getInstance()); |
94 |
|
} |
95 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
96 |
0 |
@Override... |
97 |
|
public String getItemUrlFragment(Object itemId) { |
98 |
0 |
return origin.getByPath((String) itemId).getPath(); |
99 |
|
} |
100 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
101 |
0 |
@Override... |
102 |
|
public String getItemIdByUrlFragment(String urlFragment) { |
103 |
0 |
return urlFragment; |
104 |
|
} |
105 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
106 |
|
@Override... |
107 |
|
public String getDefaultItemId() { |
108 |
|
|
109 |
|
return "/"; |
110 |
|
} |
111 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
112 |
0 |
@Override... |
113 |
|
public Item getItem(Object itemId) { |
114 |
0 |
return ResourcesContainer.newItem(origin.getByPath((String) itemId)); |
115 |
|
} |
116 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
117 |
0 |
@Override... |
118 |
|
public String getItemId(Item item) { |
119 |
0 |
Property<?> resourcePathProperty = item.getItemProperty(ResourcesContainer.RESOURCE_PATH); |
120 |
0 |
if (resourcePathProperty != null) { |
121 |
0 |
return (String) resourcePathProperty.getValue(); |
122 |
|
} |
123 |
0 |
return null; |
124 |
|
} |
125 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
126 |
0 |
@Override... |
127 |
|
public boolean canHandleItem(Object itemId) { |
128 |
0 |
return itemId instanceof String && origin.hasPath((String) itemId); |
129 |
|
} |
130 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
131 |
|
public ResourceOrigin getOrigin() {... |
132 |
|
return origin; |
133 |
|
} |
134 |
|
|
135 |
|
|
136 |
|
@value@link |
137 |
|
|
|
|
| 72.7% |
Uncovered Elements: 3 (11) |
Complexity: 3 |
Complexity Density: 0.33 |
|
138 |
3 |
public Resource createNewFolder(Resource parentResource, Item item) {... |
139 |
3 |
String fileName = item.getItemProperty(ResourcesContainer.RESOURCE_NAME) != null ? item.getItemProperty(ResourcesContainer.RESOURCE_NAME).getValue().toString() : "untitled"; |
140 |
3 |
try { |
141 |
3 |
Session session = context.getJCRSession(JcrResourceOrigin.RESOURCES_WORKSPACE); |
142 |
|
|
143 |
3 |
Node parentNode = NodeUtil.createPath(session.getRootNode(), parentResource.getPath(), NodeTypes.Folder.NAME); |
144 |
3 |
Node addedFolder = parentNode.addNode(generateUniqueNodeNameForResource(parentNode, fileName), NodeTypes.Folder.NAME); |
145 |
3 |
session.save(); |
146 |
|
|
147 |
3 |
return origin.getByPath(addedFolder.getPath()); |
148 |
|
} catch (Exception e) { |
149 |
0 |
log.error("New folder failed to be created in JCR", e); |
150 |
0 |
return null; |
151 |
|
} |
152 |
|
} |
153 |
|
|
154 |
|
|
155 |
|
@link@link@link |
156 |
|
@link@link |
157 |
|
@link |
158 |
|
|
|
|
| 91.4% |
Uncovered Elements: 3 (35) |
Complexity: 7 |
Complexity Density: 0.28 |
|
159 |
9 |
@SuppressWarnings("unchecked")... |
160 |
|
public Resource createNewResource(Resource parentResource, Item item) { |
161 |
|
|
162 |
9 |
final Property<Binary> jcrData = (Property<Binary>) item.getItemProperty(JcrConstants.JCR_DATA); |
163 |
9 |
String fileName; |
164 |
|
|
165 |
9 |
boolean hasBinary = jcrData != null; |
166 |
9 |
if (hasBinary) { |
167 |
5 |
fileName = item.getItemProperty(FileProperties.PROPERTY_FILENAME) != null ? item.getItemProperty(FileProperties.PROPERTY_FILENAME).getValue().toString() : "untitled"; |
168 |
|
} else { |
169 |
4 |
fileName = item.getItemProperty(ResourcesContainer.RESOURCE_NAME) != null ? item.getItemProperty(ResourcesContainer.RESOURCE_NAME).getValue().toString() : "untitled"; |
170 |
|
} |
171 |
9 |
try { |
172 |
9 |
final Session jcrSession = context.getJCRSession(JcrResourceOrigin.RESOURCES_WORKSPACE); |
173 |
|
|
174 |
9 |
final Node parent = NodeUtil.createPath(jcrSession.getRootNode(), parentResource.getPath(), NodeTypes.Folder.NAME); |
175 |
9 |
final Node resourceNode = parent.addNode(generateUniqueNodeNameForResource(parent, fileName), NodeTypes.Content.NAME); |
176 |
9 |
if (hasBinary) { |
177 |
5 |
MediaType type = extractMediaType(item); |
178 |
5 |
if (type.is(MediaType.ANY_TEXT_TYPE)) { |
179 |
2 |
resourceNode.setProperty(JcrResourceOrigin.TEXT_PROPERTY, IOUtils.toString(jcrData.getValue().getStream())); |
180 |
|
} else { |
181 |
3 |
Node binaryNode = resourceNode.addNode(JcrResourceOrigin.BINARY_NODE_NAME, NodeTypes.Resource.NAME); |
182 |
3 |
setBinaryProperty(binaryNode, item, FileProperties.SIZE); |
183 |
3 |
setBinaryProperty(binaryNode, item, FileProperties.EXTENSION); |
184 |
3 |
setBinaryProperty(binaryNode, item, JcrConstants.JCR_MIMETYPE); |
185 |
3 |
setBinaryProperty(binaryNode, item, JcrConstants.JCR_DATA); |
186 |
|
} |
187 |
|
} else { |
188 |
4 |
resourceNode.setProperty(JcrResourceOrigin.TEXT_PROPERTY, ""); |
189 |
|
} |
190 |
|
|
191 |
9 |
resourceNode.setProperty(NodeTypes.LastModified.LAST_MODIFIED, Calendar.getInstance()); |
192 |
9 |
jcrSession.save(); |
193 |
|
|
194 |
9 |
return origin.getByPath(resourceNode.getPath()); |
195 |
|
} catch (RepositoryException | IOException e) { |
196 |
0 |
log.error("New node failed to be created in JCR", e); |
197 |
0 |
return null; |
198 |
|
} |
199 |
|
} |
200 |
|
|
|
|
| 91.7% |
Uncovered Elements: 1 (12) |
Complexity: 4 |
Complexity Density: 0.67 |
|
201 |
12 |
private void setBinaryProperty(Node binaryNode, Item item, String propertyName) throws RepositoryException {... |
202 |
12 |
Property<?> p = item.getItemProperty(propertyName); |
203 |
12 |
if (p != null) { |
204 |
6 |
if (String.class.isAssignableFrom(p.getType())) { |
205 |
3 |
binaryNode.setProperty(propertyName, (String) p.getValue()); |
206 |
3 |
} else if (Binary.class.isAssignableFrom(p.getType())) { |
207 |
3 |
binaryNode.setProperty(propertyName, (Binary) p.getValue()); |
208 |
|
} |
209 |
|
} |
210 |
|
} |
211 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
212 |
12 |
private String generateUniqueNodeNameForResource(final Node parent, String newNodeName) throws RepositoryException {... |
213 |
12 |
return Path.getUniqueLabel(parent.getSession(), parent.getPath(), Path.getValidatedLabel(newNodeName)); |
214 |
|
} |
215 |
|
|
216 |
|
|
217 |
|
|
218 |
|
|
219 |
|
|
220 |
|
|
221 |
|
|
222 |
|
|
|
|
| 90% |
Uncovered Elements: 2 (20) |
Complexity: 5 |
Complexity Density: 0.42 |
|
223 |
10 |
final MediaType extractMediaType(Item item) throws RepositoryException {... |
224 |
10 |
Property<?> mimeType; |
225 |
10 |
if (item == null) { |
226 |
0 |
throw new IllegalArgumentException("Item can't be null"); |
227 |
|
} |
228 |
10 |
mimeType = item.getItemProperty(FileProperties.PROPERTY_CONTENTTYPE); |
229 |
10 |
if (mimeType == null) { |
230 |
2 |
mimeType = item.getItemProperty(FileProperties.PROPERTY_EXTENSION); |
231 |
2 |
if (mimeType == null) { |
232 |
1 |
throw new IllegalStateException("Expected to find either a [jcr:mimeType] or [extension] property but none was found"); |
233 |
|
} else { |
234 |
|
|
235 |
1 |
return MediaType.create("text", "x-" + item.getItemProperty(FileProperties.PROPERTY_EXTENSION).getValue()); |
236 |
|
} |
237 |
|
} else { |
238 |
|
|
239 |
8 |
if (mimeType.getValue().toString().equals(MediaType.OCTET_STREAM.toString())) { |
240 |
1 |
return MediaType.create("text", "x-" + item.getItemProperty(FileProperties.PROPERTY_EXTENSION).getValue()); |
241 |
|
} |
242 |
7 |
return MediaType.parse(mimeType.getValue().toString()); |
243 |
|
} |
244 |
|
} |
245 |
|
} |