Bump year in copyright msg in source
[metaproxy-moved-to-github.git] / src / filter_sru_to_z3950.cpp
index a4f2f6d..8bd684b 100644 (file)
@@ -1,9 +1,24 @@
-/* $Id: filter_sru_to_z3950.cpp,v 1.30 2007-03-08 09:43:37 adam Exp $
-   Copyright (c) 2005-2007, Index Data.
-
-   See the LICENSE file for details
+/* This file is part of Metaproxy.
+   Copyright (C) 2005-2009 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
+Software Foundation; either version 2, or (at your option) any later
+version.
+
+Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
 
+// make std::min actually work on Windows
+#define NOMINMAX 1
+
 #include "config.hpp"
 #include "filter.hpp"
 #include "package.hpp"
 #include <yaz/z-core.h>
 #include <yaz/srw.h>
 #include <yaz/pquery.h>
+#include <yaz/oid_db.h>
+#include <yaz/log.h>
 
 #include <boost/thread/mutex.hpp>
+#include <boost/thread/condition.hpp>
 
 #include <iostream>
 #include <sstream>
@@ -29,7 +47,6 @@ namespace mp = metaproxy_1;
 namespace mp_util = metaproxy_1::util;
 namespace yf = mp::filter;
 
-
 namespace metaproxy_1 {
     namespace filter {
         class SRUtoZ3950::Impl {
@@ -40,31 +57,37 @@ namespace metaproxy_1 {
             union SRW_query {char * cql; char * xcql; char * pqf;};
             typedef const int& SRW_query_type;
             std::map<std::string, const xmlNode *> m_database_explain;
-        private:
 
+            typedef std::map<std::string, int> ActiveUrlMap;
+
+            boost::mutex m_mutex;
+            boost::condition m_cond_url_ready;
+            ActiveUrlMap m_active_urls;
+        private:
+            void sru(metaproxy_1::Package &package, Z_GDU *zgdu_req);
             bool z3950_build_query(mp::odr &odr_en, Z_Query *z_query, 
                                    const SRW_query &query, 
                                    SRW_query_type query_type) const;
 
             bool z3950_init_request(mp::Package &package, 
                                     mp::odr &odr_en,
-                                    Z_SRW_PDU *sru_pdu_res,
-                                    const std::string 
-                                    &database = "Default") const;
+                                    std::string zurl,
+                                    Z_SRW_PDU *sru_pdu_res) const;
 
             bool z3950_close_request(mp::Package &package) const;
 
-            bool z3950_search_request(mp::Package &package,
-                                      mp::odr &odr_en,
-                                      Z_SRW_PDU *sru_pdu_res,
-                                      Z_SRW_searchRetrieveRequest 
-                                          const *sr_req) const;
+            bool z3950_search_request(
+                mp::Package &package,
+                mp::odr &odr_en,
+                Z_SRW_PDU *sru_pdu_res,
+                Z_SRW_searchRetrieveRequest const *sr_req,
+                std::string zurl) const;
 
-            bool z3950_present_request(mp::Package &package,
-                                       mp::odr &odr_en,
-                                       Z_SRW_PDU *sru_pdu_res,
-                                       Z_SRW_searchRetrieveRequest 
-                                       const *sr_req) const;
+            bool z3950_present_request(
+                mp::Package &package,
+                mp::odr &odr_en,
+                Z_SRW_PDU *sru_pdu_res,
+                Z_SRW_searchRetrieveRequest const *sr_req) const;
 
             bool z3950_scan_request(mp::Package &package,
                                     mp::odr &odr_en,
@@ -79,6 +102,8 @@ namespace metaproxy_1 {
             int z3950_to_srw_diag(mp::odr &odr_en, 
                                   Z_SRW_searchRetrieveResponse *srw_res,
                                   Z_DefaultDiagFormat *ddf) const;
+
+
         };
     }
 }
@@ -91,7 +116,7 @@ yf::SRUtoZ3950::~SRUtoZ3950()
 {  // must have a destructor because of boost::scoped_ptr
 }
 
-void yf::SRUtoZ3950::configure(const xmlNode *xmlnode)
+void yf::SRUtoZ3950::configure(const xmlNode *xmlnode, bool test_only)
 {
     m_p->configure(xmlnode);
 }
@@ -132,18 +157,8 @@ void yf::SRUtoZ3950::Impl::configure(const xmlNode *confignode)
     }
 }
 
-void yf::SRUtoZ3950::Impl::process(mp::Package &package)
+void yf::SRUtoZ3950::Impl::sru(mp::Package &package, Z_GDU *zgdu_req)
 {
-    Z_GDU *zgdu_req = package.request().get();
-
-    // ignoring all non HTTP_Request packages
-    if (!zgdu_req || !(zgdu_req->which == Z_GDU_HTTP_Request)){
-        package.move();
-        return;
-    }
-    
-    // only working on  HTTP_Request packages now
-
     bool ok = true;    
 
     mp::odr odr_de(ODR_DECODE);
@@ -176,17 +191,41 @@ void yf::SRUtoZ3950::Impl::process(mp::Package &package)
 
     // filter acts as sink for non-valid SRU requests
     if (! (sru_pdu_req = mp_util::decode_sru_request(package, odr_de, odr_en, 
-                                            sru_pdu_res, soap,
+                                            sru_pdu_res, &soap,
                                             charset, stylesheet)))
     {
-        mp_util::build_sru_explain(package, odr_en, sru_pdu_res, 
-                                   sruinfo, explainnode);
-        mp_util::build_sru_response(package, odr_en, soap, 
-                           sru_pdu_res, charset, stylesheet);
-        // package.session().close();
+        if (soap)
+        {
+            mp_util::build_sru_explain(package, odr_en, sru_pdu_res, 
+                                       sruinfo, explainnode);
+            mp_util::build_sru_response(package, odr_en, soap, 
+                                        sru_pdu_res, charset, stylesheet);
+        }
+        else
+        {
+            metaproxy_1::odr odr; 
+            Z_GDU *zgdu_res = 
+                odr.create_HTTP_Response(package.session(), 
+                                         zgdu_req->u.HTTP_Request, 400);
+            package.response() = zgdu_res;
+        }
         return;
     }
     
+    std::string zurl;
+    Z_SRW_extra_arg *arg;
+
+    for ( arg = sru_pdu_req->extra_args; arg; arg = arg->next)
+        if (!strcmp(arg->name, "x-target"))
+        {
+            zurl = std::string(arg->value);
+        }
+        else if (!strcmp(arg->name, "x-max-sockets"))
+        {
+            package.origin().set_max_sockets(atoi(arg->value));
+        }
+
+
     // filter acts as sink for SRU explain requests
     if (sru_pdu_req && sru_pdu_req->which == Z_SRW_explain_request)
     {
@@ -195,16 +234,11 @@ void yf::SRUtoZ3950::Impl::process(mp::Package &package)
         //                           sruinfo, er_req);
         mp_util::build_sru_explain(package, odr_en, sru_pdu_res, 
                                    sruinfo, explainnode, er_req);
-        mp_util::build_sru_response(package, odr_en, soap, 
-                                    sru_pdu_res, charset, stylesheet);
-        return;
     }
-
-    // searchRetrieve
     else if (sru_pdu_req 
         && sru_pdu_req->which == Z_SRW_searchRetrieve_request
         && sru_pdu_req->u.request)
-    {
+    {   // searchRetrieve
         Z_SRW_searchRetrieveRequest *sr_req = sru_pdu_req->u.request;   
         
         sru_pdu_res = yaz_srw_get(odr_en, Z_SRW_searchRetrieve_response);
@@ -213,23 +247,21 @@ void yf::SRUtoZ3950::Impl::process(mp::Package &package)
         ok = mp_util::check_sru_query_exists(package, odr_en, 
                                              sru_pdu_res, sr_req);
 
-        if (ok && z3950_init_request(package, odr_en, sru_pdu_res))
+        if (ok && z3950_init_request(package, odr_en, zurl, sru_pdu_res))
         {
-            {
-                ok = z3950_search_request(package, odr_en,
-                                          sru_pdu_res, sr_req);
-
-                if (ok 
-                    && sru_pdu_res->u.response->numberOfRecords
-                    && *(sru_pdu_res->u.response->numberOfRecords)
-                    && sr_req->maximumRecords
-                    && *(sr_req->maximumRecords))
-                    
-                    ok = z3950_present_request(package, odr_en,
-                                               sru_pdu_res,
-                                               sr_req);
-                z3950_close_request(package);
-            }
+            ok = z3950_search_request(package, odr_en,
+                                      sru_pdu_res, sr_req, zurl);
+            
+            if (ok 
+                && sru_pdu_res->u.response->numberOfRecords
+                && *(sru_pdu_res->u.response->numberOfRecords)
+                && sr_req->maximumRecords
+                && *(sr_req->maximumRecords))
+                
+                ok = z3950_present_request(package, odr_en,
+                                           sru_pdu_res,
+                                           sr_req);
+            z3950_close_request(package);
         }
     }
 
@@ -246,10 +278,10 @@ void yf::SRUtoZ3950::Impl::process(mp::Package &package)
         yaz_add_srw_diagnostic(odr_en,
                                &(sru_pdu_res->u.scan_response->diagnostics), 
                                &(sru_pdu_res->u.scan_response->num_diagnostics), 
-                               4, "scan");
+                               YAZ_SRW_UNSUPP_OPERATION, "scan");
  
         // to be used when we do scan
-        if (false && z3950_init_request(package, odr_en, sru_pdu_res))
+        if (false && z3950_init_request(package, odr_en, zurl, sru_pdu_res))
         {
             z3950_scan_request(package, odr_en, sru_pdu_res, sr_req);    
             z3950_close_request(package);
@@ -267,16 +299,58 @@ void yf::SRUtoZ3950::Impl::process(mp::Package &package)
     // build and send SRU response
     mp_util::build_sru_response(package, odr_en, soap, 
                                 sru_pdu_res, charset, stylesheet);
-    return;
 }
 
 
+void yf::SRUtoZ3950::Impl::process(mp::Package &package)
+{
+    Z_GDU *zgdu_req = package.request().get();
+
+    // ignoring all non HTTP_Request packages
+    if (!zgdu_req || !(zgdu_req->which == Z_GDU_HTTP_Request)){
+        package.move();
+        return;
+    }
+    
+    // only working on HTTP_Request packages now
+
+    // see if HTTP request is already being executed..
+    // we consider only the SRU - GET case..
+    if (zgdu_req->u.HTTP_Request->content_len == 0)
+    {
+        const char *path = zgdu_req->u.HTTP_Request->path;
+        boost::mutex::scoped_lock lock(m_mutex);
+        while (1)
+        {
+            ActiveUrlMap::iterator it = m_active_urls.find(path);
+            if (it == m_active_urls.end())
+            {
+                m_active_urls[path] = 1;
+                break;
+            }
+            yaz_log(YLOG_LOG, "Waiting for %s to complete", path);
+            m_cond_url_ready.wait(lock);
+        }
+    }
+    sru(package, zgdu_req);
+    if (zgdu_req->u.HTTP_Request->content_len == 0)
+    {
+        const char *path = zgdu_req->u.HTTP_Request->path;
+        boost::mutex::scoped_lock lock(m_mutex);
+
+        ActiveUrlMap::iterator it = m_active_urls.find(path);
+
+        m_active_urls.erase(it);
+        m_cond_url_ready.notify_all();
+    }
+}
+
 
 bool 
 yf::SRUtoZ3950::Impl::z3950_init_request(mp::Package &package, 
                                          mp::odr &odr_en,
-                                         Z_SRW_PDU *sru_pdu_res,
-                                         const std::string &database) const
+                                         std::string zurl,
+                                         Z_SRW_PDU *sru_pdu_res) const
 {
     // prepare Z3950 package
     Package z3950_package(package.session(), package.origin());
@@ -301,6 +375,14 @@ yf::SRUtoZ3950::Impl::z3950_init_request(mp::Package &package,
     ODR_MASK_SET(init_req->protocolVersion, Z_ProtocolVersion_2);
     ODR_MASK_SET(init_req->protocolVersion, Z_ProtocolVersion_3);
 
+    if (zurl.length())
+    {    
+        std::string host;
+        std::list<std::string> dblist;
+        mp_util::split_zurl(zurl, host, dblist);
+        mp_util::set_vhost_otherinfo(&init_req->otherInfo, odr_en, host, 1);
+    }
+
     z3950_package.request() = apdu;
 
     // send Z3950 package
@@ -311,7 +393,7 @@ yf::SRUtoZ3950::Impl::z3950_init_request(mp::Package &package,
         yaz_add_srw_diagnostic(odr_en,
                                &(sru_pdu_res->u.response->diagnostics),
                                &(sru_pdu_res->u.response->num_diagnostics),
-                               2, 0);
+                               YAZ_SRW_SYSTEM_TEMPORARILY_UNAVAILABLE, 0);
         return false;
     }
 
@@ -326,7 +408,7 @@ yf::SRUtoZ3950::Impl::z3950_init_request(mp::Package &package,
     yaz_add_srw_diagnostic(odr_en,
                            &(sru_pdu_res->u.response->diagnostics),
                            &(sru_pdu_res->u.response->num_diagnostics),
-                           2, 0);
+                           YAZ_SRW_SYSTEM_TEMPORARILY_UNAVAILABLE, 0);
     return false;
 }
 
@@ -357,12 +439,12 @@ yf::SRUtoZ3950::Impl::z3950_close_request(mp::Package &package) const
     return false;
 }
 
-bool 
-yf::SRUtoZ3950::Impl::z3950_search_request(mp::Package &package,  
-                                          mp::odr &odr_en,
-                                          Z_SRW_PDU *sru_pdu_res,
-                                          Z_SRW_searchRetrieveRequest 
-                                          const *sr_req) const
+bool yf::SRUtoZ3950::Impl::z3950_search_request(mp::Package &package,  
+                                                mp::odr &odr_en,
+                                                Z_SRW_PDU *sru_pdu_res,
+                                                Z_SRW_searchRetrieveRequest 
+                                                const *sr_req,
+                                                std::string zurl) const
 {
 
     assert(sru_pdu_res->u.response);
@@ -374,18 +456,22 @@ yf::SRUtoZ3950::Impl::z3950_search_request(mp::Package &package,
     Z_APDU *apdu = zget_APDU(odr_en, Z_APDU_searchRequest);
     Z_SearchRequest *z_searchRequest = apdu->u.searchRequest;
 
-    // z3950'fy database
-    z_searchRequest->num_databaseNames = 1;
-    z_searchRequest->databaseNames = (char**)
-        odr_malloc(odr_en, sizeof(char *));
 
-    if (sr_req->database)
-        z_searchRequest->databaseNames[0] 
-            = odr_strdup(odr_en, const_cast<char *>(sr_req->database));
-    else
-        z_searchRequest->databaseNames[0] 
+    if (!mp_util::set_databases_from_zurl(odr_en, zurl,
+                                     &z_searchRequest->num_databaseNames,
+                                         &z_searchRequest->databaseNames))
+    {
+        z_searchRequest->num_databaseNames = 1;
+        z_searchRequest->databaseNames = (char**)
+            odr_malloc(odr_en, sizeof(char *));
+
+        if (sr_req->database)
+            z_searchRequest->databaseNames[0] 
+                = odr_strdup(odr_en, const_cast<char *>(sr_req->database));
+        else
+            z_searchRequest->databaseNames[0] 
             = odr_strdup(odr_en, "Default");
-
+    }
 
     // z3950'fy query
     Z_Query *z_query = (Z_Query *) odr_malloc(odr_en, sizeof(Z_Query));
@@ -452,20 +538,26 @@ yf::SRUtoZ3950::Impl::z3950_search_request(mp::Package &package,
 
 bool 
 yf::SRUtoZ3950::Impl::z3950_present_request(mp::Package &package, 
-                                           mp::odr &odr_en,
-                                           Z_SRW_PDU *sru_pdu_res,
-                                           Z_SRW_searchRetrieveRequest 
-                                           const *sr_req)
+                                            mp::odr &odr_en,
+                                            Z_SRW_PDU *sru_pdu_res,
+                                            Z_SRW_searchRetrieveRequest 
+                                            const *sr_req)
     const
 {
     assert(sru_pdu_res->u.response);
+    int start = 1;
+    int max_recs = 0;
 
     if (!sr_req)
         return false;
 
-    
+    if (sr_req->maximumRecords)
+        max_recs = *sr_req->maximumRecords;
+    if (sr_req->startRecord)
+        start = *sr_req->startRecord;
+
     // no need to work if nobody wants record ..
-    if (!(sr_req->maximumRecords) || 0 == *(sr_req->maximumRecords))
+    if (max_recs == 0)
         return true;
 
     bool send_z3950_present = true;
@@ -477,7 +569,7 @@ yf::SRUtoZ3950::Impl::z3950_present_request(mp::Package &package,
         yaz_add_srw_diagnostic(odr_en,
                                &(sru_pdu_res->u.response->diagnostics), 
                                &(sru_pdu_res->u.response->num_diagnostics), 
-                               72, 0);
+                               YAZ_SRW_XPATH_RETRIEVAL_UNSUPP, 0);
     }
     
     // resultSetTTL unsupported.
@@ -488,7 +580,7 @@ yf::SRUtoZ3950::Impl::z3950_present_request(mp::Package &package,
         yaz_add_srw_diagnostic(odr_en,
                                &(sru_pdu_res->u.response->diagnostics), 
                                &(sru_pdu_res->u.response->num_diagnostics), 
-                               50, 0);
+                               YAZ_SRW_RESULT_SETS_UNSUPP, 0);
     }
     
     // sort unsupported
@@ -498,36 +590,28 @@ yf::SRUtoZ3950::Impl::z3950_present_request(mp::Package &package,
         yaz_add_srw_diagnostic(odr_en,
                                &(sru_pdu_res->u.response->diagnostics), 
                                &(sru_pdu_res->u.response->num_diagnostics), 
-                               80, 0);
+                               YAZ_SRW_SORT_UNSUPP, 0);
     }
     
     // start record requested negative, or larger than number of records
-    if (sr_req->startRecord 
-        && 
-        ((*(sr_req->startRecord) < 0)       // negative
-         ||
-         (sru_pdu_res->u.response->numberOfRecords  //out of range
-          && *(sr_req->startRecord) 
-          > *(sru_pdu_res->u.response->numberOfRecords))
-        ))
+    if (start < 0 || start > *sru_pdu_res->u.response->numberOfRecords)
     {
         send_z3950_present = false;
         yaz_add_srw_diagnostic(odr_en,
                                &(sru_pdu_res->u.response->diagnostics), 
                                &(sru_pdu_res->u.response->num_diagnostics), 
-                               61, 0);
+                               YAZ_SRW_FIRST_RECORD_POSITION_OUT_OF_RANGE, 0);
     }    
-
+    
     // maximumRecords requested negative
-    if (sr_req->maximumRecords
-        && *(sr_req->maximumRecords) < 0) 
-          
+    if (max_recs < 0)
     {
         send_z3950_present = false;
         yaz_add_srw_diagnostic(odr_en,
                                &(sru_pdu_res->u.response->diagnostics), 
                                &(sru_pdu_res->u.response->num_diagnostics), 
-                               6, "maximumRecords");
+                               YAZ_SRW_UNSUPP_PARAMETER_VALUE,
+                               "maximumRecords");
     }    
 
     // exit on all these above diagnostics
@@ -542,42 +626,36 @@ yf::SRUtoZ3950::Impl::z3950_present_request(mp::Package &package,
     assert(apdu->u.presentRequest);
 
     // z3950'fy start record position
-    if (sr_req->startRecord)
-        *(apdu->u.presentRequest->resultSetStartPoint) 
-            = *(sr_req->startRecord);
-    else 
-        *(apdu->u.presentRequest->resultSetStartPoint) = 1;
+    *apdu->u.presentRequest->resultSetStartPoint = start;
     
     // z3950'fy number of records requested 
     // protect against requesting records out of range
-    if (sr_req->maximumRecords)
-        *(apdu->u.presentRequest->numberOfRecordsRequested) 
-            = std::min(*(sr_req->maximumRecords), 
-                  *(sru_pdu_res->u.response->numberOfRecords)
-                  - *(apdu->u.presentRequest->resultSetStartPoint)
-                  + 1);
-     
+    *apdu->u.presentRequest->numberOfRecordsRequested
+        = std::min(max_recs, 
+                   *sru_pdu_res->u.response->numberOfRecords - start + 1);
+    
     // z3950'fy recordPacking
     int record_packing = Z_SRW_recordPacking_XML;
     if (sr_req->recordPacking && 's' == *(sr_req->recordPacking))
         record_packing = Z_SRW_recordPacking_string;
-
+    
     // RecordSyntax will always be XML
-    (apdu->u.presentRequest->preferredRecordSyntax)
-        = yaz_oidval_to_z3950oid (odr_en, CLASS_RECSYN, VAL_TEXT_XML);
+    apdu->u.presentRequest->preferredRecordSyntax
+        = odr_oiddup(odr_en, yaz_oid_recsyn_xml);
 
     // z3950'fy record schema
-     if (sr_req->recordSchema)
-     {
-         apdu->u.presentRequest->recordComposition 
-             = (Z_RecordComposition *) 
-               odr_malloc(odr_en, sizeof(Z_RecordComposition));
-         apdu->u.presentRequest->recordComposition->which 
-             = Z_RecordComp_simple;
-         apdu->u.presentRequest->recordComposition->u.simple 
-             = mp_util::build_esn_from_schema(odr_en,
-                                      (const char *) sr_req->recordSchema); 
-     }
+    if (sr_req->recordSchema)
+    {
+        apdu->u.presentRequest->recordComposition 
+            = (Z_RecordComposition *) 
+            odr_malloc(odr_en, sizeof(Z_RecordComposition));
+        apdu->u.presentRequest->recordComposition->which 
+            = Z_RecordComp_simple;
+        apdu->u.presentRequest->recordComposition->u.simple 
+            = mp_util::build_esn_from_schema(odr_en,
+                                             (const char *) 
+                                             sr_req->recordSchema); 
+    }
 
     // z3950'fy time to live - flagged as diagnostics above
     //if (sr_req->resultSetTTL)
@@ -598,7 +676,7 @@ yf::SRUtoZ3950::Impl::z3950_present_request(mp::Package &package,
         yaz_add_srw_diagnostic(odr_en,
                                &(sru_pdu_res->u.response->diagnostics), 
                                &(sru_pdu_res->u.response->num_diagnostics), 
-                               2, 0);
+                               YAZ_SRW_SYSTEM_TEMPORARILY_UNAVAILABLE, 0);
         return false;
     }
 
@@ -641,44 +719,37 @@ yf::SRUtoZ3950::Impl::z3950_present_request(mp::Package &package,
         // inserting all records
         for (int i = 0; i < sru_res->num_records; i++)
         {
+            int position = i + *apdu->u.presentRequest->resultSetStartPoint;
             Z_NamePlusRecord *npr 
                 = pr->records->u.databaseOrSurDiagnostics->records[i];
             
-            sru_res->records[i].recordPosition 
-                = odr_intdup(odr_en,
-                           i + *(apdu->u.presentRequest->resultSetStartPoint));
-            
             sru_res->records[i].recordPacking = record_packing;
             
-            if (npr->which != Z_NamePlusRecord_databaseRecord)
+            if (npr->which == Z_NamePlusRecord_databaseRecord &&
+                npr->u.databaseRecord->direct_reference 
+                && !oid_oidcmp(npr->u.databaseRecord->direct_reference,
+                               yaz_oid_recsyn_xml))
             {
-                sru_res->records[i].recordSchema = "diagnostic";
-                sru_res->records[i].recordData_buf = "67";
-                sru_res->records[i].recordData_len = 2;
+                // got XML record back
+                Z_External *r = npr->u.databaseRecord;
+                sru_res->records[i].recordPosition = 
+                    odr_intdup(odr_en, position);
+                sru_res->records[i].recordSchema = sr_req->recordSchema;
+                sru_res->records[i].recordData_buf
+                    = odr_strdupn(odr_en, 
+                                  (const char *)r->u.octet_aligned->buf, 
+                                  r->u.octet_aligned->len);
+                sru_res->records[i].recordData_len 
+                    = r->u.octet_aligned->len;
             }
             else
             {
-                Z_External *r = npr->u.databaseRecord;
-                oident *ent = oid_getentbyoid(r->direct_reference);
-                if (r->which == Z_External_octet 
-                    && ent->value == VAL_TEXT_XML)
-                {
-                    sru_res->records[i].recordSchema = "dc";
-                    sru_res->records[i].recordData_buf
-                        = odr_strdupn(odr_en, 
-                                      (const char *)r->u.octet_aligned->buf, 
-                                      r->u.octet_aligned->len);
-                    sru_res->records[i].recordData_len 
-                        = r->u.octet_aligned->len;
-                }
-                else
-                {
-                    sru_res->records[i].recordSchema = "diagnostic";
-                    sru_res->records[i].recordData_buf = "67";
-                    sru_res->records[i].recordData_len = 2;
-                }
-            }   
-        }    
+                // not XML or no database record at all
+                yaz_mk_sru_surrogate(
+                    odr_en, sru_res->records + i, position,
+                    YAZ_SRW_RECORD_NOT_AVAILABLE_IN_THIS_SCHEMA, 0);
+            }
+        }
     }
     
     return true;
@@ -844,8 +915,9 @@ extern "C" {
 /*
  * Local variables:
  * c-basic-offset: 4
+ * c-file-style: "Stroustrup"
  * indent-tabs-mode: nil
- * c-file-style: "stroustrup"
  * End:
  * vim: shiftwidth=4 tabstop=8 expandtab
  */
+