1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springbyexample.httpclient.solr;
18
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.springbyexample.httpclient.HttpClientOxmTemplate;
28 import org.springbyexample.httpclient.HttpClientTemplate;
29 import org.springbyexample.httpclient.ResponseCallback;
30 import org.springframework.beans.factory.DisposableBean;
31 import org.springframework.beans.factory.InitializingBean;
32 import org.springframework.oxm.Marshaller;
33 import org.springframework.oxm.Unmarshaller;
34 import org.springframework.util.Assert;
35
36
37
38
39
40
41
42
43 public class SolrOxmClient<T> implements InitializingBean, DisposableBean {
44
45 final Logger logger = LoggerFactory.getLogger(SolrOxmClient.class);
46
47 public final static String SEARCH_QUERY_PARAM = "q";
48
49 protected final static String SELECT_URL_SUFFIX = "/select";
50 protected final static String UPDATE_URL_SUFFIX = "/update";
51
52 protected final static String DELETE_ELEMENT_NAME = "delete";
53 protected final static String DELETE_ID_ELEMENT_NAME = "id";
54 protected final static String DELETE_QUERY_ELEMENT_NAME = "query";
55
56 protected final static String COMMIT_ELEMENT_NAME = "commit";
57 protected final static String ROLLBACK_REQUEST = "<rollback/>";
58 protected final static String OPTIMIZE_ELEMENT_NAME = "optimize";
59
60 protected HttpClientOxmTemplate<List<T>> selectTemplate = null;
61 protected HttpClientOxmTemplate<List<T>> updateTemplate = null;
62 protected HttpClientTemplate postTemplate = null;
63
64 protected String baseUrl = null;
65 protected Marshaller marshaller = null;
66 protected Unmarshaller unmarshaller = null;
67
68
69
70
71 public String getBaseUrl() {
72 return baseUrl;
73 }
74
75
76
77
78 public void setBaseUrl(String baseUrl) {
79 this.baseUrl = baseUrl;
80 }
81
82
83
84
85 protected String getSelectUrl() {
86 return baseUrl + SELECT_URL_SUFFIX;
87 }
88
89
90
91
92 protected String getUpdateUrl() {
93 return baseUrl + UPDATE_URL_SUFFIX;
94 }
95
96
97
98
99
100 public Marshaller getMarshaller() {
101 return marshaller;
102 }
103
104
105
106
107 public void setMarshaller(Marshaller marshaller) {
108 this.marshaller = marshaller;
109 }
110
111
112
113
114 public Unmarshaller getUnmarshaller() {
115 return unmarshaller;
116 }
117
118
119
120
121 public void setUnmarshaller(Unmarshaller unmarshaller) {
122 this.unmarshaller = unmarshaller;
123 }
124
125
126
127
128
129
130 public void afterPropertiesSet() throws Exception {
131 Assert.notNull(baseUrl);
132 Assert.notNull(marshaller);
133 Assert.notNull(unmarshaller);
134
135 selectTemplate = new HttpClientOxmTemplate<List<T>>(getSelectUrl());
136 selectTemplate.setMarshaller(marshaller);
137 selectTemplate.setUnmarshaller(unmarshaller);
138
139 selectTemplate.afterPropertiesSet();
140
141 updateTemplate = new HttpClientOxmTemplate<List<T>>(getUpdateUrl());
142 updateTemplate.setMarshaller(marshaller);
143 updateTemplate.setUnmarshaller(unmarshaller);
144
145 updateTemplate.afterPropertiesSet();
146
147 postTemplate = new HttpClientTemplate(getUpdateUrl(), true);
148 }
149
150
151
152
153
154
155 public void destroy() throws Exception {
156 selectTemplate.destroy();
157 updateTemplate.destroy();
158 postTemplate.destroy();
159 }
160
161
162
163
164
165
166 public List<T> search(String query) {
167 Map<String, String> hParams = new HashMap<String, String>();
168 hParams.put(SEARCH_QUERY_PARAM, query);
169
170 return search(hParams);
171 }
172
173
174
175
176
177
178
179 public List<T> search(String query, Map<String, String> hParams) {
180 hParams.put(SEARCH_QUERY_PARAM, query);
181
182 return search(hParams);
183 }
184
185
186
187
188
189
190 public List<T> search(final Map<String, String> hParams) {
191 final List<T> lResults = new ArrayList<T>();
192
193 selectTemplate.executeGetMethod(hParams, new ResponseCallback<List<T>>() {
194 public void doWithResponse(List<T> lBeans) throws IOException {
195 lResults.addAll(lBeans);
196 }
197 });
198
199 return lResults;
200 }
201
202
203
204
205
206
207 public void update(List<T> lBeans) {
208 update(lBeans, true);
209 }
210
211
212
213
214
215
216
217 public void update(List<T> lBeans, boolean commit) {
218 updateTemplate.executePostMethod(lBeans);
219
220 if (commit) {
221 commit();
222 }
223 }
224
225
226
227
228
229
230 public void deleteById(String id) {
231 deleteById(id, true);
232 }
233
234
235
236
237
238
239
240 public void deleteById(String id, boolean commit) {
241 delete(getEnclosingElement(DELETE_ID_ELEMENT_NAME, id), commit);
242 }
243
244
245
246
247
248
249 public void deleteByQuery(String query) {
250 deleteByQuery(query, true);
251 }
252
253
254
255
256
257
258
259 public void deleteByQuery(String query, boolean commit) {
260 delete(getEnclosingElement(DELETE_QUERY_ELEMENT_NAME, query), commit);
261 }
262
263
264
265
266
267
268
269
270 protected void delete(String query, boolean commit) {
271 String request = getEnclosingElement(DELETE_ELEMENT_NAME, query);
272
273 postTemplate.executePostMethod(request);
274
275 if (commit) {
276 commit();
277 }
278
279 logger.info("Processed delete. request='{}' commit={}", request, commit);
280 }
281
282
283
284
285 public void commit() {
286 commit(null);
287 }
288
289
290
291
292 public void commit(SolrRequestAttributes attrs) {
293 String request = getElementWithAttributes(COMMIT_ELEMENT_NAME, attrs);
294
295 postTemplate.executePostMethod(request);
296
297 logger.debug("Processed commit. request={}", request);
298 }
299
300
301
302
303 public void rollback() {
304 postTemplate.executePostMethod(ROLLBACK_REQUEST);
305
306 logger.info("Processed rollback.");
307 }
308
309
310
311
312 public void optimize() {
313 optimize(null);
314 }
315
316
317
318
319 public void optimize(SolrRequestAttributes attrs) {
320 String request = getElementWithAttributes(OPTIMIZE_ELEMENT_NAME, attrs);
321
322 postTemplate.executePostMethod(request);
323
324 logger.debug("Processed optimize. request={}", request);
325 }
326
327
328
329
330 private String getEnclosingElement(String elementName, String value) {
331 StringBuilder result = new StringBuilder();
332
333 result.append("<" +elementName + ">");
334 result.append(value);
335 result.append("</" +elementName + ">");
336
337 return result.toString();
338 }
339
340
341
342
343 private String getElementWithAttributes(String elementName, SolrRequestAttributes attrs) {
344 StringBuilder result = new StringBuilder();
345
346 result.append("<");
347 result.append(elementName);
348
349 if (attrs != null) {
350 if (attrs.getMaxSegments() != null) {
351 result.append(" maxSegments=\"" + attrs.getMaxSegments() + "\"");
352 }
353
354 if (attrs.getWaitFlush() != null) {
355 result.append(" waitFlush=\"" + attrs.getWaitFlush() + "\"");
356 }
357
358 if (attrs.getWaitSearcher() != null) {
359 result.append(" waitSearcher=\"" + attrs.getWaitSearcher() + "\"");
360 }
361 }
362
363 result.append("/>");
364
365 return result.toString();
366 }
367
368 }
369