metaproxy 1.22.4
filter_backend_test.cpp
Go to the documentation of this file.
1/* This file is part of Metaproxy.
2 Copyright (C) Index Data
3
4Metaproxy is free software; you can redistribute it and/or modify it under
5the terms of the GNU General Public License as published by the Free
6Software Foundation; either version 2, or (at your option) any later
7version.
8
9Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
10WARRANTY; without even the implied warranty of MERCHANTABILITY or
11FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program; if not, write to the Free Software
16Foundation, 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>
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
39namespace mp = metaproxy_1;
40namespace yf = mp::filter;
41using namespace mp;
42
43namespace metaproxy_1 {
44 namespace filter {
46 int dummy;
47 public:
48 Session_info() { dummy = 0; };
49 };
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
60
61 session_map<Session_info> m_sessions;
62 };
63 }
64}
65
66
67static const int result_set_size = 42;
68
69// an ISO2709 USMARC/MARC21 record that we return..
70static 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
97 m_p->m_support_named_result_sets = false;
98}
99
102
103static Z_OPACRecord *dummy_opac(const char *item_id, const char *element_set_name, ODR odr, const char *marc_input)
104{
105 Z_OPACRecord *rec;
106 rec = (Z_OPACRecord *) odr_malloc(odr, sizeof(*rec));
107 rec->bibliographicRecord =
108 z_ext_record_usmarc(odr, marc_input, strlen(marc_input));
109 int availableNow = strcmp(element_set_name, "F") == 0;
110 rec->num_holdingsData = 1;
111 rec->holdingsData = (Z_HoldingsRecord **)
112 odr_malloc(odr, sizeof(*rec->holdingsData));
113 for (int i = 0; i < rec->num_holdingsData; i++)
114 {
115 Z_HoldingsRecord *hr = (Z_HoldingsRecord *)
116 odr_malloc(odr, sizeof(*hr));
117 Z_HoldingsAndCircData *hc = (Z_HoldingsAndCircData *)
118 odr_malloc(odr, sizeof(*hc));
119
120 rec->holdingsData[i] = hr;
121 hr->which = Z_HoldingsRecord_holdingsAndCirc;
122 hr->u.holdingsAndCirc = hc;
123
124 hc->typeOfRecord = odr_strdup(odr, "u");
125
126 hc->encodingLevel = odr_strdup(odr, "u");
127
128 hc->format = 0; /* OPT */
129 hc->receiptAcqStatus = odr_strdup(odr, "0");
130 hc->generalRetention = 0; /* OPT */
131 hc->completeness = 0; /* OPT */
132 hc->dateOfReport = odr_strdup(odr, "000000");
133 hc->nucCode = odr_strdup(odr, "s-FM/GC");
134 hc->localLocation = odr_strdup(odr, "Main or Science/Business Reading Rms - STORED OFFSITE");
135 hc->shelvingLocation = 0; /* OPT */
136 hc->callNumber = odr_strdup(odr, "MLCM 89/00602 (N)");
137 hc->shelvingData = odr_strdup(odr, "FT MEADE");
138 hc->copyNumber = odr_strdup(odr, "Copy 1");
139 hc->publicNote = 0; /* OPT */
140 hc->reproductionNote = 0; /* OPT */
141 hc->termsUseRepro = 0; /* OPT */
142 hc->enumAndChron = 0; /* OPT */
143
144 hc->num_volumes = 0;
145 hc->volumes = 0;
146
147 hc->num_circulationData = 1;
148 hc->circulationData = (Z_CircRecord **)
149 odr_malloc(odr, sizeof(*hc->circulationData));
150 hc->circulationData[0] = (Z_CircRecord *)
151 odr_malloc(odr, sizeof(**hc->circulationData));
152
153 hc->circulationData[0]->availableNow = odr_booldup(odr, availableNow);
154 hc->circulationData[0]->availablityDate = 0;
155 hc->circulationData[0]->availableThru = 0;
156 hc->circulationData[0]->restrictions = 0;
157 hc->circulationData[0]->itemId = odr_strdup(odr, item_id);
158 hc->circulationData[0]->renewable = odr_booldup(odr, availableNow);
159 hc->circulationData[0]->onHold = odr_booldup(odr, !availableNow);
160 hc->circulationData[0]->enumAndChron = 0;
161 hc->circulationData[0]->midspine = 0;
162 hc->circulationData[0]->temporaryLocation = 0;
163 }
164 return rec;
165}
166
168 ODR odr, Odr_oid *preferredRecordSyntax,
169 Z_ElementSetNames *esn,
170 int start, int number, int &error_code, std::string &addinfo,
171 int *number_returned, int *next_position)
172{
173 if (number + start - 1 > result_set_size || start < 1)
174 {
175 error_code = YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE;
176 return 0;
177 }
178
179 const char *element_set_name = "F"; // default to use
180 if (esn)
181 {
182 if (esn->which != Z_ElementSetNames_generic)
183 {
184 error_code
185 = YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
186 return 0;
187 }
188 element_set_name = esn->u.generic;
189 }
190
191 if (!preferredRecordSyntax)
192 preferredRecordSyntax = odr_oiddup(odr, yaz_oid_recsyn_usmarc);
193
194 if (!oid_oidcmp(preferredRecordSyntax, yaz_oid_recsyn_xml))
195 {
196 if (strncmp(element_set_name, "FF", 2) && strcmp(element_set_name, "SD"))
197 {
198 error_code
199 = YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
200 addinfo = std::string(element_set_name);
201 return 0;
202 }
203 }
204 else if (!oid_oidcmp(preferredRecordSyntax, yaz_oid_recsyn_usmarc) || !oid_oidcmp(preferredRecordSyntax, yaz_oid_recsyn_opac))
205 {
206 if (strcmp(element_set_name, "B") && strcmp(element_set_name, "F") && strcmp(element_set_name, "SD"))
207 {
208 error_code
209 = YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_;
210 addinfo = std::string(element_set_name);
211 return 0;
212 }
213 }
214 else
215 {
216 error_code = YAZ_BIB1_RECORD_SYNTAX_UNSUPP;
217 return 0;
218 }
219 Z_Records *rec = (Z_Records *) odr_malloc(odr, sizeof(Z_Records));
220 rec->which = Z_Records_DBOSD;
221 rec->u.databaseOrSurDiagnostics = (Z_NamePlusRecordList *)
222 odr_malloc(odr, sizeof(Z_NamePlusRecordList));
223 rec->u.databaseOrSurDiagnostics->num_records = number;
224 rec->u.databaseOrSurDiagnostics->records = (Z_NamePlusRecord **)
225 odr_malloc(odr, sizeof(Z_NamePlusRecord *) * number);
226 for (int i = 0; i < number; i++)
227 {
228 if (!strcmp(element_set_name, "SD")) {
229 rec->u.databaseOrSurDiagnostics->records[i] =
230 zget_surrogateDiagRec(odr, 0, YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_, "SD");
231 continue;
232 }
233 Z_NamePlusRecord *npr = (Z_NamePlusRecord *) odr_malloc(odr, sizeof(Z_NamePlusRecord));
234 rec->u.databaseOrSurDiagnostics->records[i] = npr;
235 npr->databaseName = 0;
236 npr->which = Z_NamePlusRecord_databaseRecord;
237
238 if (!strncmp(element_set_name, "FF", 2))
239 { // XML test record with optional size in kilo-bytes .. eg FF10 for 10KB
240 size_t sz = 1024;
241 if (element_set_name[2])
242 sz = atoi(element_set_name+2) * 1024;
243 if (sz < 10 || sz > 1024*1024)
244 sz = 10;
245 char *tmp_rec = (char*) xmalloc(sz);
246
247 memset(tmp_rec, 'a', sz);
248 memcpy(tmp_rec, "<a>", 3);
249 memcpy(tmp_rec + sz - 4, "</a>", 4);
250
251 npr->u.databaseRecord = z_ext_record_xml(odr, tmp_rec, sz);
252 xfree(tmp_rec);
253 }
254 else
255 {
256 char *tmp_rec = odr_strdup(odr, marc_record);
257 char offset_str[30];
258 snprintf(offset_str, sizeof offset_str, "test__%09d_", i+start);
259 memcpy(tmp_rec+186, offset_str, strlen(offset_str));
260 if (!oid_oidcmp(preferredRecordSyntax, yaz_oid_recsyn_opac)) {
261 Z_OPACRecord *opac_rec = dummy_opac(offset_str, element_set_name, odr, tmp_rec);
262 npr->u.databaseRecord = z_ext_record_oid(odr, yaz_oid_recsyn_opac, (const char*) opac_rec, -1);
263 }
264 else
265 npr->u.databaseRecord = z_ext_record_usmarc(odr, tmp_rec, strlen(tmp_rec));
266 }
267
268 }
269 *number_returned = number;
270 if (start + number > result_set_size)
271 *next_position = 0;
272 else
273 *next_position = start + number;
274 return rec;
275}
276
277void yf::BackendTest::process(Package &package) const
278{
279 Z_GDU *gdu = package.request().get();
280
281 if (!gdu || gdu->which != Z_GDU_Z3950)
282 package.move();
283 else
284 {
285 Z_APDU *apdu_req = gdu->u.z3950;
286 Z_APDU *apdu_res = 0;
287 mp::odr odr;
288
289 if (apdu_req->which != Z_APDU_initRequest &&
290 !m_p->m_sessions.exist(package.session()))
291 {
292 apdu_res = odr.create_close(apdu_req,
293 Z_Close_protocolError,
294 "no init for filter_backend_test");
295 package.session().close();
296 }
297 else if (apdu_req->which == Z_APDU_initRequest)
298 {
299 apdu_res = odr.create_initResponse(apdu_req, 0, 0);
300 Z_InitRequest *req = apdu_req->u.initRequest;
301 Z_InitResponse *resp = apdu_res->u.initResponse;
302
303 resp->implementationName = odr_strdup(odr, "backend_test");
304 if (ODR_MASK_GET(req->options, Z_Options_namedResultSets))
305 m_p->m_support_named_result_sets = true;
306
307 int i;
308 static const int masks[] = {
309 Z_Options_search, Z_Options_present,
310 Z_Options_namedResultSets, -1
311 };
312 for (i = 0; masks[i] != -1; i++)
313 if (ODR_MASK_GET(req->options, masks[i]))
314 ODR_MASK_SET(resp->options, masks[i]);
315 static const int versions[] = {
316 Z_ProtocolVersion_1,
317 Z_ProtocolVersion_2,
318 Z_ProtocolVersion_3,
319 -1
320 };
321 for (i = 0; versions[i] != -1; i++)
322 if (ODR_MASK_GET(req->protocolVersion, versions[i]))
323 ODR_MASK_SET(resp->protocolVersion, versions[i]);
324 else
325 break;
326
327 *resp->preferredMessageSize = *req->preferredMessageSize;
328 *resp->maximumRecordSize = *req->maximumRecordSize;
329
330 Session_info info;
331 m_p->m_sessions.create(info, package.session());
332 }
333 else if (apdu_req->which == Z_APDU_searchRequest)
334 {
335 Z_SearchRequest *req = apdu_req->u.searchRequest;
336
337 if (!m_p->m_support_named_result_sets &&
338 strcmp(req->resultSetName, "default"))
339 {
340 apdu_res =
341 odr.create_searchResponse(
342 apdu_req, YAZ_BIB1_RESULT_SET_NAMING_UNSUPP, 0);
343 }
344 else
345 {
346 Z_Records *records = 0;
347 int number_returned = 0;
348 int next_position = 0;
349 int error_code = 0;
350 std::string addinfo;
351 const char *element_set_name = 0;
352
353 Odr_int number = 0;
354 mp::util::piggyback_sr(req, result_set_size,
355 number, &element_set_name);
356
357 if (number)
358 { // not a large set for sure
359 Z_ElementSetNames *esn;
360 if (number > *req->smallSetUpperBound)
361 esn = req->mediumSetElementSetNames;
362 else
363 esn = req->smallSetElementSetNames;
364 records = m_p->fetch(
365 odr, req->preferredRecordSyntax, esn,
366 1, number,
367 error_code, addinfo,
368 &number_returned,
369 &next_position);
370 }
371 if (error_code)
372 {
373 apdu_res =
374 odr.create_searchResponse(
375 apdu_req, error_code, addinfo.c_str());
376 Z_SearchResponse *resp = apdu_res->u.searchResponse;
377 *resp->resultCount = result_set_size;
378 }
379 else
380 {
381 apdu_res =
382 odr.create_searchResponse(apdu_req, 0, 0);
383 Z_SearchResponse *resp = apdu_res->u.searchResponse;
384 *resp->resultCount = result_set_size;
385 *resp->numberOfRecordsReturned = number_returned;
386 *resp->nextResultSetPosition = next_position;
387 resp->records = records;
388 }
389 }
390 }
391 else if (apdu_req->which == Z_APDU_presentRequest)
392 {
393 Z_PresentRequest *req = apdu_req->u.presentRequest;
394 int number_returned = 0;
395 int next_position = 0;
396 int error_code = 0;
397 std::string addinfo;
398 Z_ElementSetNames *esn = 0;
399
400 if (req->recordComposition)
401 {
402 if (req->recordComposition->which == Z_RecordComp_simple)
403 esn = req->recordComposition->u.simple;
404 else
405 {
406 apdu_res =
407 odr.create_presentResponse(
408 apdu_req,
409 YAZ_BIB1_ONLY_A_SINGLE_ELEMENT_SET_NAME_SUPPORTED,
410 0);
411 package.response() = apdu_res;
412 return;
413 }
414 }
415 Z_Records *records = m_p->fetch(
416 odr, req->preferredRecordSyntax, esn,
417 *req->resultSetStartPoint, *req->numberOfRecordsRequested,
418 error_code, addinfo,
419 &number_returned,
420 &next_position);
421
422 if (error_code)
423 {
424 apdu_res =
425 odr.create_presentResponse(apdu_req, error_code,
426 addinfo.c_str());
427 }
428 else
429 {
430 apdu_res =
431 odr.create_presentResponse(apdu_req, 0, 0);
432 Z_PresentResponse *resp = apdu_res->u.presentResponse;
433 resp->records = records;
434 *resp->numberOfRecordsReturned = number_returned;
435 *resp->nextResultSetPosition = next_position;
436 }
437 }
438 else if (apdu_req->which == Z_APDU_close)
439 {
440 apdu_res = odr.create_close(apdu_req,
441 Z_Close_finished, 0);
442 package.session().close();
443 }
444 else
445 {
446 apdu_res = odr.create_close(apdu_req,
447 Z_Close_protocolError,
448 "backend_test: unhandled APDU");
449 package.session().close();
450 }
451 if (apdu_res)
452 package.response() = apdu_res;
453 }
454 if (package.session().is_closed())
455 m_p->m_sessions.release(package.session());
456}
457
458void mp::filter::BackendTest::configure(const xmlNode * ptr, bool test_only,
459 const char *path)
460{
461 mp::xml::check_empty(ptr);
462}
463
464static mp::filter::Base* filter_creator()
465{
466 return new mp::filter::BackendTest;
467}
468
469extern "C" {
470 struct metaproxy_1_filter_struct metaproxy_1_filter_backend_test = {
471 0,
472 "backend_test",
474 };
475}
476
477
478/*
479 * Local variables:
480 * c-basic-offset: 4
481 * c-file-style: "Stroustrup"
482 * indent-tabs-mode: nil
483 * End:
484 * vim: shiftwidth=4 tabstop=8 expandtab
485 */
Z_Records * fetch(ODR odr, Odr_oid *preferredRecordSyntax, Z_ElementSetNames *esn, int start, int number, int &error_code, std::string &addinfo, int *number_returned, int *next_position)
void process(metaproxy_1::Package &package) const
void configure(const xmlNode *ptr, bool test_only, const char *path)
static mp::filter::Base * filter_creator()
struct metaproxy_1_filter_struct metaproxy_1_filter_backend_test
static Z_OPACRecord * dummy_opac(const char *item_id, const char *element_set_name, ODR odr, const char *marc_input)
static const char * marc_record
static const int result_set_size