Beginnings of url recipe handling
[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                 const char *rec_buf = ZOOM_record_get(recs[i], rec_type_str,
859                                                       &rec_len);
860                 if (rec_buf && b->xsp && enable_pz2_transform)
861                 {
862                     xmlDoc *rec_doc = xmlParseMemory(rec_buf, rec_len);
863                     if (rec_doc)
864                     { 
865                         xmlDoc *rec_res;
866                         rec_res = xsltApplyStylesheet(b->xsp, rec_doc, 0);
867
868                         if (rec_res)
869                             xsltSaveResultToString((xmlChar **) &rec_buf, &rec_len,
870                                                    rec_res, b->xsp);
871                     }
872                 }
873
874                 if (rec_buf)
875                 {
876                     xmlDoc *doc = xmlParseMemory(rec_buf, rec_len);
877                     mp::xml::url_recipe_handle(doc, b->sptr->urlRecipe);
878                     xmlFreeDoc(doc);
879                 }
880                 if (rec_buf)
881                 {
882                     npr = (Z_NamePlusRecord *) odr_malloc(odr, sizeof(*npr));
883                     npr->databaseName = odr_database;
884                     npr->which = Z_NamePlusRecord_databaseRecord;
885                     npr->u.databaseRecord =
886                         z_ext_record_xml(odr, rec_buf, rec_len);
887                 }
888                 else
889                 {
890                     npr = zget_surrogateDiagRec(
891                         odr, odr_database, 
892                         YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS,
893                         rec_type_str);
894                 }
895             }
896             else
897             {
898                 Z_External *ext =
899                     (Z_External *) ZOOM_record_get(recs[i], "ext", 0);
900                 if (ext)
901                 {
902                     npr = (Z_NamePlusRecord *) odr_malloc(odr, sizeof(*npr));
903                     npr->databaseName = odr_database;
904                     npr->which = Z_NamePlusRecord_databaseRecord;
905                     npr->u.databaseRecord = ext;
906                 }
907                 else
908                 {
909                     npr = zget_surrogateDiagRec(
910                         odr, odr_database, 
911                         YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS,
912                         "ZOOM_record, type ext");
913                 }
914             }
915             npl->records[i] = npr;
916         }
917         records = (Z_Records*) odr_malloc(odr, sizeof(*records));
918         records->which = Z_Records_DBOSD;
919         records->u.databaseOrSurDiagnostics = npl;
920     }
921     return records;
922 }
923     
924 struct cql_node *yf::Zoom::Impl::convert_cql_fields(struct cql_node *cn,
925                                                     ODR odr)
926 {
927     struct cql_node *r = 0;
928     if (!cn)
929         return 0;
930     switch (cn->which)
931     {
932     case CQL_NODE_ST:
933         if (cn->u.st.index)
934         {
935             std::map<std::string,std::string>::const_iterator it;
936             it = fieldmap.find(cn->u.st.index);
937             if (it == fieldmap.end())
938                 return cn;
939             if (it->second.length())
940                 cn->u.st.index = odr_strdup(odr, it->second.c_str());
941             else
942                 cn->u.st.index = 0;
943         }
944         break;
945     case CQL_NODE_BOOL:
946         r = convert_cql_fields(cn->u.boolean.left, odr);
947         if (!r)
948             r = convert_cql_fields(cn->u.boolean.right, odr);
949         break;
950     case CQL_NODE_SORT:
951         r = convert_cql_fields(cn->u.sort.search, odr);
952         break;
953     }
954     return r;
955 }
956
957 void yf::Zoom::Frontend::handle_search(mp::Package &package)
958 {
959     Z_GDU *gdu = package.request().get();
960     Z_APDU *apdu_req = gdu->u.z3950;
961     Z_APDU *apdu_res = 0;
962     mp::odr odr;
963     Z_SearchRequest *sr = apdu_req->u.searchRequest;
964     if (sr->num_databaseNames != 1)
965     {
966         apdu_res = odr.create_searchResponse(
967             apdu_req, YAZ_BIB1_TOO_MANY_DATABASES_SPECIFIED, 0);
968         package.response() = apdu_res;
969         return;
970     }
971
972     int error = 0;
973     char *addinfo_s = 0;
974     std::string db(sr->databaseNames[0]);
975     BackendPtr b = get_backend_from_databases(db, &error, &addinfo_s, odr);
976     if (error)
977     {
978         apdu_res = 
979             odr.create_searchResponse(
980                 apdu_req, error, addinfo_s);
981         package.response() = apdu_res;
982         return;
983     }
984
985     const char *addinfo_c = 0;
986     b->set_option("setname", "default");
987
988     Odr_int hits = 0;
989     Z_Query *query = sr->query;
990     WRBUF ccl_wrbuf = 0;
991     WRBUF pqf_wrbuf = 0;
992
993     if (query->which == Z_Query_type_1 || query->which == Z_Query_type_101)
994     {
995         // RPN
996         pqf_wrbuf = wrbuf_alloc();
997         yaz_rpnquery_to_wrbuf(pqf_wrbuf, query->u.type_1);
998     }
999     else if (query->which == Z_Query_type_2)
1000     {
1001         // CCL
1002         ccl_wrbuf = wrbuf_alloc();
1003         wrbuf_write(ccl_wrbuf, (const char *) query->u.type_2->buf,
1004                     query->u.type_2->len);
1005     }
1006     else if (query->which == Z_Query_type_104 &&
1007              query->u.type_104->which == Z_External_CQL)
1008     {
1009         // CQL
1010         const char *cql = query->u.type_104->u.cql;
1011         CQL_parser cp = cql_parser_create();
1012         int r = cql_parser_string(cp, cql);
1013         if (r)
1014         {
1015             cql_parser_destroy(cp);
1016             apdu_res = 
1017                 odr.create_searchResponse(apdu_req, 
1018                                           YAZ_BIB1_MALFORMED_QUERY,
1019                                           "CQL syntax error");
1020             package.response() = apdu_res;
1021             return;
1022         }
1023         struct cql_node *cn = cql_parser_result(cp);
1024         struct cql_node *cn_error = m_p->convert_cql_fields(cn, odr);
1025         if (cn_error)
1026         {
1027             // hopefully we are getting a ptr to a index+relation+term node
1028             addinfo_c = 0;
1029             if (cn_error->which == CQL_NODE_ST)
1030                 addinfo_c = cn_error->u.st.index;
1031
1032             apdu_res = 
1033                 odr.create_searchResponse(apdu_req, 
1034                                           YAZ_BIB1_UNSUPP_USE_ATTRIBUTE,
1035                                           addinfo_c);
1036             package.response() = apdu_res;
1037             return;
1038         }
1039         char ccl_buf[1024];
1040
1041         r = cql_to_ccl_buf(cn, ccl_buf, sizeof(ccl_buf));
1042         if (r == 0)
1043         {
1044             ccl_wrbuf = wrbuf_alloc();
1045             wrbuf_puts(ccl_wrbuf, ccl_buf);
1046         }
1047         cql_parser_destroy(cp);
1048         if (r)
1049         {
1050             apdu_res = 
1051                 odr.create_searchResponse(apdu_req, 
1052                                           YAZ_BIB1_MALFORMED_QUERY,
1053                                           "CQL to CCL conversion error");
1054             package.response() = apdu_res;
1055             return;
1056         }
1057     }
1058     else
1059     {
1060         apdu_res = 
1061             odr.create_searchResponse(apdu_req, YAZ_BIB1_QUERY_TYPE_UNSUPP, 0);
1062         package.response() = apdu_res;
1063         return;
1064     }
1065
1066     if (ccl_wrbuf)
1067     {
1068         // CCL to PQF
1069         assert(pqf_wrbuf == 0);
1070         int cerror, cpos;
1071         struct ccl_rpn_node *cn;
1072         yaz_log(YLOG_LOG, "CCL: %s", wrbuf_cstr(ccl_wrbuf));
1073         cn = ccl_find_str(b->sptr->ccl_bibset, wrbuf_cstr(ccl_wrbuf),
1074                           &cerror, &cpos);
1075         wrbuf_destroy(ccl_wrbuf);
1076         if (!cn)
1077         {
1078             char *addinfo = odr_strdup(odr, ccl_err_msg(cerror));
1079             int z3950_diag = YAZ_BIB1_MALFORMED_QUERY;
1080
1081             switch (cerror)
1082             {
1083             case CCL_ERR_UNKNOWN_QUAL:
1084                 z3950_diag = YAZ_BIB1_UNSUPP_USE_ATTRIBUTE;
1085                 break;
1086             case CCL_ERR_TRUNC_NOT_LEFT: 
1087             case CCL_ERR_TRUNC_NOT_RIGHT:
1088             case CCL_ERR_TRUNC_NOT_BOTH:
1089                 z3950_diag = YAZ_BIB1_UNSUPP_TRUNCATION_ATTRIBUTE;
1090                 break;
1091             }
1092             apdu_res = 
1093                 odr.create_searchResponse(apdu_req, z3950_diag, addinfo);
1094             package.response() = apdu_res;
1095             return;
1096         }
1097         pqf_wrbuf = wrbuf_alloc();
1098         ccl_pquery(pqf_wrbuf, cn);
1099         ccl_rpn_delete(cn);
1100     }
1101     
1102     assert(pqf_wrbuf);
1103     if (b->get_option("sru"))
1104     {
1105         int status = 0;
1106         Z_RPNQuery *zquery;
1107         zquery = p_query_rpn(odr, wrbuf_cstr(pqf_wrbuf));
1108         WRBUF wrb = wrbuf_alloc();
1109             
1110         if (!strcmp(b->get_option("sru"), "solr"))
1111         {
1112             solr_transform_t cqlt = solr_transform_create();
1113             
1114             status = solr_transform_rpn2solr_wrbuf(cqlt, wrb, zquery);
1115             
1116             solr_transform_close(cqlt);
1117         }
1118         else
1119         {
1120             cql_transform_t cqlt = cql_transform_create();
1121             
1122             status = cql_transform_rpn2cql_wrbuf(cqlt, wrb, zquery);
1123             
1124             cql_transform_close(cqlt);
1125         }
1126         if (status == 0)
1127         {
1128             yaz_log(YLOG_LOG, "search CQL: %s", wrbuf_cstr(wrb));
1129             b->search_cql(wrbuf_cstr(wrb), &hits, &error, &addinfo_c);
1130         }
1131         
1132         wrbuf_destroy(wrb);
1133         wrbuf_destroy(pqf_wrbuf);
1134         if (status)
1135         {
1136             apdu_res = 
1137                 odr.create_searchResponse(apdu_req, YAZ_BIB1_MALFORMED_QUERY,
1138                                           "can not convert from RPN to CQL/SOLR");
1139             package.response() = apdu_res;
1140             return;
1141         }
1142     }
1143     else
1144     {
1145         yaz_log(YLOG_LOG, "search PQF: %s", wrbuf_cstr(pqf_wrbuf));
1146         b->search_pqf(wrbuf_cstr(pqf_wrbuf), &hits, &error, &addinfo_c);
1147         wrbuf_destroy(pqf_wrbuf);
1148     }
1149     
1150     
1151     const char *element_set_name = 0;
1152     Odr_int number_to_present = 0;
1153     if (!error)
1154         mp::util::piggyback_sr(sr, hits, number_to_present, &element_set_name);
1155     
1156     Odr_int number_of_records_returned = 0;
1157     Z_Records *records = get_records(
1158         0, number_to_present, &error, &addinfo_c,
1159         &number_of_records_returned, odr, b, sr->preferredRecordSyntax,
1160         element_set_name);
1161     apdu_res = odr.create_searchResponse(apdu_req, error, addinfo_c);
1162     if (records)
1163     {
1164         apdu_res->u.searchResponse->records = records;
1165         apdu_res->u.searchResponse->numberOfRecordsReturned =
1166             odr_intdup(odr, number_of_records_returned);
1167     }
1168     apdu_res->u.searchResponse->resultCount = odr_intdup(odr, hits);
1169     package.response() = apdu_res;
1170 }
1171
1172 void yf::Zoom::Frontend::handle_present(mp::Package &package)
1173 {
1174     Z_GDU *gdu = package.request().get();
1175     Z_APDU *apdu_req = gdu->u.z3950;
1176     Z_APDU *apdu_res = 0;
1177     Z_PresentRequest *pr = apdu_req->u.presentRequest;
1178
1179     mp::odr odr;
1180     if (!m_backend)
1181     {
1182         package.response() = odr.create_presentResponse(
1183             apdu_req, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST, 0);
1184         return;
1185     }
1186     const char *element_set_name = 0;
1187     Z_RecordComposition *comp = pr->recordComposition;
1188     if (comp && comp->which != Z_RecordComp_simple)
1189     {
1190         package.response() = odr.create_presentResponse(
1191             apdu_req, 
1192             YAZ_BIB1_PRESENT_COMP_SPEC_PARAMETER_UNSUPP, 0);
1193         return;
1194     }
1195     if (comp && comp->u.simple->which == Z_ElementSetNames_generic)
1196         element_set_name = comp->u.simple->u.generic;
1197     Odr_int number_of_records_returned = 0;
1198     int error = 0;
1199     const char *addinfo = 0;
1200     Z_Records *records = get_records(
1201         *pr->resultSetStartPoint - 1, *pr->numberOfRecordsRequested,
1202         &error, &addinfo, &number_of_records_returned, odr, m_backend,
1203         pr->preferredRecordSyntax, element_set_name);
1204
1205     apdu_res = odr.create_presentResponse(apdu_req, error, addinfo);
1206     if (records)
1207     {
1208         apdu_res->u.presentResponse->records = records;
1209         apdu_res->u.presentResponse->numberOfRecordsReturned =
1210             odr_intdup(odr, number_of_records_returned);
1211     }
1212     package.response() = apdu_res;
1213 }
1214
1215 void yf::Zoom::Frontend::handle_package(mp::Package &package)
1216 {
1217     Z_GDU *gdu = package.request().get();
1218     if (!gdu)
1219         ;
1220     else if (gdu->which == Z_GDU_Z3950)
1221     {
1222         Z_APDU *apdu_req = gdu->u.z3950;
1223         if (apdu_req->which == Z_APDU_initRequest)
1224         {
1225             mp::odr odr;
1226             package.response() = odr.create_close(
1227                 apdu_req,
1228                 Z_Close_protocolError,
1229                 "double init");
1230         }
1231         else if (apdu_req->which == Z_APDU_searchRequest)
1232         {
1233             handle_search(package);
1234         }
1235         else if (apdu_req->which == Z_APDU_presentRequest)
1236         {
1237             handle_present(package);
1238         }
1239         else
1240         {
1241             mp::odr odr;
1242             package.response() = odr.create_close(
1243                 apdu_req,
1244                 Z_Close_protocolError,
1245                 "zoom filter cannot handle this APDU");
1246             package.session().close();
1247         }
1248     }
1249     else
1250     {
1251         package.session().close();
1252     }
1253 }
1254
1255 void yf::Zoom::Impl::process(mp::Package &package)
1256 {
1257     FrontendPtr f = get_frontend(package);
1258     Z_GDU *gdu = package.request().get();
1259
1260     if (f->m_is_virtual)
1261     {
1262         f->handle_package(package);
1263     }
1264     else if (gdu && gdu->which == Z_GDU_Z3950 && gdu->u.z3950->which ==
1265              Z_APDU_initRequest)
1266     {
1267         Z_InitRequest *req = gdu->u.z3950->u.initRequest;
1268         f->m_init_gdu = gdu;
1269         
1270         mp::odr odr;
1271         Z_APDU *apdu = odr.create_initResponse(gdu->u.z3950, 0, 0);
1272         Z_InitResponse *resp = apdu->u.initResponse;
1273         
1274         int i;
1275         static const int masks[] = {
1276             Z_Options_search,
1277             Z_Options_present,
1278             -1 
1279         };
1280         for (i = 0; masks[i] != -1; i++)
1281             if (ODR_MASK_GET(req->options, masks[i]))
1282                 ODR_MASK_SET(resp->options, masks[i]);
1283         
1284         static const int versions[] = {
1285             Z_ProtocolVersion_1,
1286             Z_ProtocolVersion_2,
1287             Z_ProtocolVersion_3,
1288             -1
1289         };
1290         for (i = 0; versions[i] != -1; i++)
1291             if (ODR_MASK_GET(req->protocolVersion, versions[i]))
1292                 ODR_MASK_SET(resp->protocolVersion, versions[i]);
1293             else
1294                 break;
1295         
1296         *resp->preferredMessageSize = *req->preferredMessageSize;
1297         *resp->maximumRecordSize = *req->maximumRecordSize;
1298         
1299         package.response() = apdu;
1300         f->m_is_virtual = true;
1301     }
1302     else
1303         package.move();
1304
1305     release_frontend(package);
1306 }
1307
1308
1309 static mp::filter::Base* filter_creator()
1310 {
1311     return new mp::filter::Zoom;
1312 }
1313
1314 extern "C" {
1315     struct metaproxy_1_filter_struct metaproxy_1_filter_zoom = {
1316         0,
1317         "zoom",
1318         filter_creator
1319     };
1320 }
1321
1322
1323 /*
1324  * Local variables:
1325  * c-basic-offset: 4
1326  * c-file-style: "Stroustrup"
1327  * indent-tabs-mode: nil
1328  * End:
1329  * vim: shiftwidth=4 tabstop=8 expandtab
1330  */
1331