1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2012 Index Data
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
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
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
20 #include "filter_record_transform.hpp"
21 #include <metaproxy/package.hpp>
22 #include <metaproxy/util.hpp>
23 #include "gduutil.hpp"
25 #include <yaz/diagbib1.h>
27 #include <yaz/retrieval.h>
29 #include <boost/thread/mutex.hpp>
32 #include <usemarconlib.h>
38 namespace mp = metaproxy_1;
39 namespace yf = mp::filter;
40 namespace mp_util = metaproxy_1::util;
42 namespace metaproxy_1 {
44 class RecordTransform::Impl {
48 void process(metaproxy_1::Package & package) const;
49 void configure(const xmlNode * xml_node, const char *path);
51 yaz_retrieval_t m_retrieval;
57 struct info_usemarcon {
63 Usemarcon *usemarcon1;
64 Usemarcon *usemarcon2;
67 static int convert_usemarcon(void *info, WRBUF record, WRBUF wr_error)
69 struct info_usemarcon *p = (struct info_usemarcon *) info;
71 boost::mutex::scoped_lock lock(p->m_mutex);
79 p->usemarcon1->SetMarcRecord(wrbuf_buf(record), wrbuf_len(record));
80 res = p->usemarcon1->Convert();
83 wrbuf_printf(wr_error, "usemarcon stage1 failed res=%d", res);
86 p->usemarcon1->GetMarcRecord(converted, convlen);
90 p->usemarcon2->SetMarcRecord(converted, convlen);
92 res = p->usemarcon2->Convert();
96 wrbuf_printf(wr_error, "usemarcon stage2 failed res=%d",
100 p->usemarcon2->GetMarcRecord(converted, convlen);
102 wrbuf_rewind(record);
103 wrbuf_write(record, converted, convlen);
109 static void destroy_usemarcon(void *info)
111 struct info_usemarcon *p = (struct info_usemarcon *) info;
113 delete p->usemarcon1;
114 delete p->usemarcon2;
120 static void *construct_usemarcon(const xmlNode *ptr, const char *path,
123 struct _xmlAttr *attr;
124 if (strcmp((const char *) ptr->name, "usemarcon"))
127 struct info_usemarcon *p = new(struct info_usemarcon);
133 for (attr = ptr->properties; attr; attr = attr->next)
135 if (!xmlStrcmp(attr->name, BAD_CAST "stage1") &&
136 attr->children && attr->children->type == XML_TEXT_NODE)
137 p->stage1 = xstrdup((const char *) attr->children->content);
138 else if (!xmlStrcmp(attr->name, BAD_CAST "stage2") &&
139 attr->children && attr->children->type == XML_TEXT_NODE)
140 p->stage2 = xstrdup((const char *) attr->children->content);
143 wrbuf_printf(wr_error, "Bad attribute '%s'"
144 "Expected stage1 or stage2.", attr->name);
145 destroy_usemarcon(p);
152 p->usemarcon1 = new Usemarcon();
153 p->usemarcon1->SetIniFileName(p->stage1);
157 p->usemarcon2 = new Usemarcon();
158 p->usemarcon2->SetIniFileName(p->stage2);
163 static void type_usemarcon(struct yaz_record_conv_type *t)
166 t->construct = construct_usemarcon;
167 t->convert = convert_usemarcon;
168 t->destroy = destroy_usemarcon;
172 // define Pimpl wrapper forwarding to Impl
174 yf::RecordTransform::RecordTransform() : m_p(new Impl)
178 yf::RecordTransform::~RecordTransform()
179 { // must have a destructor because of boost::scoped_ptr
182 void yf::RecordTransform::configure(const xmlNode *xmlnode, bool test_only,
185 m_p->configure(xmlnode, path);
188 void yf::RecordTransform::process(mp::Package &package) const
190 m_p->process(package);
194 yf::RecordTransform::Impl::Impl()
196 m_retrieval = yaz_retrieval_create();
200 yf::RecordTransform::Impl::~Impl()
203 yaz_retrieval_destroy(m_retrieval);
206 void yf::RecordTransform::Impl::configure(const xmlNode *xml_node,
209 yaz_retrieval_set_path(m_retrieval, path);
212 throw mp::XMLError("RecordTransform filter config: empty XML DOM");
214 // parsing down to retrieval node, which can be any of the children nodes
215 xmlNode *retrieval_node;
216 for (retrieval_node = xml_node->children;
218 retrieval_node = retrieval_node->next)
220 if (retrieval_node->type != XML_ELEMENT_NODE)
222 if (0 == strcmp((const char *) retrieval_node->name, "retrievalinfo"))
227 struct yaz_record_conv_type mt;
229 struct yaz_record_conv_type *t = &mt;
231 struct yaz_record_conv_type *t = 0;
234 // read configuration
235 if (0 != yaz_retrieval_configure_t(m_retrieval, retrieval_node, t))
237 std::string msg("RecordTransform filter config: ");
238 msg += yaz_retrieval_get_error(m_retrieval);
239 throw mp::XMLError(msg);
243 void yf::RecordTransform::Impl::process(mp::Package &package) const
246 Z_GDU *gdu_req = package.request().get();
247 Z_PresentRequest *pr_req = 0;
248 Z_SearchRequest *sr_req = 0;
250 const char *input_schema = 0;
251 Odr_oid *input_syntax = 0;
253 if (gdu_req && gdu_req->which == Z_GDU_Z3950 &&
254 gdu_req->u.z3950->which == Z_APDU_presentRequest)
256 pr_req = gdu_req->u.z3950->u.presentRequest;
259 mp_util::record_composition_to_esn(pr_req->recordComposition);
260 input_syntax = pr_req->preferredRecordSyntax;
262 else if (gdu_req && gdu_req->which == Z_GDU_Z3950 &&
263 gdu_req->u.z3950->which == Z_APDU_searchRequest)
265 sr_req = gdu_req->u.z3950->u.searchRequest;
267 input_syntax = sr_req->preferredRecordSyntax;
269 // we don't know how many hits we're going to get and therefore
270 // the effective element set name.. Therefore we can only allow
271 // two cases.. Both equal or absent.. If not, we'll just have to
272 // disable the piggyback!
273 if (sr_req->smallSetElementSetNames
275 sr_req->mediumSetElementSetNames
277 sr_req->smallSetElementSetNames->which == Z_ElementSetNames_generic
279 sr_req->mediumSetElementSetNames->which == Z_ElementSetNames_generic
281 !strcmp(sr_req->smallSetElementSetNames->u.generic,
282 sr_req->mediumSetElementSetNames->u.generic))
284 input_schema = sr_req->smallSetElementSetNames->u.generic;
286 else if (!sr_req->smallSetElementSetNames &&
287 !sr_req->mediumSetElementSetNames)
288 ; // input_schema is 0 already
291 // disable piggyback (perhaps it was disabled already)
292 *sr_req->smallSetUpperBound = 0;
293 *sr_req->largeSetLowerBound = 0;
294 *sr_req->mediumSetPresentNumber = 0;
298 // we can handle it in record_transform.
306 mp::odr odr_en(ODR_ENCODE);
308 // setting up variables for conversion state
309 yaz_record_conv_t rc = 0;
311 const char *match_schema = 0;
312 Odr_oid *match_syntax = 0;
314 const char *backend_schema = 0;
315 Odr_oid *backend_syntax = 0;
318 = yaz_retrieval_request(m_retrieval,
319 input_schema, input_syntax,
320 &match_schema, &match_syntax,
322 &backend_schema, &backend_syntax);
327 const char *details = 0;
329 if (ret_code == -1) /* error ? */
331 details = yaz_retrieval_get_error(m_retrieval);
332 error_code = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
334 else if (ret_code == 1 || ret_code == 3)
336 details = input_schema;
337 error_code = YAZ_BIB1_ELEMENT_SET_NAMES_UNSUPP;
339 else if (ret_code == 2)
341 char oidbuf[OID_STR_MAX];
342 oid_oid_to_dotstring(input_syntax, oidbuf);
343 details = odr_strdup(odr_en, oidbuf);
344 error_code = YAZ_BIB1_RECORD_SYNTAX_UNSUPP;
348 char *tmp = (char*) odr_malloc(odr_en, 80);
350 "record_transform: yaz_retrieval_get_error returned %d",
353 error_code = YAZ_BIB1_UNSPECIFIED_ERROR;
358 apdu = odr_en.create_searchResponse(
359 gdu_req->u.z3950, error_code, details);
363 apdu = odr_en.create_presentResponse(
364 gdu_req->u.z3950, error_code, details);
366 package.response() = apdu;
373 sr_req->preferredRecordSyntax = odr_oiddup(odr_en, backend_syntax);
375 sr_req->preferredRecordSyntax = 0;
379 sr_req->smallSetElementSetNames
380 = (Z_ElementSetNames *)
381 odr_malloc(odr_en, sizeof(Z_ElementSetNames));
382 sr_req->smallSetElementSetNames->which = Z_ElementSetNames_generic;
383 sr_req->smallSetElementSetNames->u.generic
384 = odr_strdup(odr_en, backend_schema);
385 sr_req->mediumSetElementSetNames = sr_req->smallSetElementSetNames;
389 sr_req->smallSetElementSetNames = 0;
390 sr_req->mediumSetElementSetNames = 0;
396 pr_req->preferredRecordSyntax = odr_oiddup(odr_en, backend_syntax);
398 pr_req->preferredRecordSyntax = 0;
402 pr_req->recordComposition
403 = (Z_RecordComposition *)
404 odr_malloc(odr_en, sizeof(Z_RecordComposition));
405 pr_req->recordComposition->which
406 = Z_RecordComp_simple;
407 pr_req->recordComposition->u.simple
408 = (Z_ElementSetNames *)
409 odr_malloc(odr_en, sizeof(Z_ElementSetNames));
410 pr_req->recordComposition->u.simple->which = Z_ElementSetNames_generic;
411 pr_req->recordComposition->u.simple->u.generic
412 = odr_strdup(odr_en, backend_schema);
415 pr_req->recordComposition = 0;
418 // attaching Z3950 package to filter chain
419 package.request() = gdu_req;
423 Z_GDU *gdu_res = package.response().get();
425 // see if we have a records list to patch!
426 Z_NamePlusRecordList *records = 0;
427 if (gdu_res && gdu_res->which == Z_GDU_Z3950 &&
428 gdu_res->u.z3950->which == Z_APDU_presentResponse)
430 Z_PresentResponse * pr_res = gdu_res->u.z3950->u.presentResponse;
433 && pr_res->numberOfRecordsReturned
434 && *(pr_res->numberOfRecordsReturned) > 0
436 && pr_res->records->which == Z_Records_DBOSD)
438 records = pr_res->records->u.databaseOrSurDiagnostics;
441 if (gdu_res && gdu_res->which == Z_GDU_Z3950 &&
442 gdu_res->u.z3950->which == Z_APDU_searchResponse)
444 Z_SearchResponse *sr_res = gdu_res->u.z3950->u.searchResponse;
447 && sr_res->numberOfRecordsReturned
448 && *(sr_res->numberOfRecordsReturned) > 0
450 && sr_res->records->which == Z_Records_DBOSD)
452 records = sr_res->records->u.databaseOrSurDiagnostics;
459 for (i = 0; i < records->num_records; i++)
461 Z_NamePlusRecord *npr = records->records[i];
462 if (npr->which == Z_NamePlusRecord_databaseRecord)
464 mp::wrbuf output_record;
465 Z_External *r = npr->u.databaseRecord;
467 if (r->which == Z_External_OPAC)
470 yaz_record_conv_opac_record(rc, r->u.opac,
473 else if (r->which == Z_External_octet)
476 yaz_record_conv_record(rc, (const char *)
477 r->u.octet_aligned->buf,
478 r->u.octet_aligned->len,
483 npr->u.databaseRecord =
484 z_ext_record_oid(odr_en, match_syntax,
486 output_record.len());
490 records->records[i] =
491 zget_surrogateDiagRec(
492 odr_en, npr->databaseName,
493 YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS,
494 yaz_record_conv_get_error(rc));
498 package.response() = gdu_res;
503 static mp::filter::Base* filter_creator()
505 return new mp::filter::RecordTransform;
509 struct metaproxy_1_filter_struct metaproxy_1_filter_record_transform = {
520 * c-file-style: "Stroustrup"
521 * indent-tabs-mode: nil
523 * vim: shiftwidth=4 tabstop=8 expandtab