FormSelectServlet.java
6.83 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.aemds.guide.utils.GuideUtils
* com.adobe.granite.resourceresolverhelper.ResourceResolverHelper
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.Session
* javax.servlet.Servlet
* org.apache.commons.lang.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.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.servlets.SlingAllMethodsServlet
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.JSONObject
* org.osgi.service.component.ComponentContext
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.fd.workflow.dorGeneration.servlet;
import com.adobe.aemds.guide.utils.GuideUtils;
import com.adobe.granite.resourceresolverhelper.ResourceResolverHelper;
import java.io.PrintWriter;
import javax.jcr.Node;
import javax.jcr.Session;
import javax.servlet.Servlet;
import org.apache.commons.lang.StringUtils;
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.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.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=1, immediate=1, label="FormsSelectServlet", description="This servlet handles AJAX calls required for client side validations for AF to DOR generation step")
@Service(value={Servlet.class})
@Properties(value={@Property(name="sling.servlet.extensions", value={"json"}), @Property(name="sling.servlet.resourceTypes", value={"fd/workflow/formsSelectServlet"}), @Property(name="sling.servlet.methods", value={"GET"})})
public class FormSelectServlet
extends SlingAllMethodsServlet {
private static final long serialVersionUID = 4099381306123242894L;
private static final String IS_ADAPTIVE_FORM = "isAdaptiveForm";
private static final String IS_DOR_CONFIGURED = "isDorConfigured";
private static final String ADAPTIVE_FORM_PROPERTY = "guide";
private static final String OPERATION_LITERAL = "operation";
private static final String AF_PATH = "formPath";
private static final String ERROR_CODE_LITERAL = "code";
@Reference
ResourceResolverHelper resourceResolverHelper;
private final Logger log = LoggerFactory.getLogger(FormSelectServlet.class);
protected void activate(ComponentContext componentContext) throws Exception {
this.log.info("Activated: FormSelectServlet started");
}
protected void deactivate(ComponentContext context) {
this.log.info("Deactivated: FormSelectServlet stopped");
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public void doGet(SlingHttpServletRequest req, SlingHttpServletResponse resp) {
String operation = req.getParameter("operation");
JSONObject result = null;
PrintWriter writer = null;
try {
writer = resp.getWriter();
if ("isAdaptiveForm".equals(operation)) {
result = this.afDorHandler(req, resp);
} else if ("isDorConfigured".equals(operation)) {
result = this.afDorConfigurationHandler(req, resp);
}
writer.write(result.toString());
}
catch (Exception e) {
try {
resp.setStatus(500);
writer = resp.getWriter();
JSONObject errorJson = new JSONObject();
String code = e.getMessage();
errorJson.put("code", (Object)code);
this.log.error(errorJson.toString(), (Throwable)e);
writer.write(errorJson.toString());
}
catch (Exception ex) {
this.log.error(ex.getMessage(), (Throwable)ex);
}
finally {
if (writer != null) {
writer.close();
}
}
}
}
private JSONObject afDorHandler(SlingHttpServletRequest req, SlingHttpServletResponse resp) throws JSONException {
String formPath = req.getParameter("formPath");
JSONObject result = null;
if (StringUtils.isNotBlank((String)formPath)) {
boolean isAF = this.isAdaptiveForm((Session)req.getResourceResolver().adaptTo(Session.class), formPath);
result = new JSONObject();
result.put("isAdaptiveForm", isAF);
}
return result;
}
private JSONObject afDorConfigurationHandler(SlingHttpServletRequest req, SlingHttpServletResponse resp) throws JSONException {
JSONObject result = null;
boolean isDor = this.isDorConfigured(req);
result = new JSONObject();
result.put("isDorConfigured", isDor);
return result;
}
private boolean isAdaptiveForm(Session session, String afPath) {
String afContentPath = afPath + "/" + "jcr:content";
try {
Node afNode;
if (session.nodeExists(afContentPath) && (afNode = session.getNode(afContentPath)).hasProperty("guide") && afNode.getProperty("guide").getString().equals("1")) {
return true;
}
}
catch (Exception ex) {
this.log.error(ex.getMessage(), (Throwable)ex);
}
return false;
}
private boolean isDorConfigured(SlingHttpServletRequest req) {
String formPath = req.getParameter("formPath");
boolean isDorConfigured = false;
if (StringUtils.isNotBlank((String)formPath)) {
ResourceResolver resourceResolver = req.getResourceResolver();
isDorConfigured = GuideUtils.isDORConfigured((Resource)resourceResolver.getResource(formPath));
}
return isDorConfigured;
}
protected void bindResourceResolverHelper(ResourceResolverHelper resourceResolverHelper) {
this.resourceResolverHelper = resourceResolverHelper;
}
protected void unbindResourceResolverHelper(ResourceResolverHelper resourceResolverHelper) {
if (this.resourceResolverHelper == resourceResolverHelper) {
this.resourceResolverHelper = null;
}
}
}