1 | /** |
---|
2 | * This software is copyright (c) 2011-2013 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.server; |
---|
18 | |
---|
19 | import java.io.IOException; |
---|
20 | import java.io.OutputStream; |
---|
21 | import java.util.Arrays; |
---|
22 | import java.util.List; |
---|
23 | import java.util.NoSuchElementException; |
---|
24 | |
---|
25 | import javax.servlet.http.HttpServletRequest; |
---|
26 | import javax.servlet.http.HttpServletResponse; |
---|
27 | import javax.xml.stream.XMLOutputFactory; |
---|
28 | import javax.xml.stream.XMLStreamException; |
---|
29 | import javax.xml.stream.XMLStreamWriter; |
---|
30 | |
---|
31 | import org.slf4j.Logger; |
---|
32 | import org.slf4j.LoggerFactory; |
---|
33 | import org.z3950.zing.cql.CQLNode; |
---|
34 | |
---|
35 | import eu.clarin.sru.server.SRUServerConfig.DatabaseInfo; |
---|
36 | import eu.clarin.sru.server.SRUServerConfig.IndexInfo; |
---|
37 | import eu.clarin.sru.server.SRUServerConfig.LocalizedString; |
---|
38 | import eu.clarin.sru.server.SRUServerConfig.SchemaInfo; |
---|
39 | import eu.clarin.sru.server.utils.SRUServerServlet; |
---|
40 | |
---|
41 | |
---|
42 | /** |
---|
43 | * SRU/CQL protocol implementation for the server-side (SRU/S). This class |
---|
44 | * implements SRU/CQL version 1.1 and and 1.2. |
---|
45 | * |
---|
46 | * @see SRUServerConfig |
---|
47 | * @see SRUSearchEngine |
---|
48 | * @see SRUServerServlet |
---|
49 | * @see <a href="http://www.loc.gov/standards/sru/">SRU/CQL protocol 1.2</a> |
---|
50 | */ |
---|
51 | public final class SRUServer { |
---|
52 | private static final String SRU_NS = |
---|
53 | "http://www.loc.gov/zing/srw/"; |
---|
54 | private static final String SRU_PREFIX = "sru"; |
---|
55 | private static final String SRU_DIAGNOSIC_NS = |
---|
56 | "http://www.loc.gov/zing/srw/diagnostic/"; |
---|
57 | private static final String SRU_DIAGNOSTIC_PREFIX = "diag"; |
---|
58 | private static final String SRU_DIAGNOSTIC_RECORD_SCHEMA = |
---|
59 | "info:srw/schema/1/diagnostics-v1.1"; |
---|
60 | private static final String SRU_EXPLAIN_NS = |
---|
61 | "http://explain.z3950.org/dtd/2.0/"; |
---|
62 | private static final String SRU_EXPLAIN_PREFIX = "zr"; |
---|
63 | private static final String SRU_XCQL_NS = |
---|
64 | "http://www.loc.gov/zing/cql/xcql/"; |
---|
65 | static final String RESPONSE_ENCODING = "utf-8"; |
---|
66 | private static final String RESPONSE_CONTENT_TYPE = "application/xml"; |
---|
67 | private static final int RESPONSE_BUFFER_SIZE = 64 * 1024; |
---|
68 | private static final Logger logger = |
---|
69 | LoggerFactory.getLogger(SRUServer.class); |
---|
70 | private final SRUServerConfig config; |
---|
71 | private final SRUSearchEngine searchEngine; |
---|
72 | private final XMLOutputFactory writerFactory; |
---|
73 | |
---|
74 | |
---|
75 | /** |
---|
76 | * Constructor. |
---|
77 | * |
---|
78 | * @param config |
---|
79 | * a SRUEndpointConfig object |
---|
80 | * @param searchEngine |
---|
81 | * an object implementing the SRUSearchEngine interface |
---|
82 | * @throws NullPointerException |
---|
83 | * if config or searchEngine is <code>null</code> |
---|
84 | * @throws SRUException |
---|
85 | * if an error occurred |
---|
86 | */ |
---|
87 | public SRUServer(SRUServerConfig config, SRUSearchEngine searchEngine) |
---|
88 | throws SRUException { |
---|
89 | if (config == null) { |
---|
90 | throw new NullPointerException("config == null"); |
---|
91 | } |
---|
92 | this.config = config; |
---|
93 | if (searchEngine == null) { |
---|
94 | throw new NullPointerException("searchEngine == null"); |
---|
95 | } |
---|
96 | this.searchEngine = searchEngine; |
---|
97 | this.writerFactory = XMLOutputFactory.newInstance(); |
---|
98 | } |
---|
99 | |
---|
100 | |
---|
101 | /** |
---|
102 | * Handle a SRU request. |
---|
103 | * |
---|
104 | * @param request |
---|
105 | * a HttpServletRequest request |
---|
106 | * @param response |
---|
107 | * a HttpServletResponse request |
---|
108 | */ |
---|
109 | public void handleRequest(HttpServletRequest request, |
---|
110 | HttpServletResponse response) { |
---|
111 | final SRURequestImpl req = new SRURequestImpl(config, request); |
---|
112 | try { |
---|
113 | // set response properties |
---|
114 | response.setContentType(RESPONSE_CONTENT_TYPE); |
---|
115 | response.setCharacterEncoding(RESPONSE_ENCODING); |
---|
116 | response.setStatus(HttpServletResponse.SC_OK); |
---|
117 | // make sure we can reset the stream later in case of error ... |
---|
118 | response.setBufferSize(RESPONSE_BUFFER_SIZE); |
---|
119 | |
---|
120 | try { |
---|
121 | if (req.checkParameters()) { |
---|
122 | switch (req.getOperation()) { |
---|
123 | case EXPLAIN: |
---|
124 | explain(req, response); |
---|
125 | break; |
---|
126 | case SCAN: |
---|
127 | scan(req, response); |
---|
128 | break; |
---|
129 | case SEARCH_RETRIEVE: |
---|
130 | search(req, response); |
---|
131 | break; |
---|
132 | } |
---|
133 | } else { |
---|
134 | // (some) parameters are malformed, send error |
---|
135 | SRUXMLStreamWriter out = |
---|
136 | createXMLStreamWriter(response.getOutputStream(), |
---|
137 | SRURecordPacking.XML, req.getIndentResponse()); |
---|
138 | writeFatalError(out, req, req.getDiagnostics()); |
---|
139 | } |
---|
140 | } catch (XMLStreamException e) { |
---|
141 | logger.error("An error occurred while serializing response", e); |
---|
142 | throw new SRUException(SRUConstants.SRU_GENERAL_SYSTEM_ERROR, |
---|
143 | "An error occurred while serializing response.", e); |
---|
144 | } catch (IOException e) { |
---|
145 | /* |
---|
146 | * Well, can't really do anything useful here ... |
---|
147 | */ |
---|
148 | logger.error("An unexpected exception occurred", e); |
---|
149 | } |
---|
150 | } catch (SRUException e) { |
---|
151 | if (!response.isCommitted()) { |
---|
152 | response.resetBuffer(); |
---|
153 | try { |
---|
154 | List<SRUDiagnostic> diagnostics = req.getDiagnostics(); |
---|
155 | if (diagnostics != null) { |
---|
156 | diagnostics.add(e.getDiagnostic()); |
---|
157 | } else { |
---|
158 | diagnostics = Arrays.asList(e.getDiagnostic()); |
---|
159 | } |
---|
160 | SRUXMLStreamWriter out = |
---|
161 | createXMLStreamWriter(response.getOutputStream(), |
---|
162 | SRURecordPacking.XML, |
---|
163 | req.getIndentResponse()); |
---|
164 | writeFatalError(out, req, diagnostics); |
---|
165 | } catch (Exception ex) { |
---|
166 | logger.error("An exception occurred while in error state", |
---|
167 | ex); |
---|
168 | } |
---|
169 | } else { |
---|
170 | logger.error("A fatal error occurred, but the response was " |
---|
171 | + "already committed", e); |
---|
172 | } |
---|
173 | } |
---|
174 | } |
---|
175 | |
---|
176 | |
---|
177 | private void explain(SRURequestImpl request, HttpServletResponse response) |
---|
178 | throws IOException, XMLStreamException, SRUException { |
---|
179 | logger.info("explain"); |
---|
180 | |
---|
181 | // commence explain ... |
---|
182 | final SRUExplainResult result = |
---|
183 | searchEngine.explain(config, request, request); |
---|
184 | |
---|
185 | try { |
---|
186 | // send results |
---|
187 | SRUXMLStreamWriter out = |
---|
188 | createXMLStreamWriter(response.getOutputStream(), |
---|
189 | request.getRecordPacking(), |
---|
190 | request.getIndentResponse()); |
---|
191 | |
---|
192 | beginResponse(out, request); |
---|
193 | |
---|
194 | // write the explain record |
---|
195 | writeExplainRecord(out, request); |
---|
196 | |
---|
197 | if (config.getEchoRequests()) { |
---|
198 | writeEchoedExplainRequest(out, request); |
---|
199 | } |
---|
200 | |
---|
201 | // diagnostics |
---|
202 | writeDiagnosticList(out, request.getDiagnostics()); |
---|
203 | |
---|
204 | // extraResponseData |
---|
205 | if (result != null) { |
---|
206 | if (result.hasExtraResponseData()) { |
---|
207 | out.writeStartElement(SRU_NS, "extraResponseData"); |
---|
208 | result.writeExtraResponseData(out); |
---|
209 | out.writeEndElement(); // "extraResponseData" element |
---|
210 | } |
---|
211 | } |
---|
212 | |
---|
213 | endResponse(out); |
---|
214 | } finally { |
---|
215 | if (result != null) { |
---|
216 | result.close(); |
---|
217 | } |
---|
218 | } |
---|
219 | } |
---|
220 | |
---|
221 | |
---|
222 | private void scan(SRURequestImpl request, HttpServletResponse response) |
---|
223 | throws IOException, XMLStreamException, SRUException { |
---|
224 | logger.info("scan: scanClause = \"{}\"", |
---|
225 | new Object[] { request.getRawScanClause() }); |
---|
226 | |
---|
227 | // commence scan |
---|
228 | final SRUScanResultSet result = |
---|
229 | searchEngine.scan(config, request, request); |
---|
230 | if (result == null) { |
---|
231 | throw new SRUException(SRUConstants.SRU_UNSUPPORTED_OPERATION, |
---|
232 | "The 'scan' operation is not supported by this endpoint."); |
---|
233 | } |
---|
234 | |
---|
235 | try { |
---|
236 | // send results |
---|
237 | SRUXMLStreamWriter out = |
---|
238 | createXMLStreamWriter(response.getOutputStream(), |
---|
239 | request.getRecordPacking(), |
---|
240 | request.getIndentResponse()); |
---|
241 | |
---|
242 | beginResponse(out, request); |
---|
243 | |
---|
244 | try { |
---|
245 | /* |
---|
246 | * a scan result without a list of terms is a valid response; |
---|
247 | * make sure, to produce the correct output and omit in that case |
---|
248 | * the <terms> ... |
---|
249 | */ |
---|
250 | boolean wroteTerms = false; |
---|
251 | while (result.nextTerm()) { |
---|
252 | if (!wroteTerms) { |
---|
253 | out.writeStartElement(SRU_NS, "terms"); |
---|
254 | wroteTerms = true; |
---|
255 | } |
---|
256 | out.writeStartElement(SRU_NS, "term"); |
---|
257 | |
---|
258 | out.writeStartElement(SRU_NS, "value"); |
---|
259 | out.writeCharacters(result.getValue()); |
---|
260 | out.writeEndElement(); // "value" element |
---|
261 | |
---|
262 | if (result.getNumberOfRecords() > -1) { |
---|
263 | out.writeStartElement(SRU_NS, "numberOfRecords"); |
---|
264 | out.writeCharacters( |
---|
265 | Integer.toString(result.getNumberOfRecords())); |
---|
266 | out.writeEndElement(); // "numberOfRecords" element |
---|
267 | } |
---|
268 | |
---|
269 | if (result.getDisplayTerm() != null) { |
---|
270 | out.writeStartElement(SRU_NS, "displayTerm"); |
---|
271 | out.writeCharacters(result.getDisplayTerm()); |
---|
272 | out.writeEndElement(); // "displayTerm" element |
---|
273 | } |
---|
274 | |
---|
275 | if (result.getWhereInList() != null) { |
---|
276 | out.writeStartElement(SRU_NS, "whereInList"); |
---|
277 | switch (result.getWhereInList()) { |
---|
278 | case FIRST: |
---|
279 | out.writeCharacters("first"); |
---|
280 | break; |
---|
281 | case LAST: |
---|
282 | out.writeCharacters("last"); |
---|
283 | break; |
---|
284 | case ONLY: |
---|
285 | out.writeCharacters("only"); |
---|
286 | break; |
---|
287 | case INNER: |
---|
288 | out.writeCharacters("inner"); |
---|
289 | break; |
---|
290 | } // switch |
---|
291 | out.writeEndElement(); // "whereInList" element |
---|
292 | } |
---|
293 | |
---|
294 | if (result.hasExtraTermData()) { |
---|
295 | out.writeStartElement(SRU_NS, "extraTermData"); |
---|
296 | result.writeExtraTermData(out); |
---|
297 | out.writeEndElement(); // "extraTermData" element |
---|
298 | } |
---|
299 | |
---|
300 | out.writeEndElement(); // "term" element |
---|
301 | } // while |
---|
302 | if (wroteTerms) { |
---|
303 | out.writeEndElement(); // "terms" element |
---|
304 | } |
---|
305 | } catch (NoSuchElementException e) { |
---|
306 | throw new SRUException(SRUConstants.SRU_GENERAL_SYSTEM_ERROR, |
---|
307 | "An internal error occurred while " |
---|
308 | + "serializing scan results."); |
---|
309 | } |
---|
310 | |
---|
311 | // echoedScanRequest |
---|
312 | if (config.getEchoRequests()) { |
---|
313 | writeEchoedScanRequest(out, request, request.getScanClause()); |
---|
314 | } |
---|
315 | |
---|
316 | // diagnostics |
---|
317 | writeDiagnosticList(out, request.getDiagnostics()); |
---|
318 | |
---|
319 | // extraResponseData |
---|
320 | if (result.hasExtraResponseData()) { |
---|
321 | out.writeStartElement(SRU_NS, "extraResponseData"); |
---|
322 | result.writeExtraResponseData(out); |
---|
323 | out.writeEndElement(); // "extraResponseData" element |
---|
324 | } |
---|
325 | |
---|
326 | endResponse(out); |
---|
327 | } finally { |
---|
328 | if (result != null) { |
---|
329 | result.close(); |
---|
330 | } |
---|
331 | } |
---|
332 | } |
---|
333 | |
---|
334 | |
---|
335 | private void search(SRURequestImpl request, HttpServletResponse response) |
---|
336 | throws IOException, XMLStreamException, SRUException { |
---|
337 | logger.info("searchRetrieve: query = \"{}\", startRecord = {}, " + |
---|
338 | "maximumRecords = {}, recordSchema = {}, resultSetTTL = {}", |
---|
339 | new Object[] { request.getRawQuery(), request.getStartRecord(), |
---|
340 | request.getMaximumRecords(), |
---|
341 | request.getRecordSchemaIdentifier(), |
---|
342 | request.getResultSetTTL() }); |
---|
343 | |
---|
344 | // commence search ... |
---|
345 | final SRUSearchResultSet result = |
---|
346 | searchEngine.search(config, request, request); |
---|
347 | if (result == null) { |
---|
348 | throw new SRUException(SRUConstants.SRU_GENERAL_SYSTEM_ERROR, |
---|
349 | "SRUSearchEngine implementation returned invalid result (null)."); |
---|
350 | } |
---|
351 | |
---|
352 | |
---|
353 | // check, of startRecord position is greater than total record set |
---|
354 | if ((result.getTotalRecordCount() >= 0) && |
---|
355 | (request.getStartRecord() > 0) && |
---|
356 | (request.getStartRecord() > result.getTotalRecordCount())) { |
---|
357 | throw new SRUException(SRUConstants.SRU_FIRST_RECORD_POSITION_OUT_OF_RANGE); |
---|
358 | } |
---|
359 | |
---|
360 | try { |
---|
361 | // send results |
---|
362 | SRUXMLStreamWriter out = |
---|
363 | createXMLStreamWriter(response.getOutputStream(), |
---|
364 | request.getRecordPacking(), |
---|
365 | request.getIndentResponse()); |
---|
366 | |
---|
367 | beginResponse(out, request); |
---|
368 | |
---|
369 | // numberOfRecords |
---|
370 | out.writeStartElement(SRU_NS, "numberOfRecords"); |
---|
371 | out.writeCharacters( |
---|
372 | Integer.toString(result.getTotalRecordCount())); |
---|
373 | out.writeEndElement(); // "numberOfRecords" element |
---|
374 | |
---|
375 | // resultSetId |
---|
376 | if (result.getResultSetId() != null) { |
---|
377 | out.writeStartElement(SRU_NS, "resultSetId"); |
---|
378 | out.writeCharacters(result.getResultSetId()); |
---|
379 | out.writeEndElement(); // "resultSetId" element |
---|
380 | } |
---|
381 | |
---|
382 | // resultSetIdleTime |
---|
383 | if (result.getResultSetIdleTime() > 0) { |
---|
384 | out.writeStartElement(SRU_NS, "resultSetIdleTime"); |
---|
385 | out.writeCharacters(Integer.toString(result |
---|
386 | .getResultSetIdleTime())); |
---|
387 | out.writeEndElement(); // "resultSetIdleTime" element |
---|
388 | } |
---|
389 | |
---|
390 | int position = (request.getStartRecord() > 0) |
---|
391 | ? request.getStartRecord() : 1; |
---|
392 | if (result.getRecordCount() > 0) { |
---|
393 | final int maxPositionOffset = |
---|
394 | (request.getMaximumRecords() != -1) |
---|
395 | ? (position + request.getMaximumRecords() - 1) |
---|
396 | : -1; |
---|
397 | try { |
---|
398 | out.writeStartElement(SRU_NS, "records"); |
---|
399 | while (result.nextRecord()) { |
---|
400 | /* |
---|
401 | * Sanity check: do not return more then the maximum |
---|
402 | * requested records. If the search engine |
---|
403 | * implementation does not honor limit truncate the |
---|
404 | * result set. |
---|
405 | */ |
---|
406 | if ((maxPositionOffset != -1) && |
---|
407 | (position > maxPositionOffset)) { |
---|
408 | logger.error("SRUSearchEngine implementation did " + |
---|
409 | "not honor limit for the amount of " + |
---|
410 | "requsted records. Result set truncated!"); |
---|
411 | break; |
---|
412 | } |
---|
413 | |
---|
414 | out.writeStartElement(SRU_NS, "record"); |
---|
415 | |
---|
416 | /* |
---|
417 | * We need to output either the record or a surrogate |
---|
418 | * diagnostic. In case of the latter, we need to output |
---|
419 | * the appropriate record schema ... |
---|
420 | */ |
---|
421 | final SRUDiagnostic diagnostic = |
---|
422 | result.getSurrogateDiagnostic(); |
---|
423 | |
---|
424 | out.writeStartElement(SRU_NS, "recordSchema"); |
---|
425 | if (diagnostic == null) { |
---|
426 | out.writeCharacters( |
---|
427 | result.getRecordSchemaIdentifier()); |
---|
428 | } else { |
---|
429 | out.writeCharacters(SRU_DIAGNOSTIC_RECORD_SCHEMA); |
---|
430 | } |
---|
431 | out.writeEndElement(); // "recordSchema" element |
---|
432 | |
---|
433 | // recordPacking |
---|
434 | writeRecordPacking(out, request.getRecordPacking()); |
---|
435 | |
---|
436 | /* |
---|
437 | * Output either record data or surrogate diagnostic ... |
---|
438 | */ |
---|
439 | out.writeStartElement(SRU_NS, "recordData"); |
---|
440 | out.startRecord(); |
---|
441 | if (diagnostic == null) { |
---|
442 | result.writeRecord(out); |
---|
443 | } else { |
---|
444 | // write a surrogate diagnostic |
---|
445 | writeDiagnostic(out, diagnostic, true); |
---|
446 | } |
---|
447 | out.endRecord(); |
---|
448 | out.writeEndElement(); // "recordData" element |
---|
449 | |
---|
450 | /* |
---|
451 | * recordIdentifier is version 1.2 only |
---|
452 | */ |
---|
453 | if (request.isVersion(SRUVersion.VERSION_1_2)) { |
---|
454 | final String identifier = |
---|
455 | result.getRecordIdentifier(); |
---|
456 | if (identifier != null) { |
---|
457 | out.writeStartElement(SRU_NS, |
---|
458 | "recordIdentifier"); |
---|
459 | out.writeCharacters(identifier); |
---|
460 | out.writeEndElement(); // "recordIdentifier" element |
---|
461 | } |
---|
462 | } |
---|
463 | |
---|
464 | out.writeStartElement(SRU_NS, "recordPosition"); |
---|
465 | out.writeCharacters(Integer.toString(position)); |
---|
466 | out.writeEndElement(); // "recordPosition" element |
---|
467 | |
---|
468 | if (result.hasExtraRecordData()) { |
---|
469 | out.writeStartElement(SRU_NS, "extraRecordData"); |
---|
470 | result.writeExtraRecordData(out); |
---|
471 | out.writeEndElement(); // "extraRecordData" |
---|
472 | } |
---|
473 | |
---|
474 | out.writeEndElement(); // "record" element |
---|
475 | |
---|
476 | position++; |
---|
477 | } // while |
---|
478 | out.writeEndElement(); // "records" element |
---|
479 | } catch (NoSuchElementException e) { |
---|
480 | throw new SRUException( |
---|
481 | SRUConstants.SRU_GENERAL_SYSTEM_ERROR, |
---|
482 | "An internal error occurred while " + |
---|
483 | "serializing search result set."); |
---|
484 | } |
---|
485 | } |
---|
486 | |
---|
487 | // nextRecordPosition |
---|
488 | if (position <= result.getTotalRecordCount()) { |
---|
489 | out.writeStartElement(SRU_NS, "nextRecordPosition"); |
---|
490 | out.writeCharacters(Integer.toString(position)); |
---|
491 | out.writeEndElement(); |
---|
492 | } |
---|
493 | |
---|
494 | // echoedSearchRetrieveRequest |
---|
495 | if (config.getEchoRequests()) { |
---|
496 | writeEchoedSearchRetrieveRequest(out, request, |
---|
497 | request.getQuery()); |
---|
498 | } |
---|
499 | |
---|
500 | // diagnostics |
---|
501 | writeDiagnosticList(out, request.getDiagnostics()); |
---|
502 | |
---|
503 | // extraResponseData |
---|
504 | if (result.hasExtraResponseData()) { |
---|
505 | out.writeStartElement(SRU_NS, "extraResponseData"); |
---|
506 | result.writeExtraResponseData(out); |
---|
507 | out.writeEndElement(); // "extraResponseData" element |
---|
508 | } |
---|
509 | |
---|
510 | endResponse(out); |
---|
511 | } finally { |
---|
512 | result.close(); |
---|
513 | } |
---|
514 | } |
---|
515 | |
---|
516 | |
---|
517 | private void beginResponse(SRUXMLStreamWriter out, SRUOperation operation, |
---|
518 | SRUVersion version, String stylesheet) throws XMLStreamException { |
---|
519 | out.writeStartDocument("utf-8", "1.0"); |
---|
520 | |
---|
521 | if (stylesheet != null) { |
---|
522 | StringBuilder param = new StringBuilder(); |
---|
523 | param.append("type=\"text/xsl\""); |
---|
524 | param.append(" "); |
---|
525 | param.append("href=\""); |
---|
526 | param.append(stylesheet); |
---|
527 | param.append("\""); |
---|
528 | out.writeProcessingInstruction("xml-stylesheet", param.toString()); |
---|
529 | } |
---|
530 | |
---|
531 | out.setPrefix(SRU_PREFIX, SRU_NS); |
---|
532 | switch (operation) { |
---|
533 | case EXPLAIN: |
---|
534 | out.writeStartElement(SRU_NS, "explainResponse"); |
---|
535 | break; |
---|
536 | case SCAN: |
---|
537 | out.writeStartElement(SRU_NS, "scanResponse"); |
---|
538 | break; |
---|
539 | case SEARCH_RETRIEVE: |
---|
540 | out.writeStartElement(SRU_NS, "searchRetrieveResponse"); |
---|
541 | break; |
---|
542 | } |
---|
543 | out.writeNamespace(SRU_PREFIX, SRU_NS); |
---|
544 | |
---|
545 | // version |
---|
546 | writeVersion(out, version); |
---|
547 | } |
---|
548 | |
---|
549 | |
---|
550 | private void beginResponse(SRUXMLStreamWriter out, SRURequest request) |
---|
551 | throws XMLStreamException { |
---|
552 | beginResponse(out, request.getOperation(), request.getVersion(), |
---|
553 | request.getStylesheet()); |
---|
554 | } |
---|
555 | |
---|
556 | |
---|
557 | private void endResponse(SRUXMLStreamWriter out) |
---|
558 | throws XMLStreamException { |
---|
559 | out.writeEndElement(); // "root" element |
---|
560 | |
---|
561 | out.writeEndDocument(); |
---|
562 | out.close(); |
---|
563 | try { |
---|
564 | out.getWriter().close(); |
---|
565 | } catch (IOException e) { |
---|
566 | /* IGNORE */ |
---|
567 | } |
---|
568 | } |
---|
569 | |
---|
570 | |
---|
571 | private void writeFatalError(SRUXMLStreamWriter out, |
---|
572 | SRURequestImpl request, List<SRUDiagnostic> diagnotics) |
---|
573 | throws XMLStreamException { |
---|
574 | /* |
---|
575 | * if operation is unknown, default to 'explain' |
---|
576 | */ |
---|
577 | SRUOperation operation = request.getOperation(); |
---|
578 | if (operation == null) { |
---|
579 | operation = SRUOperation.EXPLAIN; |
---|
580 | } |
---|
581 | SRUVersion version = request.getVersion(); |
---|
582 | if (version == null) { |
---|
583 | version = config.getDefaultVersion(); |
---|
584 | } |
---|
585 | /* |
---|
586 | * write a response which conforms to the schema |
---|
587 | */ |
---|
588 | beginResponse(out, operation, version, null); |
---|
589 | switch (operation) { |
---|
590 | case EXPLAIN: |
---|
591 | // 'explain' requires a complete explain record ... |
---|
592 | writeExplainRecord(out, request); |
---|
593 | break; |
---|
594 | case SCAN: |
---|
595 | // 'scan' fortunately does not need any elements ... |
---|
596 | break; |
---|
597 | case SEARCH_RETRIEVE: |
---|
598 | // 'searchRetrieve' needs numberOfRecords .. |
---|
599 | out.writeStartElement(SRU_NS, "numberOfRecords"); |
---|
600 | out.writeCharacters("0"); |
---|
601 | out.writeEndElement(); // "numberOfRecords" element |
---|
602 | break; |
---|
603 | } |
---|
604 | writeDiagnosticList(out, diagnotics); |
---|
605 | endResponse(out); |
---|
606 | } |
---|
607 | |
---|
608 | |
---|
609 | private void writeDiagnosticList(SRUXMLStreamWriter out, |
---|
610 | List<SRUDiagnostic> diagnostics) throws XMLStreamException { |
---|
611 | if ((diagnostics != null) && !diagnostics.isEmpty()) { |
---|
612 | out.setPrefix(SRU_DIAGNOSTIC_PREFIX, SRU_DIAGNOSIC_NS); |
---|
613 | out.writeStartElement(SRU_NS, "diagnostics"); |
---|
614 | out.writeNamespace(SRU_DIAGNOSTIC_PREFIX, SRU_DIAGNOSIC_NS); |
---|
615 | for (SRUDiagnostic diagnostic : diagnostics) { |
---|
616 | writeDiagnostic(out, diagnostic, false); |
---|
617 | } |
---|
618 | out.writeEndElement(); // "diagnostics" element |
---|
619 | } |
---|
620 | } |
---|
621 | |
---|
622 | |
---|
623 | private void writeExplainRecord(SRUXMLStreamWriter out, |
---|
624 | SRURequestImpl request) throws XMLStreamException { |
---|
625 | out.writeStartElement(SRU_NS, "record"); |
---|
626 | |
---|
627 | out.writeStartElement(SRU_NS, "recordSchema"); |
---|
628 | out.writeCharacters(SRU_EXPLAIN_NS); |
---|
629 | out.writeEndElement(); // "recordSchema" element |
---|
630 | |
---|
631 | // recordPacking |
---|
632 | writeRecordPacking(out, request.getRecordPacking()); |
---|
633 | |
---|
634 | out.writeStartElement(SRU_NS, "recordData"); |
---|
635 | |
---|
636 | out.startRecord(); |
---|
637 | |
---|
638 | // explain ... |
---|
639 | out.setPrefix(SRU_EXPLAIN_PREFIX, SRU_EXPLAIN_NS); |
---|
640 | out.writeStartElement(SRU_EXPLAIN_NS, "explain"); |
---|
641 | out.writeNamespace(SRU_EXPLAIN_PREFIX, SRU_EXPLAIN_NS); |
---|
642 | |
---|
643 | // explain/serverInfo |
---|
644 | out.writeStartElement(SRU_EXPLAIN_NS, "serverInfo"); |
---|
645 | out.writeAttribute("protocol", "SRU"); |
---|
646 | switch (config.getDefaultVersion()) { |
---|
647 | case VERSION_1_1: |
---|
648 | out.writeAttribute("version", "1.1"); |
---|
649 | break; |
---|
650 | case VERSION_1_2: |
---|
651 | out.writeAttribute("version", "1.2"); |
---|
652 | } // switch |
---|
653 | out.writeAttribute("transport", config.getTransports()); |
---|
654 | out.writeStartElement(SRU_EXPLAIN_NS, "host"); |
---|
655 | out.writeCharacters(config.getHost()); |
---|
656 | out.writeEndElement(); // "host" element |
---|
657 | out.writeStartElement(SRU_EXPLAIN_NS, "port"); |
---|
658 | out.writeCharacters(Integer.toString(config.getPort())); |
---|
659 | out.writeEndElement(); // "port" element |
---|
660 | out.writeStartElement(SRU_EXPLAIN_NS, "database"); |
---|
661 | out.writeCharacters(config.getDatabase()); |
---|
662 | out.writeEndElement(); // "database" element |
---|
663 | out.writeEndElement(); // "serverInfo" element |
---|
664 | |
---|
665 | // explain/databaseInfo |
---|
666 | final DatabaseInfo dbinfo = config.getDatabaseInfo(); |
---|
667 | if (dbinfo != null) { |
---|
668 | out.writeStartElement(SRU_EXPLAIN_NS, "databaseInfo"); |
---|
669 | writeLocalizedStrings(out, "title", dbinfo.getTitle()); |
---|
670 | writeLocalizedStrings(out, "description", dbinfo.getDescription()); |
---|
671 | writeLocalizedStrings(out, "author", dbinfo.getAuthor()); |
---|
672 | writeLocalizedStrings(out, "extent", dbinfo.getExtend()); |
---|
673 | writeLocalizedStrings(out, "history", dbinfo.getHistory()); |
---|
674 | writeLocalizedStrings(out, "langUsage", dbinfo.getLangUsage()); |
---|
675 | writeLocalizedStrings(out, "restrictions", dbinfo.getRestrictions()); |
---|
676 | writeLocalizedStrings(out, "subjects", dbinfo.getSubjects()); |
---|
677 | writeLocalizedStrings(out, "links", dbinfo.getLinks()); |
---|
678 | writeLocalizedStrings(out, "implementation", |
---|
679 | dbinfo.getImplementation()); |
---|
680 | out.writeEndElement(); // "databaseInfo" element |
---|
681 | } |
---|
682 | |
---|
683 | // explain/indexInfo |
---|
684 | final IndexInfo indexInfo = config.getIndexInfo(); |
---|
685 | if (indexInfo != null) { |
---|
686 | out.writeStartElement(SRU_EXPLAIN_NS, "indexInfo"); |
---|
687 | |
---|
688 | List<IndexInfo.Set> sets = indexInfo.getSets(); |
---|
689 | if (sets != null) { |
---|
690 | for (IndexInfo.Set set : sets) { |
---|
691 | out.writeStartElement(SRU_EXPLAIN_NS, "set"); |
---|
692 | out.writeAttribute("identifier", set.getIdentifier()); |
---|
693 | out.writeAttribute("name", set.getName()); |
---|
694 | writeLocalizedStrings(out, "title", set.getTitle()); |
---|
695 | out.writeEndElement(); // "set" element |
---|
696 | } |
---|
697 | } |
---|
698 | |
---|
699 | List<IndexInfo.Index> indexes = indexInfo.getIndexes(); |
---|
700 | if (indexes != null) { |
---|
701 | for (IndexInfo.Index index : indexes) { |
---|
702 | out.writeStartElement(SRU_EXPLAIN_NS, "index"); |
---|
703 | out.writeAttribute("search", |
---|
704 | index.canSearch() ? "true" : "false"); |
---|
705 | out.writeAttribute("scan", |
---|
706 | index.canScan() ? "true" : "false"); |
---|
707 | out.writeAttribute("sort", |
---|
708 | index.canSort() ? "true" : "false"); |
---|
709 | writeLocalizedStrings(out, "title", index.getTitle()); |
---|
710 | List<IndexInfo.Index.Map> maps = index.getMaps(); |
---|
711 | if (maps != null) { |
---|
712 | for (IndexInfo.Index.Map map : maps) { |
---|
713 | out.writeStartElement(SRU_EXPLAIN_NS, "map"); |
---|
714 | if (map.isPrimary()) { |
---|
715 | out.writeAttribute("primary", "true"); |
---|
716 | } |
---|
717 | out.writeStartElement(SRU_EXPLAIN_NS, "name"); |
---|
718 | out.writeAttribute("set", map.getSet()); |
---|
719 | out.writeCharacters(map.getName()); |
---|
720 | out.writeEndElement(); // "name" element |
---|
721 | out.writeEndElement(); // "map" element |
---|
722 | } |
---|
723 | } |
---|
724 | out.writeEndElement(); // "index" element |
---|
725 | } |
---|
726 | } |
---|
727 | out.writeEndElement(); // "indexInfo" element |
---|
728 | } |
---|
729 | |
---|
730 | // explain/schemaInfo |
---|
731 | final List<SchemaInfo> schemaInfo = |
---|
732 | config.getSchemaInfo(); |
---|
733 | if (schemaInfo != null) { |
---|
734 | out.writeStartElement(SRU_EXPLAIN_NS, "schemaInfo"); |
---|
735 | for (SRUServerConfig.SchemaInfo schema : schemaInfo) { |
---|
736 | out.writeStartElement(SRU_EXPLAIN_NS, "schema"); |
---|
737 | out.writeAttribute("identifier", schema.getIdentifier()); |
---|
738 | out.writeAttribute("name", schema.getName()); |
---|
739 | /* |
---|
740 | * default is "false", so only add attribute if set to true |
---|
741 | */ |
---|
742 | if (schema.getSort() ) { |
---|
743 | out.writeAttribute("sort", "true"); |
---|
744 | } |
---|
745 | /* |
---|
746 | * default is "true", so only add attribute if set to false |
---|
747 | */ |
---|
748 | if (!schema.getRetrieve()) { |
---|
749 | out.writeAttribute("retrieve", "false"); |
---|
750 | } |
---|
751 | writeLocalizedStrings(out, "title", schema.getTitle()); |
---|
752 | out.writeEndElement(); // "schema" element |
---|
753 | } |
---|
754 | out.writeEndElement(); // "schemaInfo" element |
---|
755 | } |
---|
756 | |
---|
757 | // explain/configInfo |
---|
758 | out.writeStartElement(SRU_EXPLAIN_NS, "configInfo"); |
---|
759 | // numberOfRecords (default) |
---|
760 | out.writeStartElement(SRU_EXPLAIN_NS, "default"); |
---|
761 | out.writeAttribute("type", "numberOfRecords"); |
---|
762 | out.writeCharacters(Integer.toString(config.getNumberOfRecords())); |
---|
763 | out.writeEndElement(); // default" element |
---|
764 | |
---|
765 | // maximumRecords (setting) |
---|
766 | out.writeStartElement(SRU_EXPLAIN_NS, "setting"); |
---|
767 | out.writeAttribute("type", "maximumRecords"); |
---|
768 | out.writeCharacters(Integer.toString(config.getMaximumRecords())); |
---|
769 | out.writeEndElement(); // "setting" element |
---|
770 | |
---|
771 | out.writeEndElement(); // "configInfo" element |
---|
772 | |
---|
773 | out.writeEndElement(); // "explain" element |
---|
774 | |
---|
775 | out.endRecord(); |
---|
776 | |
---|
777 | out.writeEndElement(); // "recordData" element |
---|
778 | out.writeEndElement(); // "record" element |
---|
779 | } |
---|
780 | |
---|
781 | |
---|
782 | private void writeDiagnostic(SRUXMLStreamWriter out, |
---|
783 | SRUDiagnostic diagnostic, boolean writeNsDecl) |
---|
784 | throws XMLStreamException { |
---|
785 | if (writeNsDecl) { |
---|
786 | out.setPrefix(SRU_DIAGNOSTIC_PREFIX, SRU_DIAGNOSIC_NS); |
---|
787 | } |
---|
788 | out.writeStartElement(SRU_DIAGNOSIC_NS, "diagnostic"); |
---|
789 | if (writeNsDecl) { |
---|
790 | out.writeNamespace(SRU_DIAGNOSTIC_PREFIX, SRU_DIAGNOSIC_NS); |
---|
791 | } |
---|
792 | out.writeStartElement(SRU_DIAGNOSIC_NS, "uri"); |
---|
793 | out.writeCharacters(SRUConstants.SRU_DIAGNOSTIC_URI_PREFIX); |
---|
794 | out.writeCharacters(Integer.toString(diagnostic.getCode())); |
---|
795 | out.writeEndElement(); // "uri" element |
---|
796 | if (diagnostic.getDetails() != null) { |
---|
797 | out.writeStartElement(SRU_DIAGNOSIC_NS, "details"); |
---|
798 | out.writeCharacters(diagnostic.getDetails()); |
---|
799 | out.writeEndElement(); // "details" element |
---|
800 | } |
---|
801 | if (diagnostic.getMessage() != null) { |
---|
802 | out.writeStartElement(SRU_DIAGNOSIC_NS, "message"); |
---|
803 | out.writeCharacters(diagnostic.getMessage()); |
---|
804 | out.writeEndElement(); // "message" element |
---|
805 | } |
---|
806 | out.writeEndElement(); // "diagnostic" element |
---|
807 | } |
---|
808 | |
---|
809 | |
---|
810 | private void writeEchoedExplainRequest(SRUXMLStreamWriter out, |
---|
811 | SRURequestImpl request) throws XMLStreamException, |
---|
812 | SRUException { |
---|
813 | // echoedSearchRetrieveRequest |
---|
814 | out.writeStartElement(SRU_NS, "echoedExplainRequest"); |
---|
815 | |
---|
816 | // echoedExplainRequest/version |
---|
817 | if (request.getRawVersion() != null) { |
---|
818 | writeVersion(out, request.getRawVersion()); |
---|
819 | } |
---|
820 | |
---|
821 | // echoedExplainRequest/recordPacking |
---|
822 | if (request.getRawRecordPacking() != null) { |
---|
823 | writeRecordPacking(out, request.getRawRecordPacking()); |
---|
824 | } |
---|
825 | |
---|
826 | // echoedExplainRequest/stylesheet |
---|
827 | if (request.getStylesheet() != null) { |
---|
828 | out.writeStartElement(SRU_NS, "stylesheet"); |
---|
829 | out.writeCharacters(request.getStylesheet()); |
---|
830 | out.writeEndElement(); // "stylesheet" element |
---|
831 | } |
---|
832 | |
---|
833 | // echoedExplainRequest/baseUrl (SRU 1.2 only) |
---|
834 | if (request.isVersion(SRUVersion.VERSION_1_2)) { |
---|
835 | writeBaseUrl(out, request); |
---|
836 | } |
---|
837 | |
---|
838 | out.writeEndElement(); // "echoedExplainRequest" element |
---|
839 | } |
---|
840 | |
---|
841 | |
---|
842 | private void writeEchoedScanRequest(SRUXMLStreamWriter out, |
---|
843 | SRURequestImpl request, CQLNode cql) throws XMLStreamException, |
---|
844 | SRUException { |
---|
845 | // echoedScanRequest |
---|
846 | out.writeStartElement(SRU_NS, "echoedScanRequest"); |
---|
847 | |
---|
848 | // echoedScanRequest/version |
---|
849 | if (request.getRawVersion() != null) { |
---|
850 | writeVersion(out, request.getRawVersion()); |
---|
851 | } |
---|
852 | |
---|
853 | // echoedScanRequest/scanClause |
---|
854 | out.writeStartElement(SRU_NS, "scanClause"); |
---|
855 | out.writeCharacters(request.getRawScanClause()); |
---|
856 | out.writeEndElement(); // "query" |
---|
857 | |
---|
858 | // echoedScanRequest/xScanClause |
---|
859 | out.setDefaultNamespace(SRU_XCQL_NS); |
---|
860 | out.writeStartElement(SRU_NS, "xScanClause"); |
---|
861 | out.writeDefaultNamespace(SRU_XCQL_NS); |
---|
862 | out.writeXCQL(cql, false); |
---|
863 | out.writeEndElement(); // "xScanClause" element |
---|
864 | |
---|
865 | // echoedScanRequest/responsePosition |
---|
866 | if (request.getResponsePosition() != -1) { |
---|
867 | out.writeStartElement(SRU_NS, "responsePosition"); |
---|
868 | out.writeCharacters( |
---|
869 | Integer.toString(request.getResponsePosition())); |
---|
870 | out.writeEndElement(); // "responsePosition" element |
---|
871 | } |
---|
872 | |
---|
873 | // echoedScanRequest/maximumTerms |
---|
874 | if (request.getMaximumTerms() != -1) { |
---|
875 | out.writeStartElement(SRU_NS, "maximumTerms"); |
---|
876 | out.writeCharacters(Integer.toString(request.getMaximumTerms())); |
---|
877 | out.writeEndElement(); // "maximumTerms" element |
---|
878 | } |
---|
879 | |
---|
880 | // echoedScanRequest/stylesheet |
---|
881 | if (request.getStylesheet() != null) { |
---|
882 | out.writeStartElement(SRU_NS, "stylesheet"); |
---|
883 | out.writeCharacters(request.getStylesheet()); |
---|
884 | out.writeEndElement(); // "stylesheet" element |
---|
885 | } |
---|
886 | |
---|
887 | // echoedScanRequest/baseUrl (SRU 1.2 only) |
---|
888 | if (request.isVersion(SRUVersion.VERSION_1_2)) { |
---|
889 | writeBaseUrl(out, request); |
---|
890 | } |
---|
891 | |
---|
892 | out.writeEndElement(); // "echoedScanRequest" element |
---|
893 | } |
---|
894 | |
---|
895 | |
---|
896 | private void writeEchoedSearchRetrieveRequest(SRUXMLStreamWriter out, |
---|
897 | SRURequestImpl request, CQLNode cql) throws XMLStreamException, |
---|
898 | SRUException { |
---|
899 | // echoedSearchRetrieveRequest |
---|
900 | out.writeStartElement(SRU_NS, "echoedSearchRetrieveRequest"); |
---|
901 | |
---|
902 | // echoedSearchRetrieveRequest/version |
---|
903 | if (request.getRawVersion() != null) { |
---|
904 | writeVersion(out, request.getRawVersion()); |
---|
905 | } |
---|
906 | |
---|
907 | // echoedSearchRetrieveRequest/query |
---|
908 | out.writeStartElement(SRU_NS, "query"); |
---|
909 | out.writeCharacters(request.getRawQuery()); |
---|
910 | out.writeEndElement(); // "query" |
---|
911 | |
---|
912 | // echoedSearchRetrieveRequest/xQuery |
---|
913 | out.setDefaultNamespace(SRU_XCQL_NS); |
---|
914 | out.writeStartElement(SRU_NS, "xQuery"); |
---|
915 | out.writeDefaultNamespace(SRU_XCQL_NS); |
---|
916 | out.writeXCQL(cql, true); |
---|
917 | out.writeEndElement(); // "xQuery" element |
---|
918 | |
---|
919 | // echoedSearchRetrieveRequest/startRecord |
---|
920 | if (request.getStartRecord() > 0) { |
---|
921 | out.writeStartElement(SRU_NS, "startRecord"); |
---|
922 | out.writeCharacters(Integer.toString(request.getStartRecord())); |
---|
923 | out.writeEndElement(); // "startRecord" element |
---|
924 | } |
---|
925 | |
---|
926 | // echoedSearchRetrieveRequest/maximumRecords |
---|
927 | if (request.getRawMaximumRecords() > 0) { |
---|
928 | out.writeStartElement(SRU_NS, "maximumRecords"); |
---|
929 | out.writeCharacters( |
---|
930 | Integer.toString(request.getRawMaximumRecords())); |
---|
931 | out.writeEndElement(); // "startRecord" element |
---|
932 | } |
---|
933 | |
---|
934 | // echoedSearchRetrieveRequest/recordPacking |
---|
935 | if (request.getRawRecordPacking() != null) { |
---|
936 | writeRecordPacking(out, request.getRawRecordPacking()); |
---|
937 | } |
---|
938 | |
---|
939 | // echoedSearchRetrieveRequest/recordSchema |
---|
940 | if (request.getRecordSchemaName() != null) { |
---|
941 | out.writeStartElement(SRU_NS, "recordSchema"); |
---|
942 | out.writeCharacters(request.getRecordSchemaName()); |
---|
943 | out.writeEndElement(); // "recordSchema" element |
---|
944 | } |
---|
945 | |
---|
946 | // echoedSearchRetrieveRequest/recordXPath (1.1) |
---|
947 | if (request.isVersion(SRUVersion.VERSION_1_1) && |
---|
948 | (request.getRecordXPath() != null)) { |
---|
949 | out.writeStartElement(SRU_NS, "recordXPath"); |
---|
950 | out.writeCharacters(request.getRecordXPath()); |
---|
951 | out.writeEndElement(); // "recordXPath" element |
---|
952 | } |
---|
953 | |
---|
954 | // echoedSearchRetrieveRequest/resultSetTTL |
---|
955 | if (request.getResultSetTTL() > 0) { |
---|
956 | out.writeStartElement(SRU_NS, "resultSetTTL"); |
---|
957 | out.writeCharacters(Long.toString(request.getResultSetTTL())); |
---|
958 | out.writeEndElement(); // "resultSetTTL" element |
---|
959 | } |
---|
960 | |
---|
961 | // echoedSearchRetrieveRequest/sortKeys |
---|
962 | if (request.isVersion(SRUVersion.VERSION_1_1) && |
---|
963 | (request.getSortKeys() != null)) { |
---|
964 | out.writeStartElement(SRU_NS, "sortKeys"); |
---|
965 | out.writeCharacters(request.getSortKeys()); |
---|
966 | out.writeEndElement(); // "sortKeys" element |
---|
967 | } |
---|
968 | |
---|
969 | // echoedSearchRetrieveRequest/xsortKeys |
---|
970 | |
---|
971 | // echoedSearchRetrieveRequest/stylesheet |
---|
972 | if (request.getStylesheet() != null) { |
---|
973 | out.writeStartElement(SRU_NS, "stylesheet"); |
---|
974 | out.writeCharacters(request.getStylesheet()); |
---|
975 | out.writeEndElement(); // "stylesheet" element |
---|
976 | } |
---|
977 | |
---|
978 | // echoedSearchRetrieveRequest/baseUrl (SRU 1.2 only) |
---|
979 | if (request.isVersion(SRUVersion.VERSION_1_2)) { |
---|
980 | writeBaseUrl(out, request); |
---|
981 | } |
---|
982 | |
---|
983 | out.writeEndElement(); // "echoedSearchRetrieveRequest" element |
---|
984 | } |
---|
985 | |
---|
986 | |
---|
987 | private void writeVersion(SRUXMLStreamWriter out, SRUVersion version) |
---|
988 | throws XMLStreamException { |
---|
989 | out.writeStartElement(SRU_NS, "version"); |
---|
990 | switch (version) { |
---|
991 | case VERSION_1_1: |
---|
992 | out.writeCharacters("1.1"); |
---|
993 | break; |
---|
994 | case VERSION_1_2: |
---|
995 | out.writeCharacters("1.2"); |
---|
996 | break; |
---|
997 | } // switch |
---|
998 | out.writeEndElement(); // "version" element |
---|
999 | } |
---|
1000 | |
---|
1001 | |
---|
1002 | private void writeRecordPacking(SRUXMLStreamWriter out, |
---|
1003 | SRURecordPacking recordPacking) throws XMLStreamException { |
---|
1004 | out.writeStartElement(SRU_NS, "recordPacking"); |
---|
1005 | switch (recordPacking) { |
---|
1006 | case XML: |
---|
1007 | out.writeCharacters("xml"); |
---|
1008 | break; |
---|
1009 | case STRING: |
---|
1010 | out.writeCharacters("string"); |
---|
1011 | break; |
---|
1012 | } // switch |
---|
1013 | out.writeEndElement(); // "recordPacking" element |
---|
1014 | } |
---|
1015 | |
---|
1016 | |
---|
1017 | private void writeBaseUrl(SRUXMLStreamWriter out, |
---|
1018 | SRURequest request) throws XMLStreamException { |
---|
1019 | out.writeStartElement(SRU_NS, "baseUrl"); |
---|
1020 | out.writeCharacters(request.getProtocolScheme()); |
---|
1021 | out.writeCharacters(config.getBaseUrl()); |
---|
1022 | out.writeEndElement(); // "baseUrl" element |
---|
1023 | } |
---|
1024 | |
---|
1025 | |
---|
1026 | private void writeLocalizedStrings(XMLStreamWriter writer, String name, |
---|
1027 | List<LocalizedString> list) throws XMLStreamException { |
---|
1028 | if ((list != null) && !list.isEmpty()) { |
---|
1029 | for (LocalizedString item : list) { |
---|
1030 | writer.writeStartElement(SRU_EXPLAIN_NS, name); |
---|
1031 | if (item.getLang() != null) { |
---|
1032 | writer.writeAttribute("lang", item.getLang()); |
---|
1033 | } |
---|
1034 | if (item.isPrimary()) { |
---|
1035 | writer.writeAttribute("primary", "true"); |
---|
1036 | } |
---|
1037 | writer.writeCharacters(item.getValue()); |
---|
1038 | writer.writeEndElement(); |
---|
1039 | } |
---|
1040 | } |
---|
1041 | } |
---|
1042 | |
---|
1043 | |
---|
1044 | private SRUXMLStreamWriter createXMLStreamWriter(OutputStream out, |
---|
1045 | SRURecordPacking recordPacking, int indent) throws SRUException { |
---|
1046 | try { |
---|
1047 | return new SRUXMLStreamWriter(out, writerFactory, recordPacking, |
---|
1048 | indent); |
---|
1049 | } catch (Exception e) { |
---|
1050 | throw new SRUException(SRUConstants.SRU_GENERAL_SYSTEM_ERROR, |
---|
1051 | "Error creating output stream.", e); |
---|
1052 | |
---|
1053 | } |
---|
1054 | } |
---|
1055 | |
---|
1056 | } // class SRUService |
---|