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));
80 else if (!strcmp((const char *) attr->name, "start"))
82 start = odr_atoi((const char *) attr->children->content);
93 for (node = ptr->children; node; node = node->next)
94 if (node->type == XML_ELEMENT_NODE)
97 sr->records = odr_malloc(o, sizeof(*sr->records) * sr->num_records);
99 for (node = ptr->children; node; node = node->next)
101 if (node->type == XML_ELEMENT_NODE)
103 Z_SRW_record *record = sr->records + i;
104 xmlBufferPtr buf = xmlBufferCreate();
105 xmlNode *tmp = xmlCopyNode(node, 1);
107 xmlNodeDump(buf, tmp->doc, tmp, 0, 0);
111 record->recordSchema = 0;
112 record->recordPacking = Z_SRW_recordPacking_XML;
113 record->recordData_len = buf->use;
114 record->recordData_buf = odr_malloc(o, buf->use + 1);
115 memcpy(record->recordData_buf, buf->content, buf->use);
116 record->recordData_buf[buf->use] = '\0';
117 // TODO Solve the real problem: Making the recordPosition 1-based due to "funny" code in zoom-sru
118 record->recordPosition = odr_intdup(o, start + offset + 1);
137 int yaz_solr_encode_request(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu,
138 ODR encode, const char *charset)
140 const char *solr_op = 0;
141 char *name[30], *value[30];
146 z_HTTP_header_add_basic_auth(encode, &hreq->headers,
147 srw_pdu->username, srw_pdu->password);
149 switch (srw_pdu->which)
151 case Z_SRW_searchRetrieve_request:
154 switch(srw_pdu->u.request->query_type)
156 case Z_SRW_query_type_pqf:
157 yaz_add_name_value_str(encode, name, value, &i,
158 "q", srw_pdu->u.request->query.pqf);
160 case Z_SRW_query_type_cql:
161 yaz_add_name_value_str(encode, name, value, &i,
162 "q", srw_pdu->u.request->query.cql);
167 if (srw_pdu->u.request->startRecord)
169 Odr_int start = *srw_pdu->u.request->startRecord - 1;
170 yaz_add_name_value_int(encode, name, value, &i,
173 yaz_add_name_value_int(encode, name, value, &i,
174 "rows", srw_pdu->u.request->maximumRecords);
175 yaz_add_name_value_str(encode, name, value, &i,
176 "fl", srw_pdu->u.request->recordSchema);
182 yaz_array_to_uri(&uri_args, encode, name, value);
184 hreq->method = "GET";
187 odr_malloc(encode, strlen(hreq->path) +
188 strlen(uri_args) + strlen(solr_op) + 4);
190 sprintf(path, "%s/%s?%s", hreq->path, solr_op, uri_args);
193 z_HTTP_header_add_content_type(encode, &hreq->headers,
194 "text/xml", charset);
202 * c-file-style: "Stroustrup"
203 * indent-tabs-mode: nil
205 * vim: shiftwidth=4 tabstop=8 expandtab