Refactor for string based queruType (SRU 2.0)
[yaz-moved-to-github.git] / include / yaz / srw.h
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2013 Index Data.
3  * All rights reserved.
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of Index Data nor the names of its contributors
13  *       may be used to endorse or promote products derived from this
14  *       software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 /**
29  * \file srw.h
30  * \brief Header for SRW/SRU
31  */
32
33 #ifndef YAZ_SRW_H
34 #define YAZ_SRW_H
35
36 #include <yaz/soap.h>
37 #include <yaz/zgdu.h>
38 #include <yaz/diagsrw.h>
39 #include <yaz/diagsru_update.h>
40 #include "facet.h"
41
42 YAZ_BEGIN_CDECL
43
44 typedef struct {
45     char *extraRecordData_buf;
46     int extraRecordData_len;
47     char *recordIdentifier;
48 } Z_SRW_extra_record;
49
50 typedef struct {
51     char *recordSchema;
52     int recordPacking;
53 #define Z_SRW_recordPacking_string 0
54 #define Z_SRW_recordPacking_XML 1
55 #define Z_SRW_recordPacking_URL 2
56     char *recordData_buf;
57     int recordData_len;
58     Odr_int *recordPosition;
59 } Z_SRW_record;
60
61 typedef struct {
62     char *uri;
63     char *details;
64     char *message;
65 } Z_SRW_diagnostic;
66
67 typedef struct {
68
69     char *queryType;
70     char *query;
71
72 #define Z_SRW_sort_type_none 1
73 #define Z_SRW_sort_type_sort 2
74 #define Z_SRW_sort_type_xSort 3
75     int sort_type;
76     union {
77         char *none;
78         char *sortKeys;
79         char *xSortKeys;
80     } sort;
81     Odr_int *startRecord;
82     Odr_int *maximumRecords;
83     char *recordSchema;
84     char *recordPacking;
85     char *recordXPath;
86     char *database;
87     char *stylesheet;
88     Odr_int *resultSetTTL;
89     Z_FacetList *facetList;
90 } Z_SRW_searchRetrieveRequest;
91
92 typedef struct {
93     Odr_int *numberOfRecords;
94     char * resultSetId;
95     Odr_int *resultSetIdleTime;
96
97     Z_SRW_record *records;
98     int num_records;
99
100     Z_SRW_diagnostic *diagnostics;
101     int num_diagnostics;
102     Odr_int *nextRecordPosition;
103
104     Z_SRW_extra_record **extra_records;  /* of size num_records */
105     Z_FacetList *facetList;
106     char *suggestions;
107 } Z_SRW_searchRetrieveResponse;
108
109 typedef struct {
110     char *recordPacking;
111     char *database;
112     char *stylesheet;
113 } Z_SRW_explainRequest;
114
115 typedef struct {
116     Z_SRW_record record;
117     Z_SRW_diagnostic *diagnostics;
118     int num_diagnostics;
119     Z_SRW_extra_record *extra_record;
120 } Z_SRW_explainResponse;
121
122 typedef struct {
123     char *queryType;
124     char *scanClause;
125     Odr_int *responsePosition;
126     Odr_int *maximumTerms;
127     char *stylesheet;
128     char *database;
129 } Z_SRW_scanRequest;
130
131 typedef struct {
132     char *value;
133     Odr_int *numberOfRecords;
134     char *displayTerm;
135     char *whereInList;
136 } Z_SRW_scanTerm;
137
138 typedef struct {
139     Z_SRW_scanTerm *terms;
140     int num_terms;
141     Z_SRW_diagnostic *diagnostics;
142     int num_diagnostics;
143 } Z_SRW_scanResponse;
144
145
146 typedef struct {
147     char *versionType;
148     char *versionValue;
149 } Z_SRW_recordVersion;
150
151 typedef struct {
152     char *database;
153     char *operation;
154     char *recordId;
155     Z_SRW_recordVersion *recordVersions;
156     int num_recordVersions;
157     Z_SRW_record *record;
158     Z_SRW_extra_record *extra_record;
159     char *extraRequestData_buf;
160     int extraRequestData_len;
161     char *stylesheet;
162 } Z_SRW_updateRequest;
163
164 typedef struct Z_SRW_extra_arg Z_SRW_extra_arg;
165 struct Z_SRW_extra_arg {
166     char *name;
167     char *value;
168     Z_SRW_extra_arg *next;
169 };
170
171 typedef struct {
172     char *operationStatus;
173     char *recordId;
174     Z_SRW_recordVersion *recordVersions;
175     int num_recordVersions;
176     Z_SRW_record *record;
177     Z_SRW_extra_record *extra_record;
178     char *extraResponseData_buf;
179     int extraResponseData_len;
180     Z_SRW_diagnostic *diagnostics;
181     int num_diagnostics;
182 } Z_SRW_updateResponse;
183
184 #define Z_SRW_searchRetrieve_request  1
185 #define Z_SRW_searchRetrieve_response 2
186 #define Z_SRW_explain_request 3
187 #define Z_SRW_explain_response 4
188 #define Z_SRW_scan_request 5
189 #define Z_SRW_scan_response 6
190 #define Z_SRW_update_request 7
191 #define Z_SRW_update_response 8
192
193 typedef struct {
194     int which;
195     union {
196         Z_SRW_searchRetrieveRequest *request;
197         Z_SRW_searchRetrieveResponse *response;
198         Z_SRW_explainRequest *explain_request;
199         Z_SRW_explainResponse *explain_response;
200         Z_SRW_scanRequest *scan_request;
201         Z_SRW_scanResponse *scan_response;
202         Z_SRW_updateRequest *update_request;
203         Z_SRW_updateResponse *update_response;
204     } u;
205     char *srw_version;
206     char *username; /* From HTTP header or request */
207     char *password; /* From HTTP header or request  */
208     Z_SRW_extra_arg *extra_args; /* extraRequestData SRU GET/POST */
209     char *extraResponseData_buf;
210     int extraResponseData_len;
211 } Z_SRW_PDU;
212
213 YAZ_EXPORT int yaz_srw_codec(ODR o, void * pptr,
214                              Z_SRW_PDU **handler_data,
215                              void *client_data, const char *ns);
216 YAZ_EXPORT int yaz_ucp_codec(ODR o, void * pptr,
217                              Z_SRW_PDU **handler_data,
218                              void *client_data, const char *ns);
219 YAZ_EXPORT Z_SRW_PDU *yaz_srw_get_core_v_1_1(ODR o);
220 YAZ_EXPORT Z_SRW_PDU *yaz_srw_get_pdu(ODR o, int which, const char *version);
221 YAZ_EXPORT Z_SRW_PDU *yaz_srw_get(ODR o, int which);
222 YAZ_EXPORT Z_SRW_recordVersion *yaz_srw_get_record_versions(ODR o, int num);
223 YAZ_EXPORT Z_SRW_extra_record *yaz_srw_get_extra_record(ODR o);
224 YAZ_EXPORT Z_SRW_record *yaz_srw_get_record(ODR o);
225 YAZ_EXPORT Z_SRW_record *yaz_srw_get_records(ODR o, int num);
226
227 YAZ_EXPORT int yaz_diag_bib1_to_srw (int bib1_code);
228
229 YAZ_EXPORT int yaz_diag_srw_to_bib1(int srw_code);
230
231 YAZ_EXPORT const char *yaz_srw_pack_to_str(int pack);
232 YAZ_EXPORT int yaz_srw_str_to_pack(const char *str);
233
234 /** \brief deprecated */
235 YAZ_EXPORT char *yaz_uri_val(const char *path, const char *name, ODR o);
236
237 /** \brief deprecated */
238 YAZ_EXPORT void yaz_uri_val_int(const char *path, const char *name,
239                                 ODR o, Odr_int **intp);
240
241 YAZ_EXPORT int yaz_uri_to_array(const char *path, ODR o,
242                                 char ***name, char ***val);
243 YAZ_EXPORT void yaz_array_to_uri(char **path, ODR o,
244                                  char **name, char **value);
245
246 /** \brief encodes URI component
247     \param dst destination string (should be at least 3*strlen(uri)+1)
248     \param uri URI component C-string (source)
249 */
250 YAZ_EXPORT void yaz_encode_uri_component(char *dst, const char *uri);
251
252 /** \brief decodes URI component
253     \param dst destination string (should be at least strlen(uri)+1)
254     \param uri URI component buffer (source)
255     \param len number of bytes to decode from uri
256 */
257 YAZ_EXPORT void yaz_decode_uri_component(char *dst, const char *uri,
258                                          size_t len);
259
260 YAZ_EXPORT int yaz_srw_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu,
261                               Z_SOAP **soap_package, ODR decode, char **charset);
262
263 YAZ_EXPORT int yaz_sru_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu,
264                               Z_SOAP **soap_package, ODR decode,
265                               char **charset,
266                               Z_SRW_diagnostic **, int *num_diagnostic);
267
268 /** \brief decode SOLR response (HTTP)
269     \param o ODR for result
270     \param hres HTTP response to be decoded
271     \param pdup SRW response pointer (set if successful)
272     \retval -1 fail
273     \retval 0 OK
274 */
275 YAZ_EXPORT int yaz_solr_decode_response(ODR o, Z_HTTP_Response *hres,
276                                         Z_SRW_PDU **pdup);
277
278
279 YAZ_EXPORT void yaz_add_srw_diagnostic(ODR o, Z_SRW_diagnostic **d,
280                                        int *num, int code,
281                                        const char *addinfo);
282
283 YAZ_EXPORT void yaz_add_sru_update_diagnostic(ODR o, Z_SRW_diagnostic **d,
284                                               int *num, int code,
285                                               const char *addinfo);
286
287 YAZ_EXPORT void yaz_mk_std_diagnostic(ODR o, Z_SRW_diagnostic *d,
288                                       int code, const char *details);
289
290 YAZ_EXPORT void yaz_add_srw_diagnostic_uri(ODR o, Z_SRW_diagnostic **d,
291                                            int *num, const char *uri,
292                                            const char *message,
293                                            const char *details);
294
295 YAZ_EXPORT void yaz_mk_srw_diagnostic(ODR o, Z_SRW_diagnostic *d,
296                                       const char *uri, const char *message,
297                                       const char *details);
298
299 YAZ_EXPORT int yaz_sru_get_encode(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu,
300                                   ODR encode, const char *charset);
301 YAZ_EXPORT int yaz_sru_post_encode(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu,
302                                    ODR encode, const char *charset);
303 YAZ_EXPORT int yaz_sru_soap_encode(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu,
304                                    ODR odr, const char *charset);
305
306 /** \brief encode SOLR request (HTTP)
307     \param hreq HTTP request to be modified (with encoded request)
308     \param srw_pdu SRW request
309     \param encode ODR for result
310     \param charset character set for request (may be NULL)
311     \retval -1 fail
312     \retval 0 OK
313 */
314 YAZ_EXPORT int yaz_solr_encode_request(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu,
315                                        ODR encode, const char *charset);
316
317 YAZ_EXPORT char *yaz_negotiate_sru_version(char *input_ver);
318
319 YAZ_EXPORT
320 void yaz_encode_sru_extra(Z_SRW_PDU *sr, ODR odr, const char *extra_args);
321
322
323 #define YAZ_XMLNS_SRU_v1_0 "http://www.loc.gov/zing/srw/v1.0/"
324 #define YAZ_XMLNS_SRU_v1_1 "http://www.loc.gov/zing/srw/"
325 #define YAZ_XMLNS_DIAG_v1_1 "http://www.loc.gov/zing/srw/diagnostic/"
326 #define YAZ_XMLNS_UPDATE_v0_9 "http://www.loc.gov/zing/srw/update/"
327 #define YAZ_XMLNS_SRU_v2_response "http://docs.oasis-open.org/ns/search-ws/sru*esponse"
328 #define YAZ_XMLNS_SRU_v1_response "http://www.loc.gov/*"
329
330 YAZ_EXPORT
331 int yaz_srw_check_content_type(Z_HTTP_Response *hres);
332
333 YAZ_EXPORT
334 int sru_decode_surrogate_diagnostics(const char *buf, size_t len,
335                                      Z_SRW_diagnostic **diag,
336                                      int *num, ODR odr);
337
338 YAZ_EXPORT
339 void yaz_mk_sru_surrogate(ODR o, Z_SRW_record *record, int pos,
340                           int code, const char *details);
341
342 /** \brief encode SRU database for HTTP path
343     \param out memory handle for resulting encoded database string
344     \param db source database
345     \returns encoded database path (includes leading /)
346 */
347 YAZ_EXPORT
348 char *yaz_encode_sru_dbpath_odr(ODR out, const char *db);
349
350 /** \brief encode SRU database for HTTP path
351     \param dst destination buffer (should be at least strlen(db) +2 in size)
352     \param db source database
353
354     The resulting database (dst) includes a leading /
355 */
356 YAZ_EXPORT
357 void yaz_encode_sru_dbpath_buf(char *dst, const char *db);
358
359 YAZ_END_CDECL
360
361 #endif
362 /*
363  * Local variables:
364  * c-basic-offset: 4
365  * c-file-style: "Stroustrup"
366  * indent-tabs-mode: nil
367  * End:
368  * vim: shiftwidth=4 tabstop=8 expandtab
369  */
370