1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2013 Index Data
3 * See the file LICENSE for details.
7 * \brief Implements ZOOM SRU
19 #include <yaz/pquery.h>
22 static void set_SRU_error(ZOOM_connection c, Z_SRW_diagnostic *d)
24 const char *uri = d->uri;
26 ZOOM_set_dset_error(c, ZOOM_uri_to_code(uri), uri, d->details, 0);
32 static zoom_ret send_srw(ZOOM_connection c, Z_SRW_PDU *sr)
35 const char *database = ZOOM_options_get(c->options, "databaseName");
37 gdu = z_get_HTTP_Request_uri(c->odr_out, c->host_port,
41 if (c->sru_mode == zoom_sru_get)
43 yaz_sru_get_encode(gdu->u.HTTP_Request, sr, c->odr_out, c->charset);
45 else if (c->sru_mode == zoom_sru_post)
47 yaz_sru_post_encode(gdu->u.HTTP_Request, sr, c->odr_out, c->charset);
49 else if (c->sru_mode == zoom_sru_soap)
51 yaz_sru_soap_encode(gdu->u.HTTP_Request, sr, c->odr_out, c->charset);
53 else if (c->sru_mode == zoom_sru_solr)
55 yaz_solr_encode_request(gdu->u.HTTP_Request, sr, c->odr_out, c->charset);
57 return ZOOM_send_GDU(c, gdu);
62 static Z_SRW_PDU *ZOOM_srw_get_pdu(ZOOM_connection c, int type)
64 Z_SRW_PDU *sr = yaz_srw_get_pdu(c->odr_out, type, c->sru_version);
65 sr->username = c->user;
66 sr->password = c->password;
72 zoom_ret ZOOM_connection_srw_send_scan(ZOOM_connection c)
76 const char *option_val = 0;
81 assert (c->tasks->which == ZOOM_TASK_SCAN);
82 scan = c->tasks->u.scan.scan;
84 sr = ZOOM_srw_get_pdu(c, Z_SRW_scan_request);
86 z_query = ZOOM_query_get_Z_Query(scan->query);
87 /* SRU scan can only carry CQL and PQF */
88 if (z_query->which == Z_Query_type_104)
90 sr->u.scan_request->query_type = Z_SRW_query_type_cql;
91 sr->u.scan_request->scanClause.cql =
92 odr_strdup(c->odr_out, ZOOM_query_get_query_string(scan->query));
94 else if (z_query->which == Z_Query_type_1
95 || z_query->which == Z_Query_type_101)
97 sr->u.scan_request->query_type = Z_SRW_query_type_pqf;
98 sr->u.scan_request->scanClause.pqf =
99 odr_strdup(c->odr_out, ZOOM_query_get_query_string(scan->query));
103 ZOOM_set_error(c, ZOOM_ERROR_UNSUPPORTED_QUERY, 0);
104 return zoom_complete;
107 sr->u.scan_request->maximumTerms = odr_intdup(
108 c->odr_out, ZOOM_options_get_int(scan->options, "number", 10));
110 sr->u.scan_request->responsePosition = odr_intdup(
111 c->odr_out, ZOOM_options_get_int(scan->options, "position", 1));
113 option_val = ZOOM_options_get(scan->options, "extraArgs");
114 yaz_encode_sru_extra(sr, c->odr_out, option_val);
115 return send_srw(c, sr);
118 zoom_ret ZOOM_connection_srw_send_scan(ZOOM_connection c)
120 return zoom_complete;
125 zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c)
127 const char *facets = 0;
130 ZOOM_resultset resultset = 0;
132 const char *option_val = 0;
133 const char *schema = 0;
135 Z_FacetList *facet_list = 0;
136 if (c->error) /* don't continue on error */
137 return zoom_complete;
139 switch(c->tasks->which)
141 case ZOOM_TASK_SEARCH:
142 resultset = c->tasks->u.search.resultset;
143 if (!resultset->setname)
144 resultset->setname = xstrdup("default");
145 ZOOM_options_set(resultset->options, "setname", resultset->setname);
146 start = &c->tasks->u.search.start;
147 count = &c->tasks->u.search.count;
148 facets = ZOOM_options_get(resultset->options, "facets");
150 facet_list = yaz_pqf_parse_facet_list(c->odr_out, facets);
151 schema = c->tasks->u.search.schema;
153 case ZOOM_TASK_RETRIEVE:
154 resultset = c->tasks->u.retrieve.resultset;
155 schema = c->tasks->u.retrieve.schema;
157 start = &c->tasks->u.retrieve.start;
158 count = &c->tasks->u.retrieve.count;
160 if (*start >= resultset->size)
161 return zoom_complete;
162 if (*start + *count > resultset->size)
163 *count = resultset->size - *start;
165 for (i = 0; i < *count; i++)
168 ZOOM_record_cache_lookup(resultset, i + *start,
169 c->tasks->u.retrieve.syntax,
170 c->tasks->u.retrieve.elementSetName,
176 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_RECV_RECORD);
177 ZOOM_connection_put_event(c, event);
184 return zoom_complete;
187 return zoom_complete;
189 assert(resultset->query);
191 sr = ZOOM_srw_get_pdu(c, Z_SRW_searchRetrieve_request);
192 z_query = ZOOM_query_get_Z_Query(resultset->query);
194 if (z_query->which == Z_Query_type_104
195 && z_query->u.type_104->which == Z_External_CQL)
197 sr->u.request->query_type = Z_SRW_query_type_cql;
198 sr->u.request->query.cql = z_query->u.type_104->u.cql;
200 else if (z_query->which == Z_Query_type_1 && z_query->u.type_1)
202 sr->u.request->query_type = Z_SRW_query_type_pqf;
203 sr->u.request->query.pqf =
204 odr_strdup(c->odr_out,
205 ZOOM_query_get_query_string(resultset->query));
209 ZOOM_set_error(c, ZOOM_ERROR_UNSUPPORTED_QUERY, 0);
210 return zoom_complete;
213 option_val = ZOOM_query_get_sru11(resultset->query);
216 sr->u.request->sort_type = Z_SRW_sort_type_sort;
217 sr->u.request->sort.sortKeys = odr_strdup(c->odr_out, option_val);
219 sr->u.request->startRecord = odr_intdup(c->odr_out, *start + 1);
220 sr->u.request->maximumRecords = odr_intdup(
221 c->odr_out, (resultset->step > 0 && resultset->step < *count) ?
222 resultset->step : *count);
223 sr->u.request->recordSchema = odr_strdup_null(c->odr_out, schema);
224 sr->u.request->facetList = facet_list;
226 option_val = ZOOM_resultset_option_get(resultset, "recordPacking");
228 sr->u.request->recordPacking = odr_strdup(c->odr_out, option_val);
230 option_val = ZOOM_resultset_option_get(resultset, "extraArgs");
231 yaz_encode_sru_extra(sr, c->odr_out, option_val);
232 return send_srw(c, sr);
235 zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c)
237 return zoom_complete;
242 static zoom_ret handle_srw_response(ZOOM_connection c,
243 Z_SRW_searchRetrieveResponse *res)
245 ZOOM_resultset resultset = 0;
250 const char *syntax, *elementSetName, *schema;
253 return zoom_complete;
255 switch(c->tasks->which)
257 case ZOOM_TASK_SEARCH:
258 resultset = c->tasks->u.search.resultset;
259 start = &c->tasks->u.search.start;
260 count = &c->tasks->u.search.count;
261 syntax = c->tasks->u.search.syntax;
262 elementSetName = c->tasks->u.search.elementSetName;
263 schema = c->tasks->u.search.schema;
264 /* Required not for reporting client hit count multiple times into session */
265 if (!c->tasks->u.search.recv_search_fired) {
266 yaz_log(YLOG_DEBUG, "posting ZOOM_EVENT_RECV_SEARCH");
267 event = ZOOM_Event_create(ZOOM_EVENT_RECV_SEARCH);
268 ZOOM_connection_put_event(c, event);
269 c->tasks->u.search.recv_search_fired = 1;
272 ZOOM_handle_facet_list(resultset, res->facetList);
274 case ZOOM_TASK_RETRIEVE:
275 resultset = c->tasks->u.retrieve.resultset;
276 start = &c->tasks->u.retrieve.start;
277 count = &c->tasks->u.retrieve.count;
278 syntax = c->tasks->u.retrieve.syntax;
279 elementSetName = c->tasks->u.retrieve.elementSetName;
280 schema = c->tasks->u.retrieve.schema;
283 return zoom_complete;
288 if (res->resultSetId)
289 ZOOM_resultset_option_set(resultset, "resultSetId", res->resultSetId);
291 yaz_log(c->log_details, "%p handle_srw_response got SRW response OK", c);
293 if (res->num_diagnostics > 0)
295 set_SRU_error(c, &res->diagnostics[0]);
299 if (res->numberOfRecords) {
300 resultset->size = *res->numberOfRecords;
302 if (res->suggestions) {
303 ZOOM_resultset_option_set(resultset, "suggestions", res->suggestions);
305 for (i = 0; i<res->num_records; i++)
307 int pos = *start + i;
308 Z_SRW_record *sru_rec;
309 Z_SRW_diagnostic *diag = 0;
312 Z_NamePlusRecord *npr = (Z_NamePlusRecord *)
313 odr_malloc(c->odr_in, sizeof(Z_NamePlusRecord));
315 /* only trust recordPosition if >= calculated position */
316 if (res->records[i].recordPosition &&
317 *res->records[i].recordPosition >= pos + 1)
318 pos = *res->records[i].recordPosition - 1;
320 sru_rec = &res->records[i];
322 npr->databaseName = 0;
323 npr->which = Z_NamePlusRecord_databaseRecord;
324 npr->u.databaseRecord = (Z_External *)
325 odr_malloc(c->odr_in, sizeof(Z_External));
326 npr->u.databaseRecord->descriptor = 0;
327 npr->u.databaseRecord->direct_reference =
328 odr_oiddup(c->odr_in, yaz_oid_recsyn_xml);
329 npr->u.databaseRecord->indirect_reference = 0;
330 npr->u.databaseRecord->which = Z_External_octet;
332 npr->u.databaseRecord->u.octet_aligned = (Odr_oct *)
333 odr_malloc(c->odr_in, sizeof(Odr_oct));
334 npr->u.databaseRecord->u.octet_aligned->buf = (unsigned char*)
335 sru_rec->recordData_buf;
336 npr->u.databaseRecord->u.octet_aligned->len =
337 npr->u.databaseRecord->u.octet_aligned->size =
338 sru_rec->recordData_len;
340 if (sru_rec->recordSchema
341 && !strcmp(sru_rec->recordSchema,
342 "info:srw/schema/1/diagnostics-v1.1"))
344 sru_decode_surrogate_diagnostics(sru_rec->recordData_buf,
345 sru_rec->recordData_len,
349 ZOOM_record_cache_add(resultset, npr, pos, syntax, elementSetName,
354 if (*count + *start > resultset->size)
355 *count = resultset->size - *start;
356 yaz_log(YLOG_DEBUG, "SRU result set size " ODR_INT_PRINTF " start %d count %d", resultset->size, *start, *count);
359 nmem = odr_extract_mem(c->odr_in);
360 nmem_transfer(odr_getmem(resultset->odr), nmem);
364 return ZOOM_connection_srw_send_search(c);
366 return zoom_complete;
371 static void handle_srw_scan_response(ZOOM_connection c,
372 Z_SRW_scanResponse *res)
374 NMEM nmem = odr_extract_mem(c->odr_in);
377 if (!c->tasks || c->tasks->which != ZOOM_TASK_SCAN)
379 scan = c->tasks->u.scan.scan;
381 if (res->num_diagnostics > 0)
382 set_SRU_error(c, &res->diagnostics[0]);
384 scan->scan_response = 0;
385 scan->srw_scan_response = res;
386 nmem_transfer(odr_getmem(scan->odr), nmem);
388 ZOOM_options_set_int(scan->options, "number", res->num_terms);
393 int ZOOM_handle_sru(ZOOM_connection c, Z_HTTP_Response *hres,
394 zoom_ret *cret, char **addinfo)
399 /* not redirect (normal response) */
400 if (!yaz_srw_check_content_type(hres))
402 *addinfo = "content-type";
405 else if (c->sru_mode == zoom_sru_solr)
408 ret = yaz_solr_decode_response(c->odr_in, hres, &sr);
411 if (sr->which == Z_SRW_searchRetrieve_response)
412 *cret = handle_srw_response(c, sr->u.response);
413 else if (sr->which == Z_SRW_scan_response)
414 handle_srw_scan_response(c, sr->u.scan_response);
419 Z_SOAP *soap_package = 0;
421 Z_SOAP_Handler soap_handlers[3] = {
422 {YAZ_XMLNS_SRU_v1_response, 0, (Z_SOAP_fun) yaz_srw_codec},
423 {YAZ_XMLNS_SRU_v2_response, 0, (Z_SOAP_fun) yaz_srw_codec},
426 ret = z_soap_codec(o, &soap_package,
427 &hres->content_buf, &hres->content_len,
429 if (!ret && soap_package->which == Z_SOAP_generic)
431 Z_SRW_PDU *sr = (Z_SRW_PDU*) soap_package->u.generic->p;
433 ZOOM_options_set(c->options, "sru_version", sr->srw_version);
434 ZOOM_options_setl(c->options, "sru_extra_response_data",
435 sr->extraResponseData_buf, sr->extraResponseData_len);
436 if (sr->which == Z_SRW_searchRetrieve_response)
437 *cret = handle_srw_response(c, sr->u.response);
438 else if (sr->which == Z_SRW_scan_response)
439 handle_srw_scan_response(c, sr->u.scan_response);
443 else if (!ret && (soap_package->which == Z_SOAP_fault
444 || soap_package->which == Z_SOAP_error))
446 ZOOM_set_HTTP_error(c, hres->code,
447 soap_package->u.fault->fault_code,
448 soap_package->u.fault->fault_string);
452 size_t max_chars = 1000;
453 size_t sz = hres->content_len;
454 if (sz > max_chars - 1)
456 *addinfo = odr_malloc(c->odr_in, sz + 4);
457 memcpy(*addinfo, hres->content_buf, sz);
459 strcpy(*addinfo + sz, "...");
461 strcpy(*addinfo + sz, "");
474 * c-file-style: "Stroustrup"
475 * indent-tabs-mode: nil
477 * vim: shiftwidth=4 tabstop=8 expandtab