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.cms.taglibs.util;
35
36 import info.magnolia.cms.core.HierarchyManager;
37 import info.magnolia.cms.beans.config.ContentRepository;
38 import info.magnolia.cms.beans.runtime.FileProperties;
39 import info.magnolia.cms.core.Content;
40 import info.magnolia.cms.core.ItemType;
41 import info.magnolia.cms.core.NodeData;
42 import info.magnolia.cms.taglibs.Resource;
43 import info.magnolia.context.MgnlContext;
44 import org.apache.commons.io.IOUtils;
45 import org.apache.commons.lang.StringUtils;
46 import org.devlib.schmidt.imageinfo.ImageInfo;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 import javax.jcr.AccessDeniedException;
51 import javax.jcr.PathNotFoundException;
52 import javax.jcr.PropertyType;
53 import javax.jcr.RepositoryException;
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
72
73 public abstract class BaseImageTag extends SimpleTagSupport {
74
75
76
77
78 protected static final String PROPERTIES_EXTENSION_VALUE = "PNG";
79
80
81
82
83 protected static final String PROPERTIES_CONTENTTYPE_VALUE = "image/png";
84
85
86
87
88 private static Logger log = LoggerFactory.getLogger(BaseImageTag.class);
89
90
91
92
93 protected String parentContentNodeName;
94
95
96
97
98
99
100 protected String imageContentNodeName;
101
102
103
104
105
106 public void setImageContentNodeName(String imageContentNodeName) {
107 this.imageContentNodeName = imageContentNodeName;
108 }
109
110
111
112
113
114 public void setParentContentNodeName(String parentContentNodeName) {
115 this.parentContentNodeName = parentContentNodeName;
116 }
117
118 protected abstract String getFilename();
119
120 protected HttpServletRequest getRequest() {
121 return (HttpServletRequest) ((PageContext) this.getJspContext()).getRequest();
122 }
123
124
125
126
127
128
129 protected Content getImageContentNode() throws PathNotFoundException, RepositoryException,
130 info.magnolia.cms.security.AccessDeniedException {
131
132 Content imageContentNode;
133 Content currentActivePage = Resource.getCurrentActivePage();
134 Content paragraph = Resource.getLocalContentNode();
135 Content parentContentNode = null;
136
137 if (StringUtils.isEmpty(this.parentContentNodeName)) {
138 parentContentNode = paragraph != null ? paragraph : currentActivePage;
139 }
140 else {
141
142 HierarchyManager hm = MgnlContext.getHierarchyManager(ContentRepository.WEBSITE);
143
144
145 if (this.parentContentNodeName.startsWith("/")) {
146 parentContentNode = hm.getContent(this.parentContentNodeName);
147 }
148 else {
149 parentContentNode = hm.getContent(currentActivePage.getHandle() + "/" + this.parentContentNodeName);
150 }
151 }
152
153 imageContentNode = null;
154 if (StringUtils.isEmpty(this.imageContentNodeName)) {
155 imageContentNode = parentContentNode;
156 }
157 else if (parentContentNode.hasContent(this.imageContentNodeName)) {
158 imageContentNode = parentContentNode.getContent(this.imageContentNodeName);
159 }
160 else {
161 imageContentNode = parentContentNode.createContent(this.imageContentNodeName, ItemType.CONTENTNODE);
162 parentContentNode.save();
163 }
164 return imageContentNode;
165 }
166
167
168
169
170
171 public String convertToSimpleString(String string) {
172
173 final StringBuffer result = new StringBuffer();
174
175 final StringCharacterIterator iterator = new StringCharacterIterator(string);
176 char character = iterator.current();
177 while (character != CharacterIterator.DONE) {
178 int charType = Character.getType(character);
179 if (charType == Character.SPACE_SEPARATOR) {
180 result.append("-");
181 }
182 else if ((charType != Character.UPPERCASE_LETTER)
183 && (charType != Character.LOWERCASE_LETTER)
184 && (charType != Character.DECIMAL_DIGIT_NUMBER)
185 && (charType != Character.CONNECTOR_PUNCTUATION)
186 && (charType != Character.DASH_PUNCTUATION)) {
187 result.append("u" + (int) character);
188
189 }
190 else {
191
192
193 result.append(character);
194 }
195 character = iterator.next();
196 }
197 return result.toString();
198 }
199
200
201
202
203
204 public int[] convertHexToRGB(String hex) {
205 hex.trim();
206 if (hex.startsWith("#")) {
207 hex = hex.substring(1);
208 }
209 if (hex.length() == 3) {
210
211 hex = String.valueOf(hex.charAt(0))
212 + String.valueOf(hex.charAt(0))
213 + String.valueOf(hex.charAt(1))
214 + String.valueOf(hex.charAt(1))
215 + String.valueOf(hex.charAt(2))
216 + String.valueOf(hex.charAt(2));
217 }
218
219 int[] rgb = new int[3];
220 try {
221
222 rgb[0] = Integer.parseInt(hex.substring(0, 2), 16);
223 rgb[1] = Integer.parseInt(hex.substring(2, 4), 16);
224 rgb[2] = Integer.parseInt(hex.substring(4), 16);
225 }
226 catch (NumberFormatException e) {
227 log.error("NumberFormatException occured during text-to-image conversion: "
228 + "Attempting to convert Hex ["
229 + hex
230 + "] color to RGB color: "
231 + e.getMessage(), e);
232 rgb = new int[]{255, 0, 0};
233 }
234 return rgb;
235 }
236
237 public Color convertHexToColor(String hex) {
238 int[] rgb = convertHexToRGB(hex);
239 Color color = new Color(rgb[0], rgb[1], rgb[2]);
240 return color;
241 }
242
243
244
245
246
247
248
249
250
251 protected void createImageNode(File imageFile, Content imageNode) throws PathNotFoundException,
252 AccessDeniedException, RepositoryException, FileNotFoundException, IOException {
253
254
255 NodeData data;
256 data = imageNode.getNodeData(getFilename());
257
258 if (!data.isExist()) {
259 data = imageNode.createNodeData(getFilename(), PropertyType.BINARY);
260 }
261
262 InputStream iis = new FileInputStream(imageFile);
263 data.setValue(iis);
264 IOUtils.closeQuietly(iis);
265
266 data.setAttribute(FileProperties.PROPERTY_FILENAME, getFilename());
267
268 data.setAttribute(FileProperties.PROPERTY_CONTENTTYPE, PROPERTIES_CONTENTTYPE_VALUE);
269
270 Calendar value = new GregorianCalendar(TimeZone.getDefault());
271 data.setAttribute(FileProperties.PROPERTY_LASTMODIFIED, value);
272
273 data.setAttribute(FileProperties.PROPERTY_SIZE, Long.toString(imageFile.length()));
274
275 data.setAttribute(FileProperties.PROPERTY_EXTENSION, PROPERTIES_EXTENSION_VALUE);
276
277 InputStream raf = null;
278 try {
279 ImageInfo ii = new ImageInfo();
280 raf = new FileInputStream(imageFile);
281 ii.setInput(raf);
282 if (ii.check()) {
283 data.setAttribute(FileProperties.PROPERTY_WIDTH, Long.toString(ii.getWidth()));
284 data.setAttribute(FileProperties.PROPERTY_HEIGHT, Long.toString(ii.getHeight()));
285
286 }
287 }
288 catch (FileNotFoundException e) {
289 log.error("FileNotFoundException caught when parsing {}, image data will not be available", imageFile
290 .getAbsolutePath());
291 }
292 finally {
293 IOUtils.closeQuietly(raf);
294 }
295
296
297 imageFile.delete();
298
299
300 imageNode.getMetaData().setModificationDate();
301 imageNode.getParent().save();
302 }
303 }