Scene7ConfigServlet.java
6.52 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.servlet.ServletException
* 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.PersistableValueMap
* 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.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.dam.scene7.impl.servlets;
import com.day.cq.dam.scene7.api.S7Config;
import com.day.cq.dam.scene7.api.S7ConfigResolver;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
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.PersistableValueMap;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(label="%cq.scene7.servlet.name", description="%cq.scene7.servlet.description")
@Service
@Properties(value={@Property(name="sling.servlet.resourceTypes", value={"dam/components/scene7/scene7page"}), @Property(name="sling.servlet.selectors", value={"config"}), @Property(name="sling.servlet.extensions", value={"json"}), @Property(name="sling.servlet.methods", value={"GET", "POST"})})
public class Scene7ConfigServlet
extends SlingAllMethodsServlet {
private static final Logger LOG = LoggerFactory.getLogger(Scene7ConfigServlet.class);
@Reference
protected S7ConfigResolver s7configResolver;
private static final String PROP = "prop";
private static final String SET_DEFAULT_CONFIG_PARAM = "setDefault";
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
block7 : {
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
Resource resource = request.getResource();
S7Config s7Config = null;
JSONObject json = new JSONObject();
try {
if (resource != null) {
String prop;
String configPath = resource.getPath();
configPath = configPath.replaceAll("/jcr:content$", "");
s7Config = this.s7configResolver.getS7Config(resource.getResourceResolver(), configPath);
if (s7Config != null && (prop = request.getParameter("prop")) != null) {
String propertyValue = s7Config.get(prop);
json.put(prop, (Object)propertyValue);
}
break block7;
}
LOG.warn("Could not find the config path parameter on the request!");
}
catch (JSONException e) {
LOG.error("Error adding element to the JSON response", (Throwable)e);
}
finally {
out.write(json.toString());
}
}
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
JSONObject json = new JSONObject();
try {
Resource resource;
ResourceResolver resolver = request.getResourceResolver();
String setDefaultConfig = request.getParameter("setDefault");
if (StringUtils.isNotEmpty((String)setDefaultConfig) && (resource = request.getResource()) != null) {
String configPath = resource.getPath();
configPath = configPath.replaceAll("/jcr:content$", "");
S7Config defaultConfig = this.s7configResolver.getS7Config(resource.getResourceResolver(), configPath);
List<S7Config> s7CloudConfigurations = this.s7configResolver.getS7Configurations(resource.getResourceResolver(), "/etc/cloudservices/scene7");
for (S7Config s7Config : s7CloudConfigurations) {
if (s7Config.equals(defaultConfig) || !s7Config.isDefault()) continue;
String s7PageConfig = s7Config.getCloudConfigPath() + "/" + "jcr:content";
Resource configResource = resolver.getResource(s7PageConfig);
if (configResource != null) {
PersistableValueMap properties = (PersistableValueMap)configResource.adaptTo(PersistableValueMap.class);
properties.put((Object)"defaultConfiguration", (Object)false);
properties.save();
break;
}
LOG.error("Cannot find page content for " + s7PageConfig);
break;
}
}
json.put("status", (Object)"ok");
}
catch (JSONException e) {
LOG.error("Error adding element to the JSON response", (Throwable)e);
}
finally {
out.write(json.toString());
}
}
protected void bindS7configResolver(S7ConfigResolver s7ConfigResolver) {
this.s7configResolver = s7ConfigResolver;
}
protected void unbindS7configResolver(S7ConfigResolver s7ConfigResolver) {
if (this.s7configResolver == s7ConfigResolver) {
this.s7configResolver = null;
}
}
}