X-Git-Url: http://sru.miketaylor.org.uk/?a=blobdiff_plain;f=src%2Ffilter_sru_to_z3950.cpp;h=2b62986299271a2e3f38014297f23668d58f6d2d;hb=9a318d33c4c99ee4158d39e3a62988af0229d7cf;hp=96c820bc4ee005c7db6757a3c440617ebbd60094;hpb=e6d572ef110d14fa55115021505b808620457bd3;p=metaproxy-moved-to-github.git
diff --git a/src/filter_sru_to_z3950.cpp b/src/filter_sru_to_z3950.cpp
index 96c820b..2b62986 100644
--- a/src/filter_sru_to_z3950.cpp
+++ b/src/filter_sru_to_z3950.cpp
@@ -1,5 +1,5 @@
/* This file is part of Metaproxy.
- Copyright (C) 2005-2010 Index Data
+ Copyright (C) 2005-2012 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
@@ -62,11 +62,11 @@ namespace metaproxy_1 {
ActiveUrlMap m_active_urls;
private:
void sru(metaproxy_1::Package &package, Z_GDU *zgdu_req);
- bool z3950_build_query(
+ int z3950_build_query(
mp::odr &odr_en, Z_Query *z_query,
const Z_SRW_searchRetrieveRequest *req
) const;
-
+
bool z3950_init_request(
mp::Package &package,
mp::odr &odr_en,
@@ -116,7 +116,8 @@ yf::SRUtoZ3950::~SRUtoZ3950()
{ // must have a destructor because of boost::scoped_ptr
}
-void yf::SRUtoZ3950::configure(const xmlNode *xmlnode, bool test_only)
+void yf::SRUtoZ3950::configure(const xmlNode *xmlnode, bool test_only,
+ const char *path)
{
m_p->configure(xmlnode);
}
@@ -156,7 +157,7 @@ void yf::SRUtoZ3950::Impl::configure(const xmlNode *confignode)
}
// assigning explain node to database name - no check yet
m_database_explain.insert(std::make_pair(database, explainnode));
- }
+ }
}
}
@@ -210,6 +211,7 @@ void yf::SRUtoZ3950::Impl::sru(mp::Package &package, Z_GDU *zgdu_req)
return;
}
+ bool enable_package_log = false;
std::string zurl;
Z_SRW_extra_arg *arg;
@@ -222,7 +224,18 @@ void yf::SRUtoZ3950::Impl::sru(mp::Package &package, Z_GDU *zgdu_req)
{
package.origin().set_max_sockets(atoi(arg->value));
}
-
+ else if (!strcmp(arg->name, "x-session-id"))
+ {
+ package.origin().set_custom_session(arg->value);
+ }
+ else if (!strcmp(arg->name, "x-log-enable"))
+ {
+ if (*arg->value == '1')
+ {
+ enable_package_log = true;
+ package.log_enable();
+ }
+ }
assert(sru_pdu_req);
// filter acts as sink for SRU explain requests
@@ -253,10 +266,8 @@ void yf::SRUtoZ3950::Impl::sru(mp::Package &package, Z_GDU *zgdu_req)
if (ok
&& sru_pdu_res->u.response->numberOfRecords
- && *(sru_pdu_res->u.response->numberOfRecords)
- && sr_req->maximumRecords
- && *(sr_req->maximumRecords))
-
+ && *(sru_pdu_res->u.response->numberOfRecords))
+
ok = z3950_present_request(package, odr_en,
sru_pdu_res,
sr_req);
@@ -286,6 +297,25 @@ void yf::SRUtoZ3950::Impl::sru(mp::Package &package, Z_GDU *zgdu_req)
YAZ_SRW_UNSUPP_OPERATION, "unknown");
}
+ if (enable_package_log)
+ {
+ std::string l;
+ package.log_reset(l);
+ if (l.length())
+ {
+ WRBUF w = wrbuf_alloc();
+
+ wrbuf_puts(w, "\n");
+ wrbuf_xmlputs(w, l.c_str());
+ wrbuf_puts(w, "");
+
+ sru_pdu_res->extraResponseData_len = wrbuf_len(w);
+ sru_pdu_res->extraResponseData_buf =
+ odr_strdup(odr_en, wrbuf_cstr(w));
+ wrbuf_destroy(w);
+ }
+ }
+
// build and send SRU response
mp_util::build_sru_response(package, odr_en, soap,
sru_pdu_res, charset, stylesheet);
@@ -370,6 +400,9 @@ yf::SRUtoZ3950::Impl::z3950_init_request(mp::Package &package,
}
init_req->idAuthentication = auth;
+
+ *init_req->preferredMessageSize = 10*1024*1024;
+ *init_req->maximumRecordSize = 10*1024*1024;
ODR_MASK_SET(init_req->options, Z_Options_search);
ODR_MASK_SET(init_req->options, Z_Options_present);
@@ -409,7 +442,7 @@ yf::SRUtoZ3950::Impl::z3950_init_request(mp::Package &package,
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;
+ return true;
yaz_add_srw_diagnostic(odr_en,
&(sru_pdu_res->u.response->diagnostics),
@@ -463,10 +496,13 @@ bool 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;
+ // RecordSyntax will always be XML
+ z_searchRequest->preferredRecordSyntax
+ = odr_oiddup(odr_en, yaz_oid_recsyn_xml);
if (!mp_util::set_databases_from_zurl(odr_en, zurl,
- &z_searchRequest->num_databaseNames,
- &z_searchRequest->databaseNames))
+ &z_searchRequest->num_databaseNames,
+ &z_searchRequest->databaseNames))
{
z_searchRequest->num_databaseNames = 1;
z_searchRequest->databaseNames = (char**)
@@ -477,19 +513,21 @@ bool yf::SRUtoZ3950::Impl::z3950_search_request(mp::Package &package,
= odr_strdup(odr_en, const_cast(sr_req->database));
else
z_searchRequest->databaseNames[0]
- = odr_strdup(odr_en, "Default");
+ = odr_strdup(odr_en, "Default");
}
// z3950'fy query
Z_Query *z_query = (Z_Query *) odr_malloc(odr_en, sizeof(Z_Query));
z_searchRequest->query = z_query;
- if (!z3950_build_query(odr_en, z_query, sr_req))
+ int sru_diagnostic = z3950_build_query(odr_en, z_query, sr_req);
+ if (sru_diagnostic)
{
yaz_add_srw_diagnostic(odr_en,
&(sru_pdu_res->u.response->diagnostics),
&(sru_pdu_res->u.response->num_diagnostics),
- 7, "query");
+ sru_diagnostic,
+ "query");
return false;
}
@@ -514,7 +552,7 @@ bool yf::SRUtoZ3950::Impl::z3950_search_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;
}
@@ -541,11 +579,11 @@ bool 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)
+yf::SRUtoZ3950::Impl::z3950_present_request(
+ mp::Package &package,
+ mp::odr &odr_en,
+ Z_SRW_PDU *sru_pdu_res,
+ const Z_SRW_searchRetrieveRequest *sr_req)
const
{
assert(sru_pdu_res->u.response);
@@ -621,148 +659,130 @@ yf::SRUtoZ3950::Impl::z3950_present_request(mp::Package &package,
// 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);
- Z_APDU *apdu = zget_APDU(odr_en, Z_APDU_presentRequest);
-
- assert(apdu->u.presentRequest);
-
- // z3950'fy start record position
- *apdu->u.presentRequest->resultSetStartPoint = start;
-
- // z3950'fy number of records requested
- // protect against requesting records out of range
- if (max_recs < *sru_pdu_res->u.response->numberOfRecords - start + 1)
- *apdu->u.presentRequest->numberOfRecordsRequested = max_recs;
- else
- *apdu->u.presentRequest->numberOfRecordsRequested =
- *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
- = 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 (max_recs > *sru_pdu_res->u.response->numberOfRecords - start)
+ max_recs = *sru_pdu_res->u.response->numberOfRecords - start + 1;
- // z3950'fy time to live - flagged as diagnostics above
- //if (sr_req->resultSetTTL)
-
- // attaching Z3950 package to filter chain
- z3950_package.request() = apdu;
-
- // sending Z30.50 present request
- z3950_package.move();
-
- //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
- || !z3950_gdu->u.z3950->u.presentResponse)
-
- {
- 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;
- }
-
-
- // everything fine, continuing
-
- 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)
+ sru_res->records = (Z_SRW_record *)
+ odr_malloc(odr_en, max_recs * sizeof(Z_SRW_record));
+ int num = 0;
+ while (num < max_recs)
{
- // srw'fy number of returned records
- sru_res->num_records
- = pr->records->u.databaseOrSurDiagnostics->num_records;
+ // now packaging the z3950 present request
+ Package z3950_package(package.session(), package.origin());
+ z3950_package.copy_filter(package);
+ Z_APDU *apdu = zget_APDU(odr_en, Z_APDU_presentRequest);
- sru_res->records
- = (Z_SRW_record *) odr_malloc(odr_en,
- sru_res->num_records
- * sizeof(Z_SRW_record));
+ assert(apdu->u.presentRequest);
-
- // 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));
+ *apdu->u.presentRequest->resultSetStartPoint = start + num;
+ *apdu->u.presentRequest->numberOfRecordsRequested = max_recs - num;
+
+ // set response packing to be same as "request" packing..
+ int record_packing = Z_SRW_recordPacking_XML;
+ if (sr_req->recordPacking && 's' == *(sr_req->recordPacking))
+ record_packing = Z_SRW_recordPacking_string;
- // inserting all records
- for (int i = 0; i < sru_res->num_records; i++)
+ // 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)
{
- int position = i + *apdu->u.presentRequest->resultSetStartPoint;
- Z_NamePlusRecord *npr
- = pr->records->u.databaseOrSurDiagnostics->records[i];
-
- sru_res->records[i].recordPacking = record_packing;
+ 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);
+ }
+
+ // attaching Z3950 package to filter chain
+ z3950_package.request() = apdu;
+
+ // sending Z30.50 present request
+ z3950_package.move();
+
+ //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
+ || !z3950_gdu->u.z3950->u.presentResponse)
- if (npr->which == Z_NamePlusRecord_databaseRecord &&
- npr->u.databaseRecord->direct_reference
- && !oid_oidcmp(npr->u.databaseRecord->direct_reference,
- yaz_oid_recsyn_xml))
- {
- // 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
+ {
+ 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;
+ }
+ // everything fine, continuing
+
+ Z_PresentResponse *pr = z3950_gdu->u.z3950->u.presentResponse;
+
+ // 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;
+
+ // if anything but database or surrogate diagnostics, stop
+ if (!pr->records || pr->records->which != Z_Records_DBOSD)
+ break;
+ else
+ {
+ // inserting all records
+ int returned_recs =
+ pr->records->u.databaseOrSurDiagnostics->num_records;
+ for (int i = 0; i < returned_recs; i++)
{
- // 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);
+ int position = i + *apdu->u.presentRequest->resultSetStartPoint;
+ Z_NamePlusRecord *npr
+ = pr->records->u.databaseOrSurDiagnostics->records[i];
+
+ sru_res->records[i + num].recordPacking = record_packing;
+
+ if (npr->which == Z_NamePlusRecord_databaseRecord &&
+ npr->u.databaseRecord->direct_reference
+ && !oid_oidcmp(npr->u.databaseRecord->direct_reference,
+ yaz_oid_recsyn_xml))
+ {
+ // got XML record back
+ Z_External *r = npr->u.databaseRecord;
+ sru_res->records[i + num].recordPosition =
+ odr_intdup(odr_en, position);
+ sru_res->records[i + num].recordSchema = sr_req->recordSchema;
+ sru_res->records[i + num].recordData_buf
+ = odr_strdupn(odr_en,
+ (const char *)r->u.octet_aligned->buf,
+ r->u.octet_aligned->len);
+ sru_res->records[i + num].recordData_len
+ = r->u.octet_aligned->len;
+ }
+ else
+ {
+ // not XML or no database record at all
+ yaz_mk_sru_surrogate(
+ odr_en, sru_res->records + i + num, position,
+ YAZ_SRW_RECORD_NOT_AVAILABLE_IN_THIS_SCHEMA, 0);
+ }
}
+ num += returned_recs;
}
}
-
+ sru_res->num_records = num;
+ if (start - 1 + num < *sru_pdu_res->u.response->numberOfRecords)
+ sru_res->nextRecordPosition =
+ odr_intdup(odr_en, start + num);
return true;
}
-bool yf::SRUtoZ3950::Impl::z3950_build_query(mp::odr &odr_en, Z_Query *z_query,
- const Z_SRW_searchRetrieveRequest *req
+int yf::SRUtoZ3950::Impl::z3950_build_query(mp::odr &odr_en, Z_Query *z_query,
+ const Z_SRW_searchRetrieveRequest *req
) const
{
if (req->query_type == Z_SRW_query_type_cql)
@@ -778,7 +798,7 @@ bool yf::SRUtoZ3950::Impl::z3950_build_query(mp::odr &odr_en, Z_Query *z_query,
z_query->which = Z_Query_type_104;
z_query->u.type_104 = ext;
- return true;
+ return 0;
}
if (req->query_type == Z_SRW_query_type_pqf)
@@ -789,18 +809,18 @@ bool yf::SRUtoZ3950::Impl::z3950_build_query(mp::odr &odr_en, Z_Query *z_query,
pqf_parser = yaz_pqf_create ();
RPNquery = yaz_pqf_parse (pqf_parser, odr_en, req->query.pqf);
+
+ yaz_pqf_destroy(pqf_parser);
+
if (!RPNquery)
- {
- std::cout << "TODO: Handeling of bad PQF\n";
- std::cout << "TODO: Diagnostic to be send\n";
- }
+ return YAZ_SRW_QUERY_SYNTAX_ERROR;
+
z_query->which = Z_Query_type_1;
z_query->u.type_1 = RPNquery;
- yaz_pqf_destroy(pqf_parser);
- return true;
+ return 0;
}
- return false;
+ return YAZ_SRW_MANDATORY_PARAMETER_NOT_SUPPLIED;
}
@@ -825,8 +845,8 @@ yf::SRUtoZ3950::Impl::z3950_to_srw_diagnostics_ok(mp::odr &odr_en,
int
yf::SRUtoZ3950::Impl::z3950_to_srw_diag(mp::odr &odr_en,
- Z_SRW_searchRetrieveResponse *sru_res,
- Z_DefaultDiagFormat *ddf) const
+ Z_SRW_searchRetrieveResponse *sru_res,
+ Z_DefaultDiagFormat *ddf) const
{
int bib1_code = *ddf->condition;
sru_res->num_diagnostics = 1;