zoom: sync up with Pazpar2 WRT authentication handling MP-492
[metaproxy-moved-to-github.git] / src / filter_sru_to_z3950.cpp
index 4f9618c..3e050ef 100644 (file)
@@ -1,5 +1,5 @@
 /* This file is part of Metaproxy.
-   Copyright (C) 2005-2012 Index Data
+   Copyright (C) Index Data
 
 Metaproxy is free software; you can redistribute it and/or modify it under
 the terms of the GNU General Public License as published by the Free
@@ -26,12 +26,13 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 #include "sru_util.hpp"
 #include "filter_sru_to_z3950.hpp"
 
-#include <yaz/zgdu.h>
-#include <yaz/z-core.h>
 #include <yaz/srw.h>
 #include <yaz/pquery.h>
 #include <yaz/oid_db.h>
 #include <yaz/log.h>
+#if YAZ_VERSIONL >= 0x50000
+#include <yaz/facet.h>
+#endif
 
 #include <boost/thread/mutex.hpp>
 #include <boost/thread/condition.hpp>
@@ -39,7 +40,6 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 #include <iostream>
 #include <sstream>
 #include <string>
-/* #include <algorithm> */
 #include <map>
 
 namespace mp = metaproxy_1;
@@ -96,7 +96,8 @@ namespace metaproxy_1 {
                 mp::odr &odr_en,
                 Z_SRW_PDU *sru_pdu_res,
                 Z_SRW_searchRetrieveRequest const *sr_req,
-                std::string zurl
+                std::string zurl,
+                std::string db_append
                 ) const;
 
             bool z3950_present_request(
@@ -227,6 +228,7 @@ void yf::SRUtoZ3950::Impl::sru(mp::Package &package, Z_GDU *zgdu_req)
 
     bool enable_package_log = false;
     std::string zurl;
+    std::string dbargs;
     Z_SRW_extra_arg *arg;
 
     for ( arg = sru_pdu_req->extra_args; arg; arg = arg->next)
@@ -250,6 +252,15 @@ void yf::SRUtoZ3950::Impl::sru(mp::Package &package, Z_GDU *zgdu_req)
                 package.log_enable();
             }
         }
+        else if (!strncmp(arg->name, "x-client-", 9) && arg->value)
+        {
+            if (dbargs.length())
+                dbargs += '&';
+            dbargs += mp_util::uri_encode(arg->name + 9);
+            dbargs += '=';
+            dbargs += mp_util::uri_encode(arg->value);
+        }
+
     assert(sru_pdu_req);
 
     // filter acts as sink for SRU explain requests
@@ -275,7 +286,7 @@ void yf::SRUtoZ3950::Impl::sru(mp::Package &package, Z_GDU *zgdu_req)
                                      zurl, sru_pdu_res, sru_pdu_req))
         {
             ok = z3950_search_request(package, odr_en,
-                                      sru_pdu_res, sr_req, zurl);
+                                      sru_pdu_res, sr_req, zurl, dbargs);
 
             if (ok
                 && sru_pdu_res->u.response->numberOfRecords
@@ -433,51 +444,6 @@ void yf::SRUtoZ3950::Impl::process(mp::Package &package)
     release_frontend(package);
 }
 
