PageTranslationServlet.java
17.4 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.aemds.guide.service.GuideLocalizationService
* com.adobe.cq.launches.api.LaunchManagerFactory
* com.adobe.granite.translation.api.TranslationConstants
* com.adobe.granite.translation.api.TranslationConstants$ContentType
* com.adobe.granite.translation.api.TranslationConstants$TranslationMethod
* com.adobe.granite.translation.api.TranslationException
* com.adobe.granite.translation.api.TranslationManager
* com.adobe.granite.translation.api.TranslationResult
* com.adobe.granite.translation.api.TranslationService
* com.adobe.granite.translation.api.TranslationService$TranslationServiceInfo
* com.adobe.granite.translation.core.TranslationCloudConfigUtil
* com.adobe.granite.ui.clientlibs.HtmlLibraryManager
* com.day.cq.wcm.api.LanguageManager
* com.day.cq.wcm.api.Page
* com.day.cq.wcm.api.PageManagerFactory
* com.day.cq.wcm.contentsync.PageExporter
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Value
* javax.servlet.RequestDispatcher
* javax.servlet.Servlet
* javax.servlet.ServletRequest
* javax.servlet.ServletResponse
* org.apache.commons.lang3.StringUtils
* org.apache.felix.scr.annotations.Component
* 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.sling.api.SlingHttpServletRequest
* org.apache.sling.api.SlingHttpServletResponse
* org.apache.sling.api.request.RequestDispatcherOptions
* org.apache.sling.api.request.RequestPathInfo
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceResolverFactory
* org.apache.sling.api.servlets.SlingSafeMethodsServlet
* org.apache.sling.commons.html.HtmlParser
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.wcm.translation.impl.servlet;
import com.adobe.aemds.guide.service.GuideLocalizationService;
import com.adobe.cq.launches.api.LaunchManagerFactory;
import com.adobe.cq.wcm.translation.impl.TranslationRuleConfigurationFile;
import com.adobe.cq.wcm.translation.impl.TranslationUtils;
import com.adobe.cq.wcm.translation.impl.process.TranslateSitePrepareProcess;
import com.adobe.granite.translation.api.TranslationConstants;
import com.adobe.granite.translation.api.TranslationException;
import com.adobe.granite.translation.api.TranslationManager;
import com.adobe.granite.translation.api.TranslationResult;
import com.adobe.granite.translation.api.TranslationService;
import com.adobe.granite.translation.core.TranslationCloudConfigUtil;
import com.adobe.granite.ui.clientlibs.HtmlLibraryManager;
import com.day.cq.wcm.api.LanguageManager;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManagerFactory;
import com.day.cq.wcm.contentsync.PageExporter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Value;
import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Reference;
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.RequestDispatcherOptions;
import org.apache.sling.api.request.RequestPathInfo;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.apache.sling.commons.html.HtmlParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=1)
@Service(value={Servlet.class})
@Properties(value={@org.apache.felix.scr.annotations.Property(name="sling.servlet.resourceTypes", value={"sling/servlet/default"}), @org.apache.felix.scr.annotations.Property(name="sling.servlet.selectors", value={"translate"}), @org.apache.felix.scr.annotations.Property(name="sling.servlet.extensions", value={"html"}), @org.apache.felix.scr.annotations.Property(name="sling.servlet.methods", value={"GET"})})
public class PageTranslationServlet
extends SlingSafeMethodsServlet {
private static final String LANGUAGE_PREFIX = "language_";
private static final long serialVersionUID = 2421863758278649256L;
private static final Logger log = LoggerFactory.getLogger(PageTranslationServlet.class);
@Reference
private TranslationManager translationManager = null;
@Reference
private PageManagerFactory pageManagerFactory = null;
@Reference
private TranslationCloudConfigUtil translationCloudConfigUtil = null;
@Reference
private HtmlLibraryManager htmlLibraryManager = null;
@Reference
private ResourceResolverFactory resolverFactory;
@Reference
private LanguageManager languageManager;
@Reference
private PageExporter pageExporter = null;
@Reference
private HtmlParser htmlParser = null;
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) {
response.setContentType("text/html");
String[] selectors = request.getRequestPathInfo().getSelectors();
ResourceResolver resourceResolver = null;
try {
Page page;
Resource contentResource;
resourceResolver = this.resolverFactory.getServiceResourceResolver(Collections.singletonMap("sling.service.subservice", "translation-job"));
Resource resource = request.getResource();
String path = resource.getPath();
log.debug("Path is {}", (Object)path);
String targetLanguage = this.getTargetLanguage(selectors);
if (StringUtils.isEmpty((CharSequence)targetLanguage)) {
targetLanguage = TranslationUtils.getResourceLanguageRoot(resource);
}
if ((page = (Page)resource.adaptTo(Page.class)) != null && !StringUtils.isEmpty((CharSequence)targetLanguage) && (contentResource = page.getContentResource()) != null) {
TranslationService tsvc = null;
try {
tsvc = this.translationManager.createTranslationService(resource);
}
catch (Exception e) {
log.error("Error while creating translation service {}", (Throwable)e);
}
if (tsvc != null && tsvc.getTranslationServiceInfo().getSupportedTranslationMethod() == TranslationConstants.TranslationMethod.MACHINE_TRANSLATION) {
ArrayList<TranslationCache> cacheList = new ArrayList<TranslationCache>();
this.collectCacheForTranslation(contentResource, resourceResolver, cacheList);
this.translateCacheNow(cacheList, targetLanguage, tsvc);
}
}
RequestDispatcherOptions rdo = new RequestDispatcherOptions();
rdo.setReplaceSelectors("");
request.getRequestDispatcher(resource, rdo).include((ServletRequest)request, (ServletResponse)response);
}
catch (Exception e) {
log.error("Error in translation {}", (Throwable)e);
}
finally {
if (resourceResolver != null && resourceResolver.isLive()) {
resourceResolver.close();
}
}
}
private void translateCacheNow(ArrayList<TranslationCache> cacheList, String targetLanguage, TranslationService tSvc) {
String[] sourceStringArray = new String[cacheList.size()];
int index = 0;
for (TranslationCache cache : cacheList) {
sourceStringArray[index] = cache.strStringToTranslate;
++index;
}
String contentCategory = tSvc.getDefaultCategory();
String strSrcLang = null;
TranslationConstants.ContentType contentType = TranslationConstants.ContentType.HTML;
try {
TranslationResult[] translationResultArray = tSvc.translateArray(sourceStringArray, strSrcLang, targetLanguage, contentType, contentCategory);
if (translationResultArray != null && translationResultArray.length == cacheList.size()) {
for (index = 0; index < translationResultArray.length; ++index) {
TranslationResult result = translationResultArray[index];
TranslationCache cache2 = cacheList.get(index);
if (!result.getSourceString().equals(cache2.strStringToTranslate)) continue;
if (cache2.indexInMultiValue == -1) {
try {
cache2.property.setValue(result.getTranslation());
}
catch (RepositoryException e) {
log.error("Error in setValue single {}", (Throwable)e);
}
continue;
}
this.setPropertyMultipleValue(cache2.property, result.getTranslation(), cache2.indexInMultiValue);
}
} else {
log.error("Mismatch in translate String and Result");
}
}
catch (TranslationException e) {
log.error("Error in translateCacheNow {}", (Throwable)e);
}
}
private void setPropertyMultipleValue(Property prop, String translation, int indexInMultiValue) {
try {
Value[] val_array;
if (prop.isMultiple() && (val_array = prop.getValues()) != null && val_array.length > 0 && indexInMultiValue < val_array.length) {
String[] newVals = new String[val_array.length];
for (int index = 0; index < val_array.length; ++index) {
Value value = val_array[index];
newVals[index] = value.getString();
}
newVals[indexInMultiValue] = translation;
prop.setValue(newVals);
}
}
catch (RepositoryException ex) {
log.error("Error in setPropertyMultipleValue {}", (Throwable)ex);
}
}
private String getTargetLanguage(String[] selectors) {
String strRetVal = null;
if (selectors != null) {
for (String strSelector : selectors) {
if (strSelector == null || strSelector.indexOf("language_") == -1) continue;
strRetVal = strSelector.substring("language_".length());
break;
}
}
return strRetVal;
}
private ArrayList<String> getTranslatablePropertyList(Resource resource, ResourceResolver resourceResolver) {
String strSourceLanguage = "";
String strDestinationLanguage = "";
ArrayList<String> arrayList = new ArrayList<String>();
TranslationRuleConfigurationFile ruleFile = new TranslationRuleConfigurationFile(resourceResolver, TranslateSitePrepareProcess.getRulesFilePath(), strSourceLanguage, strDestinationLanguage, this.translationManager, this.translationCloudConfigUtil, this.pageManagerFactory, this.htmlLibraryManager, null, this.languageManager, null, this.pageExporter, this.htmlParser);
try {
arrayList = ruleFile.getTranslatablePropertyListForNode((Node)resource.adaptTo(Node.class));
}
catch (RepositoryException e) {
arrayList.add("Error while calculating getTranslatablePropertyList");
}
catch (IOException e) {
arrayList.add("Error while calculating getTranslatablePropertyList");
}
return arrayList;
}
private void collectCacheForTranslation(Resource resource, ResourceResolver resourceResolver, ArrayList<TranslationCache> cacheList) {
ArrayList<String> translatablePropertiesList;
Iterable childList;
if (resource != null && (translatablePropertiesList = this.getTranslatablePropertyList(resource, resourceResolver)) != null) {
Node node = (Node)resource.adaptTo(Node.class);
for (String strPropertyName : translatablePropertiesList) {
this.cacheProperty(node, strPropertyName, cacheList);
}
}
if ((childList = resource.getChildren()) != null) {
for (Resource child : childList) {
this.collectCacheForTranslation(child, resourceResolver, cacheList);
}
}
}
private void cacheProperty(Node node, String strPropertyName, ArrayList<TranslationCache> cacheList) {
try {
Property prop;
if (node.hasProperty(strPropertyName) && (prop = node.getProperty(strPropertyName)) != null) {
if (prop.isMultiple()) {
Value[] val_array = prop.getValues();
if (val_array != null && val_array.length > 0) {
for (int index = 0; index < val_array.length; ++index) {
this.appendValueToCache(prop, cacheList, val_array[index], index);
}
}
} else {
Value value = prop.getValue();
this.appendValueToCache(prop, cacheList, value, -1);
}
}
}
catch (Exception ex) {
log.error("Error while translating property {}", (Throwable)ex);
}
}
private void appendValueToCache(Property prop, ArrayList<TranslationCache> cacheList, Value value, int indexToMultiValue) throws RepositoryException {
if (value != null && value.getType() == 1) {
String strStringToTranslate = value.getString();
String strTrimmedString = strStringToTranslate;
if (strTrimmedString != null) {
strTrimmedString = strTrimmedString.trim();
}
if (strTrimmedString != null && !strTrimmedString.isEmpty()) {
TranslationCache cache = new TranslationCache(prop, value, strTrimmedString, indexToMultiValue);
cacheList.add(cache);
}
}
}
protected void bindTranslationManager(TranslationManager translationManager) {
this.translationManager = translationManager;
}
protected void unbindTranslationManager(TranslationManager translationManager) {
if (this.translationManager == translationManager) {
this.translationManager = null;
}
}
protected void bindPageManagerFactory(PageManagerFactory pageManagerFactory) {
this.pageManagerFactory = pageManagerFactory;
}
protected void unbindPageManagerFactory(PageManagerFactory pageManagerFactory) {
if (this.pageManagerFactory == pageManagerFactory) {
this.pageManagerFactory = null;
}
}
protected void bindTranslationCloudConfigUtil(TranslationCloudConfigUtil translationCloudConfigUtil) {
this.translationCloudConfigUtil = translationCloudConfigUtil;
}
protected void unbindTranslationCloudConfigUtil(TranslationCloudConfigUtil translationCloudConfigUtil) {
if (this.translationCloudConfigUtil == translationCloudConfigUtil) {
this.translationCloudConfigUtil = null;
}
}
protected void bindHtmlLibraryManager(HtmlLibraryManager htmlLibraryManager) {
this.htmlLibraryManager = htmlLibraryManager;
}
protected void unbindHtmlLibraryManager(HtmlLibraryManager htmlLibraryManager) {
if (this.htmlLibraryManager == htmlLibraryManager) {
this.htmlLibraryManager = null;
}
}
protected void bindResolverFactory(ResourceResolverFactory resourceResolverFactory) {
this.resolverFactory = resourceResolverFactory;
}
protected void unbindResolverFactory(ResourceResolverFactory resourceResolverFactory) {
if (this.resolverFactory == resourceResolverFactory) {
this.resolverFactory = null;
}
}
protected void bindLanguageManager(LanguageManager languageManager) {
this.languageManager = languageManager;
}
protected void unbindLanguageManager(LanguageManager languageManager) {
if (this.languageManager == languageManager) {
this.languageManager = null;
}
}
protected void bindPageExporter(PageExporter pageExporter) {
this.pageExporter = pageExporter;
}
protected void unbindPageExporter(PageExporter pageExporter) {
if (this.pageExporter == pageExporter) {
this.pageExporter = null;
}
}
protected void bindHtmlParser(HtmlParser htmlParser) {
this.htmlParser = htmlParser;
}
protected void unbindHtmlParser(HtmlParser htmlParser) {
if (this.htmlParser == htmlParser) {
this.htmlParser = null;
}
}
class TranslationCache {
public Property property;
public Value value;
public String strStringToTranslate;
public int indexInMultiValue;
TranslationCache(Property property, Value value, String strStringToTranslate, int indexInMultiValue) {
this.property = property;
this.value = value;
this.strStringToTranslate = strStringToTranslate;
this.indexInMultiValue = indexInMultiValue;
}
}
}