51afacd9ece28bd78af62cfec9e234c9de31811d
[mp-xquery-moved-to-github.git] / src / metaproxy_filter_xquery.cpp
1 /* This file is part of mp-xquery
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 <metaproxy/package.hpp>
20 #include <metaproxy/util.hpp>
21 #include <yaz/log.h>
22 #include <yaz/oid_db.h>
23 #include <map>
24
25 #include <iostream>
26 #include <fstream>
27 #include <sstream>
28
29 #include <zorba/zorba.h>
30 #include <zorba/store_manager.h>
31 #include <zorba/serializer.h>
32 #include <zorba/singleton_item_sequence.h>
33 #include <zorba/zorba_exception.h>
34
35
36 namespace mp = metaproxy_1;
37 namespace yf = mp::filter;
38 namespace mp_util = metaproxy_1::util;
39 using namespace mp;
40 using namespace zorba;
41
42 namespace metaproxy_1 {
43     namespace filter {
44         class XQuery : public Base {
45         public:
46             ~XQuery();
47             XQuery();
48             void process(metaproxy_1::Package & package) const;
49             void configure(const xmlNode * ptr, bool test_only,
50                            const char *path);
51             void start() const;
52             void stop(int signo) const;
53         private:
54             bool convert_one_record(const char *input_buf,
55                                     size_t input_len,
56                                     std::string &result) const;
57             std::map<std::string, std::string> zorba_variables;
58             std::string zorba_filename;
59             std::string zorba_script;
60             std::string zorba_record_variable;
61             Zorba *lZorba;
62             XQuery_t lQuery;
63         };
64     }
65 }
66
67 yf::XQuery::XQuery()
68 {
69     lZorba = 0;
70 }
71
72 yf::XQuery::~XQuery()
73 {
74     if (lZorba)
75         lZorba->shutdown();
76 }
77
78 void yf::XQuery::start() const
79 {
80 }
81
82 void yf::XQuery::stop(int signo) const
83 {
84 }
85
86 bool yf::XQuery::convert_one_record(const char *input_buf,
87                                     size_t input_len,
88                                     std::string &result) const
89 {
90     XQuery_t tQuery = lQuery->clone();
91
92     zorba::DynamicContext* lDynamicContext = tQuery->getDynamicContext();
93
94     zorba::Item lItem;
95     std::map<std::string, std::string>::const_iterator it;
96     for (it = zorba_variables.begin(); it != zorba_variables.end(); it++)
97     {
98         lItem = lZorba->getItemFactory()->createString(it->second);
99         lDynamicContext->setVariable(it->first, lItem);
100     }
101     std::string rec_content = "raw:" + std::string(input_buf, input_len);
102     lItem = lZorba->getItemFactory()->createString(rec_content);
103     lDynamicContext->setVariable(zorba_record_variable, lItem);
104
105     std::stringstream ss;
106     tQuery->execute(ss);
107     result = ss.str();
108     return true;
109 }
110
111 void yf::XQuery::process(Package &package) const
112 {
113     Z_GDU *gdu_req = package.request().get();
114     Z_PresentRequest *pr_req = 0;
115     Z_SearchRequest *sr_req = 0;
116
117     const char *input_schema = 0;
118     Odr_oid *input_syntax = 0;
119
120     if (gdu_req && gdu_req->which == Z_GDU_Z3950 &&
121         gdu_req->u.z3950->which == Z_APDU_presentRequest)
122     {
123         pr_req = gdu_req->u.z3950->u.presentRequest;
124
125         input_schema =
126             mp_util::record_composition_to_esn(pr_req->recordComposition);
127         input_syntax = pr_req->preferredRecordSyntax;
128     }
129     else if (gdu_req && gdu_req->which == Z_GDU_Z3950 &&
130              gdu_req->u.z3950->which == Z_APDU_searchRequest)
131     {
132         sr_req = gdu_req->u.z3950->u.searchRequest;
133
134         input_syntax = sr_req->preferredRecordSyntax;
135
136         // we don't know how many hits we're going to get and therefore
137         // the effective element set name.. Therefore we can only allow
138         // two cases.. Both equal or absent.. If not, we'll just have to
139         // disable the piggyback!
140         if (sr_req->smallSetElementSetNames
141             &&
142             sr_req->mediumSetElementSetNames
143             &&
144             sr_req->smallSetElementSetNames->which == Z_ElementSetNames_generic
145             &&
146             sr_req->mediumSetElementSetNames->which == Z_ElementSetNames_generic
147             &&
148             !strcmp(sr_req->smallSetElementSetNames->u.generic,
149                     sr_req->mediumSetElementSetNames->u.generic))
150         {
151             input_schema = sr_req->smallSetElementSetNames->u.generic;
152         }
153         else if (!sr_req->smallSetElementSetNames &&
154                  !sr_req->mediumSetElementSetNames)
155             ; // input_schema is 0 already
156         else
157         {
158             // disable piggyback (perhaps it was disabled already)
159             *sr_req->smallSetUpperBound = 0;
160             *sr_req->largeSetLowerBound = 0;
161             *sr_req->mediumSetPresentNumber = 0;
162             package.move();
163             return;
164         }
165         // we can handle it in record_transform.
166     }
167     else
168     {
169         package.move();
170         return;
171     }
172
173     mp::odr odr_en(ODR_ENCODE);
174
175     const char *backend_schema = 0;
176     const Odr_oid *backend_syntax = 0;
177
178     if (input_schema && !strcmp(input_schema, "bibframe") &&
179         (!input_syntax || !oid_oidcmp(input_syntax, yaz_oid_recsyn_xml)))
180     {
181         backend_schema = "marcxml";
182         backend_syntax = yaz_oid_recsyn_xml;
183     }
184     else
185     {
186         package.move();
187         return;
188     }
189
190     if (sr_req)
191     {
192         if (backend_syntax)
193             sr_req->preferredRecordSyntax = odr_oiddup(odr_en, backend_syntax);
194         else
195             sr_req->preferredRecordSyntax = 0;
196         if (backend_schema)
197         {
198             sr_req->smallSetElementSetNames
199                 = (Z_ElementSetNames *)
200                 odr_malloc(odr_en, sizeof(Z_ElementSetNames));
201             sr_req->smallSetElementSetNames->which = Z_ElementSetNames_generic;
202             sr_req->smallSetElementSetNames->u.generic
203                 = odr_strdup(odr_en, backend_schema);
204             sr_req->mediumSetElementSetNames = sr_req->smallSetElementSetNames;
205         }
206         else
207         {
208             sr_req->smallSetElementSetNames = 0;
209             sr_req->mediumSetElementSetNames = 0;
210         }
211     }
212     else if (pr_req)
213     {
214         if (backend_syntax)
215             pr_req->preferredRecordSyntax = odr_oiddup(odr_en, backend_syntax);
216         else
217             pr_req->preferredRecordSyntax = 0;
218
219         if (backend_schema)
220         {
221             pr_req->recordComposition
222                 = (Z_RecordComposition *)
223                 odr_malloc(odr_en, sizeof(Z_RecordComposition));
224             pr_req->recordComposition->which
225                 = Z_RecordComp_simple;
226             pr_req->recordComposition->u.simple
227                 = (Z_ElementSetNames *)
228                 odr_malloc(odr_en, sizeof(Z_ElementSetNames));
229             pr_req->recordComposition->u.simple->which = Z_ElementSetNames_generic;
230             pr_req->recordComposition->u.simple->u.generic
231                 = odr_strdup(odr_en, backend_schema);
232         }
233         else
234             pr_req->recordComposition = 0;
235     }
236     package.move();
237
238     Z_GDU *gdu_res = package.response().get();
239
240     // see if we have a records list to patch!
241     Z_NamePlusRecordList *records = 0;
242     if (gdu_res && gdu_res->which == Z_GDU_Z3950 &&
243         gdu_res->u.z3950->which == Z_APDU_presentResponse)
244     {
245         Z_PresentResponse * pr_res = gdu_res->u.z3950->u.presentResponse;
246
247         if (pr_res
248             && pr_res->numberOfRecordsReturned
249             && *(pr_res->numberOfRecordsReturned) > 0
250             && pr_res->records
251             && pr_res->records->which == Z_Records_DBOSD)
252         {
253             records = pr_res->records->u.databaseOrSurDiagnostics;
254         }
255     }
256     if (gdu_res && gdu_res->which == Z_GDU_Z3950 &&
257         gdu_res->u.z3950->which == Z_APDU_searchResponse)
258     {
259         Z_SearchResponse *sr_res = gdu_res->u.z3950->u.searchResponse;
260
261         if (sr_res
262             && sr_res->numberOfRecordsReturned
263             && *(sr_res->numberOfRecordsReturned) > 0
264             && sr_res->records
265             && sr_res->records->which == Z_Records_DBOSD)
266         {
267             records = sr_res->records->u.databaseOrSurDiagnostics;
268         }
269     }
270     if (records)
271     {
272         int i;
273         for (i = 0; i < records->num_records; i++)
274         {
275             Z_NamePlusRecord *npr = records->records[i];
276             if (npr->which == Z_NamePlusRecord_databaseRecord)
277             {
278                 const char *details = 0;
279                 Z_External *r = npr->u.databaseRecord;
280                 int ret_trans = -1;
281                 if (r->which == Z_External_octet &&
282                     !oid_oidcmp(r->direct_reference, yaz_oid_recsyn_xml))
283                 {
284                     std::string result;
285                     if (convert_one_record(
286                         r->u.octet_aligned->buf, r->u.octet_aligned->len,
287                         result))
288                     {
289                         npr->u.databaseRecord =
290                             z_ext_record_oid(odr_en, yaz_oid_recsyn_xml,
291                                              result.c_str(),
292                                              result.length());
293                     }
294                 }
295             }
296         }
297         package.response() = gdu_res;
298     }
299 }
300
301 void yf::XQuery::configure(const xmlNode * ptr, bool test_only,
302                            const char *path)
303 {
304     for (ptr = ptr->children; ptr; ptr = ptr->next)
305     {
306         if (ptr->type != XML_ELEMENT_NODE)
307             continue;
308         if (!strcmp((const char *) ptr->name, "setVariable"))
309         {
310             std::string name;
311             std::string value;
312             struct _xmlAttr *attr;
313             for (attr = ptr->properties; attr; attr = attr->next)
314                 if (!strcmp((const char *) attr->name, "name"))
315                     name = mp::xml::get_text(attr->children);
316                 else if (!strcmp((const char *) attr->name, "value"))
317                     value = mp::xml::get_text(attr->children);
318                 else
319                     throw mp::filter::FilterException(
320                         "Bad attribute " + std::string((const char *)
321                                                        attr->name));
322             if (name.length() > 0)
323                 zorba_variables[name] = value;
324         }
325         else if (!strcmp((const char *) ptr->name, "filename"))
326         {
327             std::string value;
328             struct _xmlAttr *attr;
329             for (attr = ptr->properties; attr; attr = attr->next)
330                 if (!strcmp((const char *) attr->name, "value"))
331                     value = mp::xml::get_text(attr->children);
332                 else
333                     throw mp::filter::FilterException(
334                         "Bad attribute " + std::string((const char *)
335                                                        attr->name));
336             zorba_filename = value;
337         }
338         else if (!strcmp((const char *) ptr->name, "script"))
339         {
340             std::string value;
341             struct _xmlAttr *attr;
342             for (attr = ptr->properties; attr; attr = attr->next)
343                 if (!strcmp((const char *) attr->name, "value"))
344                     value = mp::xml::get_text(attr->children);
345                 else
346                     throw mp::filter::FilterException(
347                         "Bad attribute " + std::string((const char *)
348                                                        attr->name));
349             zorba_script = value;
350         }
351         else if (!strcmp((const char *) ptr->name, "record"))
352         {
353             std::string value;
354             struct _xmlAttr *attr;
355             for (attr = ptr->properties; attr; attr = attr->next)
356                 if (!strcmp((const char *) attr->name, "value"))
357                     value = mp::xml::get_text(attr->children);
358                 else
359                     throw mp::filter::FilterException(
360                         "Bad attribute " + std::string((const char *)
361                                                        attr->name));
362             zorba_record_variable = value;
363         }
364         else
365         {
366             throw mp::filter::FilterException("Bad element "
367                                                + std::string((const char *)
368                                                              ptr->name));
369         }
370     }
371     if (zorba_script.length() == 0)
372         throw mp::filter::FilterException("Missing element script");
373     if (zorba_record_variable.length() == 0)
374         throw mp::filter::FilterException("Missing element record");
375     if (zorba_filename.length() == 0)
376         throw mp::filter::FilterException("Missing element filename");
377     if (!test_only)
378     {
379         void* lStore = StoreManager::getStore();
380         lZorba = Zorba::getInstance(lStore);
381
382         lQuery = lZorba->createQuery();
383
384         lQuery->setFileName(zorba_filename);
385
386         std::unique_ptr<std::istream> qfile;
387         qfile.reset(new std::ifstream(zorba_script.c_str()));
388
389         Zorba_CompilerHints lHints;
390         lQuery->compile(*qfile, lHints);
391     }
392 }
393
394 static yf::Base* filter_creator()
395 {
396     return new mp::filter::XQuery;
397 }
398
399 extern "C" {
400     struct metaproxy_1_filter_struct metaproxy_1_filter_xquery = {
401         0,
402         "xquery",
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