1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2010 Index Data
3 * See the file LICENSE for details.
7 * \brief Implements ZOOM SRU
16 #include <yaz/pquery.h>
18 void handle_facet_list(ZOOM_resultset r, Z_FacetList *fl);
21 static void set_SRU_error(ZOOM_connection c, Z_SRW_diagnostic *d)
23 const char *uri = d->uri;
25 ZOOM_set_dset_error(c, ZOOM_uri_to_code(uri), uri, d->details, 0);
31 static zoom_ret send_srw(ZOOM_connection c, Z_SRW_PDU *sr)
35 const char *database = ZOOM_options_get(c->options, "databaseName");
39 fdatabase = yaz_encode_sru_dbpath_odr(c->odr_out, database);
40 gdu = z_get_HTTP_Request_host_path(c->odr_out, c->host_port,
41 fdatabase ? fdatabase : c->path);
43 if (c->sru_mode == zoom_sru_get)
45 yaz_sru_get_encode(gdu->u.HTTP_Request, sr, c->odr_out, c->charset);
47 else if (c->sru_mode == zoom_sru_post)
49 yaz_sru_post_encode(gdu->u.HTTP_Request, sr, c->odr_out, c->charset);
51 else if (c->sru_mode == zoom_sru_soap)
53 yaz_sru_soap_encode(gdu->u.HTTP_Request, sr, c->odr_out, c->charset);
55 else if (c->sru_mode == zoom_sru_solr)
57 yaz_solr_encode_request(gdu->u.HTTP_Request, sr, c->odr_out, c->charset);
59 if (!z_GDU(c->odr_out, &gdu, 0, 0))
62 z_GDU(c->odr_print, &gdu, 0, 0);
63 c->buf_out = odr_getbuf(c->odr_out, &c->len_out, 0);
65 event = ZOOM_Event_create(ZOOM_EVENT_SEND_APDU);
66 ZOOM_connection_put_event(c, event);
67 odr_reset(c->odr_out);
68 return ZOOM_send_buf(c);
73 static Z_SRW_PDU *ZOOM_srw_get_pdu(ZOOM_connection c, int type)
75 Z_SRW_PDU *sr = yaz_srw_get_pdu(c->odr_out, type, c->sru_version);
76 sr->username = c->user;
77 sr->password = c->password;
83 zoom_ret ZOOM_connection_srw_send_scan(ZOOM_connection c)
87 const char *option_val = 0;
92 assert (c->tasks->which == ZOOM_TASK_SCAN);
93 scan = c->tasks->u.scan.scan;
95 sr = ZOOM_srw_get_pdu(c, Z_SRW_scan_request);
97 z_query = ZOOM_query_get_Z_Query(scan->query);
98 /* SRU scan can only carry CQL and PQF */
99 if (z_query->which == Z_Query_type_104)
101 sr->u.scan_request->query_type = Z_SRW_query_type_cql;
102 sr->u.scan_request->scanClause.cql =
103 ZOOM_query_get_query_string(scan->query);
105 else if (z_query->which == Z_Query_type_1
106 || z_query->which == Z_Query_type_101)
108 sr->u.scan_request->query_type = Z_SRW_query_type_pqf;
109 sr->u.scan_request->scanClause.pqf =
110 ZOOM_query_get_query_string(scan->query);
114 ZOOM_set_error(c, ZOOM_ERROR_UNSUPPORTED_QUERY, 0);
115 return zoom_complete;
118 sr->u.scan_request->maximumTerms = odr_intdup(
119 c->odr_out, ZOOM_options_get_int(scan->options, "number", 10));
121 sr->u.scan_request->responsePosition = odr_intdup(
122 c->odr_out, ZOOM_options_get_int(scan->options, "position", 1));
124 option_val = ZOOM_options_get(scan->options, "extraArgs");
125 yaz_encode_sru_extra(sr, c->odr_out, option_val);
126 return send_srw(c, sr);
129 zoom_ret ZOOM_connection_srw_send_scan(ZOOM_connection c)
131 return zoom_complete;
136 zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c)
138 const char *facets = 0;
141 ZOOM_resultset resultset = 0;
143 const char *option_val = 0;
145 Z_FacetList *facet_list = 0;
146 if (c->error) /* don't continue on error */
147 return zoom_complete;
149 switch(c->tasks->which)
151 case ZOOM_TASK_SEARCH:
152 resultset = c->tasks->u.search.resultset;
153 if (!resultset->setname)
154 resultset->setname = xstrdup("default");
155 ZOOM_options_set(resultset->options, "setname", resultset->setname);
156 start = &c->tasks->u.search.start;
157 count = &c->tasks->u.search.count;
158 facets = ZOOM_options_get(resultset->options, "facets");
160 facet_list = yaz_pqf_parse_facet_list(c->odr_out, facets);
163 case ZOOM_TASK_RETRIEVE:
164 resultset = c->tasks->u.retrieve.resultset;
166 start = &c->tasks->u.retrieve.start;
167 count = &c->tasks->u.retrieve.count;
169 if (*start >= resultset->size)
170 return zoom_complete;
171 if (*start + *count > resultset->size)
172 *count = resultset->size - *start;
174 for (i = 0; i < *count; i++)
177 ZOOM_record_cache_lookup(resultset, i + *start,
178 c->tasks->u.retrieve.syntax,
179 c->tasks->u.retrieve.elementSetName);
184 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_RECV_RECORD);
185 ZOOM_connection_put_event(c, event);
192 return zoom_complete;
195 return zoom_complete;
197 assert(resultset->query);
199 sr = ZOOM_srw_get_pdu(c, Z_SRW_searchRetrieve_request);
200 z_query = ZOOM_query_get_Z_Query(resultset->query);
202 if (z_query->which == Z_Query_type_104
203 && z_query->u.type_104->which == Z_External_CQL)
205 sr->u.request->query_type = Z_SRW_query_type_cql;
206 sr->u.request->query.cql = z_query->u.type_104->u.cql;
208 else if (z_query->which == Z_Query_type_1 && z_query->u.type_1)
210 sr->u.request->query_type = Z_SRW_query_type_pqf;
211 sr->u.request->query.pqf =
212 ZOOM_query_get_query_string(resultset->query);
216 ZOOM_set_error(c, ZOOM_ERROR_UNSUPPORTED_QUERY, 0);
217 return zoom_complete;
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 = resultset->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;
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;
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 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;
282 return zoom_complete;
287 if (res->resultSetId)
288 ZOOM_resultset_option_set(resultset, "resultSetId", res->resultSetId);
290 yaz_log(c->log_details, "%p handle_srw_response got SRW response OK", c);
292 if (res->num_diagnostics > 0)
294 set_SRU_error(c, &res->diagnostics[0]);
298 if (res->numberOfRecords) {
299 resultset->size = *res->numberOfRecords;
301 for (i = 0; i<res->num_records; i++)
304 Z_SRW_record *sru_rec;
305 Z_SRW_diagnostic *diag = 0;
308 Z_NamePlusRecord *npr = (Z_NamePlusRecord *)
309 odr_malloc(c->odr_in, sizeof(Z_NamePlusRecord));
311 * TODO This does not work with 0-based recordPositions.
312 * We will iterate over one twice
314 if (res->records[i].recordPosition &&
315 *res->records[i].recordPosition > 0)
316 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->which = Z_External_octet;
331 npr->u.databaseRecord->u.octet_aligned = (Odr_oct *)
332 odr_malloc(c->odr_in, sizeof(Odr_oct));
333 npr->u.databaseRecord->u.octet_aligned->buf = (unsigned char*)
334 sru_rec->recordData_buf;
335 npr->u.databaseRecord->u.octet_aligned->len =
336 npr->u.databaseRecord->u.octet_aligned->size =
337 sru_rec->recordData_len;
339 if (sru_rec->recordSchema
340 && !strcmp(sru_rec->recordSchema,
341 "info:srw/schema/1/diagnostics-v1.1"))
343 sru_decode_surrogate_diagnostics(sru_rec->recordData_buf,
344 sru_rec->recordData_len,
348 ZOOM_record_cache_add(resultset, npr, pos, syntax, elementSetName,
349 sru_rec->recordSchema, diag);
353 if (*count + *start > resultset->size)
354 *count = resultset->size - *start;
355 yaz_log(YLOG_DEBUG, "SRU result set size " ODR_INT_PRINTF " start %d count %d", resultset->size, *start, *count);
358 nmem = odr_extract_mem(c->odr_in);
359 nmem_transfer(odr_getmem(resultset->odr), nmem);
363 return ZOOM_connection_srw_send_search(c);
365 return zoom_complete;
370 static void handle_srw_scan_response(ZOOM_connection c,
371 Z_SRW_scanResponse *res)
373 NMEM nmem = odr_extract_mem(c->odr_in);
376 if (!c->tasks || c->tasks->which != ZOOM_TASK_SCAN)
378 scan = c->tasks->u.scan.scan;
380 if (res->num_diagnostics > 0)
381 set_SRU_error(c, &res->diagnostics[0]);
383 scan->scan_response = 0;
384 scan->srw_scan_response = res;
385 nmem_transfer(odr_getmem(scan->odr), nmem);
387 ZOOM_options_set_int(scan->options, "number", res->num_terms);
392 int ZOOM_handle_sru(ZOOM_connection c, Z_HTTP_Response *hres,
396 const char *addinfo = 0;
398 /* not redirect (normal response) */
399 if (!yaz_srw_check_content_type(hres))
401 addinfo = "content-type";
404 else if (c->sru_mode == zoom_sru_solr)
407 ret = yaz_solr_decode_response(c->odr_in, hres, &sr);
409 if (sr->which == Z_SRW_searchRetrieve_response)
410 *cret = handle_srw_response(c, sr->u.response);
414 Z_SOAP *soap_package = 0;
416 Z_SOAP_Handler soap_handlers[2] = {
417 {YAZ_XMLNS_SRU_v1_1, 0, (Z_SOAP_fun) yaz_srw_codec},
420 ret = z_soap_codec(o, &soap_package,
421 &hres->content_buf, &hres->content_len,
423 if (!ret && soap_package->which == Z_SOAP_generic &&
424 soap_package->u.generic->no == 0)
426 Z_SRW_PDU *sr = (Z_SRW_PDU*) soap_package->u.generic->p;
428 ZOOM_options_set(c->options, "sru_version", sr->srw_version);
429 ZOOM_options_setl(c->options, "sru_extra_response_data",
430 sr->extraResponseData_buf, sr->extraResponseData_len);
431 if (sr->which == Z_SRW_searchRetrieve_response)
432 *cret = handle_srw_response(c, sr->u.response);
433 else if (sr->which == Z_SRW_scan_response)
434 handle_srw_scan_response(c, sr->u.scan_response);
438 else if (!ret && (soap_package->which == Z_SOAP_fault
439 || soap_package->which == Z_SOAP_error))
441 ZOOM_set_HTTP_error(c, hres->code,
442 soap_package->u.fault->fault_code,
443 soap_package->u.fault->fault_string);
454 * c-file-style: "Stroustrup"
455 * indent-tabs-mode: nil
457 * vim: shiftwidth=4 tabstop=8 expandtab