ProfileImages.java
5.76 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.wcm.api.designer.Design
* com.day.cq.wcm.api.designer.Designer
* com.day.cq.wcm.api.designer.Style
* com.day.cq.wcm.commons.AbstractImageServlet
* com.day.cq.wcm.commons.AbstractImageServlet$ImageContext
* com.day.image.Layer
* com.day.text.Text
* 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.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
*/
package com.day.cq.wcm.foundation.profile.impl;
import com.day.cq.wcm.api.designer.Design;
import com.day.cq.wcm.api.designer.Designer;
import com.day.cq.wcm.api.designer.Style;
import com.day.cq.wcm.commons.AbstractImageServlet;
import com.day.cq.wcm.foundation.Image;
import com.day.image.Layer;
import com.day.text.Text;
import java.awt.Color;
import java.awt.Paint;
import java.io.IOException;
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.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
@Component(metatype=0)
@Service(value={Servlet.class})
@Properties(value={@Property(name="sling.servlet.resourceTypes", value={"nt:file"}), @Property(name="sling.servlet.extensions", value={"jpg", "png", "gif"}), @Property(name="sling.servlet.selectors", value={"prof", "prof.thumbnail"})})
public class ProfileImages
extends AbstractImageServlet {
private static final int ICON_WIDTH = 48;
private static final int ICON_HEIGHT = 48;
private static final String THUMBNAIL = "thumbnail";
protected Layer createLayer(AbstractImageServlet.ImageContext c) throws RepositoryException, IOException {
Image image = new Image(c.resource.getResourceResolver().getResource(c.resource, ".."));
image.setItemName("file", Text.getName((String)c.resource.getPath()));
if (!image.hasContent()) {
return null;
}
String[] selectors = c.request.getRequestPathInfo().getSelectors();
int width = 0;
try {
width = Integer.parseInt(selectors[2]);
}
catch (Exception e) {
// empty catch block
}
if (width == 0) {
width = 48;
}
int height = width;
if (selectors.length > 1 && "thumbnail".equals(selectors[1])) {
float ratio;
Layer org = image.getLayer(false, false, false);
int w = org.getWidth();
int h = org.getHeight();
if (h < height && w < width) {
org.setX((int)Math.floor((width - w) / 2));
org.setY((int)Math.floor((height - h) / 2));
} else if (h == w && width == height) {
org.resize(width, height);
} else if (h > w) {
ratio = (float)h / (float)height;
w = (int)Math.floor((float)w / ratio);
org.resize(w, height);
org.setX((int)Math.floor((width - w) / 2));
} else {
ratio = (float)w / (float)width;
h = (int)Math.floor((float)h / ratio);
org.resize(width, h);
org.setY((int)Math.floor((height - h) / 2));
}
Layer outLayer = new Layer(width, height, (Paint)org.getBackgroundColor());
outLayer.setTransparency(org.getBackgroundColor());
outLayer.merge(org);
return outLayer;
}
Style style = this.getStyle(c);
if (style != null) {
image.loadStyleData(style);
}
return image.getLayer(false, true, false);
}
protected void writeLayer(SlingHttpServletRequest request, SlingHttpServletResponse response, AbstractImageServlet.ImageContext context, Layer layer) throws IOException, RepositoryException {
if (layer == null) {
response.sendError(404);
} else {
super.writeLayer(request, response, context, layer);
}
}
private Style getStyle(AbstractImageServlet.ImageContext c) {
String designId = "";
String cellPath = "";
String suffix = c.request.getRequestPathInfo().getSuffix();
if (suffix != null && suffix.length() > 0) {
int idx;
if (!suffix.startsWith("/")) {
suffix = "/" + suffix;
}
if ((idx = suffix.indexOf("/-/")) >= 0) {
designId = suffix.substring(0, idx);
cellPath = suffix.substring(idx + 3);
} else {
designId = suffix;
}
}
Design design = null;
if (designId.length() > 0) {
Designer d = (Designer)c.resolver.adaptTo(Designer.class);
design = d.getDesign(designId);
}
if (design == null) {
return null;
}
if (cellPath.length() > 0) {
return design.getStyle(cellPath);
}
return design.getStyle(Text.getName((String)c.resource.getPath()));
}
protected String getImageType(String ext) {
if ("res".equals(ext)) {
return "res";
}
return super.getImageType(ext);
}
}