Scene7FlashTemplatesServiceImpl.java
14.2 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.commons.io.IOUtils
* org.apache.commons.lang.StringUtils
* org.apache.felix.scr.annotations.Activate
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Modified
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.http.HttpEntity
* org.apache.http.HttpResponse
* org.apache.http.client.methods.HttpGet
* org.apache.http.client.methods.HttpUriRequest
* org.apache.http.impl.client.CloseableHttpClient
* org.apache.http.impl.client.HttpClientBuilder
* org.apache.http.impl.client.HttpClients
* org.apache.http.osgi.services.HttpClientBuilderFactory
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.JSONObject
* org.apache.sling.commons.json.xml.XML
* org.apache.sling.commons.osgi.OsgiUtil
* org.osgi.service.component.ComponentContext
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.dam.scene7.impl;
import com.day.cq.dam.scene7.api.Scene7FlashTemplatesService;
import com.day.cq.dam.scene7.api.constants.FlashTemplateAssetConstants;
import java.io.InputStream;
import java.util.Dictionary;
import java.util.Iterator;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Modified;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.osgi.services.HttpClientBuilderFactory;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.apache.sling.commons.json.xml.XML;
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="%cq.dam.scene7.flashtemplatesservice.name", description="%cq.dam.scene7.flashtemplatesservice.description")
@Service
@Properties(value={@Property(name="scene7FlashTemplates.rti", value={"rti"}, label="%cq.dam.scene7.flashtemplateservice.rti.label", description="%cq.dam.scene7.flashtemplateservice.rti.description"), @Property(name="scene7FlashTemplates.rsi", value={"rsi"}, label="%cq.dam.scene7.flashtemplateservice.rsi.label", description="%cq.dam.scene7.flashtemplateservice.rsi.description"), @Property(name="scene7FlashTemplates.rb", value={"rb"}, label="%cq.dam.scene7.flashtemplateservice.rb.label", description="%cq.dam.scene7.flashtemplateservice.rb.description"), @Property(name="scene7FlashTemplates.rurl", value={"rurl"}, label="%cq.dam.scene7.flashtemplateservice.rurl.label", description="%cq.dam.scene7.flashtemplateservice.rurl.description"), @Property(name="scene7FlashTemplate.urlFormatParameter", value={"fmt"}, label="%cq.dam.scene7.flashtemplateservice.urlFormatParameter.label", description="%cq.dam.scene7.flashtemplateservice.urlFormatParameter.description")})
public class Scene7FlashTemplatesServiceImpl
implements Scene7FlashTemplatesService {
private static final Logger LOG = LoggerFactory.getLogger(Scene7FlashTemplatesServiceImpl.class);
@Reference
private HttpClientBuilderFactory httpClientBuilderFactory;
private String rti;
private String rsi;
private String rb;
private String rurl;
private String formatParameter;
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
@Override
public JSONObject getFlashTemplateAssets(Resource resource, FlashTemplateAssetConstants assetType) {
JSONObject json;
block14 : {
json = new JSONObject();
ResourceResolver rr = null;
if (resource != null) {
try {
rr = resource.getResourceResolver();
ValueMap resourceProperties = (ValueMap)resource.adaptTo(ValueMap.class);
if (resourceProperties == null) break block14;
String fileReference = (String)resourceProperties.get("fileReference", String.class);
if (fileReference != null) {
InputStream is;
HttpEntity entity;
HttpGet request;
HttpResponse response;
StringBuffer sb = new StringBuffer(fileReference);
sb.append("?req=").append(FlashTemplateAssetConstants.CONTENTS.getAssetType());
CloseableHttpClient httpClient = null;
if (this.httpClientBuilderFactory != null && this.httpClientBuilderFactory.newBuilder() != null) {
HttpClientBuilder httpClientBuilder = this.httpClientBuilderFactory.newBuilder();
httpClient = httpClientBuilder.build();
} else {
httpClient = HttpClients.custom().build();
}
if (assetType != null && !assetType.equals(FlashTemplateAssetConstants.CONTENTS.getAssetType())) {
sb.append(",").append(assetType.getAssetType()).toString();
}
if ((is = (entity = (response = httpClient.execute((HttpUriRequest)(request = new HttpGet(sb.toString())))).getEntity()).getContent()) != null) {
String responseBody = IOUtils.toString((InputStream)is);
try {
json = XML.toJSONObject((String)responseBody);
}
catch (JSONException je) {
LOG.error("Unable to parse JSON response for Flash template asset: {}", (Object)je.getLocalizedMessage());
}
}
break block14;
}
LOG.error("Could not find the {} property on the resource located at {}", (Object)"fileReference", (Object)resource.getPath());
}
catch (Exception e) {
LOG.error("Error encountered while parsing XML to JSON", (Throwable)e);
}
finally {
if (rr != null) {
rr.close();
}
}
}
}
return json;
}
@Override
public String getFlashTemplateModifyURL(Resource resource) {
String url;
StringBuilder sb = new StringBuilder();
if (resource != null) {
ValueMap resourceProperties = (ValueMap)resource.adaptTo(ValueMap.class);
String fileReference = (String)resourceProperties.get("fileReference", String.class);
String manualModifiers = (String)resourceProperties.get("manualModifiers", String.class);
String outputType = (String)resourceProperties.get("outputType", String.class);
if (fileReference != null) {
sb.append(fileReference);
Resource modifiers = resource.getChild("modifiers");
if (modifiers != null) {
sb.append("?");
Iterator childrenIterator = modifiers.listChildren();
while (childrenIterator.hasNext()) {
Resource modifierNode = (Resource)childrenIterator.next();
Iterator modifiersIterator = modifierNode.listChildren();
while (modifiersIterator.hasNext()) {
Resource modifier = (Resource)modifiersIterator.next();
ValueMap modifierProperties = (ValueMap)modifier.adaptTo(ValueMap.class);
String key = (String)modifierProperties.get((Object)"key");
if (key == null) {
LOG.warn("Could not find a \"key\" property for the {} modifier for resource at {}", (Object)modifier.getPath(), (Object)resource.getPath());
continue;
}
String[] replace = (String[])modifierProperties.get("replace", String[].class);
if (replace == null) {
LOG.warn("Could not find a \"replace\" property for the {} modifier for resource at {}", (Object)modifier.getPath(), (Object)resource.getPath());
continue;
}
if (modifierNode.getName().equals("text")) {
if ("_all_".equals(key)) {
sb.append(this.rti).append("(").append(replace[0]).append(")&");
continue;
}
if (replace.length == 1) {
String replaceValue = replace[0];
String[] split = replaceValue.split("=");
if (split.length == 1) {
sb.append(this.rti).append(".").append(key).append("=").append(split[0]).append("&");
continue;
}
if (split.length != 2) continue;
sb.append(this.rti).append(".").append(key).append("(").append(replace[0]).append(")").append("&");
continue;
}
sb.append(this.rti).append(".").append(key).append("(");
for (int i = 0; i < replace.length; ++i) {
sb.append(replace[i]);
if (replace.length <= 1 || i >= replace.length - 1) continue;
sb.append(";");
}
sb.append(")&");
continue;
}
if (modifierNode.getName().equals("symbols")) {
sb.append(this.rsi).append("(").append(key).append("=").append(replace[0]).append(")&");
continue;
}
if (modifierNode.getName().equals("bitmaps")) {
sb.append(this.rb).append(".").append(key).append("=").append(replace[0]).append("&");
continue;
}
if (!modifierNode.getName().equals("urls")) continue;
sb.append(this.rurl).append("(").append(key).append("=").append(replace[0]).append(")&");
}
}
}
if (StringUtils.isNotEmpty((String)manualModifiers)) {
if (sb.indexOf("?") == -1) {
sb.append("?");
}
if (sb.lastIndexOf("&") > 0 && sb.lastIndexOf("&") < sb.length() - 1) {
sb.append("&");
}
sb.append(manualModifiers);
}
if (sb.indexOf("?") == -1) {
sb.append("?");
}
if (sb.lastIndexOf("&") > 0 && sb.lastIndexOf("&") < sb.length() - 1) {
sb.append("&");
}
sb.append(this.formatParameter).append("=");
if (StringUtils.isNotEmpty((String)outputType)) {
sb.append(outputType);
} else {
sb.append("swf");
}
}
}
if ((url = sb.toString()).endsWith("&")) {
url = url.substring(0, url.length() - 1);
}
return url;
}
@Override
public boolean renderAsHTML(Resource resource) {
ValueMap resourceProperties;
String outputType;
boolean renderAsHTML = false;
if (resource != null && StringUtils.isNotBlank((String)(outputType = (String)(resourceProperties = (ValueMap)resource.adaptTo(ValueMap.class)).get("outputType", String.class))) && !"swf".equals(outputType)) {
renderAsHTML = true;
}
return renderAsHTML;
}
@Activate
@Modified
protected void activate(ComponentContext context) {
Dictionary properties = context.getProperties();
this.rti = OsgiUtil.toString(properties.get("scene7FlashTemplates.rti"), (String)"rti");
this.rsi = OsgiUtil.toString(properties.get("scene7FlashTemplates.rsi"), (String)"rsi");
this.rb = OsgiUtil.toString(properties.get("scene7FlashTemplates.rb"), (String)"rb");
this.rurl = OsgiUtil.toString(properties.get("scene7FlashTemplates.rurl"), (String)"rurl");
this.formatParameter = OsgiUtil.toString(properties.get("scene7FlashTemplate.urlFormatParameter"), (String)"fmt");
}
protected void bindHttpClientBuilderFactory(HttpClientBuilderFactory httpClientBuilderFactory) {
this.httpClientBuilderFactory = httpClientBuilderFactory;
}
protected void unbindHttpClientBuilderFactory(HttpClientBuilderFactory httpClientBuilderFactory) {
if (this.httpClientBuilderFactory == httpClientBuilderFactory) {
this.httpClientBuilderFactory = null;
}
}
}