View Javadoc
1   /**
2    * This file Copyright (c) 2012-2018 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.templating.jsp.taglib;
35  
36  import info.magnolia.cms.beans.runtime.FileProperties;
37  import info.magnolia.cms.core.Content;
38  import info.magnolia.cms.core.NodeData;
39  import info.magnolia.context.MgnlContext;
40  import info.magnolia.jcr.util.NodeTypes;
41  
42  import java.awt.Color;
43  import java.io.File;
44  import java.io.FileInputStream;
45  import java.io.FileNotFoundException;
46  import java.io.IOException;
47  import java.io.InputStream;
48  import java.text.CharacterIterator;
49  import java.text.StringCharacterIterator;
50  import java.util.Calendar;
51  import java.util.GregorianCalendar;
52  import java.util.TimeZone;
53  
54  import javax.jcr.AccessDeniedException;
55  import javax.jcr.Node;
56  import javax.jcr.PathNotFoundException;
57  import javax.jcr.PropertyType;
58  import javax.jcr.RepositoryException;
59  import javax.jcr.Session;
60  import javax.servlet.http.HttpServletRequest;
61  import javax.servlet.jsp.PageContext;
62  import javax.servlet.jsp.tagext.SimpleTagSupport;
63  
64  import org.apache.commons.io.IOUtils;
65  import org.apache.commons.lang3.StringUtils;
66  import org.devlib.schmidt.imageinfo.ImageInfo;
67  import org.slf4j.Logger;
68  import org.slf4j.LoggerFactory;
69  
70  
71  /**
72   * A base class for image related tags e.g. {@link ScaleImageTag}.
73   */
74  public abstract class BaseImageTag extends SimpleTagSupport {
75  
76      /**
77       * The value of the extension nodeData in the properties node.
78       */
79      protected static final String PROPERTIES_EXTENSION_VALUE = "PNG";
80  
81      /**
82       * The valye of the contentType nodeData in the properties node.
83       */
84      protected static final String PROPERTIES_CONTENTTYPE_VALUE = "image/png";
85  
86      private static Logger log = LoggerFactory.getLogger(BaseImageTag.class);
87  
88      /**
89       * Attribute: The node where the images are to be saved. If null, the default will be the current active page.
90       */
91      protected String parentContentNodeName;
92  
93      /**
94       * Attribute: The name of the new content node to create. The images will be saved under this node. If this name
95       * starts with a '/', it will be assumed to be a node handle that is relative to the rooot of the website.
96       * Otherwise, it is assumed to be a path relative to the currentActivePage.
97       */
98      protected String imageContentNodeName;
99  
100     /**
101      * Setter for the <code>imageContentNodeName</code> tag attribute.
102      */
103     public void setImageContentNodeName(String imageContentNodeName) {
104         this.imageContentNodeName = imageContentNodeName;
105     }
106 
107     /**
108      * Setter for the <code>parentContentNodeName</code> tag attribute.
109      */
110     public void setParentContentNodeName(String parentContentNodeName) {
111         this.parentContentNodeName = parentContentNodeName;
112     }
113 
114     protected abstract String getFilename();
115 
116     protected HttpServletRequest getRequest() {
117         return (HttpServletRequest) ((PageContext) this.getJspContext()).getRequest();
118     }
119 
120     protected Node getImageContentNode() throws PathNotFoundException, RepositoryException,
121             info.magnolia.cms.security.AccessDeniedException {
122         Session session = MgnlContext.getJCRSession(MgnlContext.getAggregationState().getRepository());
123 
124         Node imageContentNode;
125         Node currentActivePage = MgnlContext.getJCRSession(MgnlContext.getAggregationState().getRepository()).getNode(MgnlContext.getAggregationState().getHandle());
126 
127         Node paragraph = MgnlContext.getAggregationState().getCurrentContentNode();
128 
129         Node parentContentNode = null;
130         // set the image parent node
131         if (StringUtils.isEmpty(this.parentContentNodeName)) {
132             parentContentNode = paragraph != null ? paragraph : currentActivePage;
133         } else {
134             // if this name starts with a '/', then assume it is a node handle
135             // otherwise assume that its is a path relative to the current active page
136             if (this.parentContentNodeName.startsWith("/")) {
137                 parentContentNode = session.getNode(this.parentContentNodeName);
138             } else {
139                 parentContentNode = session.getNode(currentActivePage.getPath() + "/" + this.parentContentNodeName);
140             }
141         }
142         // set the node under which the images will be saved
143         imageContentNode = null;
144         if (StringUtils.isEmpty(this.imageContentNodeName)) {
145             imageContentNode = parentContentNode;
146         } else if (parentContentNode.hasNode(this.imageContentNodeName)) {
147             imageContentNode = parentContentNode.getNode(this.imageContentNodeName);
148         } else {
149             imageContentNode = parentContentNode.addNode(this.imageContentNodeName, NodeTypes.ContentNode.NAME);
150             session.save();
151         }
152         return imageContentNode;
153     }
154 
155     /**
156      * Replace any special characters that are not letters or numbers with a replacement string. The two exceptions are
157      * '-' and '_', which are allowed.
158      */
159     public String convertToSimpleString(String string) {
160 
161         final StringBuffer result = new StringBuffer();
162 
163         final StringCharacterIterator iterator = new StringCharacterIterator(string);
164         char character = iterator.current();
165         while (character != CharacterIterator.DONE) {
166             int charType = Character.getType(character);
167             if (charType == Character.SPACE_SEPARATOR) {
168                 result.append("-");
169             } else if ((charType != Character.UPPERCASE_LETTER)
170                     && (charType != Character.LOWERCASE_LETTER)
171                     && (charType != Character.DECIMAL_DIGIT_NUMBER)
172                     && (charType != Character.CONNECTOR_PUNCTUATION)
173                     && (charType != Character.DASH_PUNCTUATION)) {
174                 result.append("u").append((int) character);
175 
176             } else {
177                 // the char is not a special one
178                 // add it to the result as is
179                 result.append(character);
180             }
181             character = iterator.next();
182         }
183         return result.toString();
184     }
185 
186     /**
187      * Converts HEX color to RGB color.
188      *
189      * @param hex HEX value
190      */
191     public int[] convertHexToRGB(String hex) {
192         hex.trim();
193         if (hex.startsWith("#")) {
194             hex = hex.substring(1);
195         }
196         if (hex.length() == 3) {
197             // allow three digit codes like for css
198             hex = String.valueOf(hex.charAt(0))
199                     + String.valueOf(hex.charAt(0))
200                     + String.valueOf(hex.charAt(1))
201                     + String.valueOf(hex.charAt(1))
202                     + String.valueOf(hex.charAt(2))
203                     + String.valueOf(hex.charAt(2));
204         }
205 
206         int[] rgb = new int[3];
207         try {
208             // Convert rrggbb string to hex ints
209             rgb[0] = Integer.parseInt(hex.substring(0, 2), 16);
210             rgb[1] = Integer.parseInt(hex.substring(2, 4), 16);
211             rgb[2] = Integer.parseInt(hex.substring(4), 16);
212         } catch (NumberFormatException e) {
213             log.error("NumberFormatException occured during text-to-image conversion: Attempting to convert Hex [{}] color to RGB color: {}", hex, e.getMessage(), e);
214             rgb = new int[]{255, 0, 0}; // red
215         }
216         return rgb;
217     }
218 
219     public Color convertHexToColor(String hex) {
220         int[] rgb = convertHexToRGB(hex);
221         Color color = new Color(rgb[0], rgb[1], rgb[2]);
222         return color;
223     }
224 
225     /**
226      * Create a new imageNode with the image in it. The node is saved under a node that groups all image nodes, whose
227      * name is set to the value of the attribute imageContentNodeName. The name of the node will be set to a name that
228      * is unique for the image. The property that stores the image will be set to the value of
229      * PROPERTIES_FILENAME_VALUE. A sub-node is also created that stores the image properties.
230      *
231      * @param imageFile the file
232      * @param imageNode The node that will contain the text images.
233      */
234     protected void createImageNode(File imageFile, Content imageNode) throws PathNotFoundException,
235             AccessDeniedException, RepositoryException, FileNotFoundException, IOException {
236 
237         // Create and save the image data
238         NodeData data;
239         data = imageNode.getNodeData(getFilename());
240 
241         if (!data.isExist()) {
242             data = imageNode.createNodeData(getFilename(), PropertyType.BINARY);
243         }
244 
245         InputStream iis = new FileInputStream(imageFile);
246         data.setValue(iis);
247         IOUtils.closeQuietly(iis);
248 
249         data.setAttribute(FileProperties.PROPERTY_FILENAME, getFilename());
250 
251         data.setAttribute(FileProperties.PROPERTY_CONTENTTYPE, PROPERTIES_CONTENTTYPE_VALUE);
252 
253         Calendar value = new GregorianCalendar(TimeZone.getDefault());
254         data.setAttribute(FileProperties.PROPERTY_LASTMODIFIED, value);
255 
256         data.setAttribute(FileProperties.PROPERTY_SIZE, Long.toString(imageFile.length()));
257 
258         data.setAttribute(FileProperties.PROPERTY_EXTENSION, PROPERTIES_EXTENSION_VALUE);
259 
260         InputStream raf = null;
261         try {
262             ImageInfo ii = new ImageInfo();
263             raf = new FileInputStream(imageFile);
264             ii.setInput(raf);
265             if (ii.check()) {
266                 data.setAttribute(FileProperties.PROPERTY_WIDTH, Long.toString(ii.getWidth()));
267                 data.setAttribute(FileProperties.PROPERTY_HEIGHT, Long.toString(ii.getHeight()));
268 
269             }
270         } catch (FileNotFoundException e) {
271             log.error("FileNotFoundException caught when parsing {}, image data will not be available", imageFile
272                     .getAbsolutePath());
273         } finally {
274             IOUtils.closeQuietly(raf);
275         }
276 
277         // delete the temporary file
278         imageFile.delete();
279 
280         // save the new image node
281         imageNode.getParent().save();
282     }
283 }