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 if (c->url_authentication && c->user)
67 Z_SRW_extra_arg **ea = &sr->extra_args;
70 *ea = (Z_SRW_extra_arg *) odr_malloc(c->odr_out, sizeof(**ea));
71 (*ea)->name = "x-username";
72 (*ea)->value = c->user;
76 *ea = (Z_SRW_extra_arg *) odr_malloc(c->odr_out, sizeof(**ea));
77 (*ea)->name = "x-password";
78 (*ea)->value = c->password;
85 sr->username = c->user;
86 sr->password = c->password;
93 zoom_ret ZOOM_connection_srw_send_scan(ZOOM_connection c)
97 const char *option_val = 0;
101 return zoom_complete;
102 assert (c->tasks->which == ZOOM_TASK_SCAN);
103 scan = c->tasks->u.scan.scan;
105 sr = ZOOM_srw_get_pdu(c, Z_SRW_scan_request);
107 z_query = ZOOM_query_get_Z_Query(scan->query);
108 /* SRU scan can only carry CQL and PQF */
109 if (z_query->which == Z_Query_type_104)
111 sr->u.scan_request->queryType = "cql";
112 sr->u.scan_request->scanClause =
113 odr_strdup(c->odr_out, ZOOM_query_get_query_string(scan->query));
115 else if (z_query->which == Z_Query_type_1
116 || z_query->which == Z_Query_type_101)
118 sr->u.scan_request->queryType = "pqf";
119 sr->u.scan_request->scanClause =
120 odr_strdup(c->odr_out, ZOOM_query_get_query_string(scan->query));
124 ZOOM_set_error(c, ZOOM_ERROR_UNSUPPORTED_QUERY, 0);
125 return zoom_complete;
128 sr->u.scan_request->maximumTerms = odr_intdup(
129 c->odr_out, ZOOM_options_get_int(scan->options, "number", 10));
131 sr->u.scan_request->responsePosition = odr_intdup(
132 c->odr_out, ZOOM_options_get_int(scan->options, "position", 1));
134 option_val = ZOOM_options_get(scan->options, "extraArgs");
135 yaz_encode_sru_extra(sr, c->odr_out, option_val);
136 return send_srw(c, sr);
139 zoom_ret ZOOM_connection_srw_send_scan(ZOOM_connection c)
141 return zoom_complete;
146 zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c)
148 const char *facets = 0;
151 ZOOM_resultset resultset = 0;
153 const char *option_val = 0;
154 const char *schema = 0;
156 Z_FacetList *facet_list = 0;
157 if (c->error) /* don't continue on error */
158 return zoom_complete;
160 switch(c->tasks->which)
162 case ZOOM_TASK_SEARCH:
163 resultset = c->tasks->u.search.resultset;
164 if (!resultset->setname)
165 resultset->setname = xstrdup("default");
166 ZOOM_options_set(resultset->options, "setname", resultset->setname);
167 start = &c->tasks->u.search.start;
168 count = &c->tasks->u.search.count;
169 facets = ZOOM_options_get(resultset->options, "facets");
171 facet_list = yaz_pqf_parse_facet_list(c->odr_out, facets);
172 schema = c->tasks->u.search.schema;
174 case ZOOM_TASK_RETRIEVE:
175 resultset = c->tasks->u.retrieve.resultset;
176 schema = c->tasks->u.retrieve.schema;
178 start = &c->tasks->u.retrieve.start;
179 count = &c->tasks->u.retrieve.count;
181 if (*start >= resultset->size)
182 return zoom_complete;
183 if (*start + *count > resultset->size)
184 *count = resultset->size - *start;
186 for (i = 0; i < *count; i++)
189 ZOOM_record_cache_lookup(resultset, i + *start,
190 c->tasks->u.retrieve.syntax,
191 c->tasks->u.retrieve.elementSetName,
197 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_RECV_RECORD);
198 ZOOM_connection_put_event(c, event);
205 return zoom_complete;
208 return zoom_complete;
210 assert(resultset->query);
212 sr = ZOOM_srw_get_pdu(c, Z_SRW_searchRetrieve_request);
213 z_query = ZOOM_query_get_Z_Query(resultset->query);
215 if (z_query->which == Z_Query_type_104
216 && z_query->u.type_104->which == Z_External_CQL)
218 sr->u.request->queryType = "cql";
219 sr->u.request->query = z_query->u.type_104->u.cql;
221 else if (z_query->which == Z_Query_type_1 && z_query->u.type_1)
223 sr->u.request->queryType = "pqf";
224 sr->u.request->query =
225 odr_strdup(c->odr_out,
226 ZOOM_query_get_query_string(resultset->query));
230 ZOOM_set_error(c, ZOOM_ERROR_UNSUPPORTED_QUERY, 0);
231 return zoom_complete;
234 option_val = ZOOM_query_get_sru11(resultset->query);
237 sr->u.request->sort_type = Z_SRW_sort_type_sort;
238 sr->u.request->sort.sortKeys = odr_strdup(c->odr_out, option_val);
240 sr->u.request->startRecord = odr_intdup(c->odr_out, *start + 1);
241 sr->u.request->maximumRecords = odr_intdup(
242 c->odr_out, (resultset->step > 0 && resultset->step < *count) ?
243 resultset->step : *count);
244 sr->u.request->recordSchema = odr_strdup_null(c->odr_out, schema);
245 sr->u.request->facetList = facet_list;
247 option_val = ZOOM_resultset_option_get(resultset, "recordPacking");
249 sr->u.request->recordPacking = odr_strdup(c->odr_out, option_val);
251 option_val = ZOOM_resultset_option_get(resultset, "extraArgs");
252 yaz_encode_sru_extra(sr, c->odr_out, option_val);
253 return send_srw(c, sr);
256 zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c)
258 return zoom_complete;
263 static zoom_ret handle_srw_response(ZOOM_connection c,
264 Z_SRW_searchRetrieveResponse *res)
266 ZOOM_resultset resultset = 0;
271 const char *syntax, *elementSetName, *schema;
274 return zoom_complete;
276 switch(c->tasks->which)
278 case ZOOM_TASK_SEARCH:
279 resultset = c->tasks->u.search.resultset;
280 start = &c->tasks->u.search.start;
281 count = &c->tasks->u.search.count;
282 syntax = c->tasks->u.search.syntax;
283 elementSetName = c->tasks->u.search.elementSetName;
284 schema = c->tasks->u.search.schema;
285 /* Required not for reporting client hit count multiple times into session */
286 if (!c->tasks->u.search.recv_search_fired) {
287 yaz_log(YLOG_DEBUG, "posting ZOOM_EVENT_RECV_SEARCH");
288 event = ZOOM_Event_create(ZOOM_EVENT_RECV_SEARCH);
289 ZOOM_connection_put_event(c, event);
290 c->tasks->u.search.recv_search_fired = 1;
293 ZOOM_handle_facet_list(resultset, res->facetList);
295 case ZOOM_TASK_RETRIEVE:
296 resultset = c->tasks->u.retrieve.resultset;
297 start = &c->tasks->u.retrieve.start;
298 count = &c->tasks->u.retrieve.count;
299 syntax = c->tasks->u.retrieve.syntax;
300 elementSetName = c->tasks->u.retrieve.elementSetName;
301 schema = c->tasks->u.retrieve.schema;
304 return zoom_complete;
309 if (res->resultSetId)
310 ZOOM_resultset_option_set(resultset, "resultSetId", res->resultSetId);
312 yaz_log(c->log_details, "%p handle_srw_response got SRW response OK", c);
314 if (res->num_diagnostics > 0)
316 set_SRU_error(c, &res->diagnostics[0]);
320 if (res->numberOfRecords) {
321 resultset->size = *res->numberOfRecords;
323 if (res->suggestions) {
324 ZOOM_resultset_option_set(resultset, "suggestions", res->suggestions);
326 for (i = 0; i<res->num_records; i++)
328 int pos = *start + i;
329 Z_SRW_record *sru_rec;
330 Z_SRW_diagnostic *diag = 0;
333 Z_NamePlusRecord *npr = (Z_NamePlusRecord *)
334 odr_malloc(c->odr_in, sizeof(Z_NamePlusRecord));
336 /* only trust recordPosition if >= calculated position */
337 if (res->records[i].recordPosition &&
338 *res->records[i].recordPosition >= pos + 1)
339 pos = *res->records[i].recordPosition - 1;
341 sru_rec = &res->records[i];
343 npr->databaseName = 0;
344 npr->which = Z_NamePlusRecord_databaseRecord;
345 npr->u.databaseRecord = (Z_External *)
346 odr_malloc(c->odr_in, sizeof(Z_External));
347 npr->u.databaseRecord->descriptor = 0;
348 npr->u.databaseRecord->direct_reference =
349 odr_oiddup(c->odr_in, yaz_oid_recsyn_xml);
350 npr->u.databaseRecord->indirect_reference = 0;
351 npr->u.databaseRecord->which = Z_External_octet;
353 npr->u.databaseRecord->u.octet_aligned =
354 odr_create_Odr_oct(c->odr_in,
355 sru_rec->recordData_buf,
356 sru_rec->recordData_len);
357 if (sru_rec->recordSchema
358 && !strcmp(sru_rec->recordSchema,
359 "info:srw/schema/1/diagnostics-v1.1"))
361 sru_decode_surrogate_diagnostics(sru_rec->recordData_buf,
362 sru_rec->recordData_len,
366 ZOOM_record_cache_add(resultset, npr, pos, syntax, elementSetName,
371 if (*count + *start > resultset->size)
372 *count = resultset->size - *start;
373 yaz_log(YLOG_DEBUG, "SRU result set size " ODR_INT_PRINTF " start %d count %d", resultset->size, *start, *count);
376 nmem = odr_extract_mem(c->odr_in);
377 nmem_transfer(odr_getmem(resultset->odr), nmem);
381 return ZOOM_connection_srw_send_search(c);
383 return zoom_complete;
388 static void handle_srw_scan_response(ZOOM_connection c,
389 Z_SRW_scanResponse *res)
391 NMEM nmem = odr_extract_mem(c->odr_in);
394 if (!c->tasks || c->tasks->which != ZOOM_TASK_SCAN)
396 scan = c->tasks->u.scan.scan;
398 if (res->num_diagnostics > 0)
399 set_SRU_error(c, &res->diagnostics[0]);
401 scan->scan_response = 0;
402 scan->srw_scan_response = res;
403 nmem_transfer(odr_getmem(scan->odr), nmem);
405 ZOOM_options_set_int(scan->options, "number", res->num_terms);
410 int ZOOM_handle_sru(ZOOM_connection c, Z_HTTP_Response *hres,
411 zoom_ret *cret, char **addinfo)
416 /* not redirect (normal response) */
417 if (!yaz_srw_check_content_type(hres))
419 *addinfo = "content-type";
422 else if (c->sru_mode == zoom_sru_solr)
425 ret = yaz_solr_decode_response(c->odr_in, hres, &sr);
428 if (sr->which == Z_SRW_searchRetrieve_response)
429 *cret = handle_srw_response(c, sr->u.response);
430 else if (sr->which == Z_SRW_scan_response)
431 handle_srw_scan_response(c, sr->u.scan_response);
436 Z_SOAP *soap_package = 0;
438 Z_SOAP_Handler soap_handlers[3] = {
439 {YAZ_XMLNS_SRU_v1_response, 0, (Z_SOAP_fun) yaz_srw_codec},
440 {YAZ_XMLNS_SRU_v2_mask, 0, (Z_SOAP_fun) yaz_srw_codec},
443 ret = z_soap_codec(o, &soap_package,
444 &hres->content_buf, &hres->content_len,
446 if (!ret && soap_package->which == Z_SOAP_generic)
448 Z_SRW_PDU *sr = (Z_SRW_PDU*) soap_package->u.generic->p;
450 ZOOM_options_set(c->options, "sru_version", sr->srw_version);
451 ZOOM_options_setl(c->options, "sru_extra_response_data",
452 sr->extraResponseData_buf, sr->extraResponseData_len);
453 if (sr->which == Z_SRW_searchRetrieve_response)
454 *cret = handle_srw_response(c, sr->u.response);
455 else if (sr->which == Z_SRW_scan_response)
456 handle_srw_scan_response(c, sr->u.scan_response);
460 else if (!ret && (soap_package->which == Z_SOAP_fault
461 || soap_package->which == Z_SOAP_error))
463 ZOOM_set_HTTP_error(c, hres->code,
464 soap_package->u.fault->fault_code,
465 soap_package->u.fault->fault_string);
469 size_t max_chars = 1000;
470 size_t sz = hres->content_len;
471 if (sz > max_chars - 1)
473 *addinfo = odr_malloc(c->odr_in, sz + 4);
474 memcpy(*addinfo, hres->content_buf, sz);
476 strcpy(*addinfo + sz, "...");
478 strcpy(*addinfo + sz, "");
491 * c-file-style: "Stroustrup"
492 * indent-tabs-mode: nil
494 * vim: shiftwidth=4 tabstop=8 expandtab