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.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
72
73
74 public abstract class BaseImageTag extends SimpleTagSupport {
75
76
77
78
79 protected static final String PROPERTIES_EXTENSION_VALUE = "PNG";
80
81
82
83
84 protected static final String PROPERTIES_CONTENTTYPE_VALUE = "image/png";
85
86
87
88
89 private static Logger log = LoggerFactory.getLogger(BaseImageTag.class);
90
91
92
93
94 protected String parentContentNodeName;
95
96
97
98
99
100
101 protected String imageContentNodeName;
102
103
104
105
106
107 public void setImageContentNodeName(String imageContentNodeName) {
108 this.imageContentNodeName = imageContentNodeName;
109 }
110
111
112
113
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
127
128
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
141 if (StringUtils.isEmpty(this.parentContentNodeName)) {
142 parentContentNode = paragraph != null ? paragraph : currentActivePage;
143 }
144 else {
145
146
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
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
171
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
194
195 result.append(character);
196 }
197 character = iterator.next();
198 }
199 return result.toString();
200 }
201
202
203
204
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
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
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};
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
247
248
249
250
251
252
253 protected void createImageNode(File imageFile, Content imageNode) throws PathNotFoundException,
254 AccessDeniedException, RepositoryException, FileNotFoundException, IOException {
255
256
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
299 imageFile.delete();
300
301
302 imageNode.getMetaData().setModificationDate();
303 imageNode.getParent().save();
304 }
305 }