Bump year in copyright msg in source
[metaproxy-moved-to-github.git] / src / filter_backend_test.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 2005-2009 Index Data
3
4 Metaproxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18
19 #include "config.hpp"
20
21 #include "filter.hpp"
22 #include "package.hpp"
23 #include "util.hpp"
24 #include "filter_backend_test.hpp"
25
26 #include <stdexcept>
27 #include <list>
28 #include <map>
29 #include <iostream>
30
31 #include <boost/thread/mutex.hpp>
32
33 #include <yaz/zgdu.h>
34 #include <yaz/log.h>
35 #include <yaz/otherinfo.h>
36 #include <yaz/diagbib1.h>
37 #include <yaz/oid_db.h>
38
39 namespace mp = metaproxy_1;
40 namespace yf = mp::filter;
41 using namespace mp;
42
43 namespace metaproxy_1 {
44     namespace filter {
45         class Session_info {
46             int dummy;
47         };
48         class BackendTest::Rep {
49             friend class BackendTest;
50
51             Z_Records *fetch(
52                 ODR odr, Odr_oid *preferredRecordSyntax,
53                 Z_ElementSetNames *esn,
54                 int start, int number, int &error_code, std::string &addinfo,
55                 int *number_returned, int *next_position);
56
57             bool m_support_named_result_sets;
58
59             session_map<Session_info> m_sessions;
60         };
61     }
62 }
63
64
65 static const int result_set_size = 42;
66
67 // an ISO2709 USMARC/MARC21 record that we return..
68 static const char *marc_record =
69   "\x30\x30\x33\x36\x36\x6E\x61\x6D\x20\x20\x32\x32\x30\x30\x31\x36"
70   "\x39\x38\x61\x20\x34\x35\x30\x30\x30\x30\x31\x30\x30\x31\x33\x30"
71   "\x30\x30\x30\x30\x30\x30\x33\x30\x30\x30\x34\x30\x30\x30\x31\x33"
72   "\x30\x30\x35\x30\x30\x31\x37\x30\x30\x30\x31\x37\x30\x30\x38\x30"
73   "\x30\x34\x31\x30\x30\x30\x33\x34\x30\x31\x30\x30\x30\x31\x37\x30"
74   "\x30\x31\x37\x39\x30\x34\x30\x30\x30\x31\x33\x30\x30\x30\x37\x35"
75   "\x30\x35\x30\x30\x30\x31\x32\x30\x30\x30\x38\x38\x31\x30\x30\x30"
76   "\x30\x31\x37\x30\x30\x31\x30\x30\x32\x34\x35\x30\x30\x33\x30\x30"
77   "\x30\x31\x31\x37\x32\x36\x30\x30\x30\x31\x32\x30\x30\x31\x34\x37"
78   "\x32\x36\x33\x30\x30\x30\x39\x30\x30\x31\x35\x39\x33\x30\x30\x30"
79   "\x30\x31\x31\x30\x30\x31\x36\x38\x1E\x20\x20\x20\x31\x31\x32\x32"
80   "\x34\x34\x36\x36\x20\x1E\x44\x4C\x43\x1E\x30\x30\x30\x30\x30\x30"
81   "\x30\x30\x30\x30\x30\x30\x30\x30\x2E\x30\x1E\x39\x31\x30\x37\x31"
82   "\x30\x63\x31\x39\x39\x31\x30\x37\x30\x31\x6E\x6A\x75\x20\x20\x20"
83   "\x20\x20\x20\x20\x20\x20\x20\x20\x30\x30\x30\x31\x30\x20\x65\x6E"
84   "\x67\x20\x20\x1E\x20\x20\x1F\x61\x44\x4C\x43\x1F\x63\x44\x4C\x43"
85   "\x1E\x30\x30\x1F\x61\x31\x32\x33\x2D\x78\x79\x7A\x1E\x31\x30\x1F"
86   "\x61\x4A\x61\x63\x6B\x20\x43\x6F\x6C\x6C\x69\x6E\x73\x1E\x31\x30"
87   "\x1F\x61\x48\x6F\x77\x20\x74\x6F\x20\x70\x72\x6F\x67\x72\x61\x6D"
88   "\x20\x61\x20\x63\x6F\x6D\x70\x75\x74\x65\x72\x1E\x31\x20\x1F\x61"
89   "\x50\x65\x6E\x67\x75\x69\x6E\x1E\x20\x20\x1F\x61\x38\x37\x31\x30"
90   "\x1E\x20\x20\x1F\x61\x70\x2E\x20\x63\x6D\x2E\x1E\x20\x20\x1F\x61"
91   "\x20\x20\x20\x31\x31\x32\x32\x34\x34\x36\x36\x20\x1E\x1D";
92
93
94 yf::BackendTest::BackendTest() : m_p(new BackendTest::Rep) {
95     m_p->m_support_named_result_sets = false;
96 }
97
98 yf::BackendTest::~BackendTest() {
99 }
100
101 Z_Records *yf::BackendTest::Rep::fetch(
102     ODR odr, Odr_oid *preferredRecordSyntax,
103     Z_ElementSetNames *esn,
104     int start, int number, int &error_code, std::string &addinfo,
105     int *number_returned, int *next_position)
106 {
107     const char *element_set_name = "F"; // default to use
108     
109     if (number + start - 1 > result_set_size || start < 1)
110     {
111         error_code = YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE;
112         return 0;
113     }
114
115     if (!preferredRecordSyntax)
116         preferredRecordSyntax = odr_oiddup(odr, yaz_oid_recsyn_usmarc);
117
118     if (preferredRecordSyntax)
119     {
120         if (!oid_oidcmp(preferredRecordSyntax, yaz_oid_recsyn_xml))
121             ;
122         else if (!oid_oidcmp(preferredRecordSyntax, yaz_oid_recsyn_usmarc))
123             ;
124         else
125         {
126             error_code = YAZ_BIB1_RECORD_SYNTAX_UNSUPP;
127             return 0;
128         }
129     }
130
131     // no element set, "B" and "F" are supported
132     if (esn)
133     {
134         if (esn->which != Z_ElementSetNames_generic)
135         {
136             error_code 
137                 = YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
138             return 0;
139         }
140         element_set_name = esn->u.generic;
141     }
142     if (!strcmp(element_set_name, "B") 
143         && !oid_oidcmp(preferredRecordSyntax, yaz_oid_recsyn_usmarc))
144         ; // Brief
145     else if (!strcmp(element_set_name, "F") 
146              && !oid_oidcmp(preferredRecordSyntax, yaz_oid_recsyn_usmarc))
147         ; // Full
148     else if (!strncmp(element_set_name, "FF", 2) 
149              && !oid_oidcmp(preferredRecordSyntax, yaz_oid_recsyn_xml))
150         ; // Huge XML test record
151     else
152     {
153         error_code 
154             = YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
155         addinfo = std::string(element_set_name);
156         return 0;
157     }
158     Z_Records *rec = (Z_Records *) odr_malloc(odr, sizeof(Z_Records));
159     rec->which = Z_Records_DBOSD;
160     rec->u.databaseOrSurDiagnostics = (Z_NamePlusRecordList *)
161         odr_malloc(odr, sizeof(Z_NamePlusRecordList));
162     rec->u.databaseOrSurDiagnostics->num_records = number;
163     rec->u.databaseOrSurDiagnostics->records = (Z_NamePlusRecord **)
164         odr_malloc(odr, sizeof(Z_NamePlusRecord *) * number);
165     int i;
166     for (i = 0; i<number; i++)
167     {
168         rec->u.databaseOrSurDiagnostics->records[i] = (Z_NamePlusRecord *)
169             odr_malloc(odr, sizeof(Z_NamePlusRecord));
170         Z_NamePlusRecord *npr = rec->u.databaseOrSurDiagnostics->records[i];
171         npr->databaseName = 0;
172         npr->which = Z_NamePlusRecord_databaseRecord;
173
174         if (!strncmp(element_set_name, "FF", 2))
175         {   // Huge XML test record
176             size_t sz = 1024;
177             if (element_set_name[2])
178                 sz = atoi(element_set_name+2) * 1024;
179             if (sz < 10)
180                 sz = 10;
181             char *tmp_rec = (char*) xmalloc(sz);
182
183             memset(tmp_rec, 'a', sz);
184             memcpy(tmp_rec, "<a>", 3);
185             memcpy(tmp_rec + sz - 4, "</a>", 4);
186
187             npr->u.databaseRecord = z_ext_record_xml(odr, tmp_rec, sz);
188             xfree(tmp_rec);
189         }
190         else
191         {
192             char *tmp_rec = odr_strdup(odr, marc_record);
193             char offset_str[30];
194             sprintf(offset_str, "test__%09d_", i+start);
195             memcpy(tmp_rec+186, offset_str, strlen(offset_str));
196             npr->u.databaseRecord = z_ext_record_usmarc(
197                 odr, tmp_rec, strlen(tmp_rec));
198         }
199
200     }
201     *number_returned = number;
202     if (start + number > result_set_size)
203         *next_position = 0;
204     else
205         *next_position = start + number;
206     return rec;
207 }
208
209 void yf::BackendTest::process(Package &package) const
210 {
211     Z_GDU *gdu = package.request().get();
212
213     if (!gdu || gdu->which != Z_GDU_Z3950)
214         package.move();
215     else
216     {
217         Z_APDU *apdu_req = gdu->u.z3950;
218         Z_APDU *apdu_res = 0;
219         mp::odr odr;
220         
221         if (apdu_req->which != Z_APDU_initRequest && 
222             !m_p->m_sessions.exist(package.session()))
223         {
224             apdu_res = odr.create_close(apdu_req,
225                                         Z_Close_protocolError,
226                                         "no init for filter_backend_test");
227             package.session().close();
228         }
229         else if (apdu_req->which == Z_APDU_initRequest)
230         {
231             apdu_res = odr.create_initResponse(apdu_req, 0, 0);
232             Z_InitRequest *req = apdu_req->u.initRequest;
233             Z_InitResponse *resp = apdu_res->u.initResponse;
234
235             resp->implementationName = "backend_test";
236             if (ODR_MASK_GET(req->options, Z_Options_namedResultSets))
237                 m_p->m_support_named_result_sets = true;
238             
239             int i;
240             static const int masks[] = {
241                 Z_Options_search, Z_Options_present,
242                 Z_Options_namedResultSets, -1 
243             };
244             for (i = 0; masks[i] != -1; i++)
245                 if (ODR_MASK_GET(req->options, masks[i]))
246                     ODR_MASK_SET(resp->options, masks[i]);
247             static const int versions[] = {
248                 Z_ProtocolVersion_1,
249                 Z_ProtocolVersion_2,
250                 Z_ProtocolVersion_3,
251                 -1
252             };
253             for (i = 0; versions[i] != -1; i++)
254                 if (ODR_MASK_GET(req->protocolVersion, versions[i]))
255                     ODR_MASK_SET(resp->protocolVersion, versions[i]);
256                 else
257                     break;
258
259             Session_info info;
260             m_p->m_sessions.create(info, package.session());
261         }
262         else if (apdu_req->which == Z_APDU_searchRequest)
263         {
264             Z_SearchRequest *req = apdu_req->u.searchRequest;
265                 
266             if (!m_p->m_support_named_result_sets && 
267                 strcmp(req->resultSetName, "default"))
268             {
269                 apdu_res = 
270                     odr.create_searchResponse(
271                         apdu_req,  YAZ_BIB1_RESULT_SET_NAMING_UNSUPP, 0);
272             }
273             else
274             {
275                 Z_Records *records = 0;
276                 int number_returned = 0;
277                 int next_position = 0;
278                 int error_code = 0;
279                 std::string addinfo;
280                 
281                 int number = 0;
282                 mp::util::piggyback(*req->smallSetUpperBound,
283                                     *req->largeSetLowerBound,
284                                     *req->mediumSetPresentNumber,
285                                     result_set_size,
286                                     number);
287                 
288                 if (number) 
289                 {   // not a large set for sure 
290                     Z_ElementSetNames *esn;
291                     if (number > *req->smallSetUpperBound)
292                         esn = req->mediumSetElementSetNames;
293                     else
294                         esn = req->smallSetElementSetNames;
295                     records = m_p->fetch(
296                         odr, req->preferredRecordSyntax, esn,
297                         1, number,
298                         error_code, addinfo,
299                         &number_returned,
300                         &next_position);
301                 }
302                 if (error_code)
303                 {
304                     apdu_res = 
305                         odr.create_searchResponse(
306                             apdu_req, error_code, addinfo.c_str());
307                     Z_SearchResponse *resp = apdu_res->u.searchResponse;
308                     *resp->resultCount = result_set_size;
309                 }
310                 else
311                 {
312                     apdu_res = 
313                         odr.create_searchResponse(apdu_req, 0, 0);
314                     Z_SearchResponse *resp = apdu_res->u.searchResponse;
315                     *resp->resultCount = result_set_size;
316                     *resp->numberOfRecordsReturned = number_returned;
317                     *resp->nextResultSetPosition = next_position;
318                     resp->records = records;
319                 }
320             }
321         }
322         else if (apdu_req->which == Z_APDU_presentRequest)
323         { 
324             Z_PresentRequest *req = apdu_req->u.presentRequest;
325             int number_returned = 0;
326             int next_position = 0;
327             int error_code = 0;
328             std::string addinfo;
329             Z_ElementSetNames *esn = 0;
330
331             if (req->recordComposition)
332             {
333                 if (req->recordComposition->which == Z_RecordComp_simple)
334                     esn = req->recordComposition->u.simple;
335                 else
336                 {
337                     apdu_res =
338                         odr.create_presentResponse(
339                             apdu_req,
340                             YAZ_BIB1_ONLY_A_SINGLE_ELEMENT_SET_NAME_SUPPORTED,
341                             0);
342                     package.response() = apdu_res;
343                     return;
344                 }
345             }
346             Z_Records *records = m_p->fetch(
347                 odr, req->preferredRecordSyntax, esn,
348                 *req->resultSetStartPoint, *req->numberOfRecordsRequested,
349                 error_code, addinfo,
350                 &number_returned,
351                 &next_position);
352
353             if (error_code)
354             {
355                 apdu_res =
356                     odr.create_presentResponse(apdu_req, error_code,
357                                                addinfo.c_str());
358             }
359             else
360             {
361                 apdu_res =
362                     odr.create_presentResponse(apdu_req, 0, 0);
363                 Z_PresentResponse *resp = apdu_res->u.presentResponse;
364                 resp->records = records;
365                 *resp->numberOfRecordsReturned = number_returned;
366                 *resp->nextResultSetPosition = next_position;
367             }
368         }
369         else
370         {
371             apdu_res = odr.create_close(apdu_req,
372                                         Z_Close_protocolError,
373                                         "backend_test: unhandled APDU");
374             package.session().close();
375         }
376         if (apdu_res)
377             package.response() = apdu_res;
378     }
379     if (package.session().is_closed())
380         m_p->m_sessions.release(package.session());
381 }
382
383 static mp::filter::Base* filter_creator()
384 {
385     return new mp::filter::BackendTest;
386 }
387
388 extern "C" {
389     struct metaproxy_1_filter_struct metaproxy_1_filter_backend_test = {
390         0,
391         "backend_test",
392         filter_creator
393     };
394 }
395
396
397 /*
398  * Local variables:
399  * c-basic-offset: 4
400  * c-file-style: "Stroustrup"
401  * indent-tabs-mode: nil
402  * End:
403  * vim: shiftwidth=4 tabstop=8 expandtab
404  */
405