c7c882faba2e83ba5e847900dbc33e58dbd1bcf1
[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/srw.h>
23 #include <metaproxy/package.hpp>
24 #include <metaproxy/util.hpp>
25 #include "torus.hpp"
26
27 #include <libxslt/xsltutils.h>
28 #include <libxslt/transform.h>
29
30 #include <boost/thread/mutex.hpp>
31 #include <boost/thread/condition.hpp>
32 #include <yaz/ccl.h>
33 #include <yaz/cql.h>
34 #include <yaz/oid_db.h>
35 #include <yaz/diagbib1.h>
36 #include <yaz/log.h>
37 #include <yaz/zgdu.h>
38 #include <yaz/querytowrbuf.h>
39
40 namespace mp = metaproxy_1;
41 namespace yf = mp::filter;
42
43 namespace metaproxy_1 {
44     namespace filter {
45         struct Zoom::Searchable : boost::noncopyable {
46             std::string authentication;
47             std::string cfAuth;
48             std::string cfProxy;
49             std::string cfSubDb;
50             std::string database;
51             std::string target;
52             std::string query_encoding;
53             std::string sru;
54             std::string request_syntax;
55             std::string element_set;
56             std::string record_encoding;
57             std::string transform_xsl_fname;
58             bool use_turbomarc;
59             bool piggyback;
60             CCL_bibset ccl_bibset;
61             Searchable();
62             ~Searchable();
63         };
64         class Zoom::Backend : boost::noncopyable {
65             friend class Impl;
66             friend class Frontend;
67             std::string zurl;
68             ZOOM_connection m_connection;
69             ZOOM_resultset m_resultset;
70             std::string m_frontend_database;
71             SearchablePtr sptr;
72             xsltStylesheetPtr xsp;
73         public:
74             Backend(SearchablePtr sptr);
75             ~Backend();
76             void connect(std::string zurl, int *error, const char **addinfo);
77             void search_pqf(const char *pqf, Odr_int *hits,
78                             int *error, const char **addinfo);
79             void present(Odr_int start, Odr_int number, ZOOM_record *recs,
80                          int *error, const char **addinfo);
81             void set_option(const char *name, const char *value);
82             int get_error(const char **addinfo);
83         };
84         class Zoom::Frontend : boost::noncopyable {
85             friend class Impl;
86             Impl *m_p;
87             bool m_is_virtual;
88             bool m_in_use;
89             yazpp_1::GDU m_init_gdu;
90             BackendPtr m_backend;
91             void handle_package(mp::Package &package);
92             void handle_search(mp::Package &package);
93             void handle_present(mp::Package &package);
94             BackendPtr get_backend_from_databases(std::string &database,
95                                                   int *error,
96                                                   const char **addinfo);
97             Z_Records *get_records(Odr_int start,
98                                    Odr_int number_to_present,
99                                    int *error,
100                                    const char **addinfo,
101                                    Odr_int *number_of_records_returned,
102                                    ODR odr, BackendPtr b,
103                                    Odr_oid *preferredRecordSyntax,
104                                    const char *element_set_name);
105         public:
106             Frontend(Impl *impl);
107             ~Frontend();
108         };
109         class Zoom::Impl {
110             friend class Frontend;
111         public:
112             Impl();
113             ~Impl();
114             void process(metaproxy_1::Package & package);
115             void configure(const xmlNode * ptr, bool test_only);
116         private:
117             FrontendPtr get_frontend(mp::Package &package);
118             void release_frontend(mp::Package &package);
119             void parse_torus(const xmlNode *ptr);
120
121             std::list<Zoom::SearchablePtr>m_searchables;
122
123             std::map<mp::Session, FrontendPtr> m_clients;            
124             boost::mutex m_mutex;
125             boost::condition m_cond_session_ready;
126             mp::Torus torus;
127         };
128     }
129 }
130
131 // define Pimpl wrapper forwarding to Impl
132  
133 yf::Zoom::Zoom() : m_p(new Impl)
134 {
135 }
136
137 yf::Zoom::~Zoom()
138 {  // must have a destructor because of boost::scoped_ptr
139 }
140
141 void yf::Zoom::configure(const xmlNode *xmlnode, bool test_only)
142 {
143     m_p->configure(xmlnode, test_only);
144 }
145
146 void yf::Zoom::process(mp::Package &package) const
147 {
148     m_p->process(package);
149 }
150
151
152 // define Implementation stuff
153
154 yf::Zoom::Backend::Backend(SearchablePtr ptr) : sptr(ptr)
155 {
156     m_connection = ZOOM_connection_create(0);
157     m_resultset = 0;
158     xsp = 0;
159 }
160
161 yf::Zoom::Backend::~Backend()
162 {
163     if (xsp)
164         xsltFreeStylesheet(xsp);
165     ZOOM_connection_destroy(m_connection);
166     ZOOM_resultset_destroy(m_resultset);
167 }
168
169 void yf::Zoom::Backend::connect(std::string zurl,
170                                 int *error, const char **addinfo)
171 {
172     ZOOM_connection_connect(m_connection, zurl.c_str(), 0);
173     *error = ZOOM_connection_error(m_connection, 0, addinfo);
174 }
175
176 void yf::Zoom::Backend::search_pqf(const char *pqf, Odr_int *hits,
177                                    int *error, const char **addinfo)
178 {
179     m_resultset = ZOOM_connection_search_pqf(m_connection, pqf);
180     *error = ZOOM_connection_error(m_connection, 0, addinfo);
181     if (*error == 0)
182         *hits = ZOOM_resultset_size(m_resultset);
183     else
184         *hits = 0;
185 }
186
187 void yf::Zoom::Backend::present(Odr_int start, Odr_int number,
188                                 ZOOM_record *recs,
189                                 int *error, const char **addinfo)
190 {
191     ZOOM_resultset_records(m_resultset, recs, start, number);
192     *error = ZOOM_connection_error(m_connection, 0, addinfo);
193 }
194
195 void yf::Zoom::Backend::set_option(const char *name, const char *value)
196 {
197     ZOOM_connection_option_set(m_connection, name, value);
198     if (m_resultset)
199         ZOOM_resultset_option_set(m_resultset, name, value);
200 }
201
202 int yf::Zoom::Backend::get_error(const char **addinfo)
203 {
204     return ZOOM_connection_error(m_connection, 0, addinfo);
205 }
206
207 yf::Zoom::Searchable::Searchable()
208 {
209     piggyback = true;
210     use_turbomarc = true;
211     ccl_bibset = ccl_qual_mk();
212 }
213
214 yf::Zoom::Searchable::~Searchable()
215 {
216     ccl_qual_rm(&ccl_bibset);
217 }
218
219 yf::Zoom::Frontend::Frontend(Impl *impl) : 
220     m_p(impl), m_is_virtual(false), m_in_use(true)
221 {
222 }
223
224 yf::Zoom::Frontend::~Frontend()
225 {
226 }
227
228 yf::Zoom::FrontendPtr yf::Zoom::Impl::get_frontend(mp::Package &package)
229 {
230     boost::mutex::scoped_lock lock(m_mutex);
231
232     std::map<mp::Session,yf::Zoom::FrontendPtr>::iterator it;
233     
234     while(true)
235     {
236         it = m_clients.find(package.session());
237         if (it == m_clients.end())
238             break;
239         
240         if (!it->second->m_in_use)
241         {
242             it->second->m_in_use = true;
243             return it->second;
244         }
245         m_cond_session_ready.wait(lock);
246     }
247     FrontendPtr f(new Frontend(this));
248     m_clients[package.session()] = f;
249     f->m_in_use = true;
250     return f;
251 }
252
253 void yf::Zoom::Impl::release_frontend(mp::Package &package)
254 {
255     boost::mutex::scoped_lock lock(m_mutex);
256     std::map<mp::Session,yf::Zoom::FrontendPtr>::iterator it;
257     
258     it = m_clients.find(package.session());
259     if (it != m_clients.end())
260     {
261         if (package.session().is_closed())
262         {
263             m_clients.erase(it);
264         }
265         else
266         {
267             it->second->m_in_use = false;
268         }
269         m_cond_session_ready.notify_all();
270     }
271 }
272
273 yf::Zoom::Impl::Impl()
274 {
275 }
276
277 yf::Zoom::Impl::~Impl()
278
279 }
280
281 void yf::Zoom::Impl::parse_torus(const xmlNode *ptr1)
282 {
283     if (!ptr1)
284         return ;
285     for (ptr1 = ptr1->children; ptr1; ptr1 = ptr1->next)
286     {
287         if (ptr1->type != XML_ELEMENT_NODE)
288             continue;
289         if (!strcmp((const char *) ptr1->name, "record"))
290         {
291             const xmlNode *ptr2 = ptr1;
292             for (ptr2 = ptr2->children; ptr2; ptr2 = ptr2->next)
293             {
294                 if (ptr2->type != XML_ELEMENT_NODE)
295                     continue;
296                 if (!strcmp((const char *) ptr2->name, "layer"))
297                 {
298                     Zoom::SearchablePtr s(new Searchable);
299
300                     const xmlNode *ptr3 = ptr2;
301                     for (ptr3 = ptr3->children; ptr3; ptr3 = ptr3->next)
302                     {
303                         if (ptr3->type != XML_ELEMENT_NODE)
304                             continue;
305                         if (!strcmp((const char *) ptr3->name,
306                                     "authentication"))
307                         {
308                             s->authentication = mp::xml::get_text(ptr3);
309                         }
310                         else if (!strcmp((const char *) ptr3->name,
311                                     "cfAuth"))
312                         {
313                             s->cfAuth = mp::xml::get_text(ptr3);
314                         } 
315                         else if (!strcmp((const char *) ptr3->name,
316                                     "cfProxy"))
317                         {
318                             s->cfProxy = mp::xml::get_text(ptr3);
319                         }  
320                         else if (!strcmp((const char *) ptr3->name,
321                                     "cfSubDb"))
322                         {
323                             s->cfSubDb = mp::xml::get_text(ptr3);
324                         }  
325                         else if (!strcmp((const char *) ptr3->name, "id"))
326                         {
327                             s->database = mp::xml::get_text(ptr3);
328                         }
329                         else if (!strcmp((const char *) ptr3->name, "zurl"))
330                         {
331                             s->target = mp::xml::get_text(ptr3);
332                         }
333                         else if (!strcmp((const char *) ptr3->name, "sru"))
334                         {
335                             s->sru = mp::xml::get_text(ptr3);
336                         }
337                         else if (!strcmp((const char *) ptr3->name,
338                                          "queryEncoding"))
339                         {
340                             s->query_encoding = mp::xml::get_text(ptr3);
341                         }
342                         else if (!strcmp((const char *) ptr3->name,
343                                          "piggyback"))
344                         {
345                             s->piggyback = mp::xml::get_bool(ptr3, true);
346                         }
347                         else if (!strcmp((const char *) ptr3->name,
348                                          "requestSyntax"))
349                         {
350                             s->request_syntax = mp::xml::get_text(ptr3);
351                         }
352                         else if (!strcmp((const char *) ptr3->name,
353                                          "elementSet"))
354                         {
355                             s->element_set = mp::xml::get_text(ptr3);
356                         }
357                         else if (!strcmp((const char *) ptr3->name,
358                                          "recordEncoding"))
359                         {
360                             s->record_encoding = mp::xml::get_text(ptr3);
361                         }
362                         else if (!strcmp((const char *) ptr3->name,
363                                          "transform"))
364                         {
365                             s->transform_xsl_fname = mp::xml::get_text(ptr3);
366                         }
367                         else if (!strcmp((const char *) ptr3->name,
368                                          "useTurboMarc"))
369                         {
370                             ; // useTurboMarc is ignored
371                         }
372                         else if (!strncmp((const char *) ptr3->name,
373                                           "cclmap_", 7))
374                         {
375                             std::string value = mp::xml::get_text(ptr3);
376                             ccl_qual_fitem(s->ccl_bibset, value.c_str(),
377                                            (const char *) ptr3->name + 7);
378                         }
379                     }
380                     if (s->database.length() && s->target.length())
381                     {
382                         yaz_log(YLOG_LOG, "add db=%s target=%s turbomarc=%s", 
383                                 s->database.c_str(), s->target.c_str(),
384                                 s->use_turbomarc ? "1" : "0");
385                         m_searchables.push_back(s);
386                     }
387                 }
388             }
389         }
390     }
391 }
392
393 void yf::Zoom::Impl::configure(const xmlNode *ptr, bool test_only)
394 {
395     for (ptr = ptr->children; ptr; ptr = ptr->next)
396     {
397         if (ptr->type != XML_ELEMENT_NODE)
398             continue;
399         if (!strcmp((const char *) ptr->name, "records"))
400         {
401             parse_torus(ptr);
402         }
403         else if (!strcmp((const char *) ptr->name, "torus"))
404         {
405             std::string url;
406             const struct _xmlAttr *attr;
407             for (attr = ptr->properties; attr; attr = attr->next)
408             {
409                 if (!strcmp((const char *) attr->name, "url"))
410                     url = mp::xml::get_text(attr->children);
411                 else
412                     throw mp::filter::FilterException(
413                         "Bad attribute " + std::string((const char *)
414                                                        attr->name));
415             }
416             torus.read_searchables(url);
417             xmlDoc *doc = torus.get_doc();
418             if (doc)
419             {
420                 xmlNode *ptr = xmlDocGetRootElement(doc);
421                 parse_torus(ptr);
422             }
423         }
424         else
425         {
426             throw mp::filter::FilterException
427                 ("Bad element " 
428                  + std::string((const char *) ptr->name)
429                  + " in zoom filter");
430         }
431     }
432 }
433
434 static std::string uri_encode(std::string s)
435 {
436     char *x = (char *) xmalloc(1 + s.length() * 3);
437     yaz_encode_uri_component(x, s.c_str());
438     std::string result(x);
439     return result;
440 }
441
442 yf::Zoom::BackendPtr yf::Zoom::Frontend::get_backend_from_databases(
443     std::string &database, int *error, const char **addinfo)
444 {
445     std::list<BackendPtr>::const_iterator map_it;
446     if (m_backend && m_backend->m_frontend_database == database)
447         return m_backend;
448
449     std::list<Zoom::SearchablePtr>::iterator map_s =
450         m_p->m_searchables.begin();
451
452     std::string c_db = mp::util::database_name_normalize(database);
453
454     while (map_s != m_p->m_searchables.end())
455     {
456         if (c_db.compare((*map_s)->database) == 0)
457             break;
458         map_s++;
459     }
460     if (map_s == m_p->m_searchables.end())
461     {
462         *error = YAZ_BIB1_DATABASE_DOES_NOT_EXIST;
463         *addinfo = database.c_str();
464         BackendPtr b;
465         return b;
466     }
467
468     xsltStylesheetPtr xsp = 0;
469     if ((*map_s)->transform_xsl_fname.length())
470     {
471         xmlDoc *xsp_doc = xmlParseFile((*map_s)->transform_xsl_fname.c_str());
472         if (!xsp_doc)
473         {
474             *error = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
475             *addinfo = "xmlParseFile failed";
476             BackendPtr b;
477             return b;
478         }
479         xsp = xsltParseStylesheetDoc(xsp_doc);
480         if (!xsp)
481         {
482             *error = YAZ_BIB1_DATABASE_DOES_NOT_EXIST;
483             *addinfo = "xsltParseStylesheetDoc failed";
484             BackendPtr b;
485             xmlFreeDoc(xsp_doc);
486             return b;
487         }
488     }
489
490     SearchablePtr sptr = *map_s;
491
492     m_backend.reset();
493
494     BackendPtr b(new Backend(sptr));
495
496     std::string cf_parm;
497     b->xsp = xsp;
498     b->m_frontend_database = database;
499     std::string authentication = sptr->authentication;
500
501     if (sptr->query_encoding.length())
502         b->set_option("rpnCharset", sptr->query_encoding.c_str());
503
504     if (sptr->cfAuth.length())
505     {
506         b->set_option("user", sptr->cfAuth.c_str());
507         if (authentication.length())
508         {
509             size_t found = authentication.find('/');
510             if (found != std::string::npos)
511             {
512                 cf_parm += "user=" + uri_encode(authentication.substr(0, found))
513                     + "&password=" + uri_encode(authentication.substr(found+1));
514             }
515             else
516                 cf_parm += "user=" + uri_encode(authentication);
517         }
518     }
519     else if (authentication.length())
520         b->set_option("user", authentication.c_str());
521
522     if (sptr->cfProxy.length())
523     {
524         if (cf_parm.length())
525             cf_parm += "&";
526         cf_parm += "proxy=" + uri_encode(sptr->cfProxy);
527     }
528     if (sptr->cfSubDb.length())
529     {
530         if (cf_parm.length())
531             cf_parm += "&";
532         cf_parm += "subdatabase=" + uri_encode(sptr->cfSubDb);
533     }
534
535     std::string url;
536     if (sptr->sru.length())
537     {
538         url = "http://" + sptr->target;
539         b->set_option("sru", sptr->sru.c_str());
540     }
541     else
542     {
543         url = sptr->target;
544     }
545     if (cf_parm.length())
546     {
547         url += "," + cf_parm;
548     }
549     b->connect(url, error, addinfo);
550     if (*error == 0)
551     {
552         m_backend = b;
553     }
554     return b;
555 }
556
557 Z_Records *yf::Zoom::Frontend::get_records(Odr_int start,
558                                            Odr_int number_to_present,
559                                            int *error,
560                                            const char **addinfo,
561                                            Odr_int *number_of_records_returned,
562                                            ODR odr,
563                                            BackendPtr b,
564                                            Odr_oid *preferredRecordSyntax,
565                                            const char *element_set_name)
566 {
567     *number_of_records_returned = 0;
568     Z_Records *records = 0;
569     bool enable_pz2_transform = false;
570
571     if (start < 0 || number_to_present <= 0)
572         return records;
573     
574     if (number_to_present > 10000)
575         number_to_present = 10000;
576     
577     ZOOM_record *recs = (ZOOM_record *)
578         odr_malloc(odr, number_to_present * sizeof(*recs));
579
580     char oid_name_str[OID_STR_MAX];
581     const char *syntax_name = 0;
582
583     if (preferredRecordSyntax)
584     {
585         if (!oid_oidcmp(preferredRecordSyntax, yaz_oid_recsyn_xml)
586             && !strcmp(element_set_name, "pz2"))
587         {
588             if (b->sptr->request_syntax.length())
589             {
590                 syntax_name = b->sptr->request_syntax.c_str();
591                 enable_pz2_transform = true;
592             }
593         }
594         else
595         {
596             syntax_name =
597                 yaz_oid_to_string_buf(preferredRecordSyntax, 0, oid_name_str);
598         }
599     }
600
601     b->set_option("preferredRecordSyntax", syntax_name);
602
603     if (enable_pz2_transform)
604     {
605         element_set_name = "F";
606         if (b->sptr->element_set.length())
607             element_set_name = b->sptr->element_set.c_str();
608     }
609
610     b->set_option("elementSetName", element_set_name);
611
612     b->present(start, number_to_present, recs, error, addinfo);
613
614     Odr_int i = 0;
615     if (!*error)
616     {
617         for (i = 0; i < number_to_present; i++)
618             if (!recs[i])
619                 break;
620     }
621     if (i > 0)
622     {  // only return records if no error and at least one record
623         char *odr_database = odr_strdup(odr,
624                                         b->m_frontend_database.c_str());
625         Z_NamePlusRecordList *npl = (Z_NamePlusRecordList *)
626             odr_malloc(odr, sizeof(*npl));
627         *number_of_records_returned = i;
628         npl->num_records = i;
629         npl->records = (Z_NamePlusRecord **)
630             odr_malloc(odr, i * sizeof(*npl->records));
631         for (i = 0; i < number_to_present; i++)
632         {
633             Z_NamePlusRecord *npr = 0;
634             const char *addinfo;
635             int sur_error = ZOOM_record_error(recs[i], 0 /* msg */,
636                                               &addinfo, 0 /* diagset */);
637                 
638             if (sur_error)
639             {
640                 npr = zget_surrogateDiagRec(odr, odr_database, sur_error,
641                                             addinfo);
642             }
643             else if (enable_pz2_transform)
644             {
645                 char rec_type_str[100];
646
647                 strcpy(rec_type_str, b->sptr->use_turbomarc ?
648                        "txml" : "xml");
649                 
650                 // prevent buffer overflow ...
651                 if (b->sptr->record_encoding.length() > 0 &&
652                     b->sptr->record_encoding.length() < 
653                     (sizeof(rec_type_str)-20))
654                 {
655                     strcat(rec_type_str, "; charset=");
656                     strcat(rec_type_str, b->sptr->record_encoding.c_str());
657                 }
658                 
659                 int rec_len;
660                 const char *rec_buf = ZOOM_record_get(recs[i], rec_type_str,
661                                                       &rec_len);
662                 if (rec_buf && b->xsp)
663                 {
664                     xmlDoc *rec_doc = xmlParseMemory(rec_buf, rec_len);
665                     if (rec_doc)
666                     { 
667                         xmlDoc *rec_res;
668                         rec_res = xsltApplyStylesheet(b->xsp, rec_doc, 0);
669
670                         if (rec_res)
671                             xsltSaveResultToString((xmlChar **) &rec_buf, &rec_len,
672                                                    rec_res, b->xsp);
673                     }
674                 }
675
676                 if (rec_buf)
677                 {
678                     npr = (Z_NamePlusRecord *) odr_malloc(odr, sizeof(*npr));
679                     npr->databaseName = odr_database;
680                     npr->which = Z_NamePlusRecord_databaseRecord;
681                     npr->u.databaseRecord =
682                         z_ext_record_xml(odr, rec_buf, rec_len);
683                 }
684                 else
685                 {
686                     npr = zget_surrogateDiagRec(
687                         odr, odr_database, 
688                         YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS,
689                         rec_type_str);
690                 }
691             }
692             else
693             {
694                 Z_External *ext =
695                     (Z_External *) ZOOM_record_get(recs[i], "ext", 0);
696                 if (ext)
697                 {
698                     npr = (Z_NamePlusRecord *) odr_malloc(odr, sizeof(*npr));
699                     npr->databaseName = odr_database;
700                     npr->which = Z_NamePlusRecord_databaseRecord;
701                     npr->u.databaseRecord = ext;
702                 }
703                 else
704                 {
705                     npr = zget_surrogateDiagRec(
706                         odr, odr_database, 
707                         YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS,
708                         "ZOOM_record, type ext");
709                 }
710             }
711             npl->records[i] = npr;
712         }
713         records = (Z_Records*) odr_malloc(odr, sizeof(*records));
714         records->which = Z_Records_DBOSD;
715         records->u.databaseOrSurDiagnostics = npl;
716     }
717     return records;
718 }
719     
720
721 void yf::Zoom::Frontend::handle_search(mp::Package &package)
722 {
723     Z_GDU *gdu = package.request().get();
724     Z_APDU *apdu_req = gdu->u.z3950;
725     Z_APDU *apdu_res = 0;
726     mp::odr odr;
727     Z_SearchRequest *sr = apdu_req->u.searchRequest;
728     if (sr->num_databaseNames != 1)
729     {
730         apdu_res = odr.create_searchResponse(
731             apdu_req, YAZ_BIB1_TOO_MANY_DATABASES_SPECIFIED, 0);
732         package.response() = apdu_res;
733         return;
734     }
735
736     int error = 0;
737     const char *addinfo = 0;
738     std::string db(sr->databaseNames[0]);
739     BackendPtr b = get_backend_from_databases(db, &error, &addinfo);
740     if (error)
741     {
742         apdu_res = 
743             odr.create_searchResponse(
744                 apdu_req, error, addinfo);
745         package.response() = apdu_res;
746         return;
747     }
748
749     b->set_option("setname", "default");
750
751     Odr_int hits = 0;
752     Z_Query *query = sr->query;
753     WRBUF ccl_wrbuf = 0;
754     WRBUF pqf_wrbuf = 0;
755
756     if (query->which == Z_Query_type_1 || query->which == Z_Query_type_101)
757     {
758         // RPN
759         pqf_wrbuf = wrbuf_alloc();
760         yaz_rpnquery_to_wrbuf(pqf_wrbuf, query->u.type_1);
761     }
762     else if (query->which == Z_Query_type_2)
763     {
764         // CCL
765         ccl_wrbuf = wrbuf_alloc();
766         wrbuf_write(ccl_wrbuf, (const char *) query->u.type_2->buf,
767                     query->u.type_2->len);
768     }
769     else if (query->which == Z_Query_type_104 &&
770              query->u.type_104->which == Z_External_CQL)
771     {
772         // CQL
773         const char *cql = query->u.type_104->u.cql;
774         CQL_parser cp = cql_parser_create();
775         int r = cql_parser_string(cp, cql);
776         if (r)
777         {
778             cql_parser_destroy(cp);
779             apdu_res = 
780                 odr.create_searchResponse(apdu_req, 
781                                           YAZ_BIB1_MALFORMED_QUERY,
782                                           "CQL syntax error");
783             package.response() = apdu_res;
784             return;
785         }
786         struct cql_node *cn = cql_parser_result(cp);
787         char ccl_buf[1024];
788
789         r = cql_to_ccl_buf(cn, ccl_buf, sizeof(ccl_buf));
790         yaz_log(YLOG_LOG, "cql_to_ccl_buf returned %d", r);
791         if (r == 0)
792         {
793             ccl_wrbuf = wrbuf_alloc();
794             wrbuf_puts(ccl_wrbuf, ccl_buf);
795         }
796         cql_parser_destroy(cp);
797         if (r)
798         {
799             apdu_res = 
800                 odr.create_searchResponse(apdu_req, 
801                                           YAZ_BIB1_MALFORMED_QUERY,
802                                           "CQL to CCL conversion error");
803             package.response() = apdu_res;
804             return;
805         }
806     }
807     else
808     {
809         apdu_res = 
810             odr.create_searchResponse(apdu_req, YAZ_BIB1_QUERY_TYPE_UNSUPP, 0);
811         package.response() = apdu_res;
812         return;
813     }
814
815     if (ccl_wrbuf)
816     {
817         // CCL to PQF
818         assert(pqf_wrbuf == 0);
819         int cerror, cpos;
820         struct ccl_rpn_node *cn;
821         cn = ccl_find_str(b->sptr->ccl_bibset, wrbuf_cstr(ccl_wrbuf),
822                           &cerror, &cpos);
823         wrbuf_destroy(ccl_wrbuf);
824         if (!cn)
825         {
826             char *addinfo = odr_strdup(odr, ccl_err_msg(cerror));
827
828             apdu_res = 
829                 odr.create_searchResponse(apdu_req, 
830                                           YAZ_BIB1_MALFORMED_QUERY,
831                                           addinfo);
832             package.response() = apdu_res;
833             return;
834         }
835         pqf_wrbuf = wrbuf_alloc();
836         ccl_pquery(pqf_wrbuf, cn);
837         ccl_rpn_delete(cn);
838     }
839     
840     assert(pqf_wrbuf);
841     b->search_pqf(wrbuf_cstr(pqf_wrbuf), &hits, &error, &addinfo);
842     
843     wrbuf_destroy(pqf_wrbuf);
844     
845     const char *element_set_name = 0;
846     Odr_int number_to_present = 0;
847     if (!error)
848         mp::util::piggyback_sr(sr, hits, number_to_present, &element_set_name);
849     
850     Odr_int number_of_records_returned = 0;
851     Z_Records *records = get_records(
852         0, number_to_present, &error, &addinfo,
853         &number_of_records_returned, odr, b, sr->preferredRecordSyntax,
854         element_set_name);
855     apdu_res = odr.create_searchResponse(apdu_req, error, addinfo);
856     if (records)
857     {
858         apdu_res->u.searchResponse->records = records;
859         apdu_res->u.searchResponse->numberOfRecordsReturned =
860             odr_intdup(odr, number_of_records_returned);
861     }
862     apdu_res->u.searchResponse->resultCount = odr_intdup(odr, hits);
863     package.response() = apdu_res;
864 }
865
866 void yf::Zoom::Frontend::handle_present(mp::Package &package)
867 {
868     Z_GDU *gdu = package.request().get();
869     Z_APDU *apdu_req = gdu->u.z3950;
870     Z_APDU *apdu_res = 0;
871     Z_PresentRequest *pr = apdu_req->u.presentRequest;
872
873     mp::odr odr;
874     if (!m_backend)
875     {
876         package.response() = odr.create_presentResponse(
877             apdu_req, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST, 0);
878         return;
879     }
880     const char *element_set_name = 0;
881     Z_RecordComposition *comp = pr->recordComposition;
882     if (comp && comp->which != Z_RecordComp_simple)
883     {
884         package.response() = odr.create_presentResponse(
885             apdu_req, 
886             YAZ_BIB1_PRESENT_COMP_SPEC_PARAMETER_UNSUPP, 0);
887         return;
888     }
889     if (comp && comp->u.simple->which == Z_ElementSetNames_generic)
890         element_set_name = comp->u.simple->u.generic;
891     Odr_int number_of_records_returned = 0;
892     int error = 0;
893     const char *addinfo = 0;
894     Z_Records *records = get_records(
895         *pr->resultSetStartPoint - 1, *pr->numberOfRecordsRequested,
896         &error, &addinfo, &number_of_records_returned, odr, m_backend,
897         pr->preferredRecordSyntax, element_set_name);
898
899     apdu_res = odr.create_presentResponse(apdu_req, error, addinfo);
900     if (records)
901     {
902         apdu_res->u.presentResponse->records = records;
903         apdu_res->u.presentResponse->numberOfRecordsReturned =
904             odr_intdup(odr, number_of_records_returned);
905     }
906     package.response() = apdu_res;
907 }
908
909 void yf::Zoom::Frontend::handle_package(mp::Package &package)
910 {
911     Z_GDU *gdu = package.request().get();
912     if (!gdu)
913         ;
914     else if (gdu->which == Z_GDU_Z3950)
915     {
916         Z_APDU *apdu_req = gdu->u.z3950;
917         if (apdu_req->which == Z_APDU_initRequest)
918         {
919             mp::odr odr;
920             package.response() = odr.create_close(
921                 apdu_req,
922                 Z_Close_protocolError,
923                 "double init");
924         }
925         else if (apdu_req->which == Z_APDU_searchRequest)
926         {
927             handle_search(package);
928         }
929         else if (apdu_req->which == Z_APDU_presentRequest)
930         {
931             handle_present(package);
932         }
933         else
934         {
935             mp::odr odr;
936             package.response() = odr.create_close(
937                 apdu_req,
938                 Z_Close_protocolError,
939                 "zoom filter cannot handle this APDU");
940             package.session().close();
941         }
942     }
943     else
944     {
945         package.session().close();
946     }
947 }
948
949 void yf::Zoom::Impl::process(mp::Package &package)
950 {
951     FrontendPtr f = get_frontend(package);
952     Z_GDU *gdu = package.request().get();
953
954     if (f->m_is_virtual)
955     {
956         f->handle_package(package);
957     }
958     else if (gdu && gdu->which == Z_GDU_Z3950 && gdu->u.z3950->which ==
959              Z_APDU_initRequest)
960     {
961         Z_InitRequest *req = gdu->u.z3950->u.initRequest;
962         f->m_init_gdu = gdu;
963         
964         mp::odr odr;
965         Z_APDU *apdu = odr.create_initResponse(gdu->u.z3950, 0, 0);
966         Z_InitResponse *resp = apdu->u.initResponse;
967         
968         int i;
969         static const int masks[] = {
970             Z_Options_search,
971             Z_Options_present,
972             -1 
973         };
974         for (i = 0; masks[i] != -1; i++)
975             if (ODR_MASK_GET(req->options, masks[i]))
976                 ODR_MASK_SET(resp->options, masks[i]);
977         
978         static const int versions[] = {
979             Z_ProtocolVersion_1,
980             Z_ProtocolVersion_2,
981             Z_ProtocolVersion_3,
982             -1
983         };
984         for (i = 0; versions[i] != -1; i++)
985             if (ODR_MASK_GET(req->protocolVersion, versions[i]))
986                 ODR_MASK_SET(resp->protocolVersion, versions[i]);
987             else
988                 break;
989         
990         *resp->preferredMessageSize = *req->preferredMessageSize;
991         *resp->maximumRecordSize = *req->maximumRecordSize;
992         
993         package.response() = apdu;
994         f->m_is_virtual = true;
995     }
996     else
997         package.move();
998
999     release_frontend(package);
1000 }
1001
1002
1003 static mp::filter::Base* filter_creator()
1004 {
1005     return new mp::filter::Zoom;
1006 }
1007
1008 extern "C" {
1009     struct metaproxy_1_filter_struct metaproxy_1_filter_zoom = {
1010         0,
1011         "zoom",
1012         filter_creator
1013     };
1014 }
1015
1016
1017 /*
1018  * Local variables:
1019  * c-basic-offset: 4
1020  * c-file-style: "Stroustrup"
1021  * indent-tabs-mode: nil
1022  * End:
1023  * vim: shiftwidth=4 tabstop=8 expandtab
1024  */
1025