OmniSearchSavedSearchServlet.java
14.5 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
280
281
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.i18n.I18n
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.servlet.Servlet
* javax.servlet.ServletException
* javax.servlet.http.HttpServletRequest
* javax.servlet.http.HttpServletResponse
* 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.resource.ValueMap
* org.apache.sling.api.servlets.SlingAllMethodsServlet
* org.apache.sling.commons.json.JSONArray
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.JSONObject
* org.apache.sling.servlets.post.HtmlResponse
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.granite.omnisearch.impl.servlets;
import com.adobe.granite.omnisearch.api.core.OmniSearchException;
import com.adobe.granite.omnisearch.api.core.OmniSearchService;
import com.day.cq.i18n.I18n;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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.resource.ValueMap;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.apache.sling.servlets.post.HtmlResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(immediate=1, metatype=0)
@Service(value={Servlet.class})
@Properties(value={@Property(name="sling.servlet.paths", value={"/libs/granite/omnisearch/savedsearch"})})
public class OmniSearchSavedSearchServlet
extends SlingAllMethodsServlet {
private static final Logger log = LoggerFactory.getLogger(OmniSearchSavedSearchServlet.class);
static final String COMMAND = ":operation";
static final String LIST_COMMAND = "list";
static final String DETAIL_COMMAND = "detail";
static final String UPDATE_COMMAND = "update";
static final String CREATE_COMMAND = "create";
static final String DELETE_COMMAND = "delete";
static final String LIMIT = "limit";
static final String OFFSET = "offset";
static final String RESOURCE_PATH = "resourcePath";
@Reference
private OmniSearchService omnisearchService;
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
if (this.checkRequireParameter(request, response)) {
String location = request.getParameter("location");
String command = request.getParameter(":operation");
long limit = 0;
if (request.getParameter("limit") != null) {
limit = Long.valueOf(request.getParameter("limit"));
}
long offset = 0;
if (request.getParameter("offset") != null) {
offset = Long.valueOf(request.getParameter("offset"));
}
if (command.equals("list")) {
Writer out = this.getJsonOutputWriter(response);
try {
Iterator<Resource> savedSearchList = this.omnisearchService.getSavedSearches(request.getResourceResolver(), location, limit, offset);
out.write(this.getJsonSavedSearches(savedSearchList, request.getResourceResolver()).toString());
}
catch (RepositoryException re) {
log.error("Error while accessing resource path of saved search", (Throwable)re);
this.createSatusResponse(500, I18n.get((HttpServletRequest)request, (String)"Error while accessing saved search list"), "", response);
}
catch (JSONException je) {
log.error("Error while updating json object ", (Throwable)je);
this.createSatusResponse(500, I18n.get((HttpServletRequest)request, (String)"Error while accessing saved search list"), "", response);
}
catch (OmniSearchException se) {
log.error("Error while extracting list of saved search for location {}", (Object)location, (Object)se);
this.createSatusResponse(500, I18n.getVar((HttpServletRequest)request, (String)se.getMessage()), "", response);
}
} else if (command.equals("detail")) {
Resource resource;
String resourcePath = request.getParameter("resourcePath");
if (resourcePath == null) {
this.createSatusResponse(400, I18n.get((HttpServletRequest)request, (String)"Resource path can not be empty"), "", response);
}
if ((resource = request.getResourceResolver().getResource(resourcePath)) == null) {
this.createSatusResponse(400, I18n.get((HttpServletRequest)request, (String)"Error while extracting the detail of saved search"), "", response);
}
ResourceResolver resolver = request.getResourceResolver();
Writer out = this.getJsonOutputWriter(response);
try {
Map<String, String> savedSearchDetail = this.omnisearchService.getSavedSearchParameters(resolver, location, resourcePath);
if (savedSearchDetail.isEmpty()) {
this.createSatusResponse(400, I18n.get((HttpServletRequest)request, (String)"No Saved Search exist on the given path {0}", (String)resourcePath), resourcePath, response);
}
JSONObject resultObj = this.getJsonSavedSearchDetails(savedSearchDetail);
Session session = (Session)resolver.adaptTo(Session.class);
boolean canEdit = false;
if (session.hasPermission(resourcePath, "set_property")) {
canEdit = true;
}
resultObj.put("canEdit", canEdit);
out.write(resultObj.toString());
}
catch (RepositoryException re) {
log.error("Error while accessing resource path of saved search", (Throwable)re);
this.createSatusResponse(500, I18n.get((HttpServletRequest)request, (String)"Error while access resource Path {0}", (String)resourcePath), resourcePath, response);
}
catch (JSONException je) {
log.error("Error while updating json object ", (Throwable)je);
this.createSatusResponse(500, I18n.get((HttpServletRequest)request, (String)"Error while access resource Path {0}", (String)resourcePath), resourcePath, response);
}
catch (OmniSearchException se) {
log.error("Error while extracting detail of saved search", (Throwable)se);
this.createSatusResponse(500, I18n.getVar((HttpServletRequest)request, (String)se.getMessage()), "", response);
}
} else {
this.createSatusResponse(400, I18n.get((HttpServletRequest)request, (String)"operation {0} does not exist", (String)command), "", response);
}
}
}
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
block14 : {
if (this.checkRequireParameter(request, response)) {
String command = request.getParameter(":operation");
String resourcePath = request.getParameter("resourcePath");
Resource resource = null;
if (!command.equals("create")) {
if (resourcePath == null) {
this.createSatusResponse(400, I18n.get((HttpServletRequest)request, (String)"Resource path can not be empty"), "", response);
}
if ((resource = request.getResourceResolver().getResource(resourcePath)) == null) {
this.createSatusResponse(400, I18n.get((HttpServletRequest)request, (String)"Error while extracting the deleting of saved search"), "", response);
return;
}
}
if (command.equals("update") || command.equals("create")) {
try {
resource = this.omnisearchService.createOrUpdateSavedSearch(request.getResourceResolver(), request.getParameterMap());
if (resource != null) {
this.createSatusResponse(200, I18n.get((HttpServletRequest)request, (String)"Saved Search Added"), resource.getPath(), response);
break block14;
}
this.createSatusResponse(400, I18n.get((HttpServletRequest)request, (String)"Error while adding the saved search"), resourcePath, response);
}
catch (OmniSearchException se) {
log.error("Error while creating or updating saved search", (Throwable)se);
this.createSatusResponse(500, I18n.getVar((HttpServletRequest)request, (String)se.getMessage()), resourcePath, response);
}
} else if (command.equals("delete")) {
String location = request.getParameter("location");
try {
boolean result = this.omnisearchService.deleteSavedSearch(request.getResourceResolver(), location, resourcePath);
if (!result) break block14;
if (result) {
this.createSatusResponse(200, I18n.get((HttpServletRequest)request, (String)"Saved Search Deleted"), resourcePath, response);
break block14;
}
this.createSatusResponse(400, I18n.get((HttpServletRequest)request, (String)"Error while deleting the saved search"), resourcePath, response);
}
catch (OmniSearchException se) {
log.error("Error while deleting saved search", (Throwable)se);
this.createSatusResponse(500, I18n.getVar((HttpServletRequest)request, (String)se.getMessage()), resourcePath, response);
}
} else {
this.createSatusResponse(400, I18n.get((HttpServletRequest)request, (String)"Operation {0} does not exist", (String)command), "", response);
}
}
}
}
private Writer getJsonOutputWriter(SlingHttpServletResponse response) throws IOException {
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
return out;
}
private JSONArray getJsonSavedSearches(Iterator<Resource> savedSearchResult, ResourceResolver resolver) throws RepositoryException, JSONException {
Session session = (Session)resolver.adaptTo(Session.class);
JSONArray resultArray = new JSONArray();
while (savedSearchResult.hasNext()) {
Resource savedSearchResource = savedSearchResult.next();
ValueMap vm = savedSearchResource.getValueMap();
String resourceTitle = (String)vm.get("jcr:title", String.class);
String resourcePath = savedSearchResource.getPath();
boolean canDelete = false;
if (session.hasPermission(resourcePath, "remove")) {
canDelete = true;
}
JSONObject resourceObj = new JSONObject();
resourceObj.put("title", (Object)resourceTitle);
resourceObj.put("path", (Object)resourcePath);
resourceObj.put("canDelete", canDelete);
resultArray.put((Object)resourceObj);
}
return resultArray;
}
private JSONObject getJsonSavedSearchDetails(Map<String, String> savedSearchDetail) throws JSONException {
JSONObject resultJsonObj = new JSONObject();
for (String key : savedSearchDetail.keySet()) {
resultJsonObj.put(key, (Object)savedSearchDetail.get(key));
}
return resultJsonObj;
}
private boolean checkRequireParameter(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
if (request.getParameter("location") == null) {
this.createSatusResponse(400, "Missing Parameter 'location'", "", response);
return false;
}
if (request.getParameter(":operation") == null) {
this.createSatusResponse(400, "Missing Parameter ':operation'", "", response);
return false;
}
return true;
}
private void createSatusResponse(int status, String message, String path, SlingHttpServletResponse response) throws IOException {
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
HtmlResponse htmlResponse = new HtmlResponse();
if (path != null) {
htmlResponse.setPath(path);
htmlResponse.setLocation(path);
}
htmlResponse.setStatus(status, message);
if (status < 400) {
htmlResponse.setTitle("OK");
} else {
htmlResponse.setTitle(" Error");
}
htmlResponse.send((HttpServletResponse)response, true);
}
protected void bindOmnisearchService(OmniSearchService omniSearchService) {
this.omnisearchService = omniSearchService;
}
protected void unbindOmnisearchService(OmniSearchService omniSearchService) {
if (this.omnisearchService == omniSearchService) {
this.omnisearchService = null;
}
}
}