View Javadoc

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