Rename SOLR HTTP response function
[yaz-moved-to-github.git] / include / yaz / srw.h
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2010 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
41 YAZ_BEGIN_CDECL
42
43 typedef struct {
44     char *extraRecordData_buf;
45     int extraRecordData_len;
46     char *recordIdentifier;
47 } Z_SRW_extra_record;
48
49 typedef struct {
50     char *recordSchema;
51     int recordPacking;
52 #define Z_SRW_recordPacking_string 0
53 #define Z_SRW_recordPacking_XML 1
54 #define Z_SRW_recordPacking_URL 2
55     char *recordData_buf;
56     int recordData_len;
57     Odr_int *recordPosition;
58 } Z_SRW_record;
59
60 typedef struct {
61     char *uri;
62     char *details;
63     char *message;
64 } Z_SRW_diagnostic;
65     
66 typedef struct {
67
68 #define Z_SRW_query_type_cql  1
69 #define Z_SRW_query_type_xcql 2
70 #define Z_SRW_query_type_pqf  3
71     int query_type;
72     union {
73         char *cql;
74         char *xcql;
75         char *pqf;
76     } query;
77
78 #define Z_SRW_sort_type_none 1
79 #define Z_SRW_sort_type_sort 2
80 #define Z_SRW_sort_type_xSort 3
81     int sort_type;
82     union {
83         char *none;
84         char *sortKeys;
85         char *xSortKeys;
86     } sort;
87     Odr_int *startRecord;
88     Odr_int *maximumRecords;
89     char *recordSchema;
90     char *recordPacking;
91     char *recordXPath;
92     char *database;
93     char *stylesheet;
94     Odr_int *resultSetTTL;
95 } Z_SRW_searchRetrieveRequest;
96
97 typedef struct {
98     Odr_int *numberOfRecords;
99     char * resultSetId;
100     Odr_int *resultSetIdleTime;
101     
102     Z_SRW_record *records;
103     int num_records;
104
105     Z_SRW_diagnostic *diagnostics;
106     int num_diagnostics;
107     Odr_int *nextRecordPosition;
108
109     Z_SRW_extra_record **extra_records;  /* of size num_records */
110 } Z_SRW_searchRetrieveResponse;
111
112 typedef struct {
113     char *recordPacking;
114     char *database;
115     char *stylesheet;
116 } Z_SRW_explainRequest;
117
118 typedef struct {
119     Z_SRW_record record;
120     Z_SRW_diagnostic *diagnostics;
121     int num_diagnostics;
122     Z_SRW_extra_record *extra_record;
123 } Z_SRW_explainResponse;
124     
125 typedef struct {
126     int query_type;
127     union {
128         char *cql;
129         char *xcql;
130         char *pqf;
131     } scanClause;
132     Odr_int *responsePosition;
133     Odr_int *maximumTerms;
134     char *stylesheet;
135     char *database;
136 } Z_SRW_scanRequest;
137
138 typedef struct {
139     char *value;
140     Odr_int *numberOfRecords;
141     char *displayTerm;
142     char *whereInList;
143 } Z_SRW_scanTerm;
144
145 typedef struct {
146     Z_SRW_scanTerm *terms;
147     int num_terms;
148     Z_SRW_diagnostic *diagnostics;
149     int num_diagnostics;
150 } Z_SRW_scanResponse;
151
152
153 typedef struct {
154     char *versionType;
155     char *versionValue;
156 } Z_SRW_recordVersion;
157
158 typedef struct {
159     char *database;
160     char *operation;
161     char *recordId;
162     Z_SRW_recordVersion *recordVersions;
163     int num_recordVersions;
164     Z_SRW_record *record;
165     Z_SRW_extra_record *extra_record;
166     char *extraRequestData_buf;
167     int extraRequestData_len;
168     char *stylesheet;
169 } Z_SRW_updateRequest;
170
171 typedef struct Z_SRW_extra_arg Z_SRW_extra_arg;
172 struct Z_SRW_extra_arg {
173     char *name;
174     char *value;
175     Z_SRW_extra_arg *next;
176 };
177
178 typedef struct {
179     char *operationStatus;
180     char *recordId;
181     Z_SRW_recordVersion *recordVersions;
182     int num_recordVersions;
183     Z_SRW_record *record;
184     Z_SRW_extra_record *extra_record;
185     char *extraResponseData_buf;
186     int extraResponseData_len;
187     Z_SRW_diagnostic *diagnostics;
188     int num_diagnostics;
189 } Z_SRW_updateResponse;
190
191 #define Z_SRW_searchRetrieve_request  1
192 #define Z_SRW_searchRetrieve_response 2
193 #define Z_SRW_explain_request 3
194 #define Z_SRW_explain_response 4
195 #define Z_SRW_scan_request 5
196 #define Z_SRW_scan_response 6
197 #define Z_SRW_update_request 7
198 #define Z_SRW_update_response 8
199
200 typedef struct {
201     int which;
202     union {
203         Z_SRW_searchRetrieveRequest *request;
204         Z_SRW_searchRetrieveResponse *response;
205         Z_SRW_explainRequest *explain_request;
206         Z_SRW_explainResponse *explain_response;
207         Z_SRW_scanRequest *scan_request;
208         Z_SRW_scanResponse *scan_response;
209         Z_SRW_updateRequest *update_request;
210         Z_SRW_updateResponse *update_response;
211     } u;
212     char *srw_version;
213     char *username; /* From HTTP header or request */
214     char *password; /* From HTTP header or request  */
215     Z_SRW_extra_arg *extra_args; /* extraRequestData SRU GET/POST */
216     char *extraResponseData_buf;
217     int extraResponseData_len;
218 } Z_SRW_PDU;
219
220 YAZ_EXPORT int yaz_srw_codec(ODR o, void * pptr,
221                              Z_SRW_PDU **handler_data,
222                              void *client_data, const char *ns);
223 YAZ_EXPORT int yaz_ucp_codec(ODR o, void * pptr,
224                              Z_SRW_PDU **handler_data,
225                              void *client_data, const char *ns);
226 YAZ_EXPORT Z_SRW_PDU *yaz_srw_get_core_v_1_1(ODR o);
227 YAZ_EXPORT Z_SRW_PDU *yaz_srw_get_pdu(ODR o, int which, const char *version);
228 YAZ_EXPORT Z_SRW_PDU *yaz_srw_get(ODR o, int which);
229 YAZ_EXPORT Z_SRW_recordVersion *yaz_srw_get_record_versions(ODR o, int num);
230 YAZ_EXPORT Z_SRW_extra_record *yaz_srw_get_extra_record(ODR o);
231 YAZ_EXPORT Z_SRW_record *yaz_srw_get_record(ODR o);
232 YAZ_EXPORT Z_SRW_record *yaz_srw_get_records(ODR o, int num);
233
234 YAZ_EXPORT int yaz_diag_bib1_to_srw (int bib1_code);
235
236 YAZ_EXPORT int yaz_diag_srw_to_bib1(int srw_code);
237
238 YAZ_EXPORT const char *yaz_srw_pack_to_str(int pack);
239 YAZ_EXPORT int yaz_srw_str_to_pack(const char *str);
240
241 /** \brief deprecated */
242 YAZ_EXPORT char *yaz_uri_val(const char *path, const char *name, ODR o);
243
244 /** \brief deprecated */
245 YAZ_EXPORT void yaz_uri_val_int(const char *path, const char *name,
246                                 ODR o, Odr_int **intp);
247
248 YAZ_EXPORT int yaz_uri_to_array(const char *path, ODR o,
249                                 char ***name, char ***val);
250 YAZ_EXPORT void yaz_array_to_uri(char **path, ODR o,
251                                  char **name, char **value);
252
253 /** \brief encodes URI component
254     \param dst destination string (should be at least 3*strlen(uri)+1)
255     \param uri URI component C-string (source)
256 */
257 YAZ_EXPORT void yaz_encode_uri_component(char *dst, const char *uri);
258
259 /** \brief decodes URI component
260     \param dst destination string (should be at least strlen(uri)+1)
261     \param uri URI component buffer (source)
262     \param len number of bytes to decode from uri
263 */
264 YAZ_EXPORT void yaz_decode_uri_component(char *dst, const char *uri,
265                                          size_t len);
266
267 YAZ_EXPORT int yaz_srw_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu,
268                               Z_SOAP **soap_package, ODR decode, char **charset);
269
270 YAZ_EXPORT int yaz_sru_decode(Z_HTTP_Request *hreq, Z_SRW_PDU **srw_pdu,
271                               Z_SOAP **soap_package, ODR decode, 
272                               char **charset,
273                               Z_SRW_diagnostic **, int *num_diagnostic);
274
275 /** \brief decode SOLR response (HTTP)
276     \param o ODR for result
277     \param hres HTTP response to be decoded
278     \param pdup SRW response pointer (set if successful)
279     \retval -1 fail
280     \retval 0 OK
281 */
282 YAZ_EXPORT int yaz_solr_decode_response(ODR o, Z_HTTP_Response *hres,
283                                         Z_SRW_PDU **pdup);
284
285
286 YAZ_EXPORT void yaz_add_srw_diagnostic(ODR o, Z_SRW_diagnostic **d,
287                                        int *num, int code,
288                                        const char *addinfo);
289     
290 YAZ_EXPORT void yaz_add_sru_update_diagnostic(ODR o, Z_SRW_diagnostic **d,
291                                               int *num, int code,
292                                               const char *addinfo);
293
294 YAZ_EXPORT void yaz_mk_std_diagnostic(ODR o, Z_SRW_diagnostic *d, 
295                                       int code, const char *details);
296
297 YAZ_EXPORT void yaz_add_srw_diagnostic_uri(ODR o, Z_SRW_diagnostic **d,
298                                            int *num, const char *uri,
299                                            const char *message,
300                                            const char *details);
301
302 YAZ_EXPORT void yaz_mk_srw_diagnostic(ODR o, Z_SRW_diagnostic *d, 
303                                       const char *uri, const char *message,
304                                       const char *details);
305
306 YAZ_EXPORT int yaz_sru_get_encode(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu,
307                                   ODR encode, const char *charset);
308 YAZ_EXPORT int yaz_sru_post_encode(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu,
309                                    ODR encode, const char *charset);
310 YAZ_EXPORT int yaz_sru_soap_encode(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu,
311                                    ODR odr, const char *charset);
312 YAZ_EXPORT int yaz_solr_encode(Z_HTTP_Request *hreq, Z_SRW_PDU *srw_pdu,
313                                ODR encode, const char *charset);
314
315 YAZ_EXPORT char *yaz_negotiate_sru_version(char *input_ver);
316
317 YAZ_EXPORT
318 void yaz_encode_sru_extra(Z_SRW_PDU *sr, ODR odr, const char *extra_args);
319
320
321 #define YAZ_XMLNS_SRU_v1_0 "http://www.loc.gov/zing/srw/v1.0/"
322 #define YAZ_XMLNS_SRU_v1_1 "http://www.loc.gov/zing/srw/"
323 #define YAZ_XMLNS_DIAG_v1_1 "http://www.loc.gov/zing/srw/diagnostic/"
324 #define YAZ_XMLNS_UPDATE_v0_9 "http://www.loc.gov/zing/srw/update/"
325
326 YAZ_EXPORT
327 int yaz_srw_check_content_type(Z_HTTP_Response *hres);
328
329 YAZ_EXPORT
330 int sru_decode_surrogate_diagnostics(const char *buf, size_t len,
331                                      Z_SRW_diagnostic **diag,
332                                      int *num, ODR odr);
333
334 YAZ_EXPORT
335 void yaz_mk_sru_surrogate(ODR o, Z_SRW_record *record, int pos,
336                           int code, const char *details);
337
338 /** \brief encode SRU database for HTTP path
339     \param out memory handle for resulting encoded database string
340     \param db source database
341     \returns encoded database path (includes leading /)
342 */
343 YAZ_EXPORT
344 char *yaz_encode_sru_dbpath_odr(ODR out, const char *db);
345
346 /** \brief encode SRU database for HTTP path
347     \param dst destination buffer (should be at least strlen(db) +2 in size)
348     \param db source database
349    
350     The resulting database (dst) includes a leading /
351 */
352 YAZ_EXPORT
353 void yaz_encode_sru_dbpath_buf(char *dst, const char *db);
354
355 YAZ_END_CDECL
356
357 #endif
358 /*
359  * Local variables:
360  * c-basic-offset: 4
361  * c-file-style: "Stroustrup"
362  * indent-tabs-mode: nil
363  * End:
364  * vim: shiftwidth=4 tabstop=8 expandtab
365  */
366