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>
19 #include <libxml/parser.h>
20 #include <libxml/tree.h>
23 int yaz_solr_decode_response(ODR o, Z_HTTP_Response *hres, Z_SRW_PDU **pdup)
26 const char *content_buf = hres->content_buf;
27 int content_len = hres->content_len;
28 xmlDocPtr doc = xmlParseMemory(content_buf, content_len);
32 Z_SRW_PDU *pdu = yaz_srw_get(o, Z_SRW_searchRetrieve_response);
33 Z_SRW_searchRetrieveResponse *sr = pdu->u.response;
41 xmlNodePtr root = xmlDocGetRootElement(doc);
46 else if (strcmp((const char *) root->name, "response"))
52 /** look for result node */
53 for (ptr = root->children; ptr; ptr = ptr->next)
55 if (ptr->type == XML_ELEMENT_NODE &&
56 !strcmp((const char *) ptr->name, "result"))
66 { /* got result node */
67 struct _xmlAttr *attr;
69 for (attr = ptr->properties; attr; attr = attr->next)
70 if (attr->children && attr->children->type == XML_TEXT_NODE)
72 if (!strcmp((const char *) attr->name, "numFound"))
77 (const char *) attr->children->content));
79 else if (!strcmp((const char *) attr->name, "start"))
81 start = odr_atoi((const char *) attr->children->content);
92 for (node = ptr->children; node; node = node->next)
93 if (node->type == XML_ELEMENT_NODE)
96 sr->records = odr_malloc(o, sizeof(*sr->records) * sr->num_records);
98 for (node = ptr->children; node; node = node->next)
100 if (node->type == XML_ELEMENT_NODE)
102 Z_SRW_record *record = sr->records + i;
103 xmlBufferPtr buf = xmlBufferCreate();
104 xmlNode *tmp = xmlCopyNode(node, 1);
106 xmlNodeDump(buf, tmp->doc, tmp, 0, 0);
110 record->recordSchema = 0;
111 record->recordPacking = Z_SRW_recordPacking_XML;
112 record->recordData_len = buf->use;
113 record->recordData_buf = odr_malloc(o, buf->use + 1);
114 memcpy(record->recordData_buf, buf->content, buf->use);
115 record->recordData_buf[buf->use] = '\0';
116 record->recordPosition = odr_intdup(o, start + offset);
135 int yaz_solr_encode_request(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu,
136 ODR encode, const char *charset)
138 const char *solr_op = 0;
139 char *name[30], *value[30];
144 z_HTTP_header_add_basic_auth(encode, &hreq->headers,
145 srw_pdu->username, srw_pdu->password);
147 switch (srw_pdu->which)
149 case Z_SRW_searchRetrieve_request:
152 switch(srw_pdu->u.request->query_type)
154 case Z_SRW_query_type_pqf:
155 yaz_add_name_value_str(encode, name, value, &i,
156 "q", srw_pdu->u.request->query.pqf);
158 case Z_SRW_query_type_cql:
159 yaz_add_name_value_str(encode, name, value, &i,
160 "q", srw_pdu->u.request->query.cql);
165 if (srw_pdu->u.request->startRecord)
167 Odr_int start = *srw_pdu->u.request->startRecord - 1;
168 yaz_add_name_value_int(encode, name, value, &i,
171 yaz_add_name_value_int(encode, name, value, &i,
172 "rows", srw_pdu->u.request->maximumRecords);
173 yaz_add_name_value_str(encode, name, value, &i,
174 "fl", srw_pdu->u.request->recordSchema);
180 yaz_array_to_uri(&uri_args, encode, name, value);
182 hreq->method = "GET";
185 odr_malloc(encode, strlen(hreq->path) +
186 strlen(uri_args) + strlen(solr_op) + 4);
188 sprintf(path, "%s/%s?%s", hreq->path, solr_op, uri_args);
191 z_HTTP_header_add_content_type(encode, &hreq->headers,
192 "text/xml", charset);
200 * c-file-style: "Stroustrup"
201 * indent-tabs-mode: nil
203 * vim: shiftwidth=4 tabstop=8 expandtab