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.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
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 private static Logger log = LoggerFactory.getLogger(BaseImageTag.class);
87
88
89
90
91 protected String parentContentNodeName;
92
93
94
95
96
97
98 protected String imageContentNodeName;
99
100
101
102
103 public void setImageContentNodeName(String imageContentNodeName) {
104 this.imageContentNodeName = imageContentNodeName;
105 }
106
107
108
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
131 if (StringUtils.isEmpty(this.parentContentNodeName)) {
132 parentContentNode = paragraph != null ? paragraph : currentActivePage;
133 } else {
134
135
136 if (this.parentContentNodeName.startsWith("/")) {
137 parentContentNode = session.getNode(this.parentContentNodeName);
138 } else {
139 parentContentNode = session.getNode(currentActivePage.getPath() + "/" + this.parentContentNodeName);
140 }
141 }
142
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
157
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
178
179 result.append(character);
180 }
181 character = iterator.next();
182 }
183 return result.toString();
184 }
185
186
187
188
189
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
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
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};
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
227
228
229
230
231
232
233
234 protected void createImageNode(File imageFile, Content imageNode) throws PathNotFoundException,
235 AccessDeniedException, RepositoryException, FileNotFoundException, IOException {
236
237
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
278 imageFile.delete();
279
280
281 imageNode.getParent().save();
282 }
283 }