OmniSearchSuggestionServlet.java
10.3 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.i18n.I18n
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.servlet.Servlet
* javax.servlet.ServletException
* javax.servlet.http.HttpServletRequest
* 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.SlingSafeMethodsServlet
* org.apache.sling.commons.json.JSONArray
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.JSONObject
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.granite.omnisearch.impl.servlets;
import com.adobe.granite.omnisearch.api.core.OmniSearchService;
import com.adobe.granite.omnisearch.api.suggestion.PredicateSuggestion;
import com.adobe.granite.omnisearch.api.suggestion.SuggestionResult;
import com.adobe.granite.omnisearch.spi.core.OmniSearchHandler;
import com.day.cq.i18n.I18n;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
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.SlingSafeMethodsServlet;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(immediate=1, metatype=0)
@Service(value={Servlet.class})
@Properties(value={@Property(name="sling.servlet.resourceTypes", value={"/libs/granite/omnisearch/components/suggest"})})
public class OmniSearchSuggestionServlet
extends SlingSafeMethodsServlet {
private static final Logger log = LoggerFactory.getLogger(OmniSearchSuggestionServlet.class);
private static final String FULLTEXT = "fulltext";
private static ConcurrentHashMap<String, OmniSearchHandler> handlerConcurrentHashMap = new ConcurrentHashMap();
@Reference
private OmniSearchService omniSearchService;
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
this.handleSuggestionQuery(request, response);
}
private Writer getJsonOutputWriter(SlingHttpServletResponse response) throws IOException {
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
return out;
}
private void handleSuggestionQuery(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
Map predicateParameters = request.getParameterMap();
String searchTerm = null;
String location = this.getLocation(predicateParameters);
if (predicateParameters.containsKey("fulltext")) {
String[] valueArray = (String[])predicateParameters.get("fulltext");
String string = searchTerm = valueArray.length == 0 ? null : valueArray[0];
}
if (searchTerm == null) {
log.warn("No query term has been provided. No suggestion require");
return;
}
ResourceResolver resourceResolver = request.getResourceResolver();
Map<String, SuggestionResult> suggestionResults = this.omniSearchService.getSuggestions(resourceResolver, new I18n((HttpServletRequest)request), searchTerm, location);
if (suggestionResults == null) {
log.debug("no suggestion obtained for search term {} and location {}", (Object)searchTerm, (Object)location);
return;
}
JSONObject result = new JSONObject();
JSONArray suggestionJsonList = new JSONArray();
HashSet<String> suggestionsSet = new HashSet<String>();
ArrayList<JSONObject> availableModules = new ArrayList<JSONObject>();
ArrayList<JSONObject> predicateSuggestionJsonList = new ArrayList<JSONObject>();
HashSet<String> spellCheckSuggestionJsonList = new HashSet<String>();
Writer out = this.getJsonOutputWriter(response);
try {
for (String key : suggestionResults.keySet()) {
List<String> spellcheckSuggestions;
List<PredicateSuggestion> predicateSuggestions;
List<String> suggestions;
SuggestionResult currSuggestionResult = suggestionResults.get(key);
Resource moduleConfigRes = this.omniSearchService.getModuleConfiguration(resourceResolver, key);
if (moduleConfigRes != null) {
Node contentNode = (Node)moduleConfigRes.adaptTo(Node.class);
JSONObject moduleDetails = new JSONObject();
if (contentNode.hasProperty("jcr:title")) {
moduleDetails.put("name", (Object)contentNode.getProperty("jcr:title").getString());
String contentNodePath = contentNode.getPath();
if (contentNodePath != null && contentNodePath.startsWith("/")) {
contentNodePath = request.getContextPath() + contentNodePath;
}
moduleDetails.put("contentNodePath", (Object)contentNodePath);
moduleDetails.put("id", (Object)key);
availableModules.add(moduleDetails);
}
}
if ((predicateSuggestions = currSuggestionResult.getPredicateSuggestions()) != null && !predicateSuggestions.isEmpty()) {
for (PredicateSuggestion predicateSuggestion : predicateSuggestions) {
Map<String, String> queryParameters;
JSONObject predicateDetails = new JSONObject();
predicateDetails.put("type", (Object)predicateSuggestion.getType());
predicateDetails.put("value", (Object)predicateSuggestion.getOptionTitle());
String typePath = predicateSuggestion.getTypePath();
String titlePath = predicateSuggestion.getOptionPath();
if (typePath != null && titlePath != null) {
predicateDetails.put("typePath", (Object)typePath);
predicateDetails.put("valuePath", (Object)titlePath);
}
if ((queryParameters = predicateSuggestion.getQueryParameters()) != null) {
JSONObject queryParametersJSON = new JSONObject();
for (String parameterKey : queryParameters.keySet()) {
queryParametersJSON.put(parameterKey, (Object)queryParameters.get(parameterKey));
}
predicateDetails.put("queryParameters", (Object)queryParametersJSON);
}
predicateSuggestionJsonList.add(predicateDetails);
}
}
if ((spellcheckSuggestions = currSuggestionResult.getSpellcheckSuggestions()) != null && !spellcheckSuggestions.isEmpty()) {
spellCheckSuggestionJsonList.addAll(spellcheckSuggestions);
}
if ((suggestions = currSuggestionResult.getSuggestions()) == null || suggestions.isEmpty()) continue;
for (String suggestion : suggestions) {
suggestionsSet.add(StringUtils.capitalize((String)suggestion.toLowerCase()));
}
}
if (!availableModules.isEmpty()) {
result.put("availableModules", availableModules);
}
if (!predicateSuggestionJsonList.isEmpty()) {
result.put("predicateSuggestions", predicateSuggestionJsonList);
}
if (!spellCheckSuggestionJsonList.isEmpty()) {
result.put("spellcheckSuggestion", spellCheckSuggestionJsonList);
}
if (!suggestionsSet.isEmpty()) {
for (String suggestion : suggestionsSet) {
JSONObject suggestionObj = new JSONObject();
suggestionObj.put("suggestion", (Object)suggestion);
suggestionJsonList.put((Object)suggestionObj);
}
result.put("suggestions", (Object)suggestionJsonList);
}
}
catch (RepositoryException e) {
log.error("Error while creating Json Response for Suggestion", (Throwable)e);
}
catch (JSONException e) {
log.error("Error while creating Json Response for Suggestion", (Throwable)e);
}
out.write(result.toString());
}
private String getLocation(Map<String, String[]> predicateParameters) {
String location = null;
if (predicateParameters.containsKey("location")) {
String[] valueArray = predicateParameters.get("location");
location = valueArray.length == 0 ? null : valueArray[0];
}
return location;
}
protected void bindOmniSearchService(OmniSearchService omniSearchService) {
this.omniSearchService = omniSearchService;
}
protected void unbindOmniSearchService(OmniSearchService omniSearchService) {
if (this.omniSearchService == omniSearchService) {
this.omniSearchService = null;
}
}
}