+ // checking non surrogate dioagnostics in Z3950 present response package
+ if (pr->records
+ && pr->records->which == Z_Records_NSD
+ && pr->records->u.nonSurrogateDiagnostic)
+ {
+ //int http_code =
+ z3950_to_srw_diag(odr_en, sru_res,
+ pr->records->u.nonSurrogateDiagnostic);
+ 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;
+ 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;
+ }
+ }
+ }
+ }
+
+ return true;
+}
+
+bool
+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);
+ Z_APDU *apdu = zget_APDU(odr_en, Z_APDU_scanRequest);
+
+ //TODO: add stuff in apdu
+ Z_ScanRequest *z_scanRequest = apdu->u.scanRequest;
+
+ // database repackaging
+ z_scanRequest->num_databaseNames = 1;
+ z_scanRequest->databaseNames = (char**)
+ odr_malloc(odr_en, sizeof(char *));
+ if (sr_req->database)
+ z_scanRequest->databaseNames[0]
+ = odr_strdup(odr_en, const_cast<char *>(sr_req->database));
+ else
+ z_scanRequest->databaseNames[0]
+ = odr_strdup(odr_en, "Default");
+
+
+ // query repackaging
+ // CQL or XCQL scan is not possible in Z3950, flagging a diagnostic
+ if (sr_req->query_type != Z_SRW_query_type_pqf)
+ {
+ //send_to_srw_client_error(7, "query");
+ return false;
+ }
+
+ // PQF query repackaging
+ // need to use Z_AttributesPlusTerm structure, not Z_Query
+ // this can be digget out of a
+ // Z_query->type1(Z_RPNQuery)->RPNStructure(Z_RPNStructure)
+ // ->u.simple(Z_Operand)->u.attributesPlusTerm(Z_AttributesPlusTerm )
+
+ //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,
+ // (const SRW_query&)sr_req->query,
+ // sr_req->query_type))
+ //{
+ //send_to_srw_client_error(7, "query");
+ // return false;
+ //}
+
+ // TODO:
+
+ z3950_package.request() = apdu;
+ std::cout << "z3950_scan_request " << *apdu << "\n";
+
+ z3950_package.move();
+ //TODO: check success condition
+ return true;
+ return false;
+}
+
+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
+{
+ if (query_type == Z_SRW_query_type_cql)
+ {
+ Z_External *ext = (Z_External *)
+ odr_malloc(odr_en, sizeof(*ext));
+ ext->direct_reference =
+ odr_getoidbystr(odr_en, "1.2.840.10003.16.2");
+ ext->indirect_reference = 0;
+ ext->descriptor = 0;
+ ext->which = Z_External_CQL;
+ ext->u.cql = const_cast<char *>(query.cql);
+
+ z_query->which = Z_Query_type_104;
+ z_query->u.type_104 = ext;
+ return true;
+ }
+
+ if (query_type == Z_SRW_query_type_pqf)
+ {
+ Z_RPNQuery *RPNquery;
+ YAZ_PQF_Parser pqf_parser;
+
+ pqf_parser = yaz_pqf_create ();
+
+ RPNquery = yaz_pqf_parse (pqf_parser, odr_en, query.pqf);
+ if (!RPNquery)
+ {
+ std::cout << "TODO: Handeling of bad PQF\n";
+ std::cout << "TODO: Diagnostic to be send\n";
+ }
+ z_query->which = Z_Query_type_1;
+ z_query->u.type_1 = RPNquery;
+
+ yaz_pqf_destroy(pqf_parser);
+ return true;
+ }
+ return false;
+}
+
+
+// std::string
+// yf::SRUtoZ3950::Impl::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
+// = mp_util::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::Impl::debug_http(const Z_HTTP_Request &http_req) 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>"
+// + mp_util::http_header_value(http_req.headers, "Content-Type")
+// + "<br/>\n";
+// message += "<b>Content-Length:</b>"
+// + mp_util::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;
+// }
+// message += "</p>\n";
+// message += "</body>\n</html>\n";
+// return message;
+// }
+
+// void yf::SRUtoZ3950::Impl::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();