Clover icon

Magnolia Resources Module 2.4.2

  1. Project Clover database Fri Nov 6 2015 16:15:26 CET
  2. Package info.magnolia.module.resources.setup

File InstallBinaryResourceTask.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart9.png
24% of files have more coverage

Code metrics

4
27
5
1
117
61
8
0.3
5.4
5
1.6

Classes

Class Line # Actions
InstallBinaryResourceTask 57 27 0% 8 6
0.833333383.3%
 

Contributing tests

This file is covered by 8 tests. .

Source view

1    /**
2    * This file Copyright (c) 2013-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.util.ClasspathResourcesUtil;
38    import info.magnolia.jcr.util.NodeTypes;
39    import info.magnolia.module.InstallContext;
40    import info.magnolia.module.delta.TaskExecutionException;
41    import info.magnolia.module.resources.ResourceTypes;
42   
43    import java.io.InputStream;
44   
45    import javax.jcr.Binary;
46    import javax.jcr.Node;
47   
48    import org.apache.commons.io.IOUtils;
49    import org.apache.commons.io.input.CountingInputStream;
50    import org.apache.commons.lang3.StringUtils;
51    import org.apache.jackrabbit.JcrConstants;
52    import org.devlib.schmidt.imageinfo.ImageInfo;
53   
54    /**
55    * Task to load single binary resource to the repository.
56    */
 
57    public class InstallBinaryResourceTask extends AbstractInstallResourceTask {
58   
 
59  3 toggle public InstallBinaryResourceTask(String resource) {
60  3 super(resource, ResourceTypes.BINARY, false, null, null, null, false);
61    }
62   
 
63  0 toggle public InstallBinaryResourceTask(String resource, boolean preserveResourceParameters) {
64  0 super(resource, ResourceTypes.BINARY, false, null, null, null, preserveResourceParameters);
65    }
66   
 
67  0 toggle public InstallBinaryResourceTask(String resource, String version, String source, String rights, boolean preserveResourceParameters) {
68  0 super(resource, ResourceTypes.BINARY, false, version, source, rights, preserveResourceParameters);
69    }
70   
 
71  8 toggle public InstallBinaryResourceTask(String resource, String version, String source, String rights, boolean preserveResourceParameters, boolean stripExtension) {
72  8 super(resource, ResourceTypes.BINARY, false, version, source, rights, preserveResourceParameters, stripExtension);
73    }
74   
 
75  11 toggle @Override
76    public void execute(InstallContext installContext) throws TaskExecutionException {
77   
78  11 super.execute(installContext);
79   
80  10 try {
81  10 InputStream stream = ClasspathResourcesUtil.getStream(resource);
82   
83  10 CountingInputStream countingStream = new CountingInputStream(stream);
84  10 Binary binary = node.getSession().getValueFactory().createBinary(countingStream);
85   
86  10 Node binaryNode = null;
87  10 if (node.hasNode(ResourceTypes.BINARY_SUFFIX)) {
88  1 node.getNode(ResourceTypes.BINARY_SUFFIX).remove();
89    }
90   
91  10 binaryNode = node.addNode(ResourceTypes.BINARY_SUFFIX, NodeTypes.Resource.NAME);
92  10 binaryNode.setProperty(JcrConstants.JCR_DATA, binary);
93  10 binaryNode.setProperty(FileProperties.PROPERTY_FILENAME, name);
94  10 binaryNode.setProperty(FileProperties.PROPERTY_EXTENSION, StringUtils.substringAfterLast(resource, "."));
95  10 binaryNode.setProperty(FileProperties.PROPERTY_SIZE, binary.getSize());
96   
97    // we have to reread the stream to get image information
98  10 InputStream imageStream = ClasspathResourcesUtil.getStream(resource);
99   
100  10 try {
101  10 ImageInfo ii = new ImageInfo();
102  10 ii.setInput(imageStream);
103  10 if (ii.check()) {
104  10 binaryNode.setProperty(FileProperties.PROPERTY_WIDTH, Long.toString(ii.getWidth()));
105  10 binaryNode.setProperty(FileProperties.PROPERTY_HEIGHT, Long.toString(ii.getHeight()));
106  10 binaryNode.setProperty(JcrConstants.JCR_MIMETYPE, ii.getMimeType());
107    }
108    } finally {
109  10 IOUtils.closeQuietly(imageStream);
110    }
111    } catch (Exception e) {
112  0 throw new TaskExecutionException("Can't install resource [" + resource + "]", e);
113    }
114   
115    }
116   
117    }