1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2011 Index Data
3 * See the file LICENSE for details.
7 * \brief Implements SOAP Webservice decoding/encoding
16 #include <yaz/matchstr.h>
17 #include <yaz/yaz-iconv.h>
19 #include <yaz/facet.h>
20 #include <yaz/wrbuf.h>
24 #define SOLR_MAX_PARAMETERS 100
27 #include <libxml/parser.h>
28 #include <libxml/tree.h>
30 static int match_xml_node_attribute(xmlNodePtr ptr, const char *node_name, const char *attribute_name, const char *value)
32 const char *attribute_value;
33 // check if the node name matches
34 if (strcmp((const char*) ptr->name, node_name))
37 attribute_value = yaz_element_attribute_value_get(ptr, node_name, attribute_name);
38 if (attribute_value && !strcmp(attribute_value, value))
41 else /* No attribute to check */
46 static void yaz_solr_decode_result_docs(ODR o, xmlNodePtr ptr, Odr_int start, Z_SRW_searchRetrieveResponse *sr) {
52 for (node = ptr->children; node; node = node->next)
53 if (node->type == XML_ELEMENT_NODE)
56 sr->records = odr_malloc(o, sizeof(*sr->records) * sr->num_records);
58 for (node = ptr->children; node; node = node->next)
60 if (node->type == XML_ELEMENT_NODE)
62 Z_SRW_record *record = sr->records + i;
63 xmlBufferPtr buf = xmlBufferCreate();
64 xmlNode *tmp = xmlCopyNode(node, 1);
66 xmlNodeDump(buf, tmp->doc, tmp, 0, 0);
70 record->recordSchema = 0;
71 record->recordPacking = Z_SRW_recordPacking_XML;
72 record->recordData_len = buf->use;
73 record->recordData_buf = odr_malloc(o, buf->use + 1);
74 memcpy(record->recordData_buf, buf->content, buf->use);
75 record->recordData_buf[buf->use] = '\0';
76 // TODO Solve the real problem in zoom-sru, that doesnt work with 0-based indexes.
77 // Work-around: Making the recordPosition 1-based.
78 record->recordPosition = odr_intdup(o, start + offset + 1);
88 static int yaz_solr_decode_result(ODR o, xmlNodePtr ptr, Z_SRW_searchRetrieveResponse *sr) {
90 struct _xmlAttr *attr;
91 for (attr = ptr->properties; attr; attr = attr->next)
92 if (attr->children && attr->children->type == XML_TEXT_NODE) {
93 if (!strcmp((const char *) attr->name, "numFound")) {
94 sr->numberOfRecords = odr_intdup(o, odr_atoi(
95 (const char *) attr->children->content));
97 else if (!strcmp((const char *) attr->name, "start")) {
98 start = odr_atoi((const char *) attr->children->content);
101 if (sr->numberOfRecords && *sr->numberOfRecords > 0)
102 yaz_solr_decode_result_docs(o, ptr, start, sr);
103 if (sr->numberOfRecords)
108 static const char *get_facet_term_count(xmlNodePtr node, int *freq) {
110 const char *term = yaz_element_attribute_value_get(node, "int", "name");
112 WRBUF wrbuf = wrbuf_alloc();
116 for (child = node->children; child ; child = child->next) {
117 if (child->type == XML_TEXT_NODE)
118 wrbuf_puts(wrbuf, (const char *) child->content);
120 *freq = atoi(wrbuf_cstr(wrbuf));
121 wrbuf_destroy(wrbuf);
125 Z_FacetField *yaz_solr_decode_facet_field(ODR o, xmlNodePtr ptr, Z_SRW_searchRetrieveResponse *sr)
128 Z_AttributeList *list;
129 Z_FacetField *facet_field;
134 const char* name = yaz_element_attribute_value_get(ptr, "lst", "name");
135 char *pos = strstr(name, "_exact");
140 list = yaz_use_atttribute_create(o, name);
141 for (node = ptr->children; node; node = node->next) {
144 facet_field = facet_field_create(o, list, num_terms);
146 for (node = ptr->children; node; node = node->next) {
148 const char *term = get_facet_term_count(node, &count);
149 facet_field_term_set(o, facet_field, facet_term_create(o, term_create(o, term), count), index);
155 static int yaz_solr_decode_facet_counts(ODR o, xmlNodePtr root, Z_SRW_searchRetrieveResponse *sr) {
157 for (ptr = root->children; ptr; ptr = ptr->next)
159 if (match_xml_node_attribute(ptr, "lst", "name", "facet_fields"))
162 Z_FacetList *facet_list;
164 for (node = ptr->children; node; node= node->next)
168 facet_list = facet_list_create(o, num_facets);
170 for (node = ptr->children; node; node= node->next)
172 facet_list_field_set(o, facet_list, yaz_solr_decode_facet_field(o, node, sr), num_facets);
175 sr->facetList = facet_list;
184 int yaz_solr_decode_response(ODR o, Z_HTTP_Response *hres, Z_SRW_PDU **pdup)
187 const char *content_buf = hres->content_buf;
188 int content_len = hres->content_len;
189 xmlDocPtr doc = xmlParseMemory(content_buf, content_len);
192 Z_SRW_PDU *pdu = yaz_srw_get(o, Z_SRW_searchRetrieve_response);
193 Z_SRW_searchRetrieveResponse *sr = pdu->u.response;
201 xmlNodePtr root = xmlDocGetRootElement(doc);
206 else if (strcmp((const char *) root->name, "response"))
212 /** look for result (required) and facets node (optional) */
215 for (ptr = root->children; ptr; ptr = ptr->next)
217 if (ptr->type == XML_ELEMENT_NODE &&
218 !strcmp((const char *) ptr->name, "result"))
219 rc_result = yaz_solr_decode_result(o, ptr, sr);
220 /* TODO The check on hits is a work-around to avoid garbled facets on zero results from the SOLR server.
221 * The work-around works because the results is before the facets in the xml. */
222 if (rc_result == 0 && *sr->numberOfRecords > 0 && match_xml_node_attribute(ptr, "lst", "name", "facet_counts"))
223 rc_facets = yaz_solr_decode_facet_counts(o, ptr, sr);
225 ret = rc_result + rc_facets;
238 static void yaz_solr_encode_facet_field(ODR encode, char **name, char **value, int *i, Z_FacetField *facet_field, int *limit) {
239 Z_AttributeList *attribute_list = facet_field->attributes;
240 struct yaz_facet_attr attr_values;
241 yaz_facet_attr_init(&attr_values);
242 yaz_facet_attr_get_z_attributes(attribute_list, &attr_values);
243 // TODO do we want to support server decided
244 if (!attr_values.errcode && attr_values.useattr) {
245 WRBUF wrbuf = wrbuf_alloc();
246 wrbuf_puts(wrbuf, (char *) attr_values.useattr);
247 /* Skip date field */
248 if (strcmp("date", attr_values.useattr) != 0)
249 wrbuf_puts(wrbuf, "_exact");
250 yaz_add_name_value_str(encode, name, value, i, "facet.field", odr_strdup(encode, wrbuf_cstr(wrbuf)));
251 if (attr_values.limit > 0) {
252 WRBUF wrbuf2 = wrbuf_alloc();
254 wrbuf_puts(wrbuf2, "f.");
255 wrbuf_puts(wrbuf2, wrbuf_cstr(wrbuf));
256 wrbuf_puts(wrbuf2, ".facet.limit");
257 olimit = attr_values.limit;
258 yaz_add_name_value_int(encode, name, value, i, odr_strdup(encode, wrbuf_cstr(wrbuf2)), &olimit);
259 wrbuf_destroy(wrbuf2);
261 wrbuf_destroy(wrbuf);
265 static void yaz_solr_encode_facet_list(ODR encode, char **name, char **value, int *i, Z_FacetList *facet_list, int *limit) {
268 for (index = 0; index < facet_list->num; index++) {
269 yaz_solr_encode_facet_field(encode, name, value, i, facet_list->elements[index], limit);
275 int yaz_solr_encode_request(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu,
276 ODR encode, const char *charset)
278 const char *solr_op = 0;
279 //TODO Change. not a nice hard coded, unchecked limit.
280 char *name[SOLR_MAX_PARAMETERS], *value[SOLR_MAX_PARAMETERS];
285 z_HTTP_header_add_basic_auth(encode, &hreq->headers,
286 srw_pdu->username, srw_pdu->password);
288 switch (srw_pdu->which)
290 case Z_SRW_searchRetrieve_request: {
291 Z_SRW_searchRetrieveRequest *request = srw_pdu->u.request;
293 switch(srw_pdu->u.request->query_type)
295 case Z_SRW_query_type_pqf:
296 yaz_add_name_value_str(encode, name, value, &i,
297 "q", request->query.pqf);
299 case Z_SRW_query_type_cql:
300 yaz_add_name_value_str(encode, name, value, &i,
301 "q", request->query.cql);
306 if (srw_pdu->u.request->startRecord)
308 Odr_int start = *request->startRecord - 1;
309 yaz_add_name_value_int(encode, name, value, &i,
312 yaz_add_name_value_int(encode, name, value, &i,
313 "rows", request->maximumRecords);
314 yaz_add_name_value_str(encode, name, value, &i,
315 "fl", request->recordSchema);
317 if (request->facetList) {
318 Z_FacetList *facet_list = request->facetList;
320 yaz_add_name_value_str(encode, name, value, &i, "facet", "true");
321 yaz_add_name_value_str(encode, name, value, &i, "facet.mincount", "1");
322 yaz_solr_encode_facet_list(encode, name, value, &i, facet_list, &limit);
325 yaz_add_name_value_int(encode, name, value, &i, "facet.limit", &olimit);
335 yaz_array_to_uri(&uri_args, encode, name, value);
337 hreq->method = "GET";
340 odr_malloc(encode, strlen(hreq->path) +
341 strlen(uri_args) + strlen(solr_op) + 4);
343 sprintf(path, "%s/%s?%s", hreq->path, solr_op, uri_args);
346 z_HTTP_header_add_content_type(encode, &hreq->headers,
347 "text/xml", charset);
355 * c-file-style: "Stroustrup"
356 * indent-tabs-mode: nil
358 * vim: shiftwidth=4 tabstop=8 expandtab