metaproxy  1.21.0
filter_backend_test.cpp
Go to the documentation of this file.
1 /* This file is part of Metaproxy.
2  Copyright (C) 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  };
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 
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  const char *element_set_name = 0;
286 
287  Odr_int number = 0;
288  mp::util::piggyback_sr(req, result_set_size,
289  number, &element_set_name);
290 
291  if (number)
292  { // not a large set for sure
293  Z_ElementSetNames *esn;
294  if (number > *req->smallSetUpperBound)
295  esn = req->mediumSetElementSetNames;
296  else
297  esn = req->smallSetElementSetNames;
298  records = m_p->fetch(
299  odr, req->preferredRecordSyntax, esn,
300  1, number,
301  error_code, addinfo,
302  &number_returned,
303  &next_position);
304  }
305  if (error_code)
306  {
307  apdu_res =
308  odr.create_searchResponse(
309  apdu_req, error_code, addinfo.c_str());
310  Z_SearchResponse *resp = apdu_res->u.searchResponse;
311  *resp->resultCount = result_set_size;
312  }
313  else
314  {
315  apdu_res =
316  odr.create_searchResponse(apdu_req, 0, 0);
317  Z_SearchResponse *resp = apdu_res->u.searchResponse;
318  *resp->resultCount = result_set_size;
319  *resp->numberOfRecordsReturned = number_returned;
320  *resp->nextResultSetPosition = next_position;
321  resp->records = records;
322  }
323  }
324  }
325  else if (apdu_req->which == Z_APDU_presentRequest)
326  {
327  Z_PresentRequest *req = apdu_req->u.presentRequest;
328  int number_returned = 0;
329  int next_position = 0;
330  int error_code = 0;
331  std::string addinfo;
332  Z_ElementSetNames *esn = 0;
333 
334  if (req->recordComposition)
335  {
336  if (req->recordComposition->which == Z_RecordComp_simple)
337  esn = req->recordComposition->u.simple;
338  else
339  {
340  apdu_res =
341  odr.create_presentResponse(
342  apdu_req,
343  YAZ_BIB1_ONLY_A_SINGLE_ELEMENT_SET_NAME_SUPPORTED,
344  0);
345  package.response() = apdu_res;
346  return;
347  }
348  }
349  Z_Records *records = m_p->fetch(
350  odr, req->preferredRecordSyntax, esn,
351  *req->resultSetStartPoint, *req->numberOfRecordsRequested,
352  error_code, addinfo,
353  &number_returned,
354  &next_position);
355 
356  if (error_code)
357  {
358  apdu_res =
359  odr.create_presentResponse(apdu_req, error_code,
360  addinfo.c_str());
361  }
362  else
363  {
364  apdu_res =
365  odr.create_presentResponse(apdu_req, 0, 0);
366  Z_PresentResponse *resp = apdu_res->u.presentResponse;
367  resp->records = records;
368  *resp->numberOfRecordsReturned = number_returned;
369  *resp->nextResultSetPosition = next_position;
370  }
371  }
372  else if (apdu_req->which == Z_APDU_close)
373  {
374  apdu_res = odr.create_close(apdu_req,
375  Z_Close_finished, 0);
376  package.session().close();
377  }
378  else
379  {
380  apdu_res = odr.create_close(apdu_req,
381  Z_Close_protocolError,
382  "backend_test: unhandled APDU");
383  package.session().close();
384  }
385  if (apdu_res)
386  package.response() = apdu_res;
387  }
388  if (package.session().is_closed())
389  m_p->m_sessions.release(package.session());
390 }
391 
392 void mp::filter::BackendTest::configure(const xmlNode * ptr, bool test_only,
393  const char *path)
394 {
395  mp::xml::check_empty(ptr);
396 }
397 
398 static mp::filter::Base* filter_creator()
399 {
400  return new mp::filter::BackendTest;
401 }
402 
403 extern "C" {
404  struct metaproxy_1_filter_struct metaproxy_1_filter_backend_test = {
405  0,
406  "backend_test",
408  };
409 }
410 
411 
412 /*
413  * Local variables:
414  * c-basic-offset: 4
415  * c-file-style: "Stroustrup"
416  * indent-tabs-mode: nil
417  * End:
418  * vim: shiftwidth=4 tabstop=8 expandtab
419  */
420 
session_map< Session_info > m_sessions
struct metaproxy_1_filter_struct metaproxy_1_filter_backend_test
static const char * marc_record
static const int result_set_size
static mp::filter::Base * filter_creator()