AdaptiveImageComponentServlet.java
6.03 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.wcm.api.designer.Style
* com.day.cq.wcm.commons.AbstractImageServlet
* com.day.cq.wcm.commons.AbstractImageServlet$ImageContext
* com.day.image.Layer
* javax.jcr.RepositoryException
* 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.commons.osgi.OsgiUtil
* org.osgi.service.component.ComponentContext
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.wcm.foundation.impl;
import com.day.cq.wcm.api.designer.Style;
import com.day.cq.wcm.commons.AbstractImageServlet;
import com.day.cq.wcm.foundation.AdaptiveImageHelper;
import com.day.cq.wcm.foundation.Image;
import com.day.image.Layer;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Dictionary;
import java.util.Iterator;
import java.util.List;
import javax.jcr.RepositoryException;
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.commons.osgi.OsgiUtil;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=1, label="Adobe CQ Adaptive Image Component Servlet", description="Render adaptive images in a variety of qualities")
@Service
@Properties(value={@Property(name="sling.servlet.resourceTypes", value={"foundation/components/adaptiveimage"}, propertyPrivate=1), @Property(name="sling.servlet.selectors", value={"img"}, propertyPrivate=1), @Property(name="sling.servlet.extensions", value={"jpg", "jpeg", "png", "gif"}, propertyPrivate=1)})
public class AdaptiveImageComponentServlet
extends AbstractImageServlet {
private static final Logger log = LoggerFactory.getLogger(AdaptiveImageComponentServlet.class);
private static final long serialVersionUID = 42;
private static final String FULL_SIZE_SELECTOR = "full";
@Property(value={"320", "480", "476", "620"}, label="Supported Widths", description="List of widths this component is permitted to generate.")
private static final String PROPERTY_SUPPORTED_WIDTHS = "adapt.supported.widths";
private List<String> supportedWidths;
protected void activate(ComponentContext componentContext) {
Dictionary properties = componentContext.getProperties();
this.supportedWidths = new ArrayList<String>();
String[] supportedWidthsArray = OsgiUtil.toStringArray(properties.get("adapt.supported.widths"));
if (supportedWidthsArray != null && supportedWidthsArray.length > 0) {
for (String width : supportedWidthsArray) {
this.supportedWidths.add(width);
}
}
}
protected Layer createLayer(AbstractImageServlet.ImageContext imageContext) throws RepositoryException, IOException {
SlingHttpServletRequest request = imageContext.request;
String[] selectors = request.getRequestPathInfo().getSelectors();
if (selectors.length != 3 && selectors.length != 1) {
log.error("Unsupported number of selectors.");
return null;
}
String widthSelector = "full";
if (selectors.length == 3) {
widthSelector = selectors[1];
}
if (!this.isDimensionSupported(widthSelector)) {
log.error("Unsupported width requested: {}.", (Object)widthSelector);
return null;
}
Image image = new Image(imageContext.resource);
if (!image.hasContent()) {
log.error("The image associated with this page does not have a valid file reference; drawing a placeholder.");
return null;
}
AdaptiveImageHelper adaptiveHelper = new AdaptiveImageHelper();
if ("full".equals(widthSelector)) {
return adaptiveHelper.applyStyleDataToImage(image, imageContext.style);
}
return adaptiveHelper.scaleThisImage(image, Integer.parseInt(widthSelector), 0, imageContext.style);
}
protected boolean isDimensionSupported(String widthStr) {
Iterator<String> iterator = this.getSupportedWidthsIterator();
if ("full".equals(widthStr)) {
return true;
}
int width = Integer.parseInt(widthStr);
while (iterator.hasNext()) {
if (width != Integer.parseInt(iterator.next())) continue;
return true;
}
return false;
}
protected Iterator<String> getSupportedWidthsIterator() {
return this.supportedWidths.iterator();
}
protected void writeLayer(SlingHttpServletRequest request, SlingHttpServletResponse response, AbstractImageServlet.ImageContext context, Layer layer) throws IOException, RepositoryException {
double quality;
String[] selectors = request.getRequestPathInfo().getSelectors();
if (selectors.length == 3) {
String imageQualitySelector = selectors[2];
quality = this.getRequestedImageQuality(imageQualitySelector);
} else {
quality = this.getImageQuality();
}
this.writeLayer(request, response, context, layer, quality);
}
private double getRequestedImageQuality(String imageQualitySelector) {
AdaptiveImageHelper.Quality newQuality = AdaptiveImageHelper.getQualityFromString(imageQualitySelector);
if (newQuality != null) {
return newQuality.getQualityValue();
}
return this.getImageQuality();
}
protected String getImageType() {
return "image/jpeg";
}
}