FormConstraintsServlet.java
4.74 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.commons.servlets.AbstractPredicateServlet
* com.day.cq.wcm.foundation.forms.FormsManager
* com.day.cq.wcm.foundation.forms.FormsManager$ComponentDescription
* javax.servlet.ServletException
* org.apache.commons.collections.Predicate
* 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.ResourceResolver
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.io.JSONWriter
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.mcm.campaign.servlets;
import com.day.cq.commons.servlets.AbstractPredicateServlet;
import com.day.cq.mcm.campaign.impl.IntegrationConfig;
import com.day.cq.wcm.foundation.forms.FormsManager;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import org.apache.commons.collections.Predicate;
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.ResourceResolver;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.io.JSONWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@SlingServlet(paths={"/libs/mcm/campaign/content/formConstraints"}, extensions={"json"}, methods={"GET"})
public class FormConstraintsServlet
extends AbstractPredicateServlet {
private final Logger logger;
@Reference
private IntegrationConfig integrationConfig;
public FormConstraintsServlet() {
this.logger = LoggerFactory.getLogger(this.getClass());
}
protected void doGet(SlingHttpServletRequest req, SlingHttpServletResponse resp, Predicate predicate) throws ServletException, IOException {
String type = req.getParameter("type");
if (type == null) {
this.logger.error("Missing 'type' parameter.");
resp.sendError(500, "Missing 'type' parameter");
return;
}
try {
JSONWriter w = new JSONWriter((Writer)resp.getWriter());
resp.setContentType("application/json");
resp.setCharacterEncoding("utf-8");
this.writeConstraints(w, type, req);
}
catch (Exception e) {
this.logger.error("Error while generating JSON list", (Throwable)e);
resp.sendError(500, e.toString());
}
}
private void writeJson(Iterator<FormsManager.ComponentDescription> descIter, JSONWriter w, boolean writeEmpty) throws JSONException {
w.array();
if (writeEmpty) {
w.object();
w.key("value").value((Object)"");
w.key("text").value((Object)"None");
w.endObject();
}
while (descIter.hasNext()) {
FormsManager.ComponentDescription desc = descIter.next();
w.object();
w.key("value");
w.value((Object)desc.getResourceType());
w.key("text");
w.value((Object)desc.getTitle());
if (desc.getHint() != null) {
w.key("qtip");
w.value((Object)desc.getHint());
}
w.endObject();
}
w.endArray();
}
private void writeConstraints(JSONWriter w, String type, SlingHttpServletRequest req) throws JSONException {
List<String> validConstraints = this.integrationConfig.getFormConstraints(type);
FormsManager formsManager = (FormsManager)req.getResourceResolver().adaptTo(FormsManager.class);
Iterator descriptions = formsManager.getConstraints();
ArrayList<FormsManager.ComponentDescription> constraints = new ArrayList<FormsManager.ComponentDescription>();
while (descriptions.hasNext()) {
FormsManager.ComponentDescription toCheck = (FormsManager.ComponentDescription)descriptions.next();
String resourceType = toCheck.getResourceType();
if (!validConstraints.contains(resourceType)) continue;
constraints.add(toCheck);
}
this.writeJson(constraints.iterator(), w, true);
}
protected void bindIntegrationConfig(IntegrationConfig integrationConfig) {
this.integrationConfig = integrationConfig;
}
protected void unbindIntegrationConfig(IntegrationConfig integrationConfig) {
if (this.integrationConfig == integrationConfig) {
this.integrationConfig = null;
}
}
}