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 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 const char *xml_node_attribute_value_get(xmlNodePtr ptr, const char *node_name, const char *attribute_name) {
32 struct _xmlAttr *attr;
33 // check if the node name matches
34 if (strcmp((const char*) ptr->name, node_name))
36 // check if the attribute name and return the value
37 for (attr = ptr->properties; attr; attr = attr->next)
38 if (attr->children && attr->children->type == XML_TEXT_NODE) {
39 if (!strcmp((const char *) attr->name, attribute_name))
40 return (const char *) attr->children->content;
46 static int match_xml_node_attribute(xmlNodePtr ptr, const char *node_name, const char *attribute_name, const char *value)
48 const char *attribute_value;
49 // check if the node name matches
50 if (strcmp((const char*) ptr->name, node_name))
52 attribute_value = xml_node_attribute_value_get(ptr, node_name, attribute_name);
53 if (attribute_value && !strcmp(attribute_value, value))
58 static void yaz_solr_decode_result_docs(ODR o, xmlNodePtr ptr, Odr_int start, Z_SRW_searchRetrieveResponse *sr) {
64 for (node = ptr->children; node; node = node->next)
65 if (node->type == XML_ELEMENT_NODE)
68 sr->records = odr_malloc(o, sizeof(*sr->records) * sr->num_records);
70 for (node = ptr->children; node; node = node->next)
72 if (node->type == XML_ELEMENT_NODE)
74 Z_SRW_record *record = sr->records + i;
75 xmlBufferPtr buf = xmlBufferCreate();
76 xmlNode *tmp = xmlCopyNode(node, 1);
78 xmlNodeDump(buf, tmp->doc, tmp, 0, 0);
82 record->recordSchema = 0;
83 record->recordPacking = Z_SRW_recordPacking_XML;
84 record->recordData_len = buf->use;
85 record->recordData_buf = odr_malloc(o, buf->use + 1);
86 memcpy(record->recordData_buf, buf->content, buf->use);
87 record->recordData_buf[buf->use] = '\0';
88 // TODO Solve the real problem in zoom-sru, that doesnt work with 0-based indexes.
89 // Work-around: Making the recordPosition 1-based.
90 record->recordPosition = odr_intdup(o, start + offset + 1);
100 static int yaz_solr_decode_result(ODR o, xmlNodePtr ptr, Z_SRW_searchRetrieveResponse *sr) {
102 struct _xmlAttr *attr;
103 for (attr = ptr->properties; attr; attr = attr->next)
104 if (attr->children && attr->children->type == XML_TEXT_NODE) {
105 if (!strcmp((const char *) attr->name, "numFound")) {
106 sr->numberOfRecords = odr_intdup(o, odr_atoi(
107 (const char *) attr->children->content));
109 else if (!strcmp((const char *) attr->name, "start")) {
110 start = odr_atoi((const char *) attr->children->content);
113 if (sr->numberOfRecords && *sr->numberOfRecords > 0)
114 yaz_solr_decode_result_docs(o, ptr, start, sr);
115 if (sr->numberOfRecords)
120 static Z_AttributeList *yaz_solr_use_atttribute_create(ODR o, const char *name) {
121 Z_AttributeList *attributes= (Z_AttributeList *) odr_malloc(o, sizeof(*attributes));
122 Z_AttributeElement ** elements;
123 attributes->num_attributes = 1;
124 /* TODO check on name instead
125 if (!attributes->num_attributes) {
126 attributes->attributes = (Z_AttributeElement**)odr_nullval();
130 elements = (Z_AttributeElement**) odr_malloc (o, attributes->num_attributes * sizeof(*elements));
131 elements[0] = (Z_AttributeElement*)odr_malloc(o,sizeof(**elements));
132 elements[0]->attributeType = odr_malloc(o, sizeof(*elements[0]->attributeType));
133 *elements[0]->attributeType = 1;
134 elements[0]->attributeSet = odr_nullval();
135 elements[0]->which = Z_AttributeValue_complex;
136 elements[0]->value.complex = (Z_ComplexAttribute *) odr_malloc(o, sizeof(Z_ComplexAttribute));
137 elements[0]->value.complex->num_list = 1;
138 elements[0]->value.complex->list = (Z_StringOrNumeric **) odr_malloc(o, 1 * sizeof(Z_StringOrNumeric *));
139 elements[0]->value.complex->list[0] = (Z_StringOrNumeric *) odr_malloc(o, sizeof(Z_StringOrNumeric));
140 elements[0]->value.complex->list[0]->which = Z_StringOrNumeric_string;
141 elements[0]->value.complex->list[0]->u.string = (Z_InternationalString *) odr_strdup(o, name);
142 elements[0]->value.complex->semanticAction = 0;
143 elements[0]->value.complex->num_semanticAction = 0;
144 attributes->attributes = elements;
149 static const char *get_facet_term_count(xmlNodePtr node, int *freq) {
151 const char *term = xml_node_attribute_value_get(node, "int", "name");
153 WRBUF wrbuf = wrbuf_alloc();
157 for (child = node->children; child ; child = child->next) {
158 if (child->type == XML_TEXT_NODE)
159 wrbuf_puts(wrbuf, (const char *) child->content);
161 *freq = atoi(wrbuf_cstr(wrbuf));
162 wrbuf_destroy(wrbuf);
166 Z_FacetField *yaz_solr_decode_facet_field(ODR o, xmlNodePtr ptr, Z_SRW_searchRetrieveResponse *sr)
169 Z_AttributeList *list;
170 Z_FacetField *facet_field;
175 const char* name = xml_node_attribute_value_get(ptr, "lst", "name");
176 char *pos = strstr(name, "_exact");
181 list = yaz_solr_use_atttribute_create(o, name);
182 for (node = ptr->children; node; node = node->next) {
185 facet_field = facet_field_create(o, list, num_terms);
187 for (node = ptr->children; node; node = node->next) {
189 const char *term = get_facet_term_count(node, &count);
190 facet_field_term_set(o, facet_field, facet_term_create(o, term_create(o, term), count), index);
196 static int yaz_solr_decode_facet_counts(ODR o, xmlNodePtr root, Z_SRW_searchRetrieveResponse *sr) {
198 for (ptr = root->children; ptr; ptr = ptr->next)
200 if (match_xml_node_attribute(ptr, "lst", "name", "facet_fields"))
203 Z_FacetList *facet_list;
205 for (node = ptr->children; node; node= node->next)
209 facet_list = facet_list_create(o, num_facets);
211 for (node = ptr->children; node; node= node->next)
213 facet_list_field_set(o, facet_list, yaz_solr_decode_facet_field(o, node, sr), num_facets);
216 sr->facetList = facet_list;
225 int yaz_solr_decode_response(ODR o, Z_HTTP_Response *hres, Z_SRW_PDU **pdup)
228 const char *content_buf = hres->content_buf;
229 int content_len = hres->content_len;
230 xmlDocPtr doc = xmlParseMemory(content_buf, content_len);
233 Z_SRW_PDU *pdu = yaz_srw_get(o, Z_SRW_searchRetrieve_response);
234 Z_SRW_searchRetrieveResponse *sr = pdu->u.response;
242 xmlNodePtr root = xmlDocGetRootElement(doc);
247 else if (strcmp((const char *) root->name, "response"))
253 /** look for result (required) and facets node (optional) */
256 for (ptr = root->children; ptr; ptr = ptr->next)
258 if (ptr->type == XML_ELEMENT_NODE &&
259 !strcmp((const char *) ptr->name, "result"))
260 rc_result = yaz_solr_decode_result(o, ptr, sr);
261 /* TODO The check on hits is a work-around to avoid garbled facets on zero results from the SOLR server.
262 * The work-around works because the results is before the facets in the xml. */
263 if (rc_result == 0 && match_xml_node_attribute(ptr, "lst", "name", "facet_counts"))
264 rc_facets = yaz_solr_decode_facet_counts(o, ptr, sr);
266 ret = rc_result + rc_facets;
279 static void yaz_solr_encode_facet_field(ODR encode, char **name, char **value, int *i, Z_FacetField *facet_field, int *limit) {
280 Z_AttributeList *attribute_list = facet_field->attributes;
281 struct yaz_facet_attr attr_values;
282 yaz_facet_attr_init(&attr_values);
283 yaz_facet_attr_get_z_attributes(attribute_list, &attr_values);
284 // TODO do we want to support server decided
285 if (!attr_values.errcode && attr_values.useattr) {
286 WRBUF wrbuf = wrbuf_alloc();
287 wrbuf_puts(wrbuf, (char *) attr_values.useattr);
288 /* Skip date field */
289 if (strcmp("date", attr_values.useattr) != 0)
290 wrbuf_puts(wrbuf, "_exact");
291 yaz_add_name_value_str(encode, name, value, i, "facet.field", odr_strdup(encode, wrbuf_cstr(wrbuf)));
292 if (attr_values.limit > 0) {
293 WRBUF wrbuf2 = wrbuf_alloc();
295 wrbuf_puts(wrbuf2, "f.");
296 wrbuf_puts(wrbuf2, wrbuf_cstr(wrbuf));
297 wrbuf_puts(wrbuf2, ".facet.limit");
298 olimit = attr_values.limit;
299 yaz_add_name_value_int(encode, name, value, i, odr_strdup(encode, wrbuf_cstr(wrbuf2)), &olimit);
300 wrbuf_destroy(wrbuf2);
302 wrbuf_destroy(wrbuf);
306 static void yaz_solr_encode_facet_list(ODR encode, char **name, char **value, int *i, Z_FacetList *facet_list, int *limit) {
309 for (index = 0; index < facet_list->num; index++) {
310 yaz_solr_encode_facet_field(encode, name, value, i, facet_list->elements[index], limit);
316 int yaz_solr_encode_request(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu,
317 ODR encode, const char *charset)
319 const char *solr_op = 0;
320 //TODO Change. not a nice hard coded, unchecked limit.
321 char *name[SOLR_MAX_PARAMETERS], *value[SOLR_MAX_PARAMETERS];
326 z_HTTP_header_add_basic_auth(encode, &hreq->headers,
327 srw_pdu->username, srw_pdu->password);
329 switch (srw_pdu->which)
331 case Z_SRW_searchRetrieve_request: {
332 Z_SRW_searchRetrieveRequest *request = srw_pdu->u.request;
334 switch(srw_pdu->u.request->query_type)
336 case Z_SRW_query_type_pqf:
337 yaz_add_name_value_str(encode, name, value, &i,
338 "q", request->query.pqf);
340 case Z_SRW_query_type_cql:
341 yaz_add_name_value_str(encode, name, value, &i,
342 "q", request->query.cql);
347 if (srw_pdu->u.request->startRecord)
349 Odr_int start = *request->startRecord - 1;
350 yaz_add_name_value_int(encode, name, value, &i,
353 yaz_add_name_value_int(encode, name, value, &i,
354 "rows", request->maximumRecords);
355 yaz_add_name_value_str(encode, name, value, &i,
356 "fl", request->recordSchema);
358 if (request->facetList) {
359 Z_FacetList *facet_list = request->facetList;
361 yaz_add_name_value_str(encode, name, value, &i, "facet", "true");
362 yaz_add_name_value_str(encode, name, value, &i, "facet.mincount", "1");
363 yaz_solr_encode_facet_list(encode, name, value, &i, facet_list, &limit);
366 yaz_add_name_value_int(encode, name, value, &i, "facet.limit", &olimit);
376 yaz_array_to_uri(&uri_args, encode, name, value);
378 hreq->method = "GET";
381 odr_malloc(encode, strlen(hreq->path) +
382 strlen(uri_args) + strlen(solr_op) + 4);
384 sprintf(path, "%s/%s?%s", hreq->path, solr_op, uri_args);
387 z_HTTP_header_add_content_type(encode, &hreq->headers,
388 "text/xml", charset);
396 * c-file-style: "Stroustrup"
397 * indent-tabs-mode: nil
399 * vim: shiftwidth=4 tabstop=8 expandtab