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 SRW/SRU utilities.
13 #include <yaz/matchstr.h>
14 #include <yaz/yaz-iconv.h>
20 #include <libxml/parser.h>
21 #include <libxml/tree.h>
24 int yaz_solr_decode_response(ODR o, Z_HTTP_Response *hres, Z_SRW_PDU **pdup)
27 const char *content_buf = hres->content_buf;
28 int content_len = hres->content_len;
29 xmlDocPtr doc = xmlParseMemory(content_buf, content_len);
33 Z_SRW_PDU *pdu = yaz_srw_get(o, Z_SRW_searchRetrieve_response);
34 Z_SRW_searchRetrieveResponse *sr = pdu->u.response;
42 xmlNodePtr root = xmlDocGetRootElement(doc);
47 else if (strcmp((const char *) root->name, "response"))
53 /** look for result node */
54 for (ptr = root->children; ptr; ptr = ptr->next)
56 if (ptr->type == XML_ELEMENT_NODE &&
57 !strcmp((const char *) ptr->name, "result"))
67 { /* got result node */
68 struct _xmlAttr *attr;
70 for (attr = ptr->properties; attr; attr = attr->next)
71 if (attr->children && attr->children->type == XML_TEXT_NODE)
73 if (!strcmp((const char *) attr->name, "numFound"))
78 (const char *) attr->children->content));
79 yaz_log(YLOG_DEBUG, "SOLR total results: %d ", atoi( attr->children->content));
81 else if (!strcmp((const char *) attr->name, "start"))
83 start = odr_atoi((const char *) attr->children->content);
84 yaz_log(YLOG_DEBUG, "SOLR start: %d ", atoi( attr->children->content));
95 for (node = ptr->children; node; node = node->next)
96 if (node->type == XML_ELEMENT_NODE)
98 yaz_log(YLOG_DEBUG, "SOLR results in response: %d ", sr->num_records);
100 sr->records = odr_malloc(o, sizeof(*sr->records) * sr->num_records);
102 for (node = ptr->children; node; node = node->next)
104 if (node->type == XML_ELEMENT_NODE)
106 Z_SRW_record *record = sr->records + i;
107 xmlBufferPtr buf = xmlBufferCreate();
108 xmlNode *tmp = xmlCopyNode(node, 1);
110 xmlNodeDump(buf, tmp->doc, tmp, 0, 0);
114 record->recordSchema = 0;
115 record->recordPacking = Z_SRW_recordPacking_XML;
116 record->recordData_len = buf->use;
117 record->recordData_buf = odr_malloc(o, buf->use + 1);
118 memcpy(record->recordData_buf, buf->content, buf->use);
119 record->recordData_buf[buf->use] = '\0';
120 // TODO Solve the real problem: Making the recordPosition 1-based due to "funny" code in zoom-sru
121 record->recordPosition = odr_intdup(o, start + offset + 1);
122 yaz_log(YLOG_DEBUG, "SOLR pos=" ODR_INT_PRINTF, *record->recordPosition);
141 int yaz_solr_encode_request(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu,
142 ODR encode, const char *charset)
144 const char *solr_op = 0;
145 char *name[30], *value[30];
150 z_HTTP_header_add_basic_auth(encode, &hreq->headers,
151 srw_pdu->username, srw_pdu->password);
153 switch (srw_pdu->which)
155 case Z_SRW_searchRetrieve_request:
158 switch(srw_pdu->u.request->query_type)
160 case Z_SRW_query_type_pqf:
161 yaz_add_name_value_str(encode, name, value, &i,
162 "q", srw_pdu->u.request->query.pqf);
164 case Z_SRW_query_type_cql:
165 yaz_add_name_value_str(encode, name, value, &i,
166 "q", srw_pdu->u.request->query.cql);
171 if (srw_pdu->u.request->startRecord)
173 Odr_int start = *srw_pdu->u.request->startRecord - 1;
174 yaz_add_name_value_int(encode, name, value, &i,
177 yaz_add_name_value_int(encode, name, value, &i,
178 "rows", srw_pdu->u.request->maximumRecords);
179 yaz_add_name_value_str(encode, name, value, &i,
180 "fl", srw_pdu->u.request->recordSchema);
186 yaz_array_to_uri(&uri_args, encode, name, value);
188 hreq->method = "GET";
191 odr_malloc(encode, strlen(hreq->path) +
192 strlen(uri_args) + strlen(solr_op) + 4);
194 sprintf(path, "%s/%s?%s", hreq->path, solr_op, uri_args);
197 z_HTTP_header_add_content_type(encode, &hreq->headers,
198 "text/xml", charset);
206 * c-file-style: "Stroustrup"
207 * indent-tabs-mode: nil
209 * vim: shiftwidth=4 tabstop=8 expandtab