-static void interpret_default_diag(int *error,
-                                   const char **addinfo,
-                                   Z_DefaultDiagFormat *r)
-{
-    *error = *r->condition;
-    *addinfo = r->u.v2Addinfo;
-}
-
-static void interpret_init_diag2(int *error,
-                                const char **addinfo,
-                                Z_DiagnosticFormat *diag)
-{
-    if (diag->num > 0)
-    {
-        Z_DiagnosticFormat_s *ds = diag->elements[0];
-        if (ds->which == Z_DiagnosticFormat_s_defaultDiagRec)
-            interpret_default_diag(error, addinfo, ds->u.defaultDiagRec);
-    }
-}
-
-static void interpret_init_diag(int *error,
-                                const char **addinfo,
-                                Z_InitResponse *initrs)
-{
-    Z_External *uif = initrs->userInformationField;
-    if (uif && uif->which == Z_External_userInfo1)
-    {
-        int i;
-        Z_OtherInformation *ui = uif->u.userInfo1;
-        for (i = 0; i < ui->num_elements; i++)
-        {
-            Z_OtherInformationUnit *unit = ui->list[i];
-            if (unit->which == Z_OtherInfo_externallyDefinedInfo &&
-                unit->information.externallyDefinedInfo &&
-                unit->information.externallyDefinedInfo->which ==
-                Z_External_diag1)
-            {
-                interpret_init_diag2(
-                    error, addinfo,
-                    unit->information.externallyDefinedInfo->u.diag1);
-            }
-        }
-    }
-}
-
 bool
 yf::SRUtoZ3950::Impl::z3950_init_request(mp::Package &package,
                                          mp::odr &odr_en,
@@ -548,11 +514,28 @@ yf::SRUtoZ3950::Impl::z3950_init_request(mp::Package &package,
         Z_InitResponse *initrs = z3950_gdu->u.z3950->u.initResponse;
         if (*initrs->result)
             return true;
-        int z_error = 0;
-        interpret_init_diag(&error, &addinfo, initrs);
-        if (z_error)
-            error = yaz_diag_bib1_to_srw(z_error);
+        int no = 0;
+        while (1)
+        {
+            Z_DefaultDiagFormat *df = yaz_decode_init_diag(no, initrs);
+
+            if (!df)
+                break;
+            yaz_add_srw_diagnostic(odr_en,
+                                   &(sru_pdu_res->u.response->diagnostics),
+                                   &(sru_pdu_res->u.response->num_diagnostics),
+                                   yaz_diag_bib1_to_srw(*df->condition),
+                                   df->u.v2Addinfo);
+            no++;
+        }
+        if (no)
+            return false; // got least one diagnostic from init
+
+        // we just have result=false.
+        error = YAZ_SRW_AUTHENTICATION_ERROR;
     }
+    else
+        addinfo = "sru_z3950: expected initResponse";
     yaz_add_srw_diagnostic(odr_en,
                            &(sru_pdu_res->u.response->diagnostics),
                            &(sru_pdu_res->u.response->num_diagnostics),
@@ -580,7 +563,8 @@ bool yf::SRUtoZ3950::Impl::z3950_search_request(mp::Package &package,
                                                 Z_SRW_PDU *sru_pdu_res,
                                                 Z_SRW_searchRetrieveRequest
                                                 const *sr_req,
-                                                std::string zurl) const
+                                                std::string zurl,
+                                                std::string dbappend) const
 {
 
     assert(sru_pdu_res->u.response);
@@ -599,17 +583,31 @@ bool yf::SRUtoZ3950::Impl::z3950_search_request(mp::Package &package,
                                           &z_searchRequest->num_databaseNames,
                                           &z_searchRequest->databaseNames))
     {
-        z_searchRequest->num_databaseNames = 1;
-        z_searchRequest->databaseNames = (char**)
-            odr_malloc(odr_en, sizeof(char *));
+        std::string db;
 
         if (sr_req->database)
-            z_searchRequest->databaseNames[0]
-                = odr_strdup(odr_en, const_cast<char *>(sr_req->database));
+            db = sr_req->database;
         else
-            z_searchRequest->databaseNames[0]
-                = odr_strdup(odr_en, "Default");
+            db = "Default";
+
+        if (dbappend.length())
+        {
+            db += ",";
+            db += dbappend;
+        }
+        z_searchRequest->num_databaseNames = 1;
+        z_searchRequest->databaseNames = (char**)
+            odr_malloc(odr_en, sizeof(char *));
+        z_searchRequest->databaseNames[0] = odr_strdup(odr_en, db.c_str());
+    }
+#if YAZ_VERSIONL >= 0x50000
+    // yaz_oi_set_facetlist not public in YAZ 4.2.66
+    if (sr_req->facetList)
+    {
+        Z_OtherInformation **oi = &z_searchRequest->otherInfo;
+        yaz_oi_set_facetlist(oi, odr_en, sr_req->facetList);
     }
+#endif
 
     Z_Query *z_query = (Z_Query *) odr_malloc(odr_en, sizeof(Z_Query));
     z_searchRequest->query = z_query;
@@ -650,7 +648,12 @@ bool yf::SRUtoZ3950::Impl::z3950_search_request(mp::Package &package,
     {
         return false;
     }
-
+#if YAZ_VERSIONL >= 0x50000
+    Z_FacetList *fl = yaz_oi_get_facetlist(&sr->additionalSearchInfo);
+    if (!fl)
+        fl =  yaz_oi_get_facetlist(&sr->otherInfo);
+    sru_pdu_res->u.response->facetList = fl;
+#endif
     sru_pdu_res->u.response->numberOfRecords
         = odr_intdup(odr_en, *sr->resultCount);
     return true;
@@ -882,7 +885,13 @@ int yf::SRUtoZ3950::Impl::z3950_build_query(
     const Z_SRW_searchRetrieveRequest *req
     ) const
 {
-    if (req->query_type == Z_SRW_query_type_cql)
+    if (
+#ifdef Z_SRW_query_type_cql
+        req->query_type == Z_SRW_query_type_cql
+#else
+        !strcmp(req->queryType, "cql")
+#endif
+        )
     {
         Z_External *ext = (Z_External *)
             odr_malloc(odr_en, sizeof(*ext));
@@ -891,22 +900,39 @@ int yf::SRUtoZ3950::Impl::z3950_build_query(
         ext->indirect_reference = 0;
         ext->descriptor = 0;
         ext->which = Z_External_CQL;
-        ext->u.cql = odr_strdup(odr_en, req->query.cql);
+        ext->u.cql = odr_strdup(odr_en,
+#ifdef Z_SRW_query_type_cql
+        req->query.cql
+#else
+        req->query
+#endif
+            );
 
         z_query->which = Z_Query_type_104;
         z_query->u.type_104 =  ext;
         return 0;
     }
 
-    if (req->query_type == Z_SRW_query_type_pqf)
+    if (
+#ifdef Z_SRW_query_type_pqf
+        req->query_type == Z_SRW_query_type_pqf
+#else
+        !strcmp(req->queryType, "pqf")
+#endif
+        )
     {
         Z_RPNQuery *RPNquery;
         YAZ_PQF_Parser pqf_parser;
 
         pqf_parser = yaz_pqf_create ();
 
-        RPNquery = yaz_pqf_parse (pqf_parser, odr_en, req->query.pqf);
-
+        RPNquery = yaz_pqf_parse (pqf_parser, odr_en,
+#ifdef Z_SRW_query_type_pqf
+        req->query.pqf
+#else
+        req->query
+#endif
+            );
         yaz_pqf_destroy(pqf_parser);
 
         if (!RPNquery)
@@ -959,7 +985,7 @@ static mp::filter::Base* filter_creator()
 }
 
 extern "C" {
-    struct metaproxy_1_filter_struct metaproxy_1_filter_sru_to_z3950 = {
+    struct metaproxy_1_filter_struct metaproxy_1_filter_sru_z3950 = {
         0,
         "sru_z3950",
         filter_creator