1 | /** |
---|
2 | * This software is copyright (c) 2011 by |
---|
3 | * - Institut fuer Deutsche Sprache (http://www.ids-mannheim.de) |
---|
4 | * This is free software. You can redistribute it |
---|
5 | * and/or modify it under the terms described in |
---|
6 | * the GNU General Public License v3 of which you |
---|
7 | * should have received a copy. Otherwise you can download |
---|
8 | * it from |
---|
9 | * |
---|
10 | * http://www.gnu.org/licenses/gpl-3.0.txt |
---|
11 | * |
---|
12 | * @copyright Institut fuer Deutsche Sprache (http://www.ids-mannheim.de) |
---|
13 | * |
---|
14 | * @license http://www.gnu.org/licenses/gpl-3.0.txt |
---|
15 | * GNU General Public License v3 |
---|
16 | */ |
---|
17 | package eu.clarin.sru.client; |
---|
18 | |
---|
19 | import java.util.ArrayList; |
---|
20 | import java.util.HashMap; |
---|
21 | import java.util.List; |
---|
22 | import java.util.Map; |
---|
23 | |
---|
24 | import javax.xml.stream.XMLStreamException; |
---|
25 | import javax.xml.stream.XMLStreamReader; |
---|
26 | |
---|
27 | import org.w3c.dom.Document; |
---|
28 | |
---|
29 | |
---|
30 | /** |
---|
31 | * A client to perform SRU operations. The response of a SRU request is wrapped |
---|
32 | * in a SRU response. |
---|
33 | * <p> |
---|
34 | * This client is reusable but not thread-safe: the application may reuse a |
---|
35 | * client object, but it may not be concurrently shared between multiple |
---|
36 | * threads. |
---|
37 | * </p> |
---|
38 | */ |
---|
39 | public class SRUClient { |
---|
40 | private final SRUSimpleClient client; |
---|
41 | private final Handler handler; |
---|
42 | /* common */ |
---|
43 | private List<SRUDiagnostic> diagnostics; |
---|
44 | private Document extraResponseData; |
---|
45 | /* explain */ |
---|
46 | private SRURecord record; |
---|
47 | /* scan */ |
---|
48 | private List<SRUTerm> terms; |
---|
49 | /* searchRetrieve */ |
---|
50 | private int numberOfRecords; |
---|
51 | private String resultSetId; |
---|
52 | private int resultSetIdleTime; |
---|
53 | private List<SRURecord> records; |
---|
54 | private int nextRecordPosition; |
---|
55 | |
---|
56 | |
---|
57 | /** |
---|
58 | * Constructor. This constructor will create a <em>strict</em> client and |
---|
59 | * use the default SRU version. |
---|
60 | * |
---|
61 | * @see #SRUClient(SRUVersion, boolean) |
---|
62 | * @see SRUSimpleClient#DEFAULT_SRU_VERSION |
---|
63 | */ |
---|
64 | public SRUClient() { |
---|
65 | this(SRUSimpleClient.DEFAULT_SRU_VERSION, true); |
---|
66 | } |
---|
67 | |
---|
68 | |
---|
69 | /** |
---|
70 | * Constructor. This constructor will create a <em>strict</em> client. |
---|
71 | * |
---|
72 | * @param defaultVersion |
---|
73 | * the default version to use for SRU requests; may be overridden |
---|
74 | * by individual requests |
---|
75 | * @see #SRUClient(SRUVersion, boolean) |
---|
76 | */ |
---|
77 | public SRUClient(SRUVersion defaultVersion) { |
---|
78 | this(defaultVersion, true); |
---|
79 | } |
---|
80 | |
---|
81 | |
---|
82 | /** |
---|
83 | * Constructor. |
---|
84 | * |
---|
85 | * @param defaultVersion |
---|
86 | * the default version to use for SRU requests; may be overridden |
---|
87 | * by individual requests |
---|
88 | * @param strictMode |
---|
89 | * if <code>true</code> the client will strictly adhere to the |
---|
90 | * SRU standard and raise fatal errors on violations, if |
---|
91 | * <code>false</code> it will act more forgiving and ignore |
---|
92 | * certain violations |
---|
93 | */ |
---|
94 | public SRUClient(SRUVersion defaultVersion, boolean strictMode) { |
---|
95 | this(defaultVersion, strictMode, |
---|
96 | new HashMap<String, SRURecordDataParser>()); |
---|
97 | } |
---|
98 | |
---|
99 | |
---|
100 | /** |
---|
101 | * Constructor. |
---|
102 | * |
---|
103 | * <p> |
---|
104 | * For internal use only. |
---|
105 | * </p> |
---|
106 | * |
---|
107 | * @param defaultVersion |
---|
108 | * the default version to use for SRU requests; may be overridden |
---|
109 | * by individual requests |
---|
110 | * @param strictMode |
---|
111 | * if <code>true</code> the client will strictly adhere to the |
---|
112 | * SRU standard and raise fatal errors on violations, if |
---|
113 | * <code>false</code> it will act more forgiving and ignore |
---|
114 | * certain violations |
---|
115 | * @param parsers |
---|
116 | * a <code>Map</code> to store record schema to record data |
---|
117 | * parser mappings |
---|
118 | */ |
---|
119 | SRUClient(SRUVersion defaultVersion, boolean strictMode, |
---|
120 | Map<String, SRURecordDataParser> parsers) { |
---|
121 | if (defaultVersion == null) { |
---|
122 | throw new NullPointerException("version == null"); |
---|
123 | } |
---|
124 | if (parsers == null) { |
---|
125 | throw new NullPointerException("parsers == null"); |
---|
126 | } |
---|
127 | this.client = new SRUSimpleClient(defaultVersion, strictMode, parsers); |
---|
128 | this.handler = new Handler(); |
---|
129 | reset(); |
---|
130 | } |
---|
131 | |
---|
132 | |
---|
133 | /** |
---|
134 | * Register a record data parser. |
---|
135 | * |
---|
136 | * @param parser |
---|
137 | * a parser instance |
---|
138 | * @throws SRUClientException |
---|
139 | * if a parser handing the same record schema is already |
---|
140 | * registered |
---|
141 | * @throws NullPointerException |
---|
142 | * if any required argument is <code>null</code> |
---|
143 | * @throws IllegalArgumentException |
---|
144 | * if the supplied parser is invalid |
---|
145 | */ |
---|
146 | public void registerRecordParser(SRURecordDataParser parser) |
---|
147 | throws SRUClientException { |
---|
148 | client.registerRecordParser(parser); |
---|
149 | } |
---|
150 | |
---|
151 | |
---|
152 | /** |
---|
153 | * Perform a <em>explain</em> operation. |
---|
154 | * |
---|
155 | * @param request |
---|
156 | * an instance of a {@link SRUExplainRequest} object |
---|
157 | * @return a {@link SRUExplainResponse} object |
---|
158 | * @throws SRUClientException |
---|
159 | * if an unrecoverable error occurred |
---|
160 | * @throws NullPointerException |
---|
161 | * if any required argument is <code>null</code> |
---|
162 | */ |
---|
163 | public SRUExplainResponse explain(SRUExplainRequest request) |
---|
164 | throws SRUClientException { |
---|
165 | if (request == null) { |
---|
166 | throw new NullPointerException("request == null"); |
---|
167 | } |
---|
168 | try { |
---|
169 | client.explain(request, handler); |
---|
170 | return new SRUExplainResponse(request, |
---|
171 | diagnostics, |
---|
172 | extraResponseData, |
---|
173 | record); |
---|
174 | } finally { |
---|
175 | reset(); |
---|
176 | } |
---|
177 | } |
---|
178 | |
---|
179 | |
---|
180 | /** |
---|
181 | * Perform a <em>scan</em> operation. |
---|
182 | * |
---|
183 | * @param request |
---|
184 | * an instance of a {@link SRUScanRequest} object |
---|
185 | * @return a {@link SRUScanResponse} object |
---|
186 | * @throws SRUClientException |
---|
187 | * if an unrecoverable error occurred |
---|
188 | * @throws NullPointerException |
---|
189 | * if any required argument is <code>null</code> |
---|
190 | */ |
---|
191 | public SRUScanResponse scan(SRUScanRequest request) |
---|
192 | throws SRUClientException { |
---|
193 | if (request == null) { |
---|
194 | throw new NullPointerException("request == null"); |
---|
195 | } |
---|
196 | try { |
---|
197 | client.scan(request, handler); |
---|
198 | return new SRUScanResponse(request, |
---|
199 | diagnostics, |
---|
200 | extraResponseData, |
---|
201 | terms); |
---|
202 | } finally { |
---|
203 | reset(); |
---|
204 | } |
---|
205 | |
---|
206 | } |
---|
207 | |
---|
208 | |
---|
209 | /** |
---|
210 | * Perform a <em>searchRetrieve</em> operation. |
---|
211 | * |
---|
212 | * @param request |
---|
213 | * an instance of a {@link SRUSearchRetrieveRequest} object |
---|
214 | * @return a {@link SRUSearchRetrieveRequest} object |
---|
215 | * @throws SRUClientException |
---|
216 | * if an unrecoverable error occurred |
---|
217 | * @throws NullPointerException |
---|
218 | * if any required argument is <code>null</code> |
---|
219 | */ |
---|
220 | public SRUSearchRetrieveResponse searchRetrieve( |
---|
221 | SRUSearchRetrieveRequest request) throws SRUClientException { |
---|
222 | if (request == null) { |
---|
223 | throw new NullPointerException("request == null"); |
---|
224 | } |
---|
225 | try { |
---|
226 | client.searchRetrieve(request, handler); |
---|
227 | return new SRUSearchRetrieveResponse(request, |
---|
228 | diagnostics, |
---|
229 | extraResponseData, |
---|
230 | numberOfRecords, |
---|
231 | resultSetId, |
---|
232 | resultSetIdleTime, |
---|
233 | records, |
---|
234 | nextRecordPosition); |
---|
235 | } finally { |
---|
236 | reset(); |
---|
237 | } |
---|
238 | } |
---|
239 | |
---|
240 | |
---|
241 | private void addTerm(SRUTerm term) { |
---|
242 | if (terms == null) { |
---|
243 | terms = new ArrayList<SRUTerm>(); |
---|
244 | } |
---|
245 | terms.add(term); |
---|
246 | } |
---|
247 | |
---|
248 | |
---|
249 | private void addRecord(SRURecord record) { |
---|
250 | if (records == null) { |
---|
251 | records = new ArrayList<SRURecord>(); |
---|
252 | } |
---|
253 | records.add(record); |
---|
254 | } |
---|
255 | |
---|
256 | |
---|
257 | private void reset() { |
---|
258 | /* common */ |
---|
259 | diagnostics = null; |
---|
260 | extraResponseData = null; |
---|
261 | /* explain */ |
---|
262 | record = null; |
---|
263 | /* scan */ |
---|
264 | terms = null; |
---|
265 | /* searchRetrieve */ |
---|
266 | numberOfRecords = -1; |
---|
267 | resultSetId = null; |
---|
268 | resultSetIdleTime = -1; |
---|
269 | records = null; |
---|
270 | nextRecordPosition = -1; |
---|
271 | } |
---|
272 | |
---|
273 | |
---|
274 | private class Handler extends SRUDefaultHandlerAdapter { |
---|
275 | @Override |
---|
276 | public void onDiagnostics(List<SRUDiagnostic> diagnostics) |
---|
277 | throws SRUClientException { |
---|
278 | SRUClient.this.diagnostics = diagnostics; |
---|
279 | } |
---|
280 | |
---|
281 | |
---|
282 | @Override |
---|
283 | public void onExtraResponseData(XMLStreamReader reader) |
---|
284 | throws XMLStreamException, SRUClientException { |
---|
285 | // TODO: parse extraResponseData |
---|
286 | } |
---|
287 | |
---|
288 | |
---|
289 | @Override |
---|
290 | public void onTerm(String value, int numberOfRecords, |
---|
291 | String displayTerm, SRUWhereInList whereInList) |
---|
292 | throws SRUClientException { |
---|
293 | SRUClient.this.addTerm(new SRUTerm(value, numberOfRecords, |
---|
294 | displayTerm, whereInList, null)); |
---|
295 | } |
---|
296 | |
---|
297 | |
---|
298 | @Override |
---|
299 | public void onExtraTermData(String value, XMLStreamReader reader) |
---|
300 | throws XMLStreamException, SRUClientException { |
---|
301 | // TODO: parse extraTermData |
---|
302 | } |
---|
303 | |
---|
304 | |
---|
305 | @Override |
---|
306 | public void onStartRecords(int numberOfRecords, String resultSetId, |
---|
307 | int resultSetIdleTime) throws SRUClientException { |
---|
308 | SRUClient.this.resultSetId = resultSetId; |
---|
309 | SRUClient.this.resultSetIdleTime = resultSetIdleTime; |
---|
310 | } |
---|
311 | |
---|
312 | |
---|
313 | @Override |
---|
314 | public void onFinishRecords(int nextRecordPosition) |
---|
315 | throws SRUClientException { |
---|
316 | SRUClient.this.nextRecordPosition = nextRecordPosition; |
---|
317 | } |
---|
318 | |
---|
319 | |
---|
320 | @Override |
---|
321 | public void onRecord(String identifier, int position, |
---|
322 | SRURecordData data) throws SRUClientException { |
---|
323 | SRUClient.this.addRecord(new SRURecord(data, identifier, |
---|
324 | position, null)); |
---|
325 | } |
---|
326 | |
---|
327 | |
---|
328 | @Override |
---|
329 | public void onSurrogateRecord(String identifier, int position, |
---|
330 | SRUDiagnostic data) throws SRUClientException { |
---|
331 | SRUClient.this.addRecord(new SRURecord( |
---|
332 | new SRUSurrogateRecordData(data), identifier, position, |
---|
333 | null)); |
---|
334 | } |
---|
335 | |
---|
336 | |
---|
337 | @Override |
---|
338 | public void onExtraRecordData(String identifier, int position, |
---|
339 | XMLStreamReader reader) throws XMLStreamException, |
---|
340 | SRUClientException { |
---|
341 | // TODO: parseExtraRecordData |
---|
342 | } |
---|
343 | } // inner class Handler |
---|
344 | |
---|
345 | } // class SRUClient |
---|