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