Fix multiple match aggregation
[metaproxy-moved-to-github.git] / src / filter_sru_to_z3950.cpp
index 6590d4d..5704479 100644 (file)
@@ -1,5 +1,5 @@
 /* This file is part of Metaproxy.
-   Copyright (C) 2005-2012 Index Data
+   Copyright (C) 2005-2013 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,8 +26,6 @@ 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>
@@ -39,7 +37,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;
@@ -492,28 +489,43 @@ yf::SRUtoZ3950::Impl::z3950_init_request(mp::Package &package,
     // send Z3950 package
     z3950_package.move();
 
-    // dead Z3950 backend detection
-    if (z3950_package.session().is_closed())
-    {
-        yaz_add_srw_diagnostic(odr_en,
-                               &(sru_pdu_res->u.response->diagnostics),
-                               &(sru_pdu_res->u.response->num_diagnostics),
-                               YAZ_SRW_SYSTEM_TEMPORARILY_UNAVAILABLE, 0);
-        return false;
-    }
-
     // check successful initResponse
     Z_GDU *z3950_gdu = z3950_package.response().get();
 
+    int error = YAZ_SRW_SYSTEM_TEMPORARILY_UNAVAILABLE;
+    const char *addinfo = 0;
     if (z3950_gdu && z3950_gdu->which == Z_GDU_Z3950
-        && z3950_gdu->u.z3950->which == Z_APDU_initResponse
-        && *z3950_gdu->u.z3950->u.initResponse->result)
-        return true;
+        && z3950_gdu->u.z3950->which == Z_APDU_initResponse)
+    {
+        Z_InitResponse *initrs = z3950_gdu->u.z3950->u.initResponse;
+        if (*initrs->result)
+            return true;
+        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),
-                           YAZ_SRW_SYSTEM_TEMPORARILY_UNAVAILABLE, 0);
+                           error, addinfo);
     return false;
 }