1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.query.solr.internal; |
21 |
|
|
22 |
|
import java.lang.reflect.Array; |
23 |
|
import java.util.ArrayList; |
24 |
|
import java.util.Arrays; |
25 |
|
import java.util.List; |
26 |
|
import java.util.Map.Entry; |
27 |
|
|
28 |
|
import javax.inject.Inject; |
29 |
|
import javax.inject.Named; |
30 |
|
import javax.inject.Provider; |
31 |
|
import javax.inject.Singleton; |
32 |
|
|
33 |
|
import org.apache.commons.lang.StringUtils; |
34 |
|
import org.apache.solr.client.solrj.SolrQuery; |
35 |
|
import org.apache.solr.client.solrj.response.QueryResponse; |
36 |
|
import org.apache.solr.common.SolrDocument; |
37 |
|
import org.apache.solr.common.SolrDocumentList; |
38 |
|
import org.slf4j.Logger; |
39 |
|
import org.xwiki.bridge.DocumentAccessBridge; |
40 |
|
import org.xwiki.component.annotation.Component; |
41 |
|
import org.xwiki.job.event.status.JobProgressManager; |
42 |
|
import org.xwiki.model.reference.DocumentReference; |
43 |
|
import org.xwiki.model.reference.DocumentReferenceResolver; |
44 |
|
import org.xwiki.query.Query; |
45 |
|
import org.xwiki.query.QueryException; |
46 |
|
import org.xwiki.query.QueryExecutor; |
47 |
|
import org.xwiki.query.SecureQuery; |
48 |
|
import org.xwiki.search.solr.internal.api.SolrInstance; |
49 |
|
|
50 |
|
import com.xpn.xwiki.XWikiContext; |
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
@link |
56 |
|
@link |
57 |
|
|
58 |
|
@version |
59 |
|
@since |
60 |
|
|
61 |
|
@Component |
62 |
|
@Named(SolrQueryExecutor.SOLR) |
63 |
|
@Singleton |
|
|
| 91.1% |
Uncovered Elements: 7 (79) |
Complexity: 22 |
Complexity Density: 0.41 |
|
64 |
|
public class SolrQueryExecutor implements QueryExecutor |
65 |
|
{ |
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
public static final String SOLR = "solr"; |
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
private static final String PARAM_SUPPORTED_LOCALES = "xwiki.supportedLocales"; |
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
@Inject |
81 |
|
protected Logger logger; |
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
@Inject |
87 |
|
protected DocumentAccessBridge documentAccessBridge; |
88 |
|
|
89 |
|
|
90 |
|
@link |
91 |
|
|
92 |
|
@Inject |
93 |
|
protected Provider<SolrInstance> solrInstanceProvider; |
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
@Inject |
99 |
|
private Provider<XWikiContext> xcontextProvider; |
100 |
|
|
101 |
|
|
102 |
|
@link@link |
103 |
|
|
104 |
|
@Inject |
105 |
|
private DocumentReferenceResolver<SolrDocument> solrDocumentReferenceResolver; |
106 |
|
|
107 |
|
@Inject |
108 |
|
private JobProgressManager progress; |
109 |
|
|
|
|
| 85.7% |
Uncovered Elements: 3 (21) |
Complexity: 7 |
Complexity Density: 0.41 |
|
110 |
52 |
@Override... |
111 |
|
public <T> List<T> execute(Query query) throws QueryException |
112 |
|
{ |
113 |
|
|
114 |
52 |
if (query instanceof SecureQuery && ((SecureQuery) query).isCurrentAuthorChecked() |
115 |
|
&& !this.documentAccessBridge.hasProgrammingRights()) { |
116 |
0 |
throw new QueryException("Solr query require programming right", query, null); |
117 |
|
} |
118 |
|
|
119 |
52 |
this.progress.startStep(query, "query.solr.progress.execute", "Execute Solr query [{}]", query); |
120 |
52 |
this.progress.pushLevelProgress(3, query); |
121 |
|
|
122 |
52 |
try { |
123 |
52 |
this.progress.startStep(query, "query.solr.progress.execute.prepare", "Prepare"); |
124 |
|
|
125 |
52 |
SolrInstance solrInstance = solrInstanceProvider.get(); |
126 |
52 |
SolrQuery solrQuery = createSolrQuery(query); |
127 |
|
|
128 |
52 |
this.progress.startStep(query, "query.solr.progress.execute.execute", "Execute"); |
129 |
|
|
130 |
52 |
QueryResponse response = solrInstance.query(solrQuery); |
131 |
|
|
132 |
52 |
this.progress.startStep(query, "query.solr.progress.execute.filter", "Filter"); |
133 |
|
|
134 |
|
|
135 |
|
|
136 |
|
|
137 |
|
|
138 |
|
|
139 |
|
|
140 |
|
|
141 |
52 |
if (!(query instanceof SecureQuery) || ((SecureQuery) query).isCurrentUserChecked()) { |
142 |
3 |
this.filterResponse(response); |
143 |
|
} |
144 |
|
|
145 |
52 |
return (List<T>) Arrays.asList(response); |
146 |
|
} catch (Exception e) { |
147 |
0 |
throw new QueryException("Exception while executing query", query, e); |
148 |
|
} finally { |
149 |
52 |
this.progress.popLevelProgress(query); |
150 |
52 |
this.progress.endStep(query); |
151 |
|
} |
152 |
|
} |
153 |
|
|
|
|
| 96.2% |
Uncovered Elements: 1 (26) |
Complexity: 7 |
Complexity Density: 0.44 |
|
154 |
52 |
private SolrQuery createSolrQuery(Query query)... |
155 |
|
{ |
156 |
52 |
SolrQuery solrQuery = new SolrQuery(query.getStatement()); |
157 |
|
|
158 |
|
|
159 |
|
|
160 |
52 |
if (query.getOffset() > 0) { |
161 |
1 |
solrQuery.setStart(query.getOffset()); |
162 |
|
} |
163 |
52 |
if (query.getLimit() > 0) { |
164 |
43 |
solrQuery.setRows(query.getLimit()); |
165 |
|
} |
166 |
|
|
167 |
|
|
168 |
|
|
169 |
52 |
for (Entry<String, Object> entry : query.getNamedParameters().entrySet()) { |
170 |
200 |
Object value = entry.getValue(); |
171 |
|
|
172 |
200 |
if (value instanceof Iterable) { |
173 |
99 |
solrQuery.set(entry.getKey(), toStringArray((Iterable) value)); |
174 |
101 |
} else if (value != null && value.getClass().isArray()) { |
175 |
2 |
solrQuery.set(entry.getKey(), toStringArray(value)); |
176 |
|
} else { |
177 |
99 |
solrQuery.set(entry.getKey(), String.valueOf(value)); |
178 |
|
} |
179 |
|
} |
180 |
|
|
181 |
|
|
182 |
|
|
183 |
|
|
184 |
52 |
if (!solrQuery.getParameterNames().contains(PARAM_SUPPORTED_LOCALES)) { |
185 |
52 |
XWikiContext xcontext = this.xcontextProvider.get(); |
186 |
52 |
solrQuery.set(PARAM_SUPPORTED_LOCALES, |
187 |
|
StringUtils.join(xcontext.getWiki().getAvailableLocales(xcontext), ",")); |
188 |
|
} |
189 |
|
|
190 |
52 |
return solrQuery; |
191 |
|
} |
192 |
|
|
193 |
|
|
194 |
|
|
195 |
|
|
196 |
|
@param |
197 |
|
@return |
198 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
199 |
2 |
private String[] toStringArray(Object array)... |
200 |
|
{ |
201 |
2 |
int length = Array.getLength(array); |
202 |
2 |
String[] args = new String[length]; |
203 |
6 |
for (int i = 0; i < length; i++) { |
204 |
4 |
args[i] = String.valueOf(Array.get(array, i)); |
205 |
|
} |
206 |
|
|
207 |
2 |
return args; |
208 |
|
} |
209 |
|
|
210 |
|
|
211 |
|
|
212 |
|
|
213 |
|
@param |
214 |
|
@return |
215 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
216 |
99 |
private String[] toStringArray(Iterable iterable)... |
217 |
|
{ |
218 |
99 |
List<String> args = new ArrayList<String>(); |
219 |
99 |
for (Object obj : iterable) { |
220 |
152 |
args.add(String.valueOf(obj)); |
221 |
|
} |
222 |
99 |
return args.toArray(new String[args.size()]); |
223 |
|
} |
224 |
|
|
225 |
|
|
226 |
|
|
227 |
|
|
228 |
|
@param |
229 |
|
|
|
|
| 81.2% |
Uncovered Elements: 3 (16) |
Complexity: 5 |
Complexity Density: 0.42 |
|
230 |
3 |
protected void filterResponse(QueryResponse response)... |
231 |
|
{ |
232 |
3 |
SolrDocumentList results = response.getResults(); |
233 |
3 |
long numFound = results.getNumFound(); |
234 |
|
|
235 |
|
|
236 |
3 |
for (SolrDocument result : new ArrayList<SolrDocument>(results)) { |
237 |
5 |
try { |
238 |
5 |
DocumentReference resultDocumentReference = this.solrDocumentReferenceResolver.resolve(result); |
239 |
|
|
240 |
5 |
if (!documentAccessBridge.exists(resultDocumentReference) |
241 |
|
|| !documentAccessBridge.isDocumentViewable(resultDocumentReference)) { |
242 |
|
|
243 |
|
|
244 |
2 |
results.remove(result); |
245 |
|
|
246 |
|
|
247 |
2 |
numFound--; |
248 |
|
|
249 |
|
|
250 |
|
|
251 |
|
|
252 |
|
|
253 |
|
} |
254 |
|
} catch (Exception e) { |
255 |
0 |
this.logger.warn("Skipping bad result: {}", result, e); |
256 |
|
} |
257 |
|
} |
258 |
|
|
259 |
|
|
260 |
3 |
if (numFound < 0) { |
261 |
|
|
262 |
0 |
numFound = 0; |
263 |
|
} |
264 |
3 |
results.setNumFound(numFound); |
265 |
|
} |
266 |
|
} |