Clover icon

Magnolia Resources App Module 2.4.7

  1. Project Clover database Fri Sep 9 2016 16:27:56 CEST
  2. Package info.magnolia.resources.app

File ResourcesContentConnector.java

 

Coverage histogram

../../../../img/srcFileCovDistChart8.png
52% of files have more coverage

Code metrics

28
64
12
1
245
155
28
0.44
5.33
12
2.33
3.7% of code in this file is excluded from these metrics.

Classes

Class Line # Actions
ResourcesContentConnector 73 64 3.7% 28 26
0.7575%
 

Contributing tests

This file is covered by 16 tests. .

Source view

1    /**
2    * This file Copyright (c) 2015-2016 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.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 ContentConnector} implementation for the new resources app.
69    * <p>
70    * In particular, it delegates creation of {@link Item Items} to the {@link ResourcesContainer},
71    * and uses {@link LayeredResource} {@link ResourcesContainer#RESOURCE_PATH} as a string representation.
72    */
 
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   
 
81  16 toggle @Inject
82    @SuppressWarnings("unchecked")
83    public ResourcesContentConnector(ResourceOrigin resourceOrigin, Context context) {
84  16 this.context = context;
85  16 this.origin = (ResourceOrigin<LayeredResource>) resourceOrigin;
86    }
87   
88    /**
89    * @deprecated since 2.4.2 - use {@link #ResourcesContentConnector(ResourceOrigin, Context)} instead.
90    */
 
91  0 toggle @Deprecated
92    public ResourcesContentConnector(ResourceOrigin resourceOrigin) {
93  0 this((ResourceOrigin<LayeredResource>) resourceOrigin, MgnlContext.getInstance());
94    }
95   
 
96  0 toggle @Override
97    public String getItemUrlFragment(Object itemId) {
98  0 return origin.getByPath((String) itemId).getPath();
99    }
100   
 
101  0 toggle @Override
102    public String getItemIdByUrlFragment(String urlFragment) {
103  0 return urlFragment;
104    }
105   
 
106    toggle @Override
107    public String getDefaultItemId() {
108    // BrowserLocation sets path to "/" if empty, which is in fact an existing Resource (origin root), so we stay in sync
109    return "/";
110    }
111   
 
112  0 toggle @Override
113    public Item getItem(Object itemId) {
114  0 return ResourcesContainer.newItem(origin.getByPath((String) itemId));
115    }
116   
 
117  0 toggle @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   
 
126  0 toggle @Override
127    public boolean canHandleItem(Object itemId) {
128  0 return itemId instanceof String && origin.hasPath((String) itemId);
129    }
130   
 
131    toggle public ResourceOrigin getOrigin() {
132    return origin;
133    }
134   
135    /**
136    * Creates a new folder ({@value NodeTypes.Folder#NAME}) in {@link JcrResourceOrigin}.
137    */
 
138  3 toggle 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    // create parent directories in JCR if they don't exist yet
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    * Creates a new {@link Resource} in JCR resources repository. Should the item have {@link JcrConstants#JCR_DATA} property and its {@link JcrConstants#JCR_MIMETYPE} be other than any text type,
156    * then the binary will be saved under a sub-node of the resource node named {@link JcrResourceOrigin#BINARY_NODE_NAME} of type {@link NodeTypes.Resource#NAME} together with related data such as mimeType, extension and size.
157    * The resource name will be by default the name of the file, unless changed by the user through the UI. In case of a text file the contents are saved directly under the resource node in the {@link JcrResourceOrigin#TEXT_PROPERTY} property.
158    */
 
159  8 toggle @SuppressWarnings("unchecked")
160    public Resource createNewResource(Resource parentResource, Item item) {
161   
162  8 final Property<Binary> jcrData = (Property<Binary>) item.getItemProperty(JcrConstants.JCR_DATA);
163  8 String fileName;
164   
165  8 boolean hasBinary = jcrData != null;
166  8 if (hasBinary) {
167  5 fileName = item.getItemProperty(FileProperties.PROPERTY_FILENAME) != null ? item.getItemProperty(FileProperties.PROPERTY_FILENAME).getValue().toString() : "untitled";
168    } else {
169  3 fileName = item.getItemProperty(ResourcesContainer.RESOURCE_NAME) != null ? item.getItemProperty(ResourcesContainer.RESOURCE_NAME).getValue().toString() : "untitled";
170    }
171  8 try {
172  8 final Session jcrSession = context.getJCRSession(JcrResourceOrigin.RESOURCES_WORKSPACE);
173   
174  8 final Node parent = NodeUtil.createPath(jcrSession.getRootNode(), parentResource.getPath(), NodeTypes.Folder.NAME);
175  8 final Node resourceNode = parent.addNode(generateUniqueNodeNameForResource(parent, fileName), NodeTypes.Content.NAME);
176  8 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  3 resourceNode.setProperty(JcrResourceOrigin.TEXT_PROPERTY, "");
189    }
190   
191  8 resourceNode.setProperty(NodeTypes.LastModified.LAST_MODIFIED, Calendar.getInstance());
192  8 jcrSession.save();
193   
194  8 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   
 
201  12 toggle 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   
 
212  11 toggle private String generateUniqueNodeNameForResource(final Node parent, String newNodeName) throws RepositoryException {
213  11 return Path.getUniqueLabel(parent.getSession(), parent.getPath(), Path.getValidatedLabel(newNodeName));
214    }
215   
216    /**
217    * This method has package visibility only for testing purposes. It will first check the binary media type in
218    * the <code>jcr:mimeType</code> property. Should that property not exist, it will try to infer it from
219    * the file extension. In the latter case it will assume a custom text mime type, e.g. <code>text/x-&lt;fileExtension&gt;</code>.
220    * A custom text mime type is also returned in case of an <code>application/octet-stream</code> mime type as the resources app
221    * can't handle it.
222    */
 
223  10 toggle 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    // assume custom text type cause it's easier to handle, e.g. edit it.
235  1 return MediaType.create("text", "x-" + item.getItemProperty(FileProperties.PROPERTY_EXTENSION).getValue());
236    }
237    } else {
238    // replace with custom text type cause we know how to handle it, e.g. edit it.
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    }