FacetListServlet.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
168
169
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.wcm.api.Page
* com.day.cq.wcm.api.PageManager
* com.day.cq.wcm.webservicesupport.Configuration
* com.day.cq.wcm.webservicesupport.ConfigurationManager
* com.day.cq.wcm.webservicesupport.ConfigurationManagerFactory
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Value
* javax.servlet.ServletException
* 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.apache.sling.commons.json.io.JSONWriter
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.searchpromote.impl.servlet;
import com.day.cq.searchpromote.SearchPromoteService;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.webservicesupport.Configuration;
import com.day.cq.wcm.webservicesupport.ConfigurationManager;
import com.day.cq.wcm.webservicesupport.ConfigurationManagerFactory;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Value;
import javax.servlet.ServletException;
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.apache.sling.commons.json.io.JSONWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=0)
@Service
@Properties(value={@Property(name="sling.servlet.methods", value={"GET"}), @Property(name="sling.servlet.selectors", value={"facetlist"}), @Property(name="sling.servlet.extensions", value={"json"}), @Property(name="sling.servlet.resourceTypes", value={"cq/searchpromote/components/facet"})})
public class FacetListServlet
extends SlingAllMethodsServlet {
private final Logger log;
@Reference
private ConfigurationManagerFactory configManagerFactory;
@Reference
private SearchPromoteService searchpromote;
public FacetListServlet() {
this.log = LoggerFactory.getLogger(this.getClass());
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
Configuration config = null;
try {
config = this.getConfiguration(request, "search-promote");
}
catch (Exception e) {
this.log.error("Could not get configuration", (Throwable)e);
}
String memberId = (String)config.get("memberid", (Object)"");
String accountNo = (String)config.get("accountno", (Object)"");
PrintWriter out = response.getWriter();
JSONWriter writer = new JSONWriter((Writer)out);
try {
String facetList = this.searchpromote.getFacetList(memberId, accountNo);
JSONObject facets = new JSONObject(facetList);
out.write(facets.toString());
}
catch (Exception e) {
this.log.error(e.getMessage(), (Throwable)e);
try {
writer.object();
writer.endObject();
}
catch (JSONException ignored) {
this.log.error(ignored.getMessage(), (Throwable)ignored);
}
}
finally {
out.flush();
}
}
private Configuration getConfiguration(SlingHttpServletRequest request, String servicename) throws RepositoryException {
String pagePath = request.getResource().getPath();
if (pagePath.indexOf("/jcr:content") > -1) {
pagePath = pagePath.substring(0, pagePath.indexOf("/jcr:content"));
}
ResourceResolver resolver = request.getResource().getResourceResolver();
PageManager pageManager = (PageManager)resolver.adaptTo(PageManager.class);
Page currentPage = pageManager.getPage(pagePath);
ConfigurationManager configManager = this.configManagerFactory.getConfigurationManager(resolver);
Resource configRes = configManager.getConfigurationResource(currentPage.getContentResource());
if (configRes != null) {
String configPath = this.getConfigurationPath(configRes, servicename);
return configManager.getConfiguration(configPath);
}
return null;
}
private String getConfigurationPath(Resource resource, String servicename) throws RepositoryException {
Resource cloudconfigsContent = resource.getChild("jcr:content");
Node cloudconfigsNode = (Node)cloudconfigsContent.adaptTo(Node.class);
javax.jcr.Property prop = cloudconfigsNode.getProperty("cq:cloudserviceconfigs");
String configPath = null;
if (prop.isMultiple()) {
for (Value val : prop.getValues()) {
String path = val.getString();
if (path == null || path.indexOf(servicename) <= -1) continue;
configPath = path;
}
} else {
String path = prop.getValue().getString();
if (path != null && path.indexOf(servicename) > -1) {
configPath = path;
}
}
return configPath;
}
protected void bindConfigManagerFactory(ConfigurationManagerFactory configurationManagerFactory) {
this.configManagerFactory = configurationManagerFactory;
}
protected void unbindConfigManagerFactory(ConfigurationManagerFactory configurationManagerFactory) {
if (this.configManagerFactory == configurationManagerFactory) {
this.configManagerFactory = null;
}
}
protected void bindSearchpromote(SearchPromoteService searchPromoteService) {
this.searchpromote = searchPromoteService;
}
protected void unbindSearchpromote(SearchPromoteService searchPromoteService) {
if (this.searchpromote == searchPromoteService) {
this.searchpromote = null;
}
}
}