-/* $Id: filter_sru_to_z3950.cpp,v 1.9 2006-09-20 15:08:27 marc Exp $
- Copyright (c) 2005-2006, Index Data.
+/* $Id: filter_sru_to_z3950.cpp,v 1.37 2008-01-29 21:25:46 adam Exp $
+ Copyright (c) 2005-2007, Index Data.
- See the LICENSE file for details
+This file is part of Metaproxy.
+
+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 Metaproxy; see the file LICENSE. If not, write to the
+Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
+02111-1307, USA.
*/
#include "config.hpp"
#include "package.hpp"
#include "util.hpp"
#include "gduutil.hpp"
+#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 <boost/thread/mutex.hpp>
#include <iostream>
#include <sstream>
#include <string>
-
+#include <algorithm>
+#include <map>
namespace mp = metaproxy_1;
+namespace mp_util = metaproxy_1::util;
namespace yf = mp::filter;
-namespace metaproxy_1
-{
-
- template<typename T>
- std::string to_string(const T& t)
- {
- std::ostringstream o;
- if(o << t)
- return o.str();
-
- return std::string();
- }
-
- std::string http_header_value(const Z_HTTP_Header* header,
- const std::string name)
- {
- while (header && header->name
- && std::string(header->name) != name)
- header = header->next;
-
- if (header && header->name && std::string(header->name) == name
- && header->value)
- return std::string(header->value);
-
- return std::string();
- }
-
-
-}
-
namespace metaproxy_1 {
namespace filter {
- class SRUtoZ3950::Rep {
- //friend class SRUtoZ3950;
+ class SRUtoZ3950::Impl {
+ public:
+ void configure(const xmlNode *xmlnode);
+ void process(metaproxy_1::Package &package);
private:
union SRW_query {char * cql; char * xcql; char * pqf;};
typedef const int& SRW_query_type;
- public:
- void configure(const xmlNode *xmlnode);
- void process(metaproxy_1::Package &package) const;
+ std::map<std::string, const xmlNode *> m_database_explain;
private:
- std::string sru_protocol(const Z_HTTP_Request &http_req) const;
- std::string debug_http(const Z_HTTP_Request &http_req) const;
- void http_response(mp::Package &package,
- const std::string &content,
- int http_code = 200) const;
- bool build_sru_debug_package(mp::Package &package) const;
- bool build_sru_response(mp::Package &package,
- mp::odr &odr_en,
- Z_SOAP *soap,
- const Z_SRW_PDU *sru_pdu_res,
- char *charset,
- const char *stylesheet) const;
- Z_SRW_PDU * decode_sru_request(mp::Package &package,
- mp::odr &odr_de,
- Z_SOAP *&soap,
- char *charset,
- const char *stylesheet) const;
+
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,
- const std::string
- &database = "Default") const;
+ mp::odr &odr_en,
+ 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;
- //const std::string &database,
- //const std::string &query,
- //int query_type) 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;
- //unsigned int start_position,
- //unsigned int records_requested) 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_scan_request(mp::Package &package,
mp::odr &odr_en,
Z_SRW_PDU *sru_pdu_res,
Z_SRW_scanRequest
const *sr_req) const;
- //const std::string &database,
- //const std::string &query,
- //int query_type) const;
+
+ bool z3950_to_srw_diagnostics_ok(mp::odr &odr_en,
+ Z_SRW_searchRetrieveResponse *srw_res,
+ Z_Records *records) const;
+
+ int z3950_to_srw_diag(mp::odr &odr_en,
+ Z_SRW_searchRetrieveResponse *srw_res,
+ Z_DefaultDiagFormat *ddf) const;
};
}
}
-yf::SRUtoZ3950::SRUtoZ3950() : m_p(new Rep)
+yf::SRUtoZ3950::SRUtoZ3950() : m_p(new Impl)
{
}
m_p->process(package);
}
-void yf::SRUtoZ3950::Rep::configure(const xmlNode *xmlnode)
+void yf::SRUtoZ3950::Impl::configure(const xmlNode *confignode)
{
+ const xmlNode * dbnode;
+
+ for (dbnode = confignode->children; dbnode; dbnode = dbnode->next){
+ if (dbnode->type != XML_ELEMENT_NODE)
+ continue;
+
+ std::string database;
+ mp::xml::check_element_mp(dbnode, "database");
+
+ for (struct _xmlAttr *attr = dbnode->properties;
+ attr; attr = attr->next){
+
+ mp::xml::check_attribute(attr, "", "name");
+ database = mp::xml::get_text(attr);
+
+ const xmlNode *explainnode;
+ for (explainnode = dbnode->children;
+ explainnode; explainnode = explainnode->next){
+ if (explainnode->type != XML_ELEMENT_NODE)
+ continue;
+ if (explainnode)
+ break;
+ }
+ // assigning explain node to database name - no check yet
+ m_database_explain.insert(std::make_pair(database, explainnode));
+ }
+ }
}
-void yf::SRUtoZ3950::Rep::process(mp::Package &package) const
+void yf::SRUtoZ3950::Impl::process(mp::Package &package)
{
Z_GDU *zgdu_req = package.request().get();
// only working on HTTP_Request packages now
-
- // TODO: Z3950 response parsing and translation to SRU package
bool ok = true;
- Z_SRW_PDU *sru_pdu_req = 0;
mp::odr odr_de(ODR_DECODE);
+ Z_SRW_PDU *sru_pdu_req = 0;
- Z_SRW_PDU *sru_pdu_res = 0;
mp::odr odr_en(ODR_ENCODE);
+ Z_SRW_PDU *sru_pdu_res = yaz_srw_get(odr_en, Z_SRW_explain_response);
+
+ // determine database with the HTTP header information only
+ mp_util::SRUServerInfo sruinfo = mp_util::get_sru_server_info(package);
+ std::map<std::string, const xmlNode *>::iterator idbexp;
+ idbexp = m_database_explain.find(sruinfo.database);
+ // assign explain config XML DOM node if database is known
+ const xmlNode *explainnode = 0;
+ if (idbexp != m_database_explain.end()){
+ explainnode = idbexp->second;
+ }
+ // just moving package if database is not known
+ else {
+ package.move();
+ return;
+ }
+
+
+ // decode SRU request
Z_SOAP *soap = 0;
char *charset = 0;
- const char *stylesheet = 0;
+ char *stylesheet = 0;
- if (! (sru_pdu_req = decode_sru_request(package, odr_de, soap,
+ // 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,
charset, stylesheet)))
{
- sru_pdu_res = yaz_srw_get(odr_en, Z_SRW_explain_response);
- std::cout << "TODO: correct error when SRW package could not be"
- " parsed\n";
- //build_sru_debug_package(package);
- build_sru_response(package, odr_en, soap,
- sru_pdu_res, charset, stylesheet);
+ 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;
}
-
- // SRU request package translation to Z3950 package
- if (sru_pdu_req)
- std::cout << *sru_pdu_req << "\n";
- else
- std::cout << "SRU empty\n";
-
+ 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);
+ }
- // explain
+
+ // filter acts as sink for SRU explain requests
if (sru_pdu_req && sru_pdu_req->which == Z_SRW_explain_request)
{
- //Z_SRW_searchRetrieveRequest *sr_req = sru_pdu_req->u.request;
- sru_pdu_res = yaz_srw_get(odr_en, Z_SRW_explain_response);
- std::cout << "TODO: implement explain response";
-
+ Z_SRW_explainRequest *er_req = sru_pdu_req->u.explain_request;
+ //mp_util::build_simple_explain(package, odr_en, sru_pdu_res,
+ // sruinfo, er_req);
+ mp_util::build_sru_explain(package, odr_en, sru_pdu_res,
+ sruinfo, explainnode, er_req);
}
-
-
- // searchRetrieve
- if (sru_pdu_req && sru_pdu_req->which == Z_SRW_searchRetrieve_request)
- {
+ 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);
- // TODO: checking of unsupported operations and proper diagnostic setup
-
- // recordXPath unsupported.
- //if (sr_req->recordXPath)
- // yaz_add_srw_diagnostic(odr_decode(),
- // &diag, &num_diags, 72, 0);
- // sort unsupported
- // if (sr_req->sort_type != Z_SRW_sort_type_none)
- // yaz_add_srw_diagnostic(odr_decode(),
- // &diag, &num_diags, 80, 0);
-
+ // checking that we have a query
+ ok = mp_util::check_sru_query_exists(package, odr_en,
+ sru_pdu_res, sr_req);
- if (z3950_init_request(package))
+ 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))
-
- 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);
}
}
// scan
- else if (sru_pdu_req && sru_pdu_req->which == Z_SRW_scan_request)
+ else if (sru_pdu_req
+ && sru_pdu_req->which == Z_SRW_scan_request
+ && sru_pdu_req->u.scan_request)
{
Z_SRW_scanRequest *sr_req = sru_pdu_req->u.scan_request;
sru_pdu_res = yaz_srw_get(odr_en, Z_SRW_scan_response);
- if (z3950_init_request(package))
+ // we do not do scan at the moment, therefore issuing a diagnostic
+ yaz_add_srw_diagnostic(odr_en,
+ &(sru_pdu_res->u.scan_response->diagnostics),
+ &(sru_pdu_res->u.scan_response->num_diagnostics),
+ 4, "scan");
+
+ // to be used when we do scan
+ 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);
}
else
{
- std::cout << "SRU OPERATION NOT SUPPORTED \n";
+ //std::cout << "SRU OPERATION NOT SUPPORTED \n";
sru_pdu_res = yaz_srw_get(odr_en, Z_SRW_explain_response);
// TODO: make nice diagnostic return package
- package.session().close();
return;
}
- //build_sru_debug_package(package);
- build_sru_response(package, odr_en, soap,
- sru_pdu_res, charset, stylesheet);
- return;
-
-
-
-
-
-
-
-}
-
-bool yf::SRUtoZ3950::Rep::build_sru_debug_package(mp::Package &package) const
-{
- Z_GDU *zgdu_req = package.request().get();
- if (zgdu_req && zgdu_req->which == Z_GDU_HTTP_Request)
- {
- Z_HTTP_Request* http_req = zgdu_req->u.HTTP_Request;
- std::string content = debug_http(*http_req);
- int http_code = 400;
- http_response(package, content, http_code);
- return true;
- }
- package.session().close();
- return false;
-}
-
-
-bool yf::SRUtoZ3950::Rep::build_sru_response(mp::Package &package,
- mp::odr &odr_en,
- Z_SOAP *soap,
- const Z_SRW_PDU *sru_pdu_res,
- char *charset,
- const char *stylesheet)
- const
-{
-
- // SRU request package translation to Z3950 package
- if (sru_pdu_res)
- std::cout << *(const_cast<Z_SRW_PDU *>(sru_pdu_res)) << "\n";
- else
- std::cout << "SRU empty\n";
-
-
- Z_GDU *zgdu_req = package.request().get();
- if (zgdu_req && zgdu_req->which == Z_GDU_HTTP_Request)
- {
- Z_GDU *zgdu_res //= z_get_HTTP_Response(odr_en, 200);
- = odr_en.create_HTTP_Response(package.session(),
- zgdu_req->u.HTTP_Request,
- 200);
- Z_HTTP_Response * http_res = zgdu_res->u.HTTP_Response;
-
- static Z_SOAP_Handler soap_handlers[4] = {
- {"http://www.loc.gov/zing/srw/", 0,
- (Z_SOAP_fun) yaz_srw_codec},
- {"http://www.loc.gov/zing/srw/v1.0/", 0,
- (Z_SOAP_fun) yaz_srw_codec},
- {"http://www.loc.gov/zing/srw/update/", 0,
- (Z_SOAP_fun) yaz_ucp_codec},
- {0, 0, 0}
- };
-
- //if (!stylesheet && assoc->server)
- //stylesheet = assoc->server->stylesheet;
-
- /* empty stylesheet means NO stylesheet */
- //if (stylesheet && *stylesheet == '\0')
- // stylesheet = 0;
- //ret = z_soap_codec_enc_xsl(assoc->encode, &soap_package,
- //&hres->content_buf, &hres->content_len,
- // soap_handlers, charset, stylesheet);
-
- // encoding SRU package
-
- soap->u.generic->p = (void*) sru_pdu_res;
- //int ret =
- z_soap_codec_enc_xsl(odr_en, &soap,
- &http_res->content_buf, &http_res->content_len,
- soap_handlers, charset, 0);
-
- package.response() = zgdu_res;
- return true;
- }
- package.session().close();
- return false;
+ // build and send SRU response
+ mp_util::build_sru_response(package, odr_en, soap,
+ sru_pdu_res, charset, stylesheet);
}
- Z_SRW_PDU * yf::SRUtoZ3950::Rep::decode_sru_request(mp::Package &package,
- mp::odr &odr_de,
- Z_SOAP *&soap,
- char *charset,
- const char *stylesheet)
- const
-{
- Z_GDU *zgdu_req = package.request().get();
- Z_SRW_PDU *sru_pdu_req = 0;
-
- // ignoring all non HTTP_Request packages
- if (!zgdu_req || !(zgdu_req->which == Z_GDU_HTTP_Request)){
- return 0;
- }
-
- Z_HTTP_Request* http_req = zgdu_req->u.HTTP_Request;
- if (! http_req)
- return 0;
-
- Z_SRW_diagnostic *diag = 0;
- int num_diags = 0;
-
- if (0 == yaz_sru_decode(http_req, &sru_pdu_req, &soap,
- odr_de, &charset, &diag, &num_diags))
- {
- if (num_diags)
- {
- std::cout << "SRU DECODE DIAGNOSTICS " << num_diags << "\n";
- // TODO: make nice diagnostic return package
- //Z_SRW_PDU *srw_pdu_res =
- // yaz_srw_get(odr(ODR_ENCODE),
- // Z_SRW_searchRetrieve_response);
- // Z_SRW_searchRetrieveResponse *srw_res = srw_pdu_res->u.response;
-
- // srw_res->diagnostics = diagnostic;
- // srw_res->num_diagnostics = num_diagnostic;
- // send_srw_response(srw_pdu_res);
- // return;
-
- // package.session().close();
- //return 0;
- }
- return sru_pdu_req;
- }
- else if (0 == yaz_srw_decode(http_req, &sru_pdu_req, &soap,
- odr_de, &charset))
- return sru_pdu_req;
- else
- {
- //std::cout << "SRU DECODING ERROR - SHOULD NEVER HAPPEN\n";
- package.session().close();
- return 0;
- }
- return 0;
-}
-
bool
-yf::SRUtoZ3950::Rep::z3950_init_request(mp::Package &package,
- const std::string &database) const
+yf::SRUtoZ3950::Impl::z3950_init_request(mp::Package &package,
+ mp::odr &odr_en,
+ std::string zurl,
+ Z_SRW_PDU *sru_pdu_res) const
{
// prepare Z3950 package
- //Session s;
- //Package z3950_package(s, package.origin());
Package z3950_package(package.session(), package.origin());
z3950_package.copy_filter(package);
// set initRequest APDU
- mp::odr odr_en(ODR_ENCODE);
Z_APDU *apdu = zget_APDU(odr_en, Z_APDU_initRequest);
Z_InitRequest *init_req = apdu->u.initRequest;
//TODO: add user name in apdu
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
- // std::cout << "z3950_init_request " << *apdu <<"\n";
z3950_package.move();
// dead Z3950 backend detection
if (z3950_package.session().is_closed()){
- package.session().close();
+ yaz_add_srw_diagnostic(odr_en,
+ &(sru_pdu_res->u.response->diagnostics),
+ &(sru_pdu_res->u.response->num_diagnostics),
+ 2, 0);
return false;
}
Z_GDU *z3950_gdu = z3950_package.response().get();
if (z3950_gdu && z3950_gdu->which == Z_GDU_Z3950
- && z3950_gdu->u.z3950->which == Z_APDU_initResponse)
+ && z3950_gdu->u.z3950->which == Z_APDU_initResponse
+ && *z3950_gdu->u.z3950->u.initResponse->result)
return true;
+ yaz_add_srw_diagnostic(odr_en,
+ &(sru_pdu_res->u.response->diagnostics),
+ &(sru_pdu_res->u.response->num_diagnostics),
+ 2, 0);
return false;
}
bool
-yf::SRUtoZ3950::Rep::z3950_close_request(mp::Package &package) const
+yf::SRUtoZ3950::Impl::z3950_close_request(mp::Package &package) const
{
- // prepare Z3950 package
+ // prepare and close Z3950 package
Package z3950_package(package.session(), package.origin());
z3950_package.copy_filter(package);
z3950_package.session().close();
// return true;
if (z3950_package.session().is_closed()){
- //package.session().close();
return true;
}
return false;
}
-bool
-yf::SRUtoZ3950::Rep::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);
+
Package z3950_package(package.session(), package.origin());
- z3950_package.copy_filter(package);
- //mp::odr odr_en(ODR_ENCODE);
- Z_APDU *apdu = zget_APDU(odr_en, Z_APDU_searchRequest);
+ z3950_package.copy_filter(package);
//add stuff in z3950 apdu
+ 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]
- = odr_strdup(odr_en, "Default");
+ 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));
(const SRW_query&)sr_req->query,
sr_req->query_type))
{
- //send_to_srw_client_error(7, "query");
+ yaz_add_srw_diagnostic(odr_en,
+ &(sru_pdu_res->u.response->diagnostics),
+ &(sru_pdu_res->u.response->num_diagnostics),
+ 7, "query");
return false;
}
z3950_package.request() = apdu;
- //std::cout << "z3950_search_request " << *apdu << "\n";
+ // send Z39.50 package off to backend
z3950_package.move();
- //TODO: check success condition
+ Z_GDU *z3950_gdu = z3950_package.response().get();
+
+ //TODO: check success condition
//int yaz_diag_bib1_to_srw (int bib1_code);
//int yaz_diag_srw_to_bib1(int srw_code);
//Se kode i src/seshigh.c (srw_bend_search, srw_bend_init).
- Z_GDU *z3950_gdu = z3950_package.response().get();
- //std::cout << "z3950_search_request " << *z3950_gdu << "\n";
-
- if (z3950_gdu && z3950_gdu->which == Z_GDU_Z3950
- && z3950_gdu->u.z3950->which == Z_APDU_searchResponse
- && z3950_gdu->u.z3950->u.searchResponse->searchStatus)
+ if (!z3950_gdu || z3950_gdu->which != Z_GDU_Z3950
+ || z3950_gdu->u.z3950->which != Z_APDU_searchResponse
+ || !z3950_gdu->u.z3950->u.searchResponse
+ || !z3950_gdu->u.z3950->u.searchResponse->searchStatus)
{
+ yaz_add_srw_diagnostic(odr_en,
+ &(sru_pdu_res->u.response->diagnostics),
+ &(sru_pdu_res->u.response->num_diagnostics),
+ 2, 0);
+ return false;
+ }
+
+ // everything fine, continuing
+ Z_SearchResponse *sr = z3950_gdu->u.z3950->u.searchResponse;
- Z_SearchResponse *sr = z3950_gdu->u.z3950->u.searchResponse;
- if (sr)
- {
- // srw'fy number of records
- sru_pdu_res->u.response->numberOfRecords
- = (int *) odr_malloc(odr_en, sizeof(int *));
- *(sru_pdu_res->u.response->numberOfRecords) = *(sr->resultCount);
- }
-
- return true;
+ // checking non surrogate diagnostics in Z3950 search response package
+ if (!z3950_to_srw_diagnostics_ok(odr_en, sru_pdu_res->u.response,
+ sr->records))
+ {
+ return false;
}
+
+ // Finally, roll on and srw'fy number of records
+ sru_pdu_res->u.response->numberOfRecords
+ = (int *) odr_malloc(odr_en, sizeof(int *));
+ *(sru_pdu_res->u.response->numberOfRecords) = *(sr->resultCount);
- return false;
+ // srw'fy nextRecordPosition
+ //sru_pdu_res->u.response->nextRecordPosition
+ // = (int *) odr_malloc(odr_en, sizeof(int *));
+ //*(sru_pdu_res->u.response->nextRecordPosition) = 1;
+
+ return true;
}
bool
-yf::SRUtoZ3950::Rep::z3950_present_request(mp::Package &package,
+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)
const
{
+ assert(sru_pdu_res->u.response);
if (!sr_req)
return false;
- // no need to work if nobody wants records seen ..
+ // no need to work if nobody wants record ..
if (!(sr_req->maximumRecords) || 0 == *(sr_req->maximumRecords))
return true;
- // creating Z3950 package
+ bool send_z3950_present = true;
+
+ // recordXPath unsupported.
+ if (sr_req->recordXPath)
+ {
+ send_z3950_present = false;
+ yaz_add_srw_diagnostic(odr_en,
+ &(sru_pdu_res->u.response->diagnostics),
+ &(sru_pdu_res->u.response->num_diagnostics),
+ 72, 0);
+ }
+
+ // resultSetTTL unsupported.
+ // resultSetIdleTime in response
+ if (sr_req->resultSetTTL)
+ {
+ send_z3950_present = false;
+ yaz_add_srw_diagnostic(odr_en,
+ &(sru_pdu_res->u.response->diagnostics),
+ &(sru_pdu_res->u.response->num_diagnostics),
+ 50, 0);
+ }
+
+ // sort unsupported
+ if (sr_req->sort_type != Z_SRW_sort_type_none)
+ {
+ send_z3950_present = false;
+ yaz_add_srw_diagnostic(odr_en,
+ &(sru_pdu_res->u.response->diagnostics),
+ &(sru_pdu_res->u.response->num_diagnostics),
+ 80, 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))
+ ))
+ {
+ 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);
+ }
+
+ // maximumRecords requested negative
+ if (sr_req->maximumRecords
+ && *(sr_req->maximumRecords) < 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");
+ }
+
+ // exit on all these above diagnostics
+ if (!send_z3950_present)
+ return false;
+
+ // now packaging the z3950 present request
Package z3950_package(package.session(), package.origin());
z3950_package.copy_filter(package);
- //mp::odr odr_en(ODR_ENCODE);
Z_APDU *apdu = zget_APDU(odr_en, Z_APDU_presentRequest);
- //TODO: add stuff in apdu
assert(apdu->u.presentRequest);
// z3950'fy start record position
*(apdu->u.presentRequest->resultSetStartPoint) = 1;
// z3950'fy number of records requested
+ // protect against requesting records out of range
if (sr_req->maximumRecords)
*(apdu->u.presentRequest->numberOfRecordsRequested)
- = *(sr_req->maximumRecords);
+ = std::min(*(sr_req->maximumRecords),
+ *(sru_pdu_res->u.response->numberOfRecords)
+ - *(apdu->u.presentRequest->resultSetStartPoint)
+ + 1);
- // TODO: z3950'fy record schema
- //if (sr_req->recordSchema)
- // *(apdu->u.presentRequest->preferredRecordSyntax)
- // = *(sr_req->recordSchema);
-
+ // 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
+ = 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);
+ }
+
+ // z3950'fy time to live - flagged as diagnostics above
+ //if (sr_req->resultSetTTL)
+
+ // attaching Z3950 package to filter chain
z3950_package.request() = apdu;
- //std::cout << "z3950_present_request " << *apdu << "\n";
+ // sending Z30.50 present request
z3950_package.move();
- //TODO: check success condition
+ //check successful Z3950 present response
Z_GDU *z3950_gdu = z3950_package.response().get();
- if (z3950_gdu && z3950_gdu->which == Z_GDU_Z3950
- && z3950_gdu->u.z3950->which == Z_APDU_presentResponse)
+ if (!z3950_gdu || z3950_gdu->which != Z_GDU_Z3950
+ || z3950_gdu->u.z3950->which != Z_APDU_presentResponse
+ || !z3950_gdu->u.z3950->u.presentResponse)
{
- //std::cout << "z3950_present_request OK\n";
+ yaz_add_srw_diagnostic(odr_en,
+ &(sru_pdu_res->u.response->diagnostics),
+ &(sru_pdu_res->u.response->num_diagnostics),
+ 2, 0);
+ return false;
+ }
- Z_PresentResponse *pr = z3950_gdu->u.z3950->u.presentResponse;
- if (pr)
- {
- // srw'fy number of returned records
- //sru_pdu_res->u.response->num_records
- // = *(pr->numberOfRecordsReturned);
- //= (int *) odr_malloc(odr_en, sizeof(int *));
- //*(sru_pdu_res->u.response->num_records)
- // = *(pr->numberOfRecordsReturned);
-
- // srw'fy nextRecordPosition
- sru_pdu_res->u.response->nextRecordPosition
- = (int *) odr_malloc(odr_en, sizeof(int *));
- *(sru_pdu_res->u.response->nextRecordPosition)
- = *(pr->nextResultSetPosition);
- }
- return true;
- }
+ // everything fine, continuing
- return false;
+ Z_PresentResponse *pr = z3950_gdu->u.z3950->u.presentResponse;
+ Z_SRW_searchRetrieveResponse *sru_res = sru_pdu_res->u.response;
+
+
+ // checking non surrogate diagnostics in Z3950 present response package
+ if (!z3950_to_srw_diagnostics_ok(odr_en, sru_pdu_res->u.response,
+ pr->records))
+ return false;
+
+
+
+ // copy all records if existing
+ if (pr->records && pr->records->which == Z_Records_DBOSD)
+ {
+ // srw'fy number of returned records
+ sru_res->num_records
+ = pr->records->u.databaseOrSurDiagnostics->num_records;
+
+ sru_res->records
+ = (Z_SRW_record *) odr_malloc(odr_en,
+ sru_res->num_records
+ * sizeof(Z_SRW_record));
+
+
+ // srw'fy nextRecordPosition
+ // next position never zero or behind the last z3950 record
+ if (pr->nextResultSetPosition
+ && *(pr->nextResultSetPosition) > 0
+ && *(pr->nextResultSetPosition)
+ <= *(sru_pdu_res->u.response->numberOfRecords))
+ sru_res->nextRecordPosition
+ = odr_intdup(odr_en, *(pr->nextResultSetPosition));
+
+ // inserting all records
+ for (int i = 0; i < sru_res->num_records; i++)
+ {
+ 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)
+ {
+ sru_res->records[i].recordSchema = "diagnostic";
+ sru_res->records[i].recordData_buf = "67";
+ sru_res->records[i].recordData_len = 2;
+ }
+ else
+ {
+ Z_External *r = npr->u.databaseRecord;
+ if (r->direct_reference
+ && !oid_oidcmp(r->direct_reference, yaz_oid_recsyn_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;
+ }
+ }
+ }
+ }
+
+ return true;
}
bool
-yf::SRUtoZ3950::Rep::z3950_scan_request(mp::Package &package,
+yf::SRUtoZ3950::Impl::z3950_scan_request(mp::Package &package,
mp::odr &odr_en,
Z_SRW_PDU *sru_pdu_res,
Z_SRW_scanRequest const *sr_req) const
{
+ assert(sru_pdu_res->u.scan_response);
+
Package z3950_package(package.session(), package.origin());
z3950_package.copy_filter(package);
//mp::odr odr_en(ODR_ENCODE);
return false;
}
-bool yf::SRUtoZ3950::Rep::z3950_build_query(mp::odr &odr_en, Z_Query *z_query,
+bool yf::SRUtoZ3950::Impl::z3950_build_query(mp::odr &odr_en, Z_Query *z_query,
const SRW_query &query,
SRW_query_type query_type) const
{
}
-std::string
-yf::SRUtoZ3950::Rep::sru_protocol(const Z_HTTP_Request &http_req) const
-{
- const std::string mime_urlencoded("application/x-www-form-urlencoded");
- const std::string mime_text_xml("text/xml");
- const std::string mime_soap_xml("application/soap+xml");
-
- const std::string http_method(http_req.method);
- const std::string http_type
- = http_header_value(http_req.headers, "Content-Type");
-
- if (http_method == "GET")
- return "SRU GET";
-
- if (http_method == "POST"
- && http_type == mime_urlencoded)
- return "SRU POST";
-
- if ( http_method == "POST"
- && (http_type == mime_text_xml
- || http_type == mime_soap_xml))
- return "SRU SOAP";
-
- return "HTTP";
-}
-
-std::string
-yf::SRUtoZ3950::Rep::debug_http(const Z_HTTP_Request &http_req) const
+bool
+yf::SRUtoZ3950::Impl::z3950_to_srw_diagnostics_ok(mp::odr &odr_en,
+ Z_SRW_searchRetrieveResponse
+ *sru_res,
+ Z_Records *records) const
{
- std::string message("<html>\n<body>\n<h1>"
- "Metaproxy SRUtoZ3950 filter"
- "</h1>\n");
-
- message += "<h3>HTTP Info</h3><br/>\n";
- message += "<p>\n";
- message += "<b>Method: </b> " + std::string(http_req.method) + "<br/>\n";
- message += "<b>Version:</b> " + std::string(http_req.version) + "<br/>\n";
- message += "<b>Path: </b> " + std::string(http_req.path) + "<br/>\n";
-
- message += "<b>Content-Type:</b>"
- + http_header_value(http_req.headers, "Content-Type")
- + "<br/>\n";
- message += "<b>Content-Length:</b>"
- + http_header_value(http_req.headers, "Content-Length")
- + "<br/>\n";
- message += "</p>\n";
-
- message += "<h3>Headers</h3><br/>\n";
- message += "<p>\n";
- Z_HTTP_Header* header = http_req.headers;
- while (header){
- message += "<b>Header: </b> <i>"
- + std::string(header->name) + ":</i> "
- + std::string(header->value) + "<br/>\n";
- header = header->next;
+ // checking non surrogate diagnostics in Z3950 present response package
+ if (records
+ && records->which == Z_Records_NSD
+ && records->u.nonSurrogateDiagnostic)
+ {
+ z3950_to_srw_diag(odr_en, sru_res,
+ records->u.nonSurrogateDiagnostic);
+ return false;
}
- message += "</p>\n";
- message += "</body>\n</html>\n";
- return message;
+ return true;
}
-void yf::SRUtoZ3950::Rep::http_response(metaproxy_1::Package &package,
- const std::string &content,
- int http_code) const
-{
- Z_GDU *zgdu_req = package.request().get();
- Z_GDU *zgdu_res = 0;
- mp::odr odr;
- zgdu_res
- = odr.create_HTTP_Response(package.session(),
- zgdu_req->u.HTTP_Request,
- http_code);
-
- zgdu_res->u.HTTP_Response->content_len = content.size();
- zgdu_res->u.HTTP_Response->content_buf
- = (char*) odr_malloc(odr, zgdu_res->u.HTTP_Response->content_len);
-
- strncpy(zgdu_res->u.HTTP_Response->content_buf,
- content.c_str(), zgdu_res->u.HTTP_Response->content_len);
-
- //z_HTTP_header_add(odr, &hres->headers,
- // "Content-Type", content_type.c_str());
- package.response() = zgdu_res;
+int
+yf::SRUtoZ3950::Impl::z3950_to_srw_diag(mp::odr &odr_en,
+ Z_SRW_searchRetrieveResponse *sru_res,
+ Z_DefaultDiagFormat *ddf) const
+{
+ int bib1_code = *ddf->condition;
+ sru_res->num_diagnostics = 1;
+ sru_res->diagnostics = (Z_SRW_diagnostic *)
+ odr_malloc(odr_en, sizeof(*sru_res->diagnostics));
+ yaz_mk_std_diagnostic(odr_en, sru_res->diagnostics,
+ yaz_diag_bib1_to_srw(bib1_code),
+ ddf->u.v2Addinfo);
+ return 0;
}
-
static mp::filter::Base* filter_creator()
{
return new mp::filter::SRUtoZ3950;
extern "C" {
struct metaproxy_1_filter_struct metaproxy_1_filter_sru_to_z3950 = {
0,
- "SRUtoZ3950",
+ "sru_z3950",
filter_creator
};
}