zoom: work on ZeeRex service
[metaproxy-moved-to-github.git] / src / filter_zoom.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 2005-2012 Index Data
3
4 Metaproxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18
19 #include "config.hpp"
20
21 #include <stdlib.h>
22 #include <sys/types.h>
23 #include "filter_zoom.hpp"
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
35 #include <yaz/yaz-version.h>
36 #include <yaz/tpath.h>
37 #include <yaz/srw.h>
38 #include <yaz/ccl_xml.h>
39 #include <yaz/ccl.h>
40 #include <yaz/rpn2cql.h>
41 #include <yaz/rpn2solr.h>
42 #include <yaz/pquery.h>
43 #include <yaz/cql.h>
44 #include <yaz/oid_db.h>
45 #include <yaz/diagbib1.h>
46 #include <yaz/log.h>
47 #include <yaz/zgdu.h>
48 #include <yaz/querytowrbuf.h>
49 #include <yaz/sortspec.h>
50 #include <yaz/tokenizer.h>
51 #include <yaz/zoom.h>
52
53 namespace mp = metaproxy_1;
54 namespace yf = mp::filter;
55
56 namespace metaproxy_1 {
57     namespace filter {
58         class Zoom::Searchable : boost::noncopyable {
59           public:
60             std::string authentication;
61             std::string cfAuth;
62             std::string cfProxy;
63             std::string cfSubDB;
64             std::string udb;
65             std::string target;
66             std::string query_encoding;
67             std::string sru;
68             std::string sru_version;
69             std::string request_syntax;
70             std::string element_set;
71             std::string record_encoding;
72             std::string transform_xsl_fname;
73             std::string transform_xsl_content;
74             std::string urlRecipe;
75             std::string contentConnector;
76             std::string sortStrategy;
77             bool use_turbomarc;
78             bool piggyback;
79             CCL_bibset ccl_bibset;
80             std::map<std::string, std::string> sortmap;
81             Searchable(CCL_bibset base);
82             ~Searchable();
83         };
84         class Zoom::Backend : boost::noncopyable {
85             friend class Impl;
86             friend class Frontend;
87             std::string zurl;
88             WRBUF m_apdu_wrbuf;
89             ZOOM_connection m_connection;
90             ZOOM_resultset m_resultset;
91             std::string m_frontend_database;
92             SearchablePtr sptr;
93             xsltStylesheetPtr xsp;
94             std::string content_session_id;
95             bool enable_cproxy;
96             bool enable_explain;
97             xmlDoc *explain_doc;
98         public:
99             Backend();
100             ~Backend();
101             void connect(std::string zurl, int *error, char **addinfo,
102                          ODR odr);
103             void search(ZOOM_query q, Odr_int *hits,
104                         int *error, char **addinfo, ODR odr);
105             void present(Odr_int start, Odr_int number, ZOOM_record *recs,
106                          int *error, char **addinfo, ODR odr);
107             void set_option(const char *name, const char *value);
108             void set_option(const char *name, std::string value);
109             const char *get_option(const char *name);
110             void get_zoom_error(int *error, char **addinfo, ODR odr);
111         };
112         class Zoom::Frontend : boost::noncopyable {
113             friend class Impl;
114             Impl *m_p;
115             bool m_is_virtual;
116             bool m_in_use;
117             yazpp_1::GDU m_init_gdu;
118             BackendPtr m_backend;
119             void handle_package(mp::Package &package);
120             void handle_search(mp::Package &package);
121
122             BackendPtr explain_search(mp::Package &package,
123                                       std::string &database,
124                                       int *error,
125                                       char **addinfo,
126                                       ODR odr,
127                                       std::string &torus_db,
128                                       std::string &realm);
129             void handle_present(mp::Package &package);
130             BackendPtr get_backend_from_databases(mp::Package &package,
131                                                   std::string &database,
132                                                   int *error,
133                                                   char **addinfo,
134                                                   ODR odr,
135                                                   int *proxy_step);
136
137             bool create_content_session(mp::Package &package,
138                                         BackendPtr b,
139                                         int *error,
140                                         char **addinfo,
141                                         ODR odr,
142                                         std::string authentication,
143                                         std::string proxy,
144                                         std::string realm);
145             
146             void prepare_elements(BackendPtr b,
147                                   Odr_oid *preferredRecordSyntax,
148                                   const char *element_set_name,
149                                   bool &enable_pz2_retrieval,
150                                   bool &enable_pz2_transform,
151                                   bool &assume_marc8_charset);
152
153             Z_Records *get_records(Package &package,
154                                    Odr_int start,
155                                    Odr_int number_to_present,
156                                    int *error,
157                                    char **addinfo,
158                                    Odr_int *number_of_records_returned,
159                                    ODR odr, BackendPtr b,
160                                    Odr_oid *preferredRecordSyntax,
161                                    const char *element_set_name);
162
163             void log_diagnostic(mp::Package &package,
164                                 int error, const char *addinfo);
165         public:
166             Frontend(Impl *impl);
167             ~Frontend();
168         };
169         class Zoom::Impl {
170             friend class Frontend;
171         public:
172             Impl();
173             ~Impl();
174             void process(metaproxy_1::Package & package);
175             void configure(const xmlNode * ptr, bool test_only,
176                            const char *path);
177         private:
178             void configure_local_records(const xmlNode * ptr, bool test_only);
179             FrontendPtr get_frontend(mp::Package &package);
180             void release_frontend(mp::Package &package);
181             SearchablePtr parse_torus_record(const xmlNode *ptr);
182             struct cql_node *convert_cql_fields(struct cql_node *cn, ODR odr);
183             std::map<mp::Session, FrontendPtr> m_clients;            
184             boost::mutex m_mutex;
185             boost::condition m_cond_session_ready;
186             std::string torus_searchable_url;
187             std::string torus_content_url;
188             std::string default_realm;
189             std::map<std::string,std::string> fieldmap;
190             std::string xsldir;
191             std::string file_path;
192             std::string content_proxy_server;
193             std::string content_tmp_file;
194             bool apdu_log;
195             CCL_bibset bibset;
196             std::string element_transform;
197             std::string element_raw;
198             std::string proxy;
199             xsltStylesheetPtr explain_xsp;
200             std::map<std::string,SearchablePtr> s_map;
201         };
202     }
203 }
204
205 // define Pimpl wrapper forwarding to Impl
206  
207 yf::Zoom::Zoom() : m_p(new Impl)
208 {
209 }
210
211 yf::Zoom::~Zoom()
212 {  // must have a destructor because of boost::scoped_ptr
213 }
214
215 void yf::Zoom::configure(const xmlNode *xmlnode, bool test_only,
216                          const char *path)
217 {
218     m_p->configure(xmlnode, test_only, path);
219 }
220
221 void yf::Zoom::process(mp::Package &package) const
222 {
223     m_p->process(package);
224 }
225
226
227 // define Implementation stuff
228
229 yf::Zoom::Backend::Backend()
230 {
231     m_apdu_wrbuf = wrbuf_alloc();
232     m_connection = ZOOM_connection_create(0);
233     ZOOM_connection_save_apdu_wrbuf(m_connection, m_apdu_wrbuf);
234     m_resultset = 0;
235     xsp = 0;
236     enable_cproxy = true;
237     enable_explain = false;
238     explain_doc = 0;
239 }
240
241 yf::Zoom::Backend::~Backend()
242 {
243     if (xsp)
244         xsltFreeStylesheet(xsp);
245     if (explain_doc)
246         xmlFreeDoc(explain_doc);
247     ZOOM_connection_destroy(m_connection);
248     ZOOM_resultset_destroy(m_resultset);
249     wrbuf_destroy(m_apdu_wrbuf);
250 }
251
252
253 void yf::Zoom::Backend::get_zoom_error(int *error, char **addinfo,
254                                        ODR odr)
255 {
256     const char *msg = 0;
257     const char *zoom_addinfo = 0;
258     const char *dset = 0;
259     int error0 = ZOOM_connection_error_x(m_connection, &msg,
260                                          &zoom_addinfo, &dset);
261     if (error0)
262     {
263         if (!dset)
264             dset = "Unknown";
265         
266         if (!strcmp(dset, "info:srw/diagnostic/1"))
267             *error = yaz_diag_srw_to_bib1(error0);
268         else if (!strcmp(dset, "Bib-1"))
269             *error = error0;
270         else
271             *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
272         
273         *addinfo = (char *) odr_malloc(
274             odr, 30 + strlen(dset) + strlen(msg) +
275             (zoom_addinfo ? strlen(zoom_addinfo) : 0));
276         **addinfo = '\0';
277         if (zoom_addinfo && *zoom_addinfo)
278         {
279             strcpy(*addinfo, zoom_addinfo);
280             strcat(*addinfo, " ");
281         }
282         sprintf(*addinfo + strlen(*addinfo), "(%s %d %s)", dset, error0, msg);
283     }
284 }
285
286 void yf::Zoom::Backend::connect(std::string zurl,
287                                 int *error, char **addinfo,
288                                 ODR odr)
289 {
290     ZOOM_connection_connect(m_connection, zurl.c_str(), 0);
291     get_zoom_error(error, addinfo, odr);
292 }
293
294 void yf::Zoom::Backend::search(ZOOM_query q, Odr_int *hits,
295                                int *error, char **addinfo, ODR odr)
296 {
297     ZOOM_resultset_destroy(m_resultset);
298     m_resultset = ZOOM_connection_search(m_connection, q);
299     get_zoom_error(error, addinfo, odr);
300     if (*error == 0)
301         *hits = ZOOM_resultset_size(m_resultset);
302     else
303         *hits = 0;
304 }
305
306 void yf::Zoom::Backend::present(Odr_int start, Odr_int number,
307                                 ZOOM_record *recs,
308                                 int *error, char **addinfo, ODR odr)
309 {
310     ZOOM_resultset_records(m_resultset, recs, start, number);
311     get_zoom_error(error, addinfo, odr);
312 }
313
314 void yf::Zoom::Backend::set_option(const char *name, const char *value)
315 {
316     ZOOM_connection_option_set(m_connection, name, value);
317     if (m_resultset)
318         ZOOM_resultset_option_set(m_resultset, name, value);
319 }
320
321 void yf::Zoom::Backend::set_option(const char *name, std::string value)
322 {
323     set_option(name, value.c_str());
324 }
325
326 const char *yf::Zoom::Backend::get_option(const char *name)
327 {
328     return ZOOM_connection_option_get(m_connection, name);
329 }
330
331 yf::Zoom::Searchable::Searchable(CCL_bibset base)
332 {
333     piggyback = true;
334     use_turbomarc = true;
335     sortStrategy = "embed";
336     urlRecipe = "${md-electronic-url}";
337     ccl_bibset = ccl_qual_dup(base);
338 }
339
340 yf::Zoom::Searchable::~Searchable()
341 {
342     ccl_qual_rm(&ccl_bibset);
343 }
344
345 yf::Zoom::Frontend::Frontend(Impl *impl) : 
346     m_p(impl), m_is_virtual(false), m_in_use(true)
347 {
348 }
349
350 yf::Zoom::Frontend::~Frontend()
351 {
352 }
353
354 yf::Zoom::FrontendPtr yf::Zoom::Impl::get_frontend(mp::Package &package)
355 {
356     boost::mutex::scoped_lock lock(m_mutex);
357
358     std::map<mp::Session,yf::Zoom::FrontendPtr>::iterator it;
359     
360     while(true)
361     {
362         it = m_clients.find(package.session());
363         if (it == m_clients.end())
364             break;
365         
366         if (!it->second->m_in_use)
367         {
368             it->second->m_in_use = true;
369             return it->second;
370         }
371         m_cond_session_ready.wait(lock);
372     }
373     FrontendPtr f(new Frontend(this));
374     m_clients[package.session()] = f;
375     f->m_in_use = true;
376     return f;
377 }
378
379 void yf::Zoom::Impl::release_frontend(mp::Package &package)
380 {
381     boost::mutex::scoped_lock lock(m_mutex);
382     std::map<mp::Session,yf::Zoom::FrontendPtr>::iterator it;
383     
384     it = m_clients.find(package.session());
385     if (it != m_clients.end())
386     {
387         if (package.session().is_closed())
388         {
389             m_clients.erase(it);
390         }
391         else
392         {
393             it->second->m_in_use = false;
394         }
395         m_cond_session_ready.notify_all();
396     }
397 }
398
399 yf::Zoom::Impl::Impl() :
400     apdu_log(false), element_transform("pz2") , element_raw("raw")
401 {
402     bibset = ccl_qual_mk();
403
404     explain_xsp = 0;
405     srand((unsigned int) time(0));
406 }
407
408 yf::Zoom::Impl::~Impl()
409 {
410     if (explain_xsp)
411         xsltFreeStylesheet(explain_xsp);
412     ccl_qual_rm(&bibset);
413 }
414
415 yf::Zoom::SearchablePtr yf::Zoom::Impl::parse_torus_record(const xmlNode *ptr)
416 {
417     Zoom::SearchablePtr s(new Searchable(bibset));
418     
419     for (ptr = ptr->children; ptr; ptr = ptr->next)
420     {
421         if (ptr->type != XML_ELEMENT_NODE)
422             continue;
423         if (!strcmp((const char *) ptr->name, "layer"))
424             ptr = ptr->children;
425         else if (!strcmp((const char *) ptr->name,
426                          "authentication"))
427         {
428             s->authentication = mp::xml::get_text(ptr);
429         }
430         else if (!strcmp((const char *) ptr->name,
431                          "cfAuth"))
432         {
433             s->cfAuth = mp::xml::get_text(ptr);
434         } 
435         else if (!strcmp((const char *) ptr->name,
436                          "cfProxy"))
437         {
438             s->cfProxy = mp::xml::get_text(ptr);
439         }  
440         else if (!strcmp((const char *) ptr->name,
441                          "cfSubDB"))
442         {
443             s->cfSubDB = mp::xml::get_text(ptr);
444         }  
445         else if (!strcmp((const char *) ptr->name,
446                          "contentConnector"))
447         {
448             s->contentConnector = mp::xml::get_text(ptr);
449         }  
450         else if (!strcmp((const char *) ptr->name, "udb"))
451         {
452             s->udb = mp::xml::get_text(ptr);
453         }
454         else if (!strcmp((const char *) ptr->name, "zurl"))
455         {
456             s->target = mp::xml::get_text(ptr);
457         }
458         else if (!strcmp((const char *) ptr->name, "sru"))
459         {
460             s->sru = mp::xml::get_text(ptr);
461         }
462         else if (!strcmp((const char *) ptr->name, "SRUVersion") ||
463                  !strcmp((const char *) ptr->name, "sruVersion"))
464         {
465             s->sru_version = mp::xml::get_text(ptr);
466         }
467         else if (!strcmp((const char *) ptr->name,
468                          "queryEncoding"))
469         {
470             s->query_encoding = mp::xml::get_text(ptr);
471         }
472         else if (!strcmp((const char *) ptr->name,
473                          "piggyback"))
474         {
475             s->piggyback = mp::xml::get_bool(ptr, true);
476         }
477         else if (!strcmp((const char *) ptr->name,
478                          "requestSyntax"))
479         {
480             s->request_syntax = mp::xml::get_text(ptr);
481         }
482         else if (!strcmp((const char *) ptr->name,
483                          "elementSet"))
484         {
485             s->element_set = mp::xml::get_text(ptr);
486         }
487         else if (!strcmp((const char *) ptr->name,
488                          "recordEncoding"))
489         {
490             s->record_encoding = mp::xml::get_text(ptr);
491         }
492         else if (!strcmp((const char *) ptr->name,
493                          "transform"))
494         {
495             s->transform_xsl_fname = mp::xml::get_text(ptr);
496         }
497         else if (!strcmp((const char *) ptr->name,
498                          "literalTransform"))
499         {
500             s->transform_xsl_content = mp::xml::get_text(ptr);
501         }
502         else if (!strcmp((const char *) ptr->name,
503                          "urlRecipe"))
504         {
505             s->urlRecipe = mp::xml::get_text(ptr);
506         }
507         else if (!strcmp((const char *) ptr->name,
508                          "useTurboMarc"))
509         {
510             ; // useTurboMarc is ignored
511         }
512         else if (!strncmp((const char *) ptr->name,
513                           "cclmap_", 7))
514         {
515             std::string value = mp::xml::get_text(ptr);
516             ccl_qual_fitem(s->ccl_bibset, value.c_str(),
517                            (const char *) ptr->name + 7);
518         }
519         else if (!strncmp((const char *) ptr->name,
520                           "sortmap_", 8))
521         {
522             std::string value = mp::xml::get_text(ptr);
523             s->sortmap[(const char *) ptr->name + 8] = value;
524         }
525         else if (!strcmp((const char *) ptr->name,
526                           "sortStrategy"))
527         {
528             s->sortStrategy = mp::xml::get_text(ptr);
529         }
530     }
531     return s;
532 }
533
534 void yf::Zoom::Impl::configure_local_records(const xmlNode *ptr, bool test_only)
535 {
536     while (ptr && ptr->type != XML_ELEMENT_NODE)
537         ptr = ptr->next;
538     
539     if (ptr)
540     {
541         if (!strcmp((const char *) ptr->name, "records"))
542         {
543             for (ptr = ptr->children; ptr; ptr = ptr->next)
544             {
545                 if (ptr->type != XML_ELEMENT_NODE)
546                     continue;
547                 if (!strcmp((const char *) ptr->name, "record"))
548                 {
549                     SearchablePtr s = parse_torus_record(ptr);
550                     if (s)
551                     {
552                         std::string udb = s->udb;
553                         if (udb.length())
554                             s_map[s->udb] = s;
555                         else
556                         {
557                             throw mp::filter::FilterException
558                                 ("No udb for local torus record");
559                         }
560                     }
561                 }
562                 else
563                 {
564                     throw mp::filter::FilterException
565                         ("Bad element " 
566                          + std::string((const char *) ptr->name)
567                          + " in zoom filter inside element "
568                          "<torus><records>");
569                 }
570             }
571         }
572         else
573         {
574             throw mp::filter::FilterException
575                 ("Bad element " 
576                  + std::string((const char *) ptr->name)
577                  + " in zoom filter inside element <torus>");
578         }
579     }
580 }
581
582 void yf::Zoom::Impl::configure(const xmlNode *ptr, bool test_only,
583                                const char *path)
584 {
585     std::string explain_xslt_fname;
586
587     content_tmp_file = "/tmp/cf.XXXXXX.p";
588     if (path && *path)
589     {
590         file_path = path;
591     }
592     for (ptr = ptr->children; ptr; ptr = ptr->next)
593     {
594         if (ptr->type != XML_ELEMENT_NODE)
595             continue;
596         else if (!strcmp((const char *) ptr->name, "torus"))
597         {
598             const struct _xmlAttr *attr;
599             for (attr = ptr->properties; attr; attr = attr->next)
600             {
601                 if (!strcmp((const char *) attr->name, "url"))
602                     torus_searchable_url = mp::xml::get_text(attr->children);
603                 else if (!strcmp((const char *) attr->name, "content_url"))
604                     torus_content_url = mp::xml::get_text(attr->children);
605                 else if (!strcmp((const char *) attr->name, "realm"))
606                     default_realm = mp::xml::get_text(attr->children);
607                 else if (!strcmp((const char *) attr->name, "xsldir"))
608                     xsldir = mp::xml::get_text(attr->children);
609                 else if (!strcmp((const char *) attr->name, "element_transform"))
610                     element_transform = mp::xml::get_text(attr->children);
611                 else if (!strcmp((const char *) attr->name, "element_raw"))
612                     element_raw = mp::xml::get_text(attr->children);
613                 else if (!strcmp((const char *) attr->name, "proxy"))
614                     proxy = mp::xml::get_text(attr->children);
615                 else if (!strcmp((const char *) attr->name, "explain_xsl"))
616                     explain_xslt_fname = mp::xml::get_text(attr->children);
617                 else
618                     throw mp::filter::FilterException(
619                         "Bad attribute " + std::string((const char *)
620                                                        attr->name));
621             }
622             // If content_url is not given, use value of searchable, to
623             // ensure backwards compatibility
624             if (!torus_content_url.length())
625                 torus_content_url = torus_searchable_url;
626             configure_local_records(ptr->children, test_only);
627         }
628         else if (!strcmp((const char *) ptr->name, "cclmap"))
629         {
630             const char *addinfo = 0;
631             ccl_xml_config(bibset, ptr, &addinfo);
632         }
633         else if (!strcmp((const char *) ptr->name, "fieldmap"))
634         {
635             const struct _xmlAttr *attr;
636             std::string ccl_field;
637             std::string cql_field;
638             for (attr = ptr->properties; attr; attr = attr->next)
639             {
640                 if (!strcmp((const char *) attr->name, "ccl"))
641                     ccl_field = mp::xml::get_text(attr->children);
642                 else if (!strcmp((const char *) attr->name, "cql"))
643                     cql_field = mp::xml::get_text(attr->children);
644                 else
645                     throw mp::filter::FilterException(
646                         "Bad attribute " + std::string((const char *)
647                                                        attr->name));
648             }
649             if (cql_field.length())
650                 fieldmap[cql_field] = ccl_field;
651         }
652         else if (!strcmp((const char *) ptr->name, "contentProxy"))
653         {
654             const struct _xmlAttr *attr;
655             for (attr = ptr->properties; attr; attr = attr->next)
656             {
657                 if (!strcmp((const char *) attr->name, "server"))
658                     content_proxy_server = mp::xml::get_text(attr->children);
659                 else if (!strcmp((const char *) attr->name, "tmp_file"))
660                     content_tmp_file = mp::xml::get_text(attr->children);
661                 else
662                     throw mp::filter::FilterException(
663                         "Bad attribute " + std::string((const char *)
664                                                        attr->name));
665             }
666         }
667         else if (!strcmp((const char *) ptr->name, "log"))
668         { 
669             const struct _xmlAttr *attr;
670             for (attr = ptr->properties; attr; attr = attr->next)
671             {
672                 if (!strcmp((const char *) attr->name, "apdu"))
673                     apdu_log = mp::xml::get_bool(attr->children, false);
674                 else
675                     throw mp::filter::FilterException(
676                         "Bad attribute " + std::string((const char *)
677                                                        attr->name));
678             }
679         }
680         else
681         {
682             throw mp::filter::FilterException
683                 ("Bad element " 
684                  + std::string((const char *) ptr->name)
685                  + " in zoom filter");
686         }
687     }
688
689     if (explain_xslt_fname.length())
690     {
691         const char *path = 0;
692         
693         if (xsldir.length())
694             path = xsldir.c_str();
695         else
696             path = file_path.c_str();
697         
698         char fullpath[1024];
699         char *cp = yaz_filepath_resolve(explain_xslt_fname.c_str(),
700                                         path, 0, fullpath);
701         if (!cp)
702         {
703             throw mp::filter::FilterException
704                 ("Cannot read XSLT " + explain_xslt_fname);
705         }
706
707         xmlDoc *xsp_doc = xmlParseFile(cp);
708         if (!xsp_doc)
709         {
710             throw mp::filter::FilterException
711                 ("Cannot parse XSLT " + explain_xslt_fname);
712         }
713
714         explain_xsp = xsltParseStylesheetDoc(xsp_doc);
715         if (!explain_xsp)
716         {
717             xmlFreeDoc(xsp_doc);
718             throw mp::filter::FilterException
719                 ("Cannot parse XSLT " + explain_xslt_fname);
720             
721         }
722     }
723 }
724
725 bool yf::Zoom::Frontend::create_content_session(mp::Package &package,
726                                                 BackendPtr b,
727                                                 int *error, char **addinfo,
728                                                 ODR odr,
729                                                 std::string authentication,
730                                                 std::string proxy,
731                                                 std::string realm)
732 {
733     if (b->sptr->contentConnector.length())
734     {
735         char *fname = (char *) xmalloc(m_p->content_tmp_file.length() + 8);
736         strcpy(fname, m_p->content_tmp_file.c_str());
737         char *xx = strstr(fname, "XXXXXX");
738         if (!xx)
739         {
740             xx = fname + strlen(fname);
741             strcat(fname, "XXXXXX");
742         }
743         char tmp_char = xx[6];
744         sprintf(xx, "%06d", ((unsigned) rand()) % 1000000);
745         xx[6] = tmp_char;
746
747         FILE *file = fopen(fname, "w");
748         if (!file)
749         {
750             package.log("zoom", YLOG_WARN|YLOG_ERRNO, "create %s", fname);
751             *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
752             *addinfo = (char *)  odr_malloc(odr, 40 + strlen(fname));
753             sprintf(*addinfo, "Could not create %s", fname);
754             xfree(fname);
755             return false;
756         }
757         b->content_session_id.assign(xx, 6);
758         WRBUF w = wrbuf_alloc();
759         wrbuf_puts(w, "#content_proxy\n");
760         wrbuf_printf(w, "connector: %s\n", b->sptr->contentConnector.c_str());
761         if (authentication.length())
762             wrbuf_printf(w, "auth: %s\n", authentication.c_str());
763         if (proxy.length())
764             wrbuf_printf(w, "proxy: %s\n", proxy.c_str());
765         if (realm.length())
766             wrbuf_printf(w, "realm: %s\n", realm.c_str());
767
768         fwrite(wrbuf_buf(w), 1, wrbuf_len(w), file);
769         fclose(file);
770         package.log("zoom", YLOG_LOG, "content file: %s", fname);
771         xfree(fname);
772         wrbuf_destroy(w);
773     }
774     return true;
775 }
776
777 yf::Zoom::BackendPtr yf::Zoom::Frontend::get_backend_from_databases(
778     mp::Package &package,
779     std::string &database, int *error, char **addinfo, ODR odr,
780     int *proxy_step)
781 {
782     std::list<BackendPtr>::const_iterator map_it;
783     if (m_backend && m_backend->m_frontend_database == database)
784         return m_backend;
785
786     std::string input_args;
787     std::string torus_db;
788     size_t db_arg_pos = database.find(',');
789     if (db_arg_pos != std::string::npos)
790     {
791         torus_db = database.substr(0, db_arg_pos);
792         input_args = database.substr(db_arg_pos + 1);
793     }
794     else
795         torus_db = database;
796
797     std::string authentication;
798     std::string content_authentication;
799     std::string proxy;
800     std::string content_proxy;
801     std::string realm = m_p->default_realm;
802
803     const char *param_user = 0;
804     const char *param_password = 0;
805     const char *param_content_user = 0;
806     const char *param_content_password = 0;
807     const char *param_nocproxy = 0;
808     int no_parms = 0;
809
810     char **names;
811     char **values;
812     int no_out_args = 0;
813     if (input_args.length())
814         no_parms = yaz_uri_to_array(input_args.c_str(),
815                                     odr, &names, &values);
816     // adding 10 because we'll be adding other URL args
817     const char **out_names = (const char **)
818         odr_malloc(odr, (10 + no_parms) * sizeof(*out_names));
819     const char **out_values = (const char **)
820         odr_malloc(odr, (10 + no_parms) * sizeof(*out_values));
821     
822     // may be changed if it's a content connection
823     std::string torus_url = m_p->torus_searchable_url;
824     int i;
825     for (i = 0; i < no_parms; i++)
826     {
827         const char *name = names[i];
828         const char *value = values[i];
829         assert(name);
830         assert(value);
831         if (!strcmp(name, "user"))
832             param_user = value;
833         else if (!strcmp(name, "password"))
834             param_password = value;
835         else if (!strcmp(name, "content-user"))
836             param_content_user = value;
837         else if (!strcmp(name, "content-password"))
838             param_content_password = value;
839         else if (!strcmp(name, "content-proxy"))
840             content_proxy = value;
841         else if (!strcmp(name, "nocproxy"))
842             param_nocproxy = value;
843         else if (!strcmp(name, "proxy"))
844         {
845             char **dstr;
846             int dnum = 0;
847             nmem_strsplit(odr->mem, ",", value, &dstr, &dnum);
848             if (*proxy_step >= dnum)
849                 *proxy_step = 0;
850             else
851             {
852                 proxy = dstr[*proxy_step];
853                 
854                 (*proxy_step)++;
855                 if (*proxy_step == dnum)
856                     *proxy_step = 0;
857             }
858         }
859         else if (!strcmp(name, "cproxysession"))
860         {
861             out_names[no_out_args] = name;
862             out_values[no_out_args++] = value;
863             torus_url = m_p->torus_content_url;
864         }
865         else if (!strcmp(name, "realm"))
866             realm = value;
867         else if (name[0] == 'x' && name[1] == '-')
868         {
869             out_names[no_out_args] = name;
870             out_values[no_out_args++] = value;
871         }
872         else
873         {
874             BackendPtr notfound;
875             char *msg = (char*) odr_malloc(odr, strlen(name) + 30);
876             *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
877             sprintf(msg, "Bad database argument: %s", name);
878             *addinfo = msg;
879             return notfound;
880         }
881     }
882     if (param_user)
883     {
884         authentication = std::string(param_user);
885         if (param_password)
886             authentication += "/" + std::string(param_password);
887     }
888     if (param_content_user)
889     {
890         content_authentication = std::string(param_content_user);
891         if (param_content_password)
892             content_authentication += "/" + std::string(param_content_password);
893     }
894
895     if (torus_db.compare("IR-Explain---1") == 0)
896         return explain_search(package, database, error, addinfo, odr, torus_db,
897             realm);
898     
899     SearchablePtr sptr;
900
901     std::map<std::string,SearchablePtr>::iterator it;
902     it = m_p->s_map.find(torus_db);
903     if (it != m_p->s_map.end())
904         sptr = it->second;
905     else if (torus_url.length() > 0)
906     {
907         std::string torus_query = "udb=" + torus_db;
908         xmlDoc *doc = mp::get_searchable(package,torus_url, torus_db,
909                                          torus_query,
910                                          realm, m_p->proxy);
911         if (!doc)
912         {
913             *error = YAZ_BIB1_DATABASE_DOES_NOT_EXIST;
914             *addinfo = odr_strdup(odr, database.c_str());
915             BackendPtr b;
916             return b;
917         }
918         const xmlNode *ptr = xmlDocGetRootElement(doc);
919         if (ptr)
920         {   // presumably ptr is a records element node
921             // parse first record in document
922             for (ptr = ptr->children; ptr; ptr = ptr->next)
923             {
924                 if (ptr->type == XML_ELEMENT_NODE
925                     && !strcmp((const char *) ptr->name, "record"))
926                 {
927                     if (sptr)
928                     {
929                         *error = YAZ_BIB1_UNSPECIFIED_ERROR;
930                         *addinfo = (char*) odr_malloc(odr, 40 + database.length()),
931                         sprintf(*addinfo, "multiple records for udb=%s",
932                                  database.c_str());
933                         xmlFreeDoc(doc);
934                         BackendPtr b;
935                         return b;
936                     }
937                     sptr = m_p->parse_torus_record(ptr);
938                 }
939             }
940         }
941         xmlFreeDoc(doc);
942     }
943
944     if (!sptr)
945     {
946         *error = YAZ_BIB1_DATABASE_DOES_NOT_EXIST;
947         *addinfo = odr_strdup(odr, database.c_str());
948         BackendPtr b;
949         return b;
950     }
951         
952     xsltStylesheetPtr xsp = 0;
953     if (sptr->transform_xsl_content.length())
954     {
955         xmlDoc *xsp_doc = xmlParseMemory(sptr->transform_xsl_content.c_str(),
956                                          sptr->transform_xsl_content.length());
957         if (!xsp_doc)
958         {
959             *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
960             *addinfo = (char *) odr_malloc(odr, 40);
961             sprintf(*addinfo, "xmlParseMemory failed");
962             BackendPtr b;
963             return b;
964         }
965         xsp = xsltParseStylesheetDoc(xsp_doc);
966         if (!xsp)
967         {
968             *error = YAZ_BIB1_DATABASE_DOES_NOT_EXIST;
969             *addinfo = odr_strdup(odr, "xsltParseStylesheetDoc failed");
970             BackendPtr b;
971             xmlFreeDoc(xsp_doc);
972             return b;
973         }
974     }
975     else if (sptr->transform_xsl_fname.length())
976     {
977         const char *path = 0;
978
979         if (m_p->xsldir.length())
980             path = m_p->xsldir.c_str();
981         else
982             path = m_p->file_path.c_str();
983         std::string fname;
984
985         char fullpath[1024];
986         char *cp = yaz_filepath_resolve(sptr->transform_xsl_fname.c_str(),
987                                         path, 0, fullpath);
988         if (cp)
989             fname.assign(cp);
990         else
991         {
992             *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
993             *addinfo = (char *)
994                 odr_malloc(odr, 40 + sptr->transform_xsl_fname.length());
995             sprintf(*addinfo, "File could not be read: %s", 
996                     sptr->transform_xsl_fname.c_str());
997             BackendPtr b;
998             return b;
999         }
1000         xmlDoc *xsp_doc = xmlParseFile(fname.c_str());
1001         if (!xsp_doc)
1002         {
1003             *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1004             *addinfo = (char *) odr_malloc(odr, 40 + fname.length());
1005             sprintf(*addinfo, "xmlParseFile failed. File: %s", fname.c_str());
1006             BackendPtr b;
1007             return b;
1008         }
1009         xsp = xsltParseStylesheetDoc(xsp_doc);
1010         if (!xsp)
1011         {
1012             *error = YAZ_BIB1_DATABASE_DOES_NOT_EXIST;
1013             *addinfo = odr_strdup(odr, "xsltParseStylesheetDoc failed");
1014             BackendPtr b;
1015             xmlFreeDoc(xsp_doc);
1016             return b;
1017         }
1018     }
1019
1020     m_backend.reset();
1021
1022     BackendPtr b(new Backend);
1023
1024     b->sptr = sptr;
1025     b->xsp = xsp;
1026     b->m_frontend_database = database;
1027     b->enable_cproxy = param_nocproxy ? false : true;
1028
1029     if (sptr->query_encoding.length())
1030         b->set_option("rpnCharset", sptr->query_encoding);
1031
1032     b->set_option("timeout", "40");
1033     
1034     if (m_p->apdu_log) 
1035         b->set_option("apdulog", "1");
1036
1037     if (sptr->piggyback && sptr->sru.length())
1038         b->set_option("count", "1"); /* some SRU servers INSIST on getting
1039                                         maximumRecords > 0 */
1040     b->set_option("piggyback", sptr->piggyback ? "1" : "0");
1041
1042     if (authentication.length() == 0)
1043         authentication = sptr->authentication;
1044
1045     if (proxy.length() == 0)
1046         proxy = sptr->cfProxy;
1047     
1048     if (sptr->cfAuth.length())
1049     {
1050         // A CF target
1051         b->set_option("user", sptr->cfAuth);
1052         if (authentication.length())
1053         {
1054             size_t found = authentication.find('/');
1055             if (found != std::string::npos)
1056             {
1057                 out_names[no_out_args] = "user";
1058                 out_values[no_out_args++] =
1059                     odr_strdup(odr, authentication.substr(0, found).c_str());
1060
1061                 out_names[no_out_args] = "password";
1062                 out_values[no_out_args++] =
1063                     odr_strdup(odr, authentication.substr(found+1).c_str());
1064             }
1065             else
1066             {
1067                 out_names[no_out_args] = "user";
1068                 out_values[no_out_args++] =
1069                     odr_strdup(odr, authentication.c_str());
1070             }                
1071         }
1072         if (proxy.length())
1073         {
1074             out_names[no_out_args] = "proxy";
1075             out_values[no_out_args++] = odr_strdup(odr, proxy.c_str());
1076         }
1077         if (sptr->cfSubDB.length())
1078         {
1079             out_names[no_out_args] = "subdatabase";
1080             out_values[no_out_args++] = odr_strdup(odr, sptr->cfSubDB.c_str());
1081         }
1082         if (param_nocproxy)
1083         {
1084             out_names[no_out_args] = "nocproxy";
1085             out_values[no_out_args++] = odr_strdup(odr, param_nocproxy);
1086         }
1087     }
1088     else
1089     {
1090         size_t found = authentication.find('/');
1091         
1092         if (sptr->sru.length() && found != std::string::npos)
1093         {
1094             b->set_option("user", authentication.substr(0, found));
1095             b->set_option("password", authentication.substr(found+1));
1096         }
1097         else
1098             b->set_option("user", authentication);
1099
1100         if (proxy.length())
1101             b->set_option("proxy", proxy);
1102     }
1103     if (proxy.length())
1104         package.log("zoom", YLOG_LOG, "proxy: %s", proxy.c_str());
1105                 
1106     std::string url;
1107     if (sptr->sru.length())
1108     {
1109         url = "http://" + sptr->target;
1110         b->set_option("sru", sptr->sru);
1111
1112         if (sptr->sru_version.length())
1113             b->set_option("sru_version", sptr->sru_version);
1114     }
1115     else
1116     {
1117         url = sptr->target;
1118     }
1119     if (no_out_args)
1120     {
1121         char *x_args = 0;
1122         out_names[no_out_args] = 0; // terminate list
1123         
1124         yaz_array_to_uri(&x_args, odr, (char **) out_names,
1125                          (char **) out_values);
1126         url += "," + std::string(x_args);
1127     }
1128     package.log("zoom", YLOG_LOG, "url: %s", url.c_str());
1129     b->connect(url, error, addinfo, odr);
1130     if (*error == 0 && b->enable_cproxy)
1131         create_content_session(package, b, error, addinfo, odr,
1132                                content_authentication.length() ?
1133                                content_authentication : authentication,
1134                                content_proxy.length() ? content_proxy : proxy,
1135                                realm);
1136     if (*error == 0)
1137         m_backend = b;
1138     return b;
1139 }
1140
1141 void yf::Zoom::Frontend::prepare_elements(BackendPtr b,
1142                                           Odr_oid *preferredRecordSyntax,
1143                                           const char *element_set_name,
1144                                           bool &enable_pz2_retrieval,
1145                                           bool &enable_pz2_transform,
1146                                           bool &assume_marc8_charset)
1147
1148 {
1149     char oid_name_str[OID_STR_MAX];
1150     const char *syntax_name = 0;
1151     
1152     if (preferredRecordSyntax &&
1153         !oid_oidcmp(preferredRecordSyntax, yaz_oid_recsyn_xml)
1154         && element_set_name)
1155     {
1156         if (!strcmp(element_set_name, m_p->element_transform.c_str()))
1157         {
1158             enable_pz2_retrieval = true;
1159             enable_pz2_transform = true;
1160         }
1161         else if (!strcmp(element_set_name, m_p->element_raw.c_str()))
1162         {
1163             enable_pz2_retrieval = true;
1164         }
1165     }
1166     
1167     if (enable_pz2_retrieval)
1168     {
1169         std::string configured_request_syntax = b->sptr->request_syntax;
1170         if (configured_request_syntax.length())
1171         {
1172             syntax_name = configured_request_syntax.c_str();
1173             const Odr_oid *syntax_oid = 
1174                 yaz_string_to_oid(yaz_oid_std(), CLASS_RECSYN, syntax_name);
1175             if (!oid_oidcmp(syntax_oid, yaz_oid_recsyn_usmarc)
1176                 || !oid_oidcmp(syntax_oid, yaz_oid_recsyn_opac))
1177                 assume_marc8_charset = true;
1178         }
1179     }
1180     else if (preferredRecordSyntax)
1181         syntax_name =
1182             yaz_oid_to_string_buf(preferredRecordSyntax, 0, oid_name_str);
1183
1184     if (b->sptr->sru.length())
1185         syntax_name = "XML";
1186
1187     b->set_option("preferredRecordSyntax", syntax_name);
1188
1189     if (enable_pz2_retrieval)
1190     {
1191         element_set_name = 0;
1192         if (b->sptr->element_set.length())
1193             element_set_name = b->sptr->element_set.c_str();
1194     }
1195
1196     b->set_option("elementSetName", element_set_name);
1197     if (b->sptr->sru.length() && element_set_name)
1198         b->set_option("schema", element_set_name);
1199 }
1200
1201 Z_Records *yf::Zoom::Frontend::get_records(Package &package,
1202                                            Odr_int start,
1203                                            Odr_int number_to_present,
1204                                            int *error,
1205                                            char **addinfo,
1206                                            Odr_int *number_of_records_returned,
1207                                            ODR odr,
1208                                            BackendPtr b,
1209                                            Odr_oid *preferredRecordSyntax,
1210                                            const char *element_set_name)
1211 {
1212     *number_of_records_returned = 0;
1213     Z_Records *records = 0;
1214     bool enable_pz2_retrieval = false; // whether target profile is used
1215     bool enable_pz2_transform = false; // whether XSLT is used as well
1216     bool assume_marc8_charset = false;
1217
1218     prepare_elements(b, preferredRecordSyntax,
1219                      element_set_name,
1220                      enable_pz2_retrieval,
1221                      enable_pz2_transform,
1222                      assume_marc8_charset);
1223
1224     package.log("zoom", YLOG_LOG, "pz2_retrieval: %s . pz2_transform: %s",
1225                 enable_pz2_retrieval ? "yes" : "no",
1226                 enable_pz2_transform ? "yes" : "no");
1227
1228     if (start < 0 || number_to_present <=0)
1229         return records;
1230     
1231     if (number_to_present > 10000)
1232         number_to_present = 10000;
1233
1234     ZOOM_record *recs = (ZOOM_record *)
1235         odr_malloc(odr, (size_t) number_to_present * sizeof(*recs));
1236
1237     b->present(start, number_to_present, recs, error, addinfo, odr);
1238
1239     int i = 0;
1240     if (!*error)
1241     {
1242         for (i = 0; i < number_to_present; i++)
1243             if (!recs[i])
1244                 break;
1245     }
1246     if (i > 0)
1247     {  // only return records if no error and at least one record
1248         char *odr_database = odr_strdup(odr,
1249                                         b->m_frontend_database.c_str());
1250         Z_NamePlusRecordList *npl = (Z_NamePlusRecordList *)
1251             odr_malloc(odr, sizeof(*npl));
1252         *number_of_records_returned = i;
1253         npl->num_records = i;
1254         npl->records = (Z_NamePlusRecord **)
1255             odr_malloc(odr, i * sizeof(*npl->records));
1256         for (i = 0; i < number_to_present; i++)
1257         {
1258             Z_NamePlusRecord *npr = 0;
1259             const char *addinfo;
1260
1261             int sur_error = ZOOM_record_error(recs[i], 0 /* msg */,
1262                                               &addinfo, 0 /* diagset */);
1263                 
1264             if (sur_error)
1265             {
1266                 log_diagnostic(package, sur_error, addinfo);
1267                 npr = zget_surrogateDiagRec(odr, odr_database, sur_error,
1268                                             addinfo);
1269             }
1270             else if (enable_pz2_retrieval)
1271             {
1272                 char rec_type_str[100];
1273                 const char *record_encoding = 0;
1274
1275                 if (b->sptr->record_encoding.length())
1276                     record_encoding = b->sptr->record_encoding.c_str();
1277                 else if (assume_marc8_charset)
1278                     record_encoding = "marc8";
1279
1280                 strcpy(rec_type_str, b->sptr->use_turbomarc ? "txml" : "xml");
1281                 if (record_encoding)
1282                 {
1283                     strcat(rec_type_str, "; charset=");
1284                     strcat(rec_type_str, record_encoding);
1285                 }
1286
1287                 package.log("zoom", YLOG_LOG, "Getting record of type %s",
1288                             rec_type_str);
1289                 int rec_len;
1290                 xmlChar *xmlrec_buf = 0;
1291                 const char *rec_buf = ZOOM_record_get(recs[i], rec_type_str,
1292                                                       &rec_len);
1293                 if (!rec_buf && !npr)
1294                 {
1295                     std::string addinfo("ZOOM_record_get failed for type ");
1296
1297                     int error = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
1298                     addinfo += rec_type_str;
1299                     log_diagnostic(package, error, addinfo.c_str());
1300                     npr = zget_surrogateDiagRec(odr, odr_database,
1301                                                 error, addinfo.c_str());
1302                 }
1303                 else
1304                 {
1305                     package.log_write(rec_buf, rec_len);
1306                     package.log_write("\r\n", 2);
1307                 }
1308
1309                 if (rec_buf && b->xsp && enable_pz2_transform)
1310                 {
1311                     xmlDoc *rec_doc = xmlParseMemory(rec_buf, rec_len);
1312                     if (!rec_doc)
1313                     {
1314                         const char *addinfo = "xml parse failed for record";
1315                         int error = YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
1316                         log_diagnostic(package, error, addinfo);
1317                         npr = zget_surrogateDiagRec(
1318                             odr, odr_database, error, addinfo);
1319                     }
1320                     else
1321                     { 
1322                         xmlDoc *rec_res = 
1323                             xsltApplyStylesheet(b->xsp, rec_doc, 0);
1324
1325                         if (rec_res)
1326                         {
1327                             xsltSaveResultToString(&xmlrec_buf, &rec_len,
1328                                                    rec_res, b->xsp);
1329                             rec_buf = (const char *) xmlrec_buf;
1330                             package.log("zoom", YLOG_LOG, "xslt successful");
1331                             package.log_write(rec_buf, rec_len);
1332
1333                             xmlFreeDoc(rec_res);
1334                         }
1335                         if (!rec_buf)
1336                         {
1337                             std::string addinfo;
1338                             int error =
1339                                 YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS;
1340
1341                             addinfo = "xslt apply failed for "
1342                                 + b->sptr->transform_xsl_fname;
1343                             log_diagnostic(package, error, addinfo.c_str());
1344                             npr = zget_surrogateDiagRec(
1345                                 odr, odr_database, error, addinfo.c_str());
1346                         }
1347                         xmlFreeDoc(rec_doc);
1348                     }
1349                 }
1350
1351                 if (rec_buf && b->enable_cproxy)
1352                 {
1353                     xmlDoc *doc = xmlParseMemory(rec_buf, rec_len);
1354                     std::string res = 
1355                         mp::xml::url_recipe_handle(doc, b->sptr->urlRecipe);
1356                     if (res.length() && b->content_session_id.length())
1357                     {
1358                         size_t off = res.find_first_of("://");
1359                         if (off != std::string::npos)
1360                         {
1361                             char tmp[1024];
1362                             sprintf(tmp, "%s.%s/",
1363                                     b->content_session_id.c_str(),
1364                                     m_p->content_proxy_server.c_str());
1365                             res.insert(off + 3, tmp);
1366                         }
1367                     }
1368                     if (res.length())
1369                     {
1370                         xmlNode *ptr = xmlDocGetRootElement(doc);
1371                         while (ptr && ptr->type != XML_ELEMENT_NODE)
1372                             ptr = ptr->next;
1373                         xmlNode *c = 
1374                             xmlNewChild(ptr, 0, BAD_CAST "metadata", 0);
1375                         xmlNewProp(c, BAD_CAST "type", BAD_CAST
1376                                    "generated-url");
1377                         xmlNode * t = xmlNewText(BAD_CAST res.c_str());
1378                         xmlAddChild(c, t);
1379
1380                         if (xmlrec_buf)
1381                             xmlFree(xmlrec_buf);
1382
1383                         xmlDocDumpMemory(doc, &xmlrec_buf, &rec_len);
1384                         rec_buf = (const char *) xmlrec_buf;
1385                     }
1386                     xmlFreeDoc(doc);
1387                 }
1388                 if (!npr)
1389                 {
1390                     if (!rec_buf)
1391                         npr = zget_surrogateDiagRec(
1392                             odr, odr_database, 
1393                             YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS,
1394                             rec_type_str);
1395                     else
1396                     {
1397                         npr = (Z_NamePlusRecord *)
1398                             odr_malloc(odr, sizeof(*npr));
1399                         npr->databaseName = odr_database;
1400                         npr->which = Z_NamePlusRecord_databaseRecord;
1401                         npr->u.databaseRecord =
1402                             z_ext_record_xml(odr, rec_buf, rec_len);
1403                     }
1404                 }
1405                 if (xmlrec_buf)
1406                     xmlFree(xmlrec_buf);
1407             }
1408             else
1409             {
1410                 Z_External *ext =
1411                     (Z_External *) ZOOM_record_get(recs[i], "ext", 0);
1412                 if (ext)
1413                 {
1414                     npr = (Z_NamePlusRecord *) odr_malloc(odr, sizeof(*npr));
1415                     npr->databaseName = odr_database;
1416                     npr->which = Z_NamePlusRecord_databaseRecord;
1417                     npr->u.databaseRecord = ext;
1418                 }
1419                 else
1420                 {
1421                     npr = zget_surrogateDiagRec(
1422                         odr, odr_database, 
1423                         YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS,
1424                         "ZOOM_record, type ext");
1425                 }
1426             }
1427             npl->records[i] = npr;
1428         }
1429         records = (Z_Records*) odr_malloc(odr, sizeof(*records));
1430         records->which = Z_Records_DBOSD;
1431         records->u.databaseOrSurDiagnostics = npl;
1432     }
1433     return records;
1434 }
1435
1436 struct cql_node *yf::Zoom::Impl::convert_cql_fields(struct cql_node *cn,
1437                                                     ODR odr)
1438 {
1439     struct cql_node *r = 0;
1440     if (!cn)
1441         return 0;
1442     switch (cn->which)
1443     {
1444     case CQL_NODE_ST:
1445         if (cn->u.st.index)
1446         {
1447             std::map<std::string,std::string>::const_iterator it;
1448             it = fieldmap.find(cn->u.st.index);
1449             if (it == fieldmap.end())
1450                 return cn;
1451             if (it->second.length())
1452                 cn->u.st.index = odr_strdup(odr, it->second.c_str());
1453             else
1454                 cn->u.st.index = 0;
1455         }
1456         break;
1457     case CQL_NODE_BOOL:
1458         r = convert_cql_fields(cn->u.boolean.left, odr);
1459         if (!r)
1460             r = convert_cql_fields(cn->u.boolean.right, odr);
1461         break;
1462     case CQL_NODE_SORT:
1463         r = convert_cql_fields(cn->u.sort.search, odr);
1464         break;
1465     }
1466     return r;
1467 }
1468
1469 void yf::Zoom::Frontend::log_diagnostic(mp::Package &package,
1470                                         int error, const char *addinfo)
1471 {
1472     const char *err_msg = yaz_diag_bib1_str(error);
1473     if (addinfo)
1474         package.log("zoom", YLOG_WARN, "Diagnostic %d %s: %s",
1475                     error, err_msg, addinfo);
1476     else
1477         package.log("zoom", YLOG_WARN, "Diagnostic %d %s:",
1478                     error, err_msg);
1479 }
1480
1481 yf::Zoom::BackendPtr yf::Zoom::Frontend::explain_search(mp::Package &package,
1482                                                         std::string &database,
1483                                                         int *error,
1484                                                         char **addinfo,
1485                                                         ODR odr,
1486                                                         std::string &torus_db,
1487                                                         std::string &realm)
1488 {
1489     m_backend.reset();
1490
1491     BackendPtr b(new Backend);
1492
1493     b->m_frontend_database = database;
1494     b->enable_explain = true;
1495    
1496     Z_GDU *gdu = package.request().get();
1497     Z_APDU *apdu_req = gdu->u.z3950;
1498     Z_APDU *apdu_res = 0;
1499     Z_SearchRequest *sr = apdu_req->u.searchRequest;
1500     Z_Query *query = sr->query;
1501
1502     if (query->which == Z_Query_type_104 &&
1503         query->u.type_104->which == Z_External_CQL)
1504     {
1505         std::string torus_url = m_p->torus_searchable_url;
1506         std::string torus_query(query->u.type_104->u.cql);
1507         xmlDoc *doc = mp::get_searchable(package, torus_url, "",
1508                                          torus_query,
1509                                          realm, m_p->proxy);
1510         if (m_p->explain_xsp)
1511         {
1512             xmlDoc *rec_res =  xsltApplyStylesheet(m_p->explain_xsp, doc, 0);
1513
1514             xmlFreeDoc(doc);
1515             doc = rec_res;
1516         }
1517         *error = YAZ_BIB1_QUERY_TYPE_UNSUPP;
1518         *addinfo = odr_strdup(odr, "CQL, IR-Explain---1");
1519
1520         xmlChar *buf_out = 0;
1521         int len_out;
1522         xmlDocDumpMemory(doc, &buf_out, &len_out);
1523
1524         fwrite(buf_out, 1, len_out, yaz_log_file());
1525         
1526         xmlFree(buf_out);
1527         if (b->explain_doc)
1528             xmlFreeDoc(b->explain_doc);
1529         b->explain_doc = doc;
1530         return m_backend;
1531     }
1532     else
1533     {
1534         *error = YAZ_BIB1_QUERY_TYPE_UNSUPP;
1535         *addinfo = odr_strdup(odr, "RPN/CCL, IR-Explain---1");
1536         return m_backend;
1537     }
1538 }
1539
1540 void yf::Zoom::Frontend::handle_search(mp::Package &package)
1541 {
1542     Z_GDU *gdu = package.request().get();
1543     Z_APDU *apdu_req = gdu->u.z3950;
1544     Z_APDU *apdu_res = 0;
1545     mp::odr odr;
1546     Z_SearchRequest *sr = apdu_req->u.searchRequest;
1547     if (sr->num_databaseNames != 1)
1548     {
1549         int error = YAZ_BIB1_TOO_MANY_DATABASES_SPECIFIED;
1550         log_diagnostic(package, error, 0);
1551         apdu_res = odr.create_searchResponse(apdu_req, error, 0);
1552         package.response() = apdu_res;
1553         return;
1554     }
1555     int proxy_step = 0;
1556
1557 next_proxy:
1558
1559     int error = 0;
1560     char *addinfo = 0;
1561     std::string db(sr->databaseNames[0]);
1562
1563     BackendPtr b = get_backend_from_databases(package, db, &error,
1564                                               &addinfo, odr, &proxy_step);
1565     if (error && proxy_step)
1566     {
1567         package.log("zoom", YLOG_WARN,
1568                     "create backend failed: trying next proxy");
1569         goto next_proxy;
1570     }
1571     if (error)
1572     {
1573         log_diagnostic(package, error, addinfo);
1574         apdu_res = odr.create_searchResponse(apdu_req, error, addinfo);
1575         package.response() = apdu_res;
1576         return;
1577     }
1578     if (b->enable_explain)
1579         return;
1580
1581     b->set_option("setname", "default");
1582
1583     bool enable_pz2_retrieval = false;
1584     bool enable_pz2_transform = false;
1585     bool assume_marc8_charset = false;
1586     prepare_elements(b, sr->preferredRecordSyntax, 0 /*element_set_name */,
1587                      enable_pz2_retrieval,
1588                      enable_pz2_transform,
1589                      assume_marc8_charset);
1590
1591     Odr_int hits = 0;
1592     Z_Query *query = sr->query;
1593     WRBUF ccl_wrbuf = 0;
1594     WRBUF pqf_wrbuf = 0;
1595     std::string sortkeys;
1596
1597     if (query->which == Z_Query_type_1 || query->which == Z_Query_type_101)
1598     {
1599         // RPN
1600         pqf_wrbuf = wrbuf_alloc();
1601         yaz_rpnquery_to_wrbuf(pqf_wrbuf, query->u.type_1);
1602     }
1603     else if (query->which == Z_Query_type_2)
1604     {
1605         // CCL
1606         ccl_wrbuf = wrbuf_alloc();
1607         wrbuf_write(ccl_wrbuf, (const char *) query->u.type_2->buf,
1608                     query->u.type_2->len);
1609     }
1610     else if (query->which == Z_Query_type_104 &&
1611              query->u.type_104->which == Z_External_CQL)
1612     {
1613         // CQL
1614         const char *cql = query->u.type_104->u.cql;
1615         CQL_parser cp = cql_parser_create();
1616         int r = cql_parser_string(cp, cql);
1617         package.log("zoom", YLOG_LOG, "CQL: %s", cql);
1618         if (r)
1619         {
1620             cql_parser_destroy(cp);
1621             error = YAZ_BIB1_MALFORMED_QUERY;
1622             const char *addinfo = "CQL syntax error";
1623             log_diagnostic(package, error, addinfo);
1624             apdu_res = 
1625                 odr.create_searchResponse(apdu_req, error, addinfo);
1626             package.response() = apdu_res;
1627             return;
1628         }
1629         struct cql_node *cn = cql_parser_result(cp);
1630         struct cql_node *cn_error = m_p->convert_cql_fields(cn, odr);
1631         if (cn_error)
1632         {
1633             // hopefully we are getting a ptr to a index+relation+term node
1634             error = YAZ_BIB1_UNSUPP_USE_ATTRIBUTE;
1635             addinfo = 0;
1636             if (cn_error->which == CQL_NODE_ST)
1637                 addinfo = cn_error->u.st.index;
1638             
1639             log_diagnostic(package, error, addinfo);
1640             apdu_res = odr.create_searchResponse(apdu_req, error, addinfo);
1641             package.response() = apdu_res;
1642             cql_parser_destroy(cp);
1643             return;
1644         }
1645         char ccl_buf[1024];
1646         r = cql_to_ccl_buf(cn, ccl_buf, sizeof(ccl_buf));
1647         if (r)
1648         {
1649             error = YAZ_BIB1_MALFORMED_QUERY;
1650             const char *addinfo = "CQL to CCL conversion error";
1651
1652             log_diagnostic(package, error, addinfo);
1653             apdu_res = odr.create_searchResponse(apdu_req, error, addinfo);
1654             package.response() = apdu_res;
1655             cql_parser_destroy(cp);
1656             return;
1657         }
1658
1659         WRBUF sru_sortkeys_wrbuf = wrbuf_alloc();
1660         if (cql_sortby_to_sortkeys(cn, wrbuf_vp_puts, sru_sortkeys_wrbuf))
1661         {
1662             error = YAZ_BIB1_ILLEGAL_SORT_RELATION;
1663             const char *addinfo = "CQL to CCL sortby conversion";
1664
1665             log_diagnostic(package, error, addinfo);
1666             apdu_res = odr.create_searchResponse(apdu_req, error, addinfo);
1667             package.response() = apdu_res;
1668             wrbuf_destroy(sru_sortkeys_wrbuf);
1669             cql_parser_destroy(cp);
1670             return;
1671         }
1672         WRBUF sort_spec_wrbuf = wrbuf_alloc();
1673         yaz_srw_sortkeys_to_sort_spec(wrbuf_cstr(sru_sortkeys_wrbuf),
1674                                       sort_spec_wrbuf);
1675         wrbuf_destroy(sru_sortkeys_wrbuf);
1676
1677         ccl_wrbuf = wrbuf_alloc();
1678         wrbuf_puts(ccl_wrbuf, ccl_buf);
1679         
1680         yaz_tok_cfg_t tc = yaz_tok_cfg_create();
1681         yaz_tok_parse_t tp =
1682             yaz_tok_parse_buf(tc, wrbuf_cstr(sort_spec_wrbuf));
1683         yaz_tok_cfg_destroy(tc);
1684         
1685         /* go through sortspec and map fields */
1686         int token = yaz_tok_move(tp);
1687         while (token != YAZ_TOK_EOF)
1688         {
1689             if (token == YAZ_TOK_STRING)
1690             {
1691                 const char *field = yaz_tok_parse_string(tp);
1692                 std::map<std::string,std::string>::iterator it;
1693                 it = b->sptr->sortmap.find(field);
1694                 if (it != b->sptr->sortmap.end())
1695                     sortkeys += it->second;
1696                 else
1697                     sortkeys += field;
1698             }
1699             sortkeys += " ";
1700             token = yaz_tok_move(tp);
1701             if (token == YAZ_TOK_STRING)
1702             {
1703                 sortkeys += yaz_tok_parse_string(tp);
1704             }
1705             if (token != YAZ_TOK_EOF)
1706             {
1707                 sortkeys += " ";
1708                 token = yaz_tok_move(tp);
1709             }
1710         }
1711         yaz_tok_parse_destroy(tp);
1712         wrbuf_destroy(sort_spec_wrbuf);
1713
1714         cql_parser_destroy(cp);
1715     }
1716     else
1717     {
1718         error = YAZ_BIB1_QUERY_TYPE_UNSUPP;
1719         const char *addinfo = 0;
1720         log_diagnostic(package, error, addinfo);
1721         apdu_res =  odr.create_searchResponse(apdu_req, error, addinfo);
1722         package.response() = apdu_res;
1723         return;
1724     }
1725
1726     if (ccl_wrbuf)
1727     {
1728         // CCL to PQF
1729         assert(pqf_wrbuf == 0);
1730         int cerror, cpos;
1731         struct ccl_rpn_node *cn;
1732         package.log("zoom", YLOG_LOG, "CCL: %s", wrbuf_cstr(ccl_wrbuf));
1733         cn = ccl_find_str(b->sptr->ccl_bibset, wrbuf_cstr(ccl_wrbuf),
1734                           &cerror, &cpos);
1735         wrbuf_destroy(ccl_wrbuf);
1736         if (!cn)
1737         {
1738             char *addinfo = odr_strdup(odr, ccl_err_msg(cerror));
1739             error = YAZ_BIB1_MALFORMED_QUERY;
1740
1741             switch (cerror)
1742             {
1743             case CCL_ERR_UNKNOWN_QUAL:
1744                 error = YAZ_BIB1_UNSUPP_USE_ATTRIBUTE;
1745                 break;
1746             case CCL_ERR_TRUNC_NOT_LEFT: 
1747             case CCL_ERR_TRUNC_NOT_RIGHT:
1748             case CCL_ERR_TRUNC_NOT_BOTH:
1749                 error = YAZ_BIB1_UNSUPP_TRUNCATION_ATTRIBUTE;
1750                 break;
1751             }
1752             log_diagnostic(package, error, addinfo);
1753             apdu_res = odr.create_searchResponse(apdu_req, error, addinfo);
1754             package.response() = apdu_res;
1755             return;
1756         }
1757         pqf_wrbuf = wrbuf_alloc();
1758         ccl_pquery(pqf_wrbuf, cn);
1759         package.log("zoom", YLOG_LOG, "RPN: %s", wrbuf_cstr(pqf_wrbuf));
1760         ccl_rpn_delete(cn);
1761     }
1762     
1763     assert(pqf_wrbuf);
1764
1765     ZOOM_query q = ZOOM_query_create();
1766     ZOOM_query_sortby2(q, b->sptr->sortStrategy.c_str(), sortkeys.c_str());
1767
1768     if (b->get_option("sru"))
1769     {
1770         int status = 0;
1771         Z_RPNQuery *zquery;
1772         zquery = p_query_rpn(odr, wrbuf_cstr(pqf_wrbuf));
1773         WRBUF wrb = wrbuf_alloc();
1774             
1775         if (!strcmp(b->get_option("sru"), "solr"))
1776         {
1777             solr_transform_t cqlt = solr_transform_create();
1778             
1779             status = solr_transform_rpn2solr_wrbuf(cqlt, wrb, zquery);
1780             
1781             solr_transform_close(cqlt);
1782         }
1783         else
1784         {
1785             cql_transform_t cqlt = cql_transform_create();
1786             
1787             status = cql_transform_rpn2cql_wrbuf(cqlt, wrb, zquery);
1788             
1789             cql_transform_close(cqlt);
1790         }
1791         if (status == 0)
1792         {
1793             ZOOM_query_cql(q, wrbuf_cstr(wrb));
1794             package.log("zoom", YLOG_LOG, "CQL: %s", wrbuf_cstr(wrb));
1795             b->search(q, &hits, &error, &addinfo, odr);
1796         }
1797         ZOOM_query_destroy(q);
1798         
1799         wrbuf_destroy(wrb);
1800         wrbuf_destroy(pqf_wrbuf);
1801         if (status)
1802         {
1803             error = YAZ_BIB1_MALFORMED_QUERY;
1804             const char *addinfo = "can not convert from RPN to CQL/SOLR";
1805             log_diagnostic(package, error, addinfo);
1806             apdu_res = odr.create_searchResponse(apdu_req, error, addinfo);
1807             package.response() = apdu_res;
1808             return;
1809         }
1810     }
1811     else
1812     {
1813         ZOOM_query_prefix(q, wrbuf_cstr(pqf_wrbuf));
1814         package.log("zoom", YLOG_LOG, "search PQF: %s", wrbuf_cstr(pqf_wrbuf));
1815         b->search(q, &hits, &error, &addinfo, odr);
1816         ZOOM_query_destroy(q);
1817         wrbuf_destroy(pqf_wrbuf);
1818     }
1819
1820     if (error && proxy_step)
1821     {
1822         // reset below prevent reuse in get_backend_from_databases
1823         m_backend.reset();
1824         package.log("zoom", YLOG_WARN, "search failed: trying next proxy");
1825         goto next_proxy;
1826     }
1827
1828     const char *element_set_name = 0;
1829     Odr_int number_to_present = 0;
1830     if (!error)
1831         mp::util::piggyback_sr(sr, hits, number_to_present, &element_set_name);
1832     
1833     Odr_int number_of_records_returned = 0;
1834     Z_Records *records = get_records(
1835         package,
1836         0, number_to_present, &error, &addinfo,
1837         &number_of_records_returned, odr, b, sr->preferredRecordSyntax,
1838         element_set_name);
1839     if (error)
1840         log_diagnostic(package, error, addinfo);
1841     apdu_res = odr.create_searchResponse(apdu_req, error, addinfo);
1842     if (records)
1843     {
1844         apdu_res->u.searchResponse->records = records;
1845         apdu_res->u.searchResponse->numberOfRecordsReturned =
1846             odr_intdup(odr, number_of_records_returned);
1847     }
1848     apdu_res->u.searchResponse->resultCount = odr_intdup(odr, hits);
1849     package.response() = apdu_res;
1850 }
1851
1852 void yf::Zoom::Frontend::handle_present(mp::Package &package)
1853 {
1854     Z_GDU *gdu = package.request().get();
1855     Z_APDU *apdu_req = gdu->u.z3950;
1856     Z_APDU *apdu_res = 0;
1857     Z_PresentRequest *pr = apdu_req->u.presentRequest;
1858
1859     mp::odr odr;
1860     if (!m_backend)
1861     {
1862         package.response() = odr.create_presentResponse(
1863             apdu_req, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST, 0);
1864         return;
1865     }
1866     const char *element_set_name = 0;
1867     Z_RecordComposition *comp = pr->recordComposition;
1868     if (comp && comp->which != Z_RecordComp_simple)
1869     {
1870         package.response() = odr.create_presentResponse(
1871             apdu_req, 
1872             YAZ_BIB1_PRESENT_COMP_SPEC_PARAMETER_UNSUPP, 0);
1873         return;
1874     }
1875     if (comp && comp->u.simple->which == Z_ElementSetNames_generic)
1876         element_set_name = comp->u.simple->u.generic;
1877     Odr_int number_of_records_returned = 0;
1878     int error = 0;
1879     char *addinfo = 0;
1880
1881     if (m_backend->enable_explain)
1882     {
1883         package.response() = odr.create_presentResponse(
1884             apdu_req, YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS,
1885             "IR-Explain---1 fetch not implemented");
1886         return;
1887     }
1888     else
1889     {
1890         Z_Records *records =
1891             get_records(package,
1892                         *pr->resultSetStartPoint - 1, *pr->numberOfRecordsRequested,
1893                         &error, &addinfo, &number_of_records_returned, odr, m_backend,
1894                         pr->preferredRecordSyntax, element_set_name);
1895         
1896         apdu_res = odr.create_presentResponse(apdu_req, error, addinfo);
1897         if (records)
1898         {
1899             apdu_res->u.presentResponse->records = records;
1900             apdu_res->u.presentResponse->numberOfRecordsReturned =
1901                 odr_intdup(odr, number_of_records_returned);
1902         }
1903         package.response() = apdu_res;
1904     }
1905 }
1906
1907 void yf::Zoom::Frontend::handle_package(mp::Package &package)
1908 {
1909     Z_GDU *gdu = package.request().get();
1910     if (!gdu)
1911         ;
1912     else if (gdu->which == Z_GDU_Z3950)
1913     {
1914         Z_APDU *apdu_req = gdu->u.z3950;
1915
1916         if (m_backend)
1917             wrbuf_rewind(m_backend->m_apdu_wrbuf);
1918         if (apdu_req->which == Z_APDU_initRequest)
1919         {
1920             mp::odr odr;
1921             package.response() = odr.create_close(
1922                 apdu_req,
1923                 Z_Close_protocolError,
1924                 "double init");
1925         }
1926         else if (apdu_req->which == Z_APDU_searchRequest)
1927         {
1928             handle_search(package);
1929         }
1930         else if (apdu_req->which == Z_APDU_presentRequest)
1931         {
1932             handle_present(package);
1933         }
1934         else
1935         {
1936             mp::odr odr;
1937             package.response() = odr.create_close(
1938                 apdu_req,
1939                 Z_Close_protocolError,
1940                 "zoom filter cannot handle this APDU");
1941             package.session().close();
1942         }
1943         if (m_backend)
1944         {
1945             WRBUF w = m_backend->m_apdu_wrbuf;
1946             package.log_write(wrbuf_buf(w), wrbuf_len(w));
1947         }
1948     }
1949     else
1950     {
1951         package.session().close();
1952     }
1953 }
1954
1955 void yf::Zoom::Impl::process(mp::Package &package)
1956 {
1957     FrontendPtr f = get_frontend(package);
1958     Z_GDU *gdu = package.request().get();
1959
1960     if (f->m_is_virtual)
1961     {
1962         f->handle_package(package);
1963     }
1964     else if (gdu && gdu->which == Z_GDU_Z3950 && gdu->u.z3950->which ==
1965              Z_APDU_initRequest)
1966     {
1967         Z_InitRequest *req = gdu->u.z3950->u.initRequest;
1968         f->m_init_gdu = gdu;
1969         
1970         mp::odr odr;
1971         Z_APDU *apdu = odr.create_initResponse(gdu->u.z3950, 0, 0);
1972         Z_InitResponse *resp = apdu->u.initResponse;
1973         
1974         int i;
1975         static const int masks[] = {
1976             Z_Options_search,
1977             Z_Options_present,
1978             -1 
1979         };
1980         for (i = 0; masks[i] != -1; i++)
1981             if (ODR_MASK_GET(req->options, masks[i]))
1982                 ODR_MASK_SET(resp->options, masks[i]);
1983         
1984         static const int versions[] = {
1985             Z_ProtocolVersion_1,
1986             Z_ProtocolVersion_2,
1987             Z_ProtocolVersion_3,
1988             -1
1989         };
1990         for (i = 0; versions[i] != -1; i++)
1991             if (ODR_MASK_GET(req->protocolVersion, versions[i]))
1992                 ODR_MASK_SET(resp->protocolVersion, versions[i]);
1993             else
1994                 break;
1995         
1996         *resp->preferredMessageSize = *req->preferredMessageSize;
1997         *resp->maximumRecordSize = *req->maximumRecordSize;
1998         
1999         package.response() = apdu;
2000         f->m_is_virtual = true;
2001     }
2002     else
2003         package.move();
2004
2005     release_frontend(package);
2006 }
2007
2008
2009 static mp::filter::Base* filter_creator()
2010 {
2011     return new mp::filter::Zoom;
2012 }
2013
2014 extern "C" {
2015     struct metaproxy_1_filter_struct metaproxy_1_filter_zoom = {
2016         0,
2017         "zoom",
2018         filter_creator
2019     };
2020 }
2021
2022
2023 /*
2024  * Local variables:
2025  * c-basic-offset: 4
2026  * c-file-style: "Stroustrup"
2027  * indent-tabs-mode: nil
2028  * End:
2029  * vim: shiftwidth=4 tabstop=8 expandtab
2030  */
2031