TranslationSupportServlet.java
12.8 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.cq.projects.api.Project
* com.adobe.cq.projects.api.ProjectLink
* com.adobe.granite.translation.api.TranslationConfig
* com.adobe.granite.translation.api.TranslationException
* com.adobe.granite.translation.api.TranslationManager
* com.adobe.granite.xss.XSSAPI
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.servlet.ServletException
* org.apache.commons.lang3.StringUtils
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.sling.SlingServlet
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.SlingHttpServletResponse
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.api.servlets.SlingAllMethodsServlet
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.io.JSONWriter
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.wcm.translation.impl.servlet;
import com.adobe.cq.projects.api.Project;
import com.adobe.cq.projects.api.ProjectLink;
import com.adobe.cq.wcm.translation.impl.TranslationPodImpl;
import com.adobe.granite.translation.api.TranslationConfig;
import com.adobe.granite.translation.api.TranslationException;
import com.adobe.granite.translation.api.TranslationManager;
import com.adobe.granite.xss.XSSAPI;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.servlet.ServletException;
import org.apache.commons.lang3.StringUtils;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
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.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.io.JSONWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@SlingServlet(metatype=0, methods={"GET", "POST"}, resourceTypes={"cq/gui/components/projects/admin/translation"})
public class TranslationSupportServlet
extends SlingAllMethodsServlet {
private static final Logger LOGGER = LoggerFactory.getLogger(TranslationSupportServlet.class);
private static final String PROJECTS = "projects";
private static final String LANGUAGES = "languages";
private static final String TRANSLATION_JOB_PATH = "translationJobPath";
private static final String OPERATION = "operation";
private static final String CLEAR_ERROR = "clearError";
private static final String PROJECT_TITLE_END_STRING = " [";
@Reference
private XSSAPI xssAPI;
@Reference
private TranslationConfig translationConfig;
@Reference
private TranslationManager translationManager;
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
LOGGER.trace("In function: doGet");
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
JSONWriter writer = new JSONWriter((Writer)response.getWriter());
ResourceResolver resolver = request.getResourceResolver();
try {
String strGetLanguages;
writer.object();
String strGetProjects = request.getParameter("projects");
if (strGetProjects != null && strGetProjects.equalsIgnoreCase("true")) {
this.writeTranslationProjectsToJson(resolver, writer);
}
if ((strGetLanguages = request.getParameter("languages")) != null && strGetLanguages.equalsIgnoreCase("true")) {
this.writeSupportedLanguagesToJson(writer);
}
writer.endObject();
}
catch (Exception e) {
LOGGER.error("Failed to get translation projects and language info", (Throwable)e);
response.setStatus(500);
}
}
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
ResourceResolver resourceResolver = request.getResourceResolver();
String strOperation = request.getParameter("operation");
String translationJobPath = request.getParameter("translationJobPath");
if (!StringUtils.isEmpty((CharSequence)translationJobPath) && StringUtils.equals((CharSequence)strOperation, (CharSequence)"clearError")) {
try {
Resource translationJobResource = resourceResolver.resolve(translationJobPath);
ProjectLink link = (ProjectLink)translationJobResource.adaptTo(ProjectLink.class);
Project project = link.getProject();
Node translationJobNode = (Node)translationJobResource.adaptTo(Node.class);
Session userSession = (Session)resourceResolver.adaptTo(Session.class);
TranslationPodImpl translationPod = new TranslationPodImpl(translationJobNode, translationJobResource, resourceResolver, project, userSession, this.translationManager);
translationPod.setWorkflowOperationError(TranslationPodImpl.WorkflowOperationError.NO_ERROR);
userSession.save();
}
catch (Exception e) {
LOGGER.error("Failed to clear error", (Throwable)e);
response.setStatus(500);
}
}
}
private boolean isProject(Resource resource) {
ValueMap properties = (ValueMap)resource.adaptTo(ValueMap.class);
String silngResourceType = (String)properties.get("sling:resourceType", String.class);
return StringUtils.equals((CharSequence)silngResourceType, (CharSequence)"cq/gui/components/projects/admin/card/projectcard");
}
private boolean isTranslationProject(Resource resource) {
LOGGER.trace("In function: isTranslationProject with {}", (Object)resource.getName());
if (this.isProject(resource)) {
try {
Node jcrContent = ((Node)resource.adaptTo(Node.class)).getNode("{http://www.jcp.org/jcr/1.0}content");
Property property = jcrContent.getProperty("cq:template");
if (property == null) {
return false;
}
return StringUtils.equals((CharSequence)property.getString(), (CharSequence)"/libs/cq/core/content/projects/templates/create-translation-project");
}
catch (RepositoryException e) {
return false;
}
}
return false;
}
private void writeTranslationProjectDetailsToJson(Resource project, JSONWriter writer) {
LOGGER.trace("In function: writeTranslationProjectDetailsToJson");
try {
Resource jcrContent = project.getChild("jcr:content");
ValueMap properties = (ValueMap)jcrContent.adaptTo(ValueMap.class);
String title = (String)properties.get("jcr:title", String.class);
int titleEndIndex = title.lastIndexOf(" [");
if (titleEndIndex > 0) {
title = title.substring(0, titleEndIndex);
}
String path = project.getPath();
String sourceLanguage = (String)properties.get("sourceLanguage", String.class);
String destinationLanguage = (String)properties.get("destinationLanguage", String.class);
String translationMethod = (String)properties.get("translationMethod", String.class);
String translationProvider = (String)properties.get("translationProvider", String.class);
String description = (String)properties.get("jcr:description", String.class);
String contentCategory = (String)properties.get("contentCategory", String.class);
Boolean active = (Boolean)properties.get("active", Boolean.class);
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Title: {}", (Object)title);
LOGGER.trace("Path: {}", (Object)path);
LOGGER.trace("Source Language: {}", (Object)sourceLanguage);
LOGGER.trace("Destination Language: {}", (Object)destinationLanguage);
LOGGER.trace("Translation Method: {}", (Object)translationMethod);
LOGGER.trace("Translation Provider: {}", (Object)translationProvider);
LOGGER.trace("Description: {}", (Object)description);
LOGGER.trace("Description: {}", (Object)contentCategory);
LOGGER.trace("Status: {}", (Object)(active != false ? "Active" : "Inactive"));
}
writer.object();
writer.key("title").value((Object)title);
writer.key("path").value((Object)path);
writer.key("sourceLanguage").value((Object)sourceLanguage);
writer.key("destinationLanguage").value((Object)destinationLanguage);
writer.key("translationMethod").value((Object)translationMethod);
writer.key("translationProvider").value((Object)translationProvider);
writer.key("description").value((Object)description);
writer.key("contentCategory").value((Object)contentCategory);
writer.key("active").value((Object)active);
writer.endObject();
}
catch (Exception e) {
LOGGER.trace("Exception in reading translation project", (Throwable)e);
}
}
private boolean isSlingOrderedFolder(Resource resource) {
ValueMap props = (ValueMap)resource.adaptTo(ValueMap.class);
if (props == null) {
return false;
}
String resourceType = resource.getResourceType();
return StringUtils.equals((CharSequence)resourceType, (CharSequence)"sling:OrderedFolder");
}
private void writeTranslationProjectsToJson(ResourceResolver resolver, JSONWriter writer) throws JSONException {
LOGGER.trace("In function: writeTranslationProjectsToJson");
Resource projects = resolver.getResource("/content/projects");
writer.key("projects");
writer.array();
this.writeAllTranslationProjectsToJson(projects, writer);
writer.endArray();
}
private void writeAllTranslationProjectsToJson(Resource parent, JSONWriter writer) {
Iterable children = parent.getChildren();
if (children != null) {
for (Resource child : children) {
LOGGER.trace("{}", (Object)child.getPath());
if (this.isTranslationProject(child)) {
LOGGER.trace("Translation project found");
this.writeTranslationProjectDetailsToJson(child, writer);
continue;
}
LOGGER.trace("NOT a translation project");
if (!this.isSlingOrderedFolder(child)) continue;
this.writeAllTranslationProjectsToJson(child, writer);
}
}
}
private void writeSupportedLanguagesToJson(JSONWriter writer) throws TranslationException, JSONException {
LOGGER.trace("In function: writeSupportedLanguagesToJson");
Map languagesUnsorted = this.translationConfig.getLanguages();
TreeMap languagesSorted = new TreeMap(languagesUnsorted);
Set languageEntries = languagesSorted.entrySet();
writer.key("languages");
writer.array();
for (Map.Entry entry : languageEntries) {
writer.object();
writer.key("code").value(entry.getKey());
writer.key("title").value(entry.getValue());
writer.key("display").value((Object)(entry.getValue() + " (" + entry.getKey() + ")"));
writer.endObject();
}
writer.endArray();
}
protected void bindXssAPI(XSSAPI xSSAPI) {
this.xssAPI = xSSAPI;
}
protected void unbindXssAPI(XSSAPI xSSAPI) {
if (this.xssAPI == xSSAPI) {
this.xssAPI = null;
}
}
protected void bindTranslationConfig(TranslationConfig translationConfig) {
this.translationConfig = translationConfig;
}
protected void unbindTranslationConfig(TranslationConfig translationConfig) {
if (this.translationConfig == translationConfig) {
this.translationConfig = null;
}
}
protected void bindTranslationManager(TranslationManager translationManager) {
this.translationManager = translationManager;
}
protected void unbindTranslationManager(TranslationManager translationManager) {
if (this.translationManager == translationManager) {
this.translationManager = null;
}
}
}