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