ImageSpriteServlet.java
19.6 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.image.Layer
* javax.jcr.RepositoryException
* javax.servlet.Servlet
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.SlingHttpServletResponse
* org.apache.sling.api.request.RequestPathInfo
* org.osgi.service.component.ComponentContext
*/
package com.day.cq.extwidget.servlets;
import com.day.cq.extwidget.servlets.AbstractImageServlet;
import com.day.image.Layer;
import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Composite;
import java.awt.GradientPaint;
import java.awt.Paint;
import java.awt.Polygon;
import java.awt.RenderingHints;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.io.IOException;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Map;
import javax.jcr.RepositoryException;
import javax.servlet.Servlet;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.request.RequestPathInfo;
import org.osgi.service.component.ComponentContext;
@Component(metatype=1, label="Day CQ Image Sprite Servlet", description="Servlet that draws image sprites bases on basic 2D drawing commands")
@Service(value={Servlet.class})
@Properties(value={@Property(name="service.description", value={"CQ Image Sprite Servlet"}), @Property(name="sling.servlet.resourceTypes", value={"cq/ui/components/imagesprite"}, propertyPrivate=1), @Property(name="sling.servlet.extensions", value={"png", "gif", "jpg", "jpeg"}, propertyPrivate=1)})
public class ImageSpriteServlet
extends AbstractImageServlet {
@Property(intValue={1000}, label="Max image width allowed")
public static final String MAX_WIDTH_PROPERTY = "maxWidth";
@Property(intValue={1000}, label="Max image height allowed")
public static final String MAX_HEIGHT_PROPERTY = "maxHeight";
private static final long YEAR_IN_MS = 31536000000L;
private int maxWidth = 1000;
private int maxHeight = 1000;
private static Map<String, Color> CSS_COLORS = new HashMap<String, Color>();
protected void activate(ComponentContext context) {
this.maxWidth = (Integer)context.getProperties().get("maxWidth");
this.maxHeight = (Integer)context.getProperties().get("maxHeight");
}
protected Layer createLayer(AbstractImageServlet.ImageContext context) throws RepositoryException, IOException {
Parser parser = new Parser(context.request.getRequestPathInfo().getSelectors());
Integer width = parser.nextIntToken();
Integer height = parser.nextIntToken();
if (width == null || height == null || width > this.maxWidth || height > this.maxHeight) {
return null;
}
Layer layer = new Layer(width.intValue(), height.intValue(), null);
layer.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Shape shape = parser.nextShape();
while (shape != null) {
shape.draw(layer);
shape = parser.nextShape();
}
return layer;
}
protected void writeLayer(SlingHttpServletRequest request, SlingHttpServletResponse response, AbstractImageServlet.ImageContext context, Layer layer) throws IOException, RepositoryException {
response.setDateHeader("Expires", System.currentTimeMillis() + 31536000000L);
super.writeLayer(request, response, context, layer);
}
protected static Color getColor(String colorStr) {
if (CSS_COLORS.containsKey(colorStr)) {
return CSS_COLORS.get(colorStr);
}
try {
if (colorStr.startsWith("rgb(") || colorStr.startsWith("rgb<")) {
String[] splits = (colorStr = colorStr.substring(4, colorStr.length() - 1)).split(",");
if (splits.length == 3) {
int r = Integer.parseInt(splits[0].trim());
int g = Integer.parseInt(splits[1].trim());
int b = Integer.parseInt(splits[2].trim());
return new Color(r, g, b);
}
} else if (colorStr.startsWith("rgba(") || colorStr.startsWith("rgba<")) {
String[] splits = (colorStr = colorStr.substring(5, colorStr.length() - 1)).split(",");
if (splits.length == 4) {
int r = Integer.parseInt(splits[0].trim());
int g = Integer.parseInt(splits[1].trim());
int b = Integer.parseInt(splits[2].trim());
int a = Integer.parseInt(splits[3].trim());
return new Color(r, g, b, a);
}
} else {
if (colorStr.length() == 1) {
int c = 17 * Integer.parseInt(colorStr, 16);
return new Color(c, c, c);
}
if (colorStr.length() == 2) {
int c = Integer.parseInt(colorStr, 16);
return new Color(c, c, c);
}
if (colorStr.length() == 3) {
int r = 17 * Integer.parseInt(colorStr.substring(0, 1), 16);
int g = 17 * Integer.parseInt(colorStr.substring(1, 2), 16);
int b = 17 * Integer.parseInt(colorStr.substring(2, 3), 16);
return new Color(r, g, b);
}
if (colorStr.length() == 6) {
int r = Integer.parseInt(colorStr.substring(0, 2), 16);
int g = Integer.parseInt(colorStr.substring(2, 4), 16);
int b = Integer.parseInt(colorStr.substring(4, 6), 16);
return new Color(r, g, b);
}
}
}
catch (NumberFormatException e) {
// empty catch block
}
return null;
}
static {
CSS_COLORS.put("aqua", new Color(0, 255, 255));
CSS_COLORS.put("black", Color.black);
CSS_COLORS.put("blue", Color.blue);
CSS_COLORS.put("fuchsia", new Color(255, 0, 255));
CSS_COLORS.put("gray", Color.gray);
CSS_COLORS.put("green", new Color(0, 128, 0));
CSS_COLORS.put("lime", Color.green);
CSS_COLORS.put("maroon", new Color(128, 0, 0));
CSS_COLORS.put("navy", new Color(0, 0, 128));
CSS_COLORS.put("olive", new Color(128, 128, 0));
CSS_COLORS.put("purple", new Color(128, 0, 128));
CSS_COLORS.put("red", Color.red);
CSS_COLORS.put("silver", new Color(192, 192, 192));
CSS_COLORS.put("teal", new Color(0, 128, 128));
CSS_COLORS.put("white", Color.white);
CSS_COLORS.put("yellow", Color.yellow);
CSS_COLORS.put("transparent", new Color(0, 0, 0, 0));
}
private static class AlphaComp
extends Shape {
static final String TOKEN = "ac";
AlphaComposite ac;
private AlphaComp() {
super();
}
boolean parse(Parser parser) {
String type = parser.nextToken();
if (type == null) {
return false;
}
if ("src".equals(type)) {
this.ac = AlphaComposite.Src;
} else if ("srcAtop".equals(type)) {
this.ac = AlphaComposite.SrcAtop;
} else if ("srcIn".equals(type)) {
this.ac = AlphaComposite.SrcIn;
} else if ("srcOut".equals(type)) {
this.ac = AlphaComposite.SrcOut;
} else if ("srcOver".equals(type)) {
this.ac = AlphaComposite.SrcOver;
} else if ("dst".equals(type)) {
this.ac = AlphaComposite.Dst;
} else if ("dstAtop".equals(type)) {
this.ac = AlphaComposite.DstAtop;
} else if ("dstIn".equals(type)) {
this.ac = AlphaComposite.DstIn;
} else if ("dstOut".equals(type)) {
this.ac = AlphaComposite.DstOut;
} else if ("dstOver".equals(type)) {
this.ac = AlphaComposite.DstOver;
} else if ("clear".equals(type)) {
this.ac = AlphaComposite.Clear;
} else if ("xor".equals(type)) {
this.ac = AlphaComposite.Xor;
}
return true;
}
void draw(Layer layer) {
layer.setComposite((Composite)this.ac);
}
java.awt.Shape createAWTShape() {
return null;
}
}
private static class Ellipse
extends Rect {
static final String TOKEN = "ellipse";
private Ellipse() {
super();
}
java.awt.Shape createAWTShape() {
return new Ellipse2D.Double(this.x, this.y, this.width, this.height);
}
}
private static class Poly
extends Rect {
static final String TOKEN = "poly";
Polygon polygon;
private Poly() {
super();
}
boolean parse(Parser parser) {
this.polygon = new Polygon();
int i = 0;
while (parser.nextTokenIsInt()) {
Integer x = parser.nextIntToken();
if (!parser.nextTokenIsInt()) break;
Integer y = parser.nextIntToken();
this.polygon.addPoint(x, y);
++i;
}
this.parseFillAndStroke(parser);
return true;
}
java.awt.Shape createAWTShape() {
return this.polygon;
}
}
private static class RoundRect
extends Rect {
static final String TOKEN = "roundrect";
float roundness;
private RoundRect() {
super();
}
boolean parse(Parser parser) {
if (!this.parsePos(parser)) {
return false;
}
if (!this.parseSize(parser)) {
return false;
}
if (!parser.nextTokenIsInt()) {
return false;
}
this.roundness = parser.nextIntToken().intValue();
this.parseFillAndStroke(parser);
return true;
}
java.awt.Shape createAWTShape() {
return new RoundRectangle2D.Double(this.x, this.y, this.width, this.height, this.roundness, this.roundness);
}
void draw(Layer layer) {
super.draw(layer);
}
}
private static class Rect
extends Shape {
static final String TOKEN = "rect";
int width;
int height;
private Rect() {
super();
}
boolean parse(Parser parser) {
if (!this.parsePos(parser)) {
return false;
}
if (!this.parseSize(parser)) {
return false;
}
this.parseFillAndStroke(parser);
return true;
}
boolean parseSize(Parser parser) {
if (!parser.nextTokenIsInt()) {
return false;
}
this.width = parser.nextIntToken();
if (!parser.nextTokenIsInt()) {
return false;
}
this.height = parser.nextIntToken();
return true;
}
java.awt.Shape createAWTShape() {
return new Rectangle2D.Double(this.x, this.y, this.width, this.height);
}
}
private static class Line
extends Shape {
static final String TOKEN = "line";
int x2;
int y2;
private Line() {
super();
}
boolean parse(Parser parser) {
if (!this.parsePos(parser)) {
return false;
}
if (!this.parseEndPos(parser)) {
return false;
}
this.parseFillAndStroke(parser);
return true;
}
boolean parseEndPos(Parser parser) {
if (!parser.nextTokenIsInt()) {
return false;
}
this.x2 = parser.nextIntToken();
if (!parser.nextTokenIsInt()) {
return false;
}
this.y2 = parser.nextIntToken();
return true;
}
java.awt.Shape createAWTShape() {
return new Line2D.Double(this.x, this.y, this.x2, this.y2);
}
}
private static class Stroke {
static final String TOKEN = "S";
float strokeWidth;
Color color;
private Stroke() {
}
boolean parse(Parser parser) {
if (!"S".equals(parser.peekNextToken())) {
return false;
}
parser.nextToken();
if (!parser.nextTokenIsInt()) {
return false;
}
this.strokeWidth = (float)parser.nextIntToken().intValue() / 10.0f;
String colorStr = parser.nextToken();
if (colorStr == null) {
return false;
}
this.color = ImageSpriteServlet.getColor(colorStr);
return true;
}
void apply(Layer layer) {
layer.setPaint((Paint)this.color);
layer.setStroke((java.awt.Stroke)new BasicStroke(this.strokeWidth, 2, 2));
}
}
private static class Fill {
static final String TOKEN = "F";
Color color;
GradientPaint gradientPaint;
private Fill() {
}
boolean parse(Parser parser) {
if (!"F".equals(parser.peekNextToken())) {
return false;
}
parser.nextToken();
String colorStr = parser.nextToken();
if (colorStr == null) {
return false;
}
this.color = ImageSpriteServlet.getColor(colorStr);
if (parser.nextTokenIsInt()) {
Integer x1 = parser.nextIntToken();
if (!parser.nextTokenIsInt()) {
return true;
}
Integer y1 = parser.nextIntToken();
colorStr = parser.nextToken();
if (colorStr == null) {
return false;
}
Color color2 = ImageSpriteServlet.getColor(colorStr);
if (color2 == null) {
return true;
}
if (!parser.nextTokenIsInt()) {
return true;
}
Integer x2 = parser.nextIntToken();
if (!parser.nextTokenIsInt()) {
return true;
}
Integer y2 = parser.nextIntToken();
this.gradientPaint = new GradientPaint(x1.intValue(), y1.intValue(), this.color, x2.intValue(), y2.intValue(), color2);
}
return true;
}
void apply(Layer layer) {
if (this.gradientPaint != null) {
layer.setPaint((Paint)this.gradientPaint);
} else {
layer.setPaint((Paint)this.color);
}
}
}
private static abstract class Shape {
int x;
int y;
Fill fill;
Stroke stroke;
boolean strokeBeforeFill = false;
private Shape() {
}
abstract boolean parse(Parser var1);
boolean parsePos(Parser parser) {
if (!parser.nextTokenIsInt()) {
return false;
}
this.x = parser.nextIntToken();
if (!parser.nextTokenIsInt()) {
return false;
}
this.y = parser.nextIntToken();
return true;
}
void parseFillAndStroke(Parser parser) {
if ("F".equals(parser.peekNextToken())) {
Stroke stroke;
this.strokeBeforeFill = false;
Fill fill = new Fill();
if (fill.parse(parser)) {
this.fill = fill;
}
if ((stroke = new Stroke()).parse(parser)) {
this.stroke = stroke;
}
} else if ("S".equals(parser.peekNextToken())) {
Fill fill;
this.strokeBeforeFill = true;
Stroke stroke = new Stroke();
if (stroke.parse(parser)) {
this.stroke = stroke;
}
if ((fill = new Fill()).parse(parser)) {
this.fill = fill;
}
}
}
boolean hasFill() {
return this.fill != null;
}
boolean hasStroke() {
return this.stroke != null;
}
void draw(Layer layer) {
java.awt.Shape awtShape = this.createAWTShape();
if (this.strokeBeforeFill) {
if (this.hasStroke()) {
this.stroke.apply(layer);
layer.draw(awtShape);
}
if (this.hasFill()) {
this.fill.apply(layer);
layer.fill(awtShape);
}
} else {
if (this.hasFill()) {
this.fill.apply(layer);
layer.fill(awtShape);
}
if (this.hasStroke()) {
this.stroke.apply(layer);
layer.draw(awtShape);
}
}
}
abstract java.awt.Shape createAWTShape();
}
private static class Parser {
private final String[] selectors;
private int pos = 0;
public Parser(String[] selectors) {
this.selectors = selectors;
}
String peekNextToken() {
if (this.pos >= this.selectors.length) {
return null;
}
return this.selectors[this.pos];
}
String nextToken() {
if (this.pos >= this.selectors.length) {
return null;
}
return this.selectors[this.pos++];
}
boolean nextTokenIsInt() {
return this.getInt(this.peekNextToken()) != null;
}
Integer nextIntToken() {
String token = this.nextToken();
if (token == null) {
return null;
}
return this.getInt(token);
}
private Integer getInt(String str) {
if (str == null) {
return null;
}
try {
return Integer.valueOf(str);
}
catch (NumberFormatException e) {
return null;
}
}
Shape nextShape() {
String type = this.nextToken();
if (type == null) {
return null;
}
Shape shape = null;
if ("line".equals(type)) {
shape = new Line();
} else if ("rect".equals(type)) {
shape = new Rect();
} else if ("roundrect".equals(type)) {
shape = new RoundRect();
} else if ("poly".equals(type)) {
shape = new Poly();
} else if ("ellipse".equals(type)) {
shape = new Ellipse();
} else if ("ac".equals(type)) {
shape = new AlphaComp();
}
if (shape == null) {
return null;
}
if (!shape.parse(this)) {
return null;
}
return shape;
}
}
}