URL recipe in place
[metaproxy-moved-to-github.git] / src / filter_zoom.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 #include "filter_zoom.hpp"
21 #include <yaz/zoom.h>
22 #include <yaz/yaz-version.h>
23 #include <yaz/srw.h>
24 #include <metaproxy/package.hpp>
25 #include <metaproxy/util.hpp>
26 #include <metaproxy/xmlutil.hpp>
27 #include "torus.hpp"
28
29 #include <libxslt/xsltutils.h>
30 #include <libxslt/transform.h>
31
32 #include <boost/thread/mutex.hpp>
33 #include <boost/thread/condition.hpp>
34 #include <yaz/ccl_xml.h>
35 #include <yaz/ccl.h>
36 #include <yaz/rpn2cql.h>
37 #include <yaz/rpn2solr.h>
38 #include <yaz/pquery.h>
39 #include <yaz/cql.h>
40 #include <yaz/oid_db.h>
41 #include <yaz/diagbib1.h>
42 #include <yaz/log.h>
43 #include <yaz/zgdu.h>
44 #include <yaz/querytowrbuf.h>
45
46 namespace mp = metaproxy_1;
47 namespace yf = mp::filter;
48
49 namespace metaproxy_1 {
50     namespace filter {
51         struct Zoom::Searchable : boost::noncopyable {
52             std::string authentication;
53             std::string cfAuth;
54             std::string cfProxy;
55             std::string cfSubDb;
56             std::string udb;
57             std::string target;
58             std::string query_encoding;
59             std::string sru;
60             std::string request_syntax;
61             std::string element_set;
62             std::string record_encoding;
63             std::string transform_xsl_fname;
64             std::string urlRecipe;
65             bool use_turbomarc;
66             bool piggyback;
67             CCL_bibset ccl_bibset;
68             Searchable(CCL_bibset base);
69             ~Searchable();
70         };
71         class Zoom::Backend : boost::noncopyable {
72             friend class Impl;
73             friend class Frontend;
74             std::string zurl;
75             ZOOM_connection m_connection;
76             ZOOM_resultset m_resultset;
77             std::string m_frontend_database;
78             SearchablePtr sptr;
79             xsltStylesheetPtr xsp;
80         public:
81             Backend(SearchablePtr sptr);
82             ~Backend();
83             void connect(std::string zurl, int *error, const char **addinfo);
84             void search_pqf(const char *pqf, Odr_int *hits,
85                             int *error, const char **addinfo);
86             void search_cql(const char *cql, Odr_int *hits,
87                             int *error, const char **addinfo);
88             void present(Odr_int start, Odr_int number, ZOOM_record *recs,
89                          int *error, const char **addinfo);
90             void set_option(const char *name, const char *value);
91             const char *get_option(const char *name);
92             void get_zoom_error(int *error, const char **addinfo);
93         };
94         class Zoom::Frontend : boost::noncopyable {
95             friend class Impl;
96             Impl *m_p;
97             bool m_is_virtual;
98             bool m_in_use;
99             yazpp_1::GDU m_init_gdu;
100             BackendPtr m_backend;
101             void handle_package(mp::Package &package);
102             void handle_search(mp::Package &package);
103             void handle_present(mp::Package &package);
104             BackendPtr get_backend_from_databases(std::string &database,
105                                                   int *error,
106                                                   char **addinfo,
107                                                   ODR odr);
108             Z_Records *get_records(Odr_int start,
109                                    Odr_int number_to_present,
110                                    int *error,
111                                    const char **addinfo,
112                                    Odr_int *number_of_records_returned,
113                                    ODR odr, BackendPtr b,
114                                    Odr_oid *preferredRecordSyntax,
115                                    const char *element_set_name);
116         public:
117             Frontend(Impl *impl);
118             ~Frontend();
119         };
120         class Zoom::Impl {
121             friend class Frontend;
122         public:
123             Impl();
124             ~Impl();
125             void process(metaproxy_1::Package & package);
126             void configure(const xmlNode * ptr, bool test_only);
127         private:
128             void configure_local_records(const xmlNode * ptr, bool test_only);
129             FrontendPtr get_frontend(mp::Package &package);
130             void release_frontend(mp::Package &package);
131             SearchablePtr parse_torus_record(const xmlNode *ptr);
132             struct cql_node *convert_cql_fields(struct cql_node *cn, ODR odr);
133             std::map<mp::Session, FrontendPtr> m_clients;            
134             boost::mutex m_mutex;
135             boost::condition m_cond_session_ready;
136             std::string torus_url;
137             std::map<std::string,std::string> fieldmap;
138             std::string xsldir;
139             CCL_bibset bibset;
140             std::string element_transform;
141             std::string element_raw;
142             std::map<std::string,SearchablePtr> s_map;
143         };
144     }
145 }
146
147 // define Pimpl wrapper forwarding to Impl
148  
149 yf::Zoom::Zoom() : m_p(new Impl)
150 {
151 }
152
153 yf::Zoom::~Zoom()
154 {  // must have a destructor because of boost::scoped_ptr
155 }
156
157 void yf::Zoom::configure(const xmlNode *xmlnode, bool test_only)
158 {
159     m_p->configure(xmlnode, test_only);
160 }
161
162 void yf::Zoom::process(mp::Package &package) const
163 {
164     m_p->process(package);
165 }
166
167
168 // define Implementation stuff
169
170 yf::Zoom::Backend::Backend(SearchablePtr ptr) : sptr(ptr)
171 {
172     m_connection = ZOOM_connection_create(0);
173     m_resultset = 0;
174     xsp = 0;
175 }
176
177 yf::Zoom::Backend::~Backend()
178 {
179     if (xsp)
180         xsltFreeStylesheet(xsp);
181     ZOOM_connection_destroy(m_connection);
182     ZOOM_resultset_destroy(m_resultset);
183 }
184
185
186 void yf::Zoom::Backend::get_zoom_error(int *error, const char **addinfo)
187 {
188     const char *msg = 0;
189     *error = ZOOM_connection_error(m_connection, &msg, addinfo);
190     if (*error)
191     {
192         if (*error >= ZOOM_ERROR_CONNECT)
193         {
194             // turn ZOOM diagnostic into a Bib-1 2: with addinfo=zoom err msg
195             *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
196             if (addinfo)
197                 *addinfo = msg;
198         }
199     }
200 }
201
202 void yf::Zoom::Backend::connect(std::string zurl,
203                                 int *error, const char **addinfo)
204 {
205     ZOOM_connection_connect(m_connection, zurl.c_str(), 0);
206     get_zoom_error(error, addinfo);
207 }
208
209 void yf::Zoom::Backend::search_pqf(const char *pqf, Odr_int *hits,
210                                    int *error, const char **addinfo)
211 {
212     m_resultset = ZOOM_connection_search_pqf(m_connection, pqf);
213     get_zoom_error(error, addinfo);
214     if (*error == 0)
215         *hits = ZOOM_resultset_size(m_resultset);
216     else
217         *hits = 0;
218 }
219
220 void yf::Zoom::Backend::search_cql(const char *cql, Odr_int *hits,
221                                    int *error, const char **addinfo)
222 {
223     ZOOM_query q = ZOOM_query_create();
224
225     ZOOM_query_cql(q, cql);
226
227     m_resultset = ZOOM_connection_search(m_connection, q);
228     ZOOM_query_destroy(q);
229     get_zoom_error(error, addinfo);
230     if (*error == 0)
231         *hits = ZOOM_resultset_size(m_resultset);
232     else
233         *hits = 0;
234 }
235
236 void yf::Zoom::Backend::present(Odr_int start, Odr_int number,
237                                 ZOOM_record *recs,
238                                 int *error, const char **addinfo)
239 {
240     ZOOM_resultset_records(m_resultset, recs, start, number);
241     get_zoom_error(error, addinfo);
242 }
243
244 void yf::Zoom::Backend::set_option(const char *name, const char *value)
245 {
246     ZOOM_connection_option_set(m_connection, name, value);
247     if (m_resultset)
248         ZOOM_resultset_option_set(m_resultset, name, value);
249 }
250
251 const char *yf::Zoom::Backend::get_option(const char *name)
252 {
253     return ZOOM_connection_option_get(m_connection, name);
254 }
255
256 yf::Zoom::Searchable::Searchable(CCL_bibset base)
257 {
258     piggyback = true;
259     use_turbomarc = true;
260     ccl_bibset = ccl_qual_dup(base);
261 }
262
263 yf::Zoom::Searchable::~Searchable()
264 {
265     ccl_qual_rm(&ccl_bibset);
266 }
267
268 yf::Zoom::Frontend::Frontend(Impl *impl) : 
269     m_p(impl), m_is_virtual(false), m_in_use(true)
270 {
271 }
272
273 yf::Zoom::Frontend::~Frontend()
274 {
275 }
276
277 yf::Zoom::FrontendPtr yf::Zoom::Impl::get_frontend(mp::Package &package)
278 {
279     boost::mutex::scoped_lock lock(m_mutex);
280
281     std::map<mp::Session,yf::Zoom::FrontendPtr>::iterator it;
282     
283     while(true)
284     {
285         it = m_clients.find(package.session());
286         if (it == m_clients.end())
287             break;
288         
289         if (!it->second->m_in_use)
290         {
291             it->second->m_in_use = true;
292             return it->second;
293         }
294         m_cond_session_ready.wait(lock);
295     }
296     FrontendPtr f(new Frontend(this));
297     m_clients[package.session()] = f;
298     f->m_in_use = true;
299     return f;
300 }
301
302 void yf::Zoom::Impl::release_frontend(mp::Package &package)
303 {
304     boost::mutex::scoped_lock lock(m_mutex);
305     std::map<mp::Session,yf::Zoom::FrontendPtr>::iterator it;
306     
307     it = m_clients.find(package.session());
308     if (it != m_clients.end())
309     {
310         if (package.session().is_closed())
311         {
312             m_clients.erase(it);
313         }
314         else
315         {
316             it->second->m_in_use = false;
317         }
318         m_cond_session_ready.notify_all();
319     }
320 }
321
322 yf::Zoom::Impl::Impl() : element_transform("pz2") , element_raw("raw")
323 {
324     bibset = ccl_qual_mk();
325 }
326
327 yf::Zoom::Impl::~Impl()
328
329     ccl_qual_rm(&bibset);
330 }
331
332 yf::Zoom::SearchablePtr yf::Zoom::Impl::parse_torus_record(const xmlNode *ptr)
333 {
334     Zoom::SearchablePtr s(new Searchable(bibset));
335     
336     for (ptr = ptr->children; ptr; ptr = ptr->next)
337     {
338         if (ptr->type != XML_ELEMENT_NODE)
339             continue;
340         if (!strcmp((const char *) ptr->name, "layer"))
341             ptr = ptr->children;
342         else if (!strcmp((const char *) ptr->name,
343                          "authentication"))
344         {
345             s->authentication = mp::xml::get_text(ptr);
346         }
347         else if (!strcmp((const char *) ptr->name,
348                          "cfAuth"))
349         {
350             s->cfAuth = mp::xml::get_text(ptr);
351         } 
352         else if (!strcmp((const char *) ptr->name,
353                          "cfProxy"))
354         {
355             s->cfProxy = mp::xml::get_text(ptr);
356         }  
357         else if (!strcmp((const char *) ptr->name,
358                          "cfSubDb"))
359         {
360             s->cfSubDb = mp::xml::get_text(ptr);
361         }  
362         else if (!strcmp((const char *) ptr->name, "udb"))
363         {
364             s->udb = mp::xml::get_text(ptr);
365         }
366         else if (!strcmp((const char *) ptr->name, "zurl"))
367         {
368             s->target = mp::xml::get_text(ptr);
369         }
370         else if (!strcmp((const char *) ptr->name, "sru"))
371         {
372             s->sru = mp::xml::get_text(ptr);
373         }
374         else if (!strcmp((const char *) ptr->name,
375                          "queryEncoding"))
376         {
377             s->query_encoding = mp::xml::get_text(ptr);
378         }
379         else if (!strcmp((const char *) ptr->name,
380                          "piggyback"))
381         {
382             s->piggyback = mp::xml::get_bool(ptr, true);
383         }
384         else if (!strcmp((const char *) ptr->name,
385                          "requestSyntax"))
386         {
387             s->request_syntax = mp::xml::get_text(ptr);
388         }
389         else if (!strcmp((const char *) ptr->name,
390                          "elementSet"))
391         {
392             s->element_set = mp::xml::get_text(ptr);
393         }
394         else if (!strcmp((const char *) ptr->name,
395                          "recordEncoding"))
396         {
397             s->record_encoding = mp::xml::get_text(ptr);
398         }
399         else if (!strcmp((const char *) ptr->name,
400                          "transform"))
401         {
402             s->transform_xsl_fname = mp::xml::get_text(ptr);
403         }
404         else if (!strcmp((const char *) ptr->name,
405                          "urlRecipe"))
406         {
407             s->urlRecipe = mp::xml::get_text(ptr);
408         }
409         else if (!strcmp((const char *) ptr->name,
410                          "useTurboMarc"))
411         {
412             ; // useTurboMarc is ignored
413         }
414         else if (!strncmp((const char *) ptr->name,
415                           "cclmap_", 7))
416         {
417             std::string value = mp::xml::get_text(ptr);
418             ccl_qual_fitem(s->ccl_bibset, value.c_str(),
419                            (const char *) ptr->name + 7);
420         }
421     }
422     return s;
423 }
424
425 void yf::Zoom::Impl::configure_local_records(const xmlNode *ptr, bool test_only)
426 {
427     while (ptr && ptr->type != XML_ELEMENT_NODE)
428         ptr = ptr->next;
429     
430     if (ptr)
431     {
432         if (!strcmp((const char *) ptr->name, "records"))
433         {
434             for (ptr = ptr->children; ptr; ptr = ptr->next)
435             {
436                 if (ptr->type != XML_ELEMENT_NODE)
437                     continue;
438                 if (!strcmp((const char *) ptr->name, "record"))
439                 {
440                     SearchablePtr s = parse_torus_record(ptr);
441                     if (s)
442                     {
443                         std::string udb = s->udb;
444                         if (udb.length())
445                             s_map[s->udb] = s;
446                         else
447                         {
448                             throw mp::filter::FilterException
449                                 ("No udb for local torus record");
450                         }
451                     }
452                 }
453                 else
454                 {
455                     throw mp::filter::FilterException
456                         ("Bad element " 
457                          + std::string((const char *) ptr->name)
458                          + " in zoom filter inside element "
459                          "<torus><records>");
460                 }
461             }
462         }
463         else
464         {
465             throw mp::filter::FilterException
466                 ("Bad element " 
467                  + std::string((const char *) ptr->name)
468                  + " in zoom filter inside element <torus>");
469         }
470     }
471 }
472
473 void yf::Zoom::Impl::configure(const xmlNode *ptr, bool test_only)
474 {
475     for (ptr = ptr->children; ptr; ptr = ptr->next)
476     {
477         if (ptr->type != XML_ELEMENT_NODE)
478             continue;
479         else if (!strcmp((const char *) ptr->name, "torus"))
480         {
481             const struct _xmlAttr *attr;
482             for (attr = ptr->properties; attr; attr = attr->next)
483             {
484                 if (!strcmp((const char *) attr->name, "url"))
485                     torus_url = mp::xml::get_text(attr->children);
486                 else if (!strcmp((const char *) attr->name, "xsldir"))
487                     xsldir = mp::xml::get_text(attr->children);
488                 else if (!strcmp((const char *) attr->name, "element_transform"))
489                     element_transform = mp::xml::get_text(attr->children);
490                 else if (!strcmp((const char *) attr->name, "element_raw"))
491                     element_raw = mp::xml::get_text(attr->children);
492                 else
493                     throw mp::filter::FilterException(
494                         "Bad attribute " + std::string((const char *)
495                                                        attr->name));
496             }
497             configure_local_records(ptr->children, test_only);
498         }
499         else if (!strcmp((const char *) ptr->name, "cclmap"))
500         {
501             const char *addinfo = 0;
502             ccl_xml_config(bibset, ptr, &addinfo);
503         }
504         else if (!strcmp((const char *) ptr->name, "fieldmap"))
505         {
506             const struct _xmlAttr *attr;
507             std::string ccl_field;
508             std::string cql_field;
509             for (attr = ptr->properties; attr; attr = attr->next)
510             {
511                 if (!strcmp((const char *) attr->name, "ccl"))
512                     ccl_field = mp::xml::get_text(attr->children);
513                 else if (!strcmp((const char *) attr->name, "cql"))
514                     cql_field = mp::xml::get_text(attr->children);
515                 else
516                     throw mp::filter::FilterException(
517                         "Bad attribute " + std::string((const char *)
518                                                        attr->name));
519             }
520             if (cql_field.length())
521                 fieldmap[cql_field] = ccl_field;
522         }
523         else
524         {
525             throw mp::filter::FilterException
526                 ("Bad element " 
527                  + std::string((const char *) ptr->name)
528                  + " in zoom filter");
529         }
530     }
531 }
532
533 yf::Zoom::BackendPtr yf::Zoom::Frontend::get_backend_from_databases(
534     std::string &database, int *error, char **addinfo, ODR odr)
535 {
536     std::list<BackendPtr>::const_iterator map_it;
537     if (m_backend && m_backend->m_frontend_database == database)
538         return m_backend;
539
540     std::string db_args;
541     std::string torus_db;
542     size_t db_arg_pos = database.find(',');
543     if (db_arg_pos != std::string::npos)
544     {
545         torus_db = database.substr(0, db_arg_pos);
546         db_args = database.substr(db_arg_pos + 1);
547     }
548     else
549         torus_db = database;
550  
551     SearchablePtr sptr;
552
553     std::map<std::string,SearchablePtr>::iterator it;
554     it = m_p->s_map.find(torus_db);
555     if (it != m_p->s_map.end())
556         sptr = it->second;
557     else
558     {
559         xmlDoc *doc = mp::get_searchable(m_p->torus_url, torus_db);
560         if (!doc)
561         {
562             *error = YAZ_BIB1_DATABASE_DOES_NOT_EXIST;
563             *addinfo = odr_strdup(odr, database.c_str());
564             BackendPtr b;
565             return b;
566         }
567         const xmlNode *ptr = xmlDocGetRootElement(doc);
568         if (ptr)
569         {   // presumably ptr is a records element node
570             // parse first record in document
571             for (ptr = ptr->children; ptr; ptr = ptr->next)
572             {
573                 if (ptr->type == XML_ELEMENT_NODE
574                     && !strcmp((const char *) ptr->name, "record"))
575                 {
576                     sptr = m_p->parse_torus_record(ptr);
577                     break;
578                 }
579             }
580         }
581         xmlFreeDoc(doc);
582     }
583
584     if (!sptr)
585     {
586         *error = YAZ_BIB1_DATABASE_DOES_NOT_EXIST;
587         *addinfo = odr_strdup(odr, database.c_str());
588         BackendPtr b;
589         return b;
590     }
591         
592     xsltStylesheetPtr xsp = 0;
593     if (sptr->transform_xsl_fname.length())
594     {
595         std::string fname;
596
597         if (m_p->xsldir.length()) 
598             fname = m_p->xsldir + "/" + sptr->transform_xsl_fname;
599         else
600             fname = sptr->transform_xsl_fname;
601         xmlDoc *xsp_doc = xmlParseFile(fname.c_str());
602         if (!xsp_doc)
603         {
604             *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
605             *addinfo = (char *) odr_malloc(odr, 40 + strlen(fname.c_str()));
606             sprintf(*addinfo, "xmlParseFile failed. File %s", fname.c_str());
607             BackendPtr b;
608             return b;
609         }
610         xsp = xsltParseStylesheetDoc(xsp_doc);
611         if (!xsp)
612         {
613             *error = YAZ_BIB1_DATABASE_DOES_NOT_EXIST;
614             *addinfo = odr_strdup(odr, "xsltParseStylesheetDoc failed");
615             BackendPtr b;
616             xmlFreeDoc(xsp_doc);
617             return b;
618         }
619     }
620
621     m_backend.reset();
622
623     BackendPtr b(new Backend(sptr));
624
625     b->xsp = xsp;
626     b->m_frontend_database = database;
627     std::string authentication = sptr->authentication;
628         
629     b->set_option("timeout", "40");
630
631     if (sptr->query_encoding.length())
632         b->set_option("rpnCharset", sptr->query_encoding.c_str());
633
634     if (sptr->cfAuth.length())
635     {
636         // A CF target
637         b->set_option("user", sptr->cfAuth.c_str());
638         if (authentication.length() && db_args.length() == 0)
639         {
640             // no database (auth) args specified already.. and the
641             // Torus authentication has it.. Generate the args that CF
642             // understands..
643             size_t found = authentication.find('/');
644             if (found != std::string::npos)
645             {
646                 db_args += "user=" + mp::util::uri_encode(authentication.substr(0, found))
647                     + "&password=" + mp::util::uri_encode(authentication.substr(found+1));
648             }
649             else
650                 db_args += "user=" + mp::util::uri_encode(authentication);
651         }
652     }
653     else
654     {
655         // A non-CF target
656         if (db_args.length())
657         {
658             // user has specified backend authentication
659             const char *param_user = 0;
660             const char *param_password = 0;
661             char **names;
662             char **values;
663             int i;
664             int no_parms = yaz_uri_to_array(db_args.c_str(),
665                                             odr, &names, &values);
666             for (i = 0; i < no_parms; i++)
667             {
668                 const char *name = names[i];
669                 const char *value = values[i];
670                 if (!strcmp(name, "user"))
671                     param_user = value;
672                 else if (!strcmp(name, "password"))
673                     param_password = value;
674                 else
675                 {
676                     BackendPtr notfound;
677                     char *msg = (char*) odr_malloc(odr, strlen(name) + 30);
678                     *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
679                     sprintf(msg, "Bad database argument: %s", name);
680                     *addinfo = msg;
681                     return notfound;
682                 }
683             }
684             if (param_user && param_password)
685             {
686                 char *auth = (char*) odr_malloc(
687                     odr, strlen(param_user) + strlen(param_password) + 2);
688                 strcpy(auth, param_user);
689                 strcat(auth, "/");
690                 strcat(auth, param_password);
691                 b->set_option("user", auth);
692             }
693             db_args.clear(); // no arguments to be passed (non-CF)
694         }
695         else
696         {
697             // use authentication from Torus, if given
698             if (authentication.length())
699                 b->set_option("user", authentication.c_str());
700         }
701     }
702     if (sptr->cfProxy.length())
703     {
704         if (db_args.length())
705             db_args += "&";
706         db_args += "proxy=" + mp::util::uri_encode(sptr->cfProxy);
707     }
708     if (sptr->cfSubDb.length())
709     {
710         if (db_args.length())
711             db_args += "&";
712         db_args += "subdatabase=" + mp::util::uri_encode(sptr->cfSubDb);
713     }
714
715     std::string url;
716     if (sptr->sru.length())
717     {
718         url = "http://" + sptr->target;
719         b->set_option("sru", sptr->sru.c_str());
720     }
721     else
722     {
723         url = sptr->target;
724     }
725     if (db_args.length())
726         url += "," + db_args;
727     yaz_log(YLOG_LOG, "url=%s", url.c_str());
728     const char *addinfo_c = 0;
729     b->connect(url, error, &addinfo_c);
730     if (addinfo_c)
731         *addinfo = odr_strdup(odr, addinfo_c);
732     if (*error == 0)
733     {
734         m_backend = b;
735     }
736     return b;
737 }
738
739 Z_Records *yf::Zoom::Frontend::get_records(Odr_int start,
740                                            Odr_int number_to_present,
741                                            int *error,
742                                            const char **addinfo,
743                                            Odr_int *number_of_records_returned,
744                                            ODR odr,
745                                            BackendPtr b,
746                                            Odr_oid *preferredRecordSyntax,
747                                            const char *element_set_name)
748 {
749     *number_of_records_returned = 0;
750     Z_Records *records = 0;
751     bool enable_pz2_retrieval = false; // whether target profile is used
752     bool enable_pz2_transform = false; // whether XSLT is used as well
753     bool assume_marc8_charset = false;
754
755     if (start < 0 || number_to_present <= 0)
756         return records;
757     
758     if (number_to_present > 10000)
759         number_to_present = 10000;
760     
761     ZOOM_record *recs = (ZOOM_record *)
762         odr_malloc(odr, number_to_present * sizeof(*recs));
763
764     char oid_name_str[OID_STR_MAX];
765     const char *syntax_name = 0;
766     
767     if (preferredRecordSyntax &&
768         !oid_oidcmp(preferredRecordSyntax, yaz_oid_recsyn_xml)
769         && element_set_name)
770     {
771         if (!strcmp(element_set_name, m_p->element_transform.c_str()))
772         {
773             enable_pz2_retrieval = true;
774             enable_pz2_transform = true;
775         }
776         else if (!strcmp(element_set_name, m_p->element_raw.c_str()))
777         {
778             enable_pz2_retrieval = true;
779         }
780     }
781     
782     if (enable_pz2_retrieval)
783     {
784         if (b->sptr->request_syntax.length())
785         {
786             syntax_name = b->sptr->request_syntax.c_str();
787             const Odr_oid *syntax_oid = 
788                 yaz_string_to_oid(yaz_oid_std(), CLASS_RECSYN, syntax_name);
789             if (!oid_oidcmp(syntax_oid, yaz_oid_recsyn_usmarc)
790                 || !oid_oidcmp(syntax_oid, yaz_oid_recsyn_opac))
791                 assume_marc8_charset = true;
792         }
793     }
794     else if (preferredRecordSyntax)
795         syntax_name =
796             yaz_oid_to_string_buf(preferredRecordSyntax, 0, oid_name_str);
797
798     b->set_option("preferredRecordSyntax", syntax_name);
799
800     if (enable_pz2_retrieval)
801     {
802         element_set_name = 0;
803         if (b->sptr->element_set.length())
804             element_set_name = b->sptr->element_set.c_str();
805     }
806
807     b->set_option("elementSetName", element_set_name);
808
809     b->present(start, number_to_present, recs, error, addinfo);
810
811     Odr_int i = 0;
812     if (!*error)
813     {
814         for (i = 0; i < number_to_present; i++)
815             if (!recs[i])
816                 break;
817     }
818     if (i > 0)
819     {  // only return records if no error and at least one record
820         char *odr_database = odr_strdup(odr,
821                                         b->m_frontend_database.c_str());
822         Z_NamePlusRecordList *npl = (Z_NamePlusRecordList *)
823             odr_malloc(odr, sizeof(*npl));
824         *number_of_records_returned = i;
825         npl->num_records = i;
826         npl->records = (Z_NamePlusRecord **)
827             odr_malloc(odr, i * sizeof(*npl->records));
828         for (i = 0; i < number_to_present; i++)
829         {
830             Z_NamePlusRecord *npr = 0;
831             const char *addinfo;
832             int sur_error = ZOOM_record_error(recs[i], 0 /* msg */,
833                                               &addinfo, 0 /* diagset */);
834                 
835             if (sur_error)
836             {
837                 npr = zget_surrogateDiagRec(odr, odr_database, sur_error,
838                                             addinfo);
839             }
840             else if (enable_pz2_retrieval)
841             {
842                 char rec_type_str[100];
843                 const char *record_encoding = 0;
844
845                 if (b->sptr->record_encoding.length())
846                     record_encoding = b->sptr->record_encoding.c_str();
847                 else if (assume_marc8_charset)
848                     record_encoding = "marc8";
849
850                 strcpy(rec_type_str, b->sptr->use_turbomarc ? "txml" : "xml");
851                 if (record_encoding)
852                 {
853                     strcat(rec_type_str, "; charset=");
854                     strcat(rec_type_str, record_encoding);
855                 }
856                 
857                 int rec_len;
858                 xmlChar *xmlrec_buf = 0;
859                 const char *rec_buf = ZOOM_record_get(recs[i], rec_type_str,
860                                                       &rec_len);
861                 if (rec_buf && b->xsp && enable_pz2_transform)
862                 {
863                     xmlDoc *rec_doc = xmlParseMemory(rec_buf, rec_len);
864                     if (rec_doc)
865                     { 
866                         xmlDoc *rec_res;
867                         rec_res = xsltApplyStylesheet(b->xsp, rec_doc, 0);
868
869                         if (rec_res)
870                             xsltSaveResultToString(&xmlrec_buf, &rec_len,
871                                                    rec_res, b->xsp);
872                         rec_buf = (const char *) xmlrec_buf;
873                         xmlFreeDoc(rec_doc);
874                         xmlFreeDoc(rec_res);
875                     }
876                 }
877
878                 if (rec_buf)
879                 {
880                     xmlDoc *doc = xmlParseMemory(rec_buf, rec_len);
881                     std::string res = 
882                         mp::xml::url_recipe_handle(doc, b->sptr->urlRecipe);
883                     if (res.length())
884                     {
885                         xmlNode *ptr = xmlDocGetRootElement(doc);
886                         while (ptr && ptr->type != XML_ELEMENT_NODE)
887                             ptr = ptr->next;
888                         xmlNode *c = 
889                             xmlNewChild(ptr, 0, BAD_CAST "generated-url", 0);
890                         xmlNode * t = xmlNewText(BAD_CAST res.c_str());
891                         xmlAddChild(c, t);
892
893                         if (xmlrec_buf)
894                             xmlFree(xmlrec_buf);
895
896                         xmlDocDumpMemory(doc, &xmlrec_buf, &rec_len);
897                         rec_buf = (const char *) xmlrec_buf;
898                     }
899                     xmlFreeDoc(doc);
900                 }
901                 if (rec_buf)
902                 {
903                     npr = (Z_NamePlusRecord *) odr_malloc(odr, sizeof(*npr));
904                     npr->databaseName = odr_database;
905                     npr->which = Z_NamePlusRecord_databaseRecord;
906                     npr->u.databaseRecord =
907                         z_ext_record_xml(odr, rec_buf, rec_len);
908                 }
909                 else
910                 {
911                     npr = zget_surrogateDiagRec(
912                         odr, odr_database, 
913                         YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS,
914                         rec_type_str);
915                 }
916                 if (xmlrec_buf)
917                     xmlFree(xmlrec_buf);
918             }
919             else
920             {
921                 Z_External *ext =
922                     (Z_External *) ZOOM_record_get(recs[i], "ext", 0);
923                 if (ext)
924                 {
925                     npr = (Z_NamePlusRecord *) odr_malloc(odr, sizeof(*npr));
926                     npr->databaseName = odr_database;
927                     npr->which = Z_NamePlusRecord_databaseRecord;
928                     npr->u.databaseRecord = ext;
929                 }
930                 else
931                 {
932                     npr = zget_surrogateDiagRec(
933                         odr, odr_database, 
934                         YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS,
935                         "ZOOM_record, type ext");
936                 }
937             }
938             npl->records[i] = npr;
939         }
940         records = (Z_Records*) odr_malloc(odr, sizeof(*records));
941         records->which = Z_Records_DBOSD;
942         records->u.databaseOrSurDiagnostics = npl;
943     }
944     return records;
945 }
946     
947 struct cql_node *yf::Zoom::Impl::convert_cql_fields(struct cql_node *cn,
948                                                     ODR odr)
949 {
950     struct cql_node *r = 0;
951     if (!cn)
952         return 0;
953     switch (cn->which)
954     {
955     case CQL_NODE_ST:
956         if (cn->u.st.index)
957         {
958             std::map<std::string,std::string>::const_iterator it;
959             it = fieldmap.find(cn->u.st.index);
960             if (it == fieldmap.end())
961                 return cn;
962             if (it->second.length())
963                 cn->u.st.index = odr_strdup(odr, it->second.c_str());
964             else
965                 cn->u.st.index = 0;
966         }
967         break;
968     case CQL_NODE_BOOL:
969         r = convert_cql_fields(cn->u.boolean.left, odr);
970         if (!r)
971             r = convert_cql_fields(cn->u.boolean.right, odr);
972         break;
973     case CQL_NODE_SORT:
974         r = convert_cql_fields(cn->u.sort.search, odr);
975         break;
976     }
977     return r;
978 }
979
980 void yf::Zoom::Frontend::handle_search(mp::Package &package)
981 {
982     Z_GDU *gdu = package.request().get();
983     Z_APDU *apdu_req = gdu->u.z3950;
984     Z_APDU *apdu_res = 0;
985     mp::odr odr;
986     Z_SearchRequest *sr = apdu_req->u.searchRequest;
987     if (sr->num_databaseNames != 1)
988     {
989         apdu_res = odr.create_searchResponse(
990             apdu_req, YAZ_BIB1_TOO_MANY_DATABASES_SPECIFIED, 0);
991         package.response() = apdu_res;
992         return;
993     }
994
995     int error = 0;
996     char *addinfo_s = 0;
997     std::string db(sr->databaseNames[0]);
998     BackendPtr b = get_backend_from_databases(db, &error, &addinfo_s, odr);
999     if (error)
1000     {
1001         apdu_res = 
1002             odr.create_searchResponse(
1003                 apdu_req, error, addinfo_s);
1004         package.response() = apdu_res;
1005         return;
1006     }
1007
1008     const char *addinfo_c = 0;
1009     b->set_option("setname", "default");
1010
1011     Odr_int hits = 0;
1012     Z_Query *query = sr->query;
1013     WRBUF ccl_wrbuf = 0;
1014     WRBUF pqf_wrbuf = 0;
1015
1016     if (query->which == Z_Query_type_1 || query->which == Z_Query_type_101)
1017     {
1018         // RPN
1019         pqf_wrbuf = wrbuf_alloc();
1020         yaz_rpnquery_to_wrbuf(pqf_wrbuf, query->u.type_1);
1021     }
1022     else if (query->which == Z_Query_type_2)
1023     {
1024         // CCL
1025         ccl_wrbuf = wrbuf_alloc();
1026         wrbuf_write(ccl_wrbuf, (const char *) query->u.type_2->buf,
1027                     query->u.type_2->len);
1028     }
1029     else if (query->which == Z_Query_type_104 &&
1030              query->u.type_104->which == Z_External_CQL)
1031     {
1032         // CQL
1033         const char *cql = query->u.type_104->u.cql;
1034         CQL_parser cp = cql_parser_create();
1035         int r = cql_parser_string(cp, cql);
1036         if (r)
1037         {
1038             cql_parser_destroy(cp);
1039             apdu_res = 
1040                 odr.create_searchResponse(apdu_req, 
1041                                           YAZ_BIB1_MALFORMED_QUERY,
1042                                           "CQL syntax error");
1043             package.response() = apdu_res;
1044             return;
1045         }
1046         struct cql_node *cn = cql_parser_result(cp);
1047         struct cql_node *cn_error = m_p->convert_cql_fields(cn, odr);
1048         if (cn_error)
1049         {
1050             // hopefully we are getting a ptr to a index+relation+term node
1051             addinfo_c = 0;
1052             if (cn_error->which == CQL_NODE_ST)
1053                 addinfo_c = cn_error->u.st.index;
1054
1055             apdu_res = 
1056                 odr.create_searchResponse(apdu_req, 
1057                                           YAZ_BIB1_UNSUPP_USE_ATTRIBUTE,
1058                                           addinfo_c);
1059             package.response() = apdu_res;
1060             return;
1061         }
1062         char ccl_buf[1024];
1063
1064         r = cql_to_ccl_buf(cn, ccl_buf, sizeof(ccl_buf));
1065         if (r == 0)
1066         {
1067             ccl_wrbuf = wrbuf_alloc();
1068             wrbuf_puts(ccl_wrbuf, ccl_buf);
1069         }
1070         cql_parser_destroy(cp);
1071         if (r)
1072         {
1073             apdu_res = 
1074                 odr.create_searchResponse(apdu_req, 
1075                                           YAZ_BIB1_MALFORMED_QUERY,
1076                                           "CQL to CCL conversion error");
1077             package.response() = apdu_res;
1078             return;
1079         }
1080     }
1081     else
1082     {
1083         apdu_res = 
1084             odr.create_searchResponse(apdu_req, YAZ_BIB1_QUERY_TYPE_UNSUPP, 0);
1085         package.response() = apdu_res;
1086         return;
1087     }
1088
1089     if (ccl_wrbuf)
1090     {
1091         // CCL to PQF
1092         assert(pqf_wrbuf == 0);
1093         int cerror, cpos;
1094         struct ccl_rpn_node *cn;
1095         yaz_log(YLOG_LOG, "CCL: %s", wrbuf_cstr(ccl_wrbuf));
1096         cn = ccl_find_str(b->sptr->ccl_bibset, wrbuf_cstr(ccl_wrbuf),
1097                           &cerror, &cpos);
1098         wrbuf_destroy(ccl_wrbuf);
1099         if (!cn)
1100         {
1101             char *addinfo = odr_strdup(odr, ccl_err_msg(cerror));
1102             int z3950_diag = YAZ_BIB1_MALFORMED_QUERY;
1103
1104             switch (cerror)
1105             {
1106             case CCL_ERR_UNKNOWN_QUAL:
1107                 z3950_diag = YAZ_BIB1_UNSUPP_USE_ATTRIBUTE;
1108                 break;
1109             case CCL_ERR_TRUNC_NOT_LEFT: 
1110             case CCL_ERR_TRUNC_NOT_RIGHT:
1111             case CCL_ERR_TRUNC_NOT_BOTH:
1112                 z3950_diag = YAZ_BIB1_UNSUPP_TRUNCATION_ATTRIBUTE;
1113                 break;
1114             }
1115             apdu_res = 
1116                 odr.create_searchResponse(apdu_req, z3950_diag, addinfo);
1117             package.response() = apdu_res;
1118             return;
1119         }
1120         pqf_wrbuf = wrbuf_alloc();
1121         ccl_pquery(pqf_wrbuf, cn);
1122         ccl_rpn_delete(cn);
1123     }
1124     
1125     assert(pqf_wrbuf);
1126     if (b->get_option("sru"))
1127     {
1128         int status = 0;
1129         Z_RPNQuery *zquery;
1130         zquery = p_query_rpn(odr, wrbuf_cstr(pqf_wrbuf));
1131         WRBUF wrb = wrbuf_alloc();
1132             
1133         if (!strcmp(b->get_option("sru"), "solr"))
1134         {
1135             solr_transform_t cqlt = solr_transform_create();
1136             
1137             status = solr_transform_rpn2solr_wrbuf(cqlt, wrb, zquery);
1138             
1139             solr_transform_close(cqlt);
1140         }
1141         else
1142         {
1143             cql_transform_t cqlt = cql_transform_create();
1144             
1145             status = cql_transform_rpn2cql_wrbuf(cqlt, wrb, zquery);
1146             
1147             cql_transform_close(cqlt);
1148         }
1149         if (status == 0)
1150         {
1151             yaz_log(YLOG_LOG, "search CQL: %s", wrbuf_cstr(wrb));
1152             b->search_cql(wrbuf_cstr(wrb), &hits, &error, &addinfo_c);
1153         }
1154         
1155         wrbuf_destroy(wrb);
1156         wrbuf_destroy(pqf_wrbuf);
1157         if (status)
1158         {
1159             apdu_res = 
1160                 odr.create_searchResponse(apdu_req, YAZ_BIB1_MALFORMED_QUERY,
1161                                           "can not convert from RPN to CQL/SOLR");
1162             package.response() = apdu_res;
1163             return;
1164         }
1165     }
1166     else
1167     {
1168         yaz_log(YLOG_LOG, "search PQF: %s", wrbuf_cstr(pqf_wrbuf));
1169         b->search_pqf(wrbuf_cstr(pqf_wrbuf), &hits, &error, &addinfo_c);
1170         wrbuf_destroy(pqf_wrbuf);
1171     }
1172     
1173     
1174     const char *element_set_name = 0;
1175     Odr_int number_to_present = 0;
1176     if (!error)
1177         mp::util::piggyback_sr(sr, hits, number_to_present, &element_set_name);
1178     
1179     Odr_int number_of_records_returned = 0;
1180     Z_Records *records = get_records(
1181         0, number_to_present, &error, &addinfo_c,
1182         &number_of_records_returned, odr, b, sr->preferredRecordSyntax,
1183         element_set_name);
1184     apdu_res = odr.create_searchResponse(apdu_req, error, addinfo_c);
1185     if (records)
1186     {
1187         apdu_res->u.searchResponse->records = records;
1188         apdu_res->u.searchResponse->numberOfRecordsReturned =
1189             odr_intdup(odr, number_of_records_returned);
1190     }
1191     apdu_res->u.searchResponse->resultCount = odr_intdup(odr, hits);
1192     package.response() = apdu_res;
1193 }
1194
1195 void yf::Zoom::Frontend::handle_present(mp::Package &package)
1196 {
1197     Z_GDU *gdu = package.request().get();
1198     Z_APDU *apdu_req = gdu->u.z3950;
1199     Z_APDU *apdu_res = 0;
1200     Z_PresentRequest *pr = apdu_req->u.presentRequest;
1201
1202     mp::odr odr;
1203     if (!m_backend)
1204     {
1205         package.response() = odr.create_presentResponse(
1206             apdu_req, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST, 0);
1207         return;
1208     }
1209     const char *element_set_name = 0;
1210     Z_RecordComposition *comp = pr->recordComposition;
1211     if (comp && comp->which != Z_RecordComp_simple)
1212     {
1213         package.response() = odr.create_presentResponse(
1214             apdu_req, 
1215             YAZ_BIB1_PRESENT_COMP_SPEC_PARAMETER_UNSUPP, 0);
1216         return;
1217     }
1218     if (comp && comp->u.simple->which == Z_ElementSetNames_generic)
1219         element_set_name = comp->u.simple->u.generic;
1220     Odr_int number_of_records_returned = 0;
1221     int error = 0;
1222     const char *addinfo = 0;
1223     Z_Records *records = get_records(
1224         *pr->resultSetStartPoint - 1, *pr->numberOfRecordsRequested,
1225         &error, &addinfo, &number_of_records_returned, odr, m_backend,
1226         pr->preferredRecordSyntax, element_set_name);
1227
1228     apdu_res = odr.create_presentResponse(apdu_req, error, addinfo);
1229     if (records)
1230     {
1231         apdu_res->u.presentResponse->records = records;
1232         apdu_res->u.presentResponse->numberOfRecordsReturned =
1233             odr_intdup(odr, number_of_records_returned);
1234     }
1235     package.response() = apdu_res;
1236 }
1237
1238 void yf::Zoom::Frontend::handle_package(mp::Package &package)
1239 {
1240     Z_GDU *gdu = package.request().get();
1241     if (!gdu)
1242         ;
1243     else if (gdu->which == Z_GDU_Z3950)
1244     {
1245         Z_APDU *apdu_req = gdu->u.z3950;
1246         if (apdu_req->which == Z_APDU_initRequest)
1247         {
1248             mp::odr odr;
1249             package.response() = odr.create_close(
1250                 apdu_req,
1251                 Z_Close_protocolError,
1252                 "double init");
1253         }
1254         else if (apdu_req->which == Z_APDU_searchRequest)
1255         {
1256             handle_search(package);
1257         }
1258         else if (apdu_req->which == Z_APDU_presentRequest)
1259         {
1260             handle_present(package);
1261         }
1262         else
1263         {
1264             mp::odr odr;
1265             package.response() = odr.create_close(
1266                 apdu_req,
1267                 Z_Close_protocolError,
1268                 "zoom filter cannot handle this APDU");
1269             package.session().close();
1270         }
1271     }
1272     else
1273     {
1274         package.session().close();
1275     }
1276 }
1277
1278 void yf::Zoom::Impl::process(mp::Package &package)
1279 {
1280     FrontendPtr f = get_frontend(package);
1281     Z_GDU *gdu = package.request().get();
1282
1283     if (f->m_is_virtual)
1284     {
1285         f->handle_package(package);
1286     }
1287     else if (gdu && gdu->which == Z_GDU_Z3950 && gdu->u.z3950->which ==
1288              Z_APDU_initRequest)
1289     {
1290         Z_InitRequest *req = gdu->u.z3950->u.initRequest;
1291         f->m_init_gdu = gdu;
1292         
1293         mp::odr odr;
1294         Z_APDU *apdu = odr.create_initResponse(gdu->u.z3950, 0, 0);
1295         Z_InitResponse *resp = apdu->u.initResponse;
1296         
1297         int i;
1298         static const int masks[] = {
1299             Z_Options_search,
1300             Z_Options_present,
1301             -1 
1302         };
1303         for (i = 0; masks[i] != -1; i++)
1304             if (ODR_MASK_GET(req->options, masks[i]))
1305                 ODR_MASK_SET(resp->options, masks[i]);
1306         
1307         static const int versions[] = {
1308             Z_ProtocolVersion_1,
1309             Z_ProtocolVersion_2,
1310             Z_ProtocolVersion_3,
1311             -1
1312         };
1313         for (i = 0; versions[i] != -1; i++)
1314             if (ODR_MASK_GET(req->protocolVersion, versions[i]))
1315                 ODR_MASK_SET(resp->protocolVersion, versions[i]);
1316             else
1317                 break;
1318         
1319         *resp->preferredMessageSize = *req->preferredMessageSize;
1320         *resp->maximumRecordSize = *req->maximumRecordSize;
1321         
1322         package.response() = apdu;
1323         f->m_is_virtual = true;
1324     }
1325     else
1326         package.move();
1327
1328     release_frontend(package);
1329 }
1330
1331
1332 static mp::filter::Base* filter_creator()
1333 {
1334     return new mp::filter::Zoom;
1335 }
1336
1337 extern "C" {
1338     struct metaproxy_1_filter_struct metaproxy_1_filter_zoom = {
1339         0,
1340         "zoom",
1341         filter_creator
1342     };
1343 }
1344
1345
1346 /*
1347  * Local variables:
1348  * c-basic-offset: 4
1349  * c-file-style: "Stroustrup"
1350  * indent-tabs-mode: nil
1351  * End:
1352  * vim: shiftwidth=4 tabstop=8 expandtab
1353  */
1354