1 /* This file is part of Metaproxy.
2 Copyright (C) Index Data
4 Metaproxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #include <metaproxy/package.hpp>
20 #include <metaproxy/util.hpp>
23 #include <yaz/diagbib1.h>
24 #include <yaz/match_glob.h>
25 #include <boost/scoped_ptr.hpp>
26 #include <boost/thread/mutex.hpp>
27 #include <boost/thread/condition.hpp>
32 namespace mp = metaproxy_1;
33 namespace yf = mp::filter;
35 namespace metaproxy_1 {
37 class SPARQL : public Base {
43 typedef boost::shared_ptr<Session> SessionPtr;
44 typedef boost::shared_ptr<Conf> ConfPtr;
46 typedef boost::shared_ptr<FrontendSet> FrontendSetPtr;
47 typedef std::map<std::string,FrontendSetPtr> FrontendSets;
51 void process(metaproxy_1::Package & package) const;
52 void configure(const xmlNode * ptr, bool test_only,
54 SessionPtr get_session(Package &package, Z_APDU **apdu) const;
55 void release_session(Package &package) const;
56 boost::scoped_ptr<Rep> m_p;
57 std::list<ConfPtr> db_conf;
68 boost::condition m_cond_session_ready;
70 std::map<mp::Session,SessionPtr> m_clients;
72 class SPARQL::FrontendSet {
82 class SPARQL::Session {
84 Session(const SPARQL *);
86 void handle_z(Package &package, Z_APDU *apdu);
87 Z_APDU *run_sparql(mp::Package &package,
90 const char *sparql_query,
94 ODR odr, Odr_oid *preferredRecordSyntax,
95 Z_ElementSetNames *esn,
96 int start, int number, int &error_code, std::string &addinfo,
97 int *number_returned, int *next_position);
100 bool m_support_named_result_sets;
101 FrontendSets m_frontend_sets;
102 const SPARQL *m_sparql;
107 yf::SPARQL::FrontendSet::~FrontendSet()
113 yf::SPARQL::FrontendSet::FrontendSet()
118 yf::SPARQL::SPARQL() : m_p(new Rep)
122 yf::SPARQL::~SPARQL()
126 void yf::SPARQL::configure(const xmlNode *xmlnode, bool test_only,
129 const xmlNode *ptr = xmlnode->children;
131 for (; ptr; ptr = ptr->next)
133 if (ptr->type != XML_ELEMENT_NODE)
135 if (!strcmp((const char *) ptr->name, "db"))
137 yaz_sparql_t s = yaz_sparql_create();
138 ConfPtr conf(new Conf);
141 const struct _xmlAttr *attr;
142 for (attr = ptr->properties; attr; attr = attr->next)
144 if (!strcmp((const char *) attr->name, "path"))
145 conf->db = mp::xml::get_text(attr->children);
146 else if (!strcmp((const char *) attr->name, "uri"))
147 conf->uri = mp::xml::get_text(attr->children);
149 throw mp::filter::FilterException(
150 "Bad attribute " + std::string((const char *)
153 xmlNode *p = ptr->children;
154 for (; p; p = p->next)
156 if (p->type != XML_ELEMENT_NODE)
158 std::string name = (const char *) p->name;
159 const struct _xmlAttr *attr;
160 for (attr = p->properties; attr; attr = attr->next)
162 if (!strcmp((const char *) attr->name, "type"))
165 name.append(mp::xml::get_text(attr->children));
168 throw mp::filter::FilterException(
169 "Bad attribute " + std::string((const char *)
172 std::string value = mp::xml::get_text(p);
173 if (yaz_sparql_add_pattern(s, name.c_str(), value.c_str()))
175 throw mp::filter::FilterException(
176 "Bad SPARQL config " + name);
179 if (!conf->uri.length())
181 throw mp::filter::FilterException("Missing uri");
183 if (!conf->db.length())
185 throw mp::filter::FilterException("Missing path");
187 db_conf.push_back(conf);
191 throw mp::filter::FilterException
193 + std::string((const char *) ptr->name)
194 + " in sparql filter");
199 yf::SPARQL::Conf::~Conf()
201 yaz_sparql_destroy(s);
204 yf::SPARQL::Session::Session(const SPARQL *sparql) :
206 m_support_named_result_sets(false),
211 yf::SPARQL::Session::~Session()
215 yf::SPARQL::SessionPtr yf::SPARQL::get_session(Package & package,
220 Z_GDU *gdu = package.request().get();
222 boost::mutex::scoped_lock lock(m_p->m_mutex);
224 std::map<mp::Session,SPARQL::SessionPtr>::iterator it;
226 if (gdu && gdu->which == Z_GDU_Z3950)
227 *apdu = gdu->u.z3950;
233 it = m_p->m_clients.find(package.session());
234 if (it == m_p->m_clients.end())
236 if (!it->second->m_in_use)
238 it->second->m_in_use = true;
241 m_p->m_cond_session_ready.wait(lock);
246 // new Z39.50 session ..
247 SessionPtr p(new Session(this));
248 m_p->m_clients[package.session()] = p;
252 void yf::SPARQL::release_session(Package &package) const
254 boost::mutex::scoped_lock lock(m_p->m_mutex);
255 std::map<mp::Session,SessionPtr>::iterator it;
257 it = m_p->m_clients.find(package.session());
258 if (it != m_p->m_clients.end())
260 it->second->m_in_use = false;
262 if (package.session().is_closed())
263 m_p->m_clients.erase(it);
264 m_p->m_cond_session_ready.notify_all();
268 static xmlNode *get_result(xmlDoc *doc, Odr_int *sz, Odr_int pos)
270 xmlNode *ptr = xmlDocGetRootElement(doc);
272 for (; ptr; ptr = ptr->next)
273 if (ptr->type == XML_ELEMENT_NODE &&
274 !strcmp((const char *) ptr->name, "sparql"))
278 for (ptr = ptr->children; ptr; ptr = ptr->next)
279 if (ptr->type == XML_ELEMENT_NODE &&
280 !strcmp((const char *) ptr->name, "results"))
285 for (ptr = ptr->children; ptr; ptr = ptr->next)
286 if (ptr->type == XML_ELEMENT_NODE &&
287 !strcmp((const char *) ptr->name, "result"))
298 Z_Records *yf::SPARQL::Session::fetch(
300 ODR odr, Odr_oid *preferredRecordSyntax,
301 Z_ElementSetNames *esn,
302 int start, int number, int &error_code, std::string &addinfo,
303 int *number_returned, int *next_position)
305 Z_Records *rec = (Z_Records *) odr_malloc(odr, sizeof(Z_Records));
306 rec->which = Z_Records_DBOSD;
307 rec->u.databaseOrSurDiagnostics = (Z_NamePlusRecordList *)
308 odr_malloc(odr, sizeof(Z_NamePlusRecordList));
309 rec->u.databaseOrSurDiagnostics->records = (Z_NamePlusRecord **)
310 odr_malloc(odr, sizeof(Z_NamePlusRecord *) * number);
312 for (i = 0; i < number; i++)
314 rec->u.databaseOrSurDiagnostics->records[i] = (Z_NamePlusRecord *)
315 odr_malloc(odr, sizeof(Z_NamePlusRecord));
316 Z_NamePlusRecord *npr = rec->u.databaseOrSurDiagnostics->records[i];
317 npr->databaseName = odr_strdup(odr, fset->db.c_str());
318 npr->which = Z_NamePlusRecord_databaseRecord;
320 xmlNode *node = get_result(fset->doc, 0, start - 1 + i);
323 assert(node->type == XML_ELEMENT_NODE);
324 assert(!strcmp((const char *) node->name, "result"));
325 xmlNode *tmp = xmlCopyNode(node, 1);
326 xmlBufferPtr buf = xmlBufferCreate();
327 xmlNodeDump(buf, tmp->doc, tmp, 0, 0);
328 npr->u.databaseRecord =
329 z_ext_record_xml(odr, (const char *) buf->content, buf->use);
333 rec->u.databaseOrSurDiagnostics->num_records = i;
334 *number_returned = i;
335 if (start + number > fset->hits)
338 *next_position = start + number;
342 Z_APDU *yf::SPARQL::Session::run_sparql(mp::Package &package,
345 const char *sparql_query,
348 Z_SearchRequest *req = apdu_req->u.searchRequest;
349 Package http_package(package.session(), package.origin());
351 http_package.copy_filter(package);
352 Z_GDU *gdu = z_get_HTTP_Request_uri(odr, uri, 0, 1);
354 z_HTTP_header_add(odr, &gdu->u.HTTP_Request->headers,
355 "Content-Type", "application/x-www-form-urlencoded");
356 const char *names[2];
359 const char *values[1];
360 values[0] = sparql_query;
362 yaz_array_to_uri(&path, odr, (char **) names, (char **) values);
364 gdu->u.HTTP_Request->content_buf = path;
365 gdu->u.HTTP_Request->content_len = strlen(path);
367 yaz_log(YLOG_LOG, "sparql: HTTP request\n%s", sparql_query);
369 http_package.request() = gdu;
372 Z_GDU *gdu_resp = http_package.response().get();
373 Z_APDU *apdu_res = 0;
374 if (gdu_resp && gdu_resp->which == Z_GDU_HTTP_Response)
376 Z_HTTP_Response *resp = gdu_resp->u.HTTP_Response;
377 FrontendSetPtr fset(new FrontendSet);
379 fset->doc = xmlParseMemory(resp->content_buf, resp->content_len);
380 fset->db = req->databaseNames[0];
382 apdu_res = odr.create_searchResponse(apdu_req,
383 YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
384 "invalid XML from backendbackend");
387 Z_Records *records = 0;
388 int number_returned = 0;
389 int next_position = 0;
393 get_result(fset->doc, &fset->hits, -1);
394 m_frontend_sets[req->resultSetName] = fset;
397 const char *element_set_name = 0;
398 mp::util::piggyback_sr(req, fset->hits, number, &element_set_name);
401 Z_ElementSetNames *esn;
403 if (number > *req->smallSetUpperBound)
404 esn = req->mediumSetElementSetNames;
406 esn = req->smallSetElementSetNames;
407 records = fetch(fset,
408 odr, req->preferredRecordSyntax, esn,
417 odr.create_searchResponse(
418 apdu_req, error_code, addinfo.c_str());
423 odr.create_searchResponse(apdu_req, 0, 0);
424 Z_SearchResponse *resp = apdu_res->u.searchResponse;
425 *resp->resultCount = fset->hits;
426 *resp->numberOfRecordsReturned = number_returned;
427 *resp->nextResultSetPosition = next_position;
428 resp->records = records;
434 yaz_log(YLOG_LOG, "sparql: no HTTP response");
435 apdu_res = odr.create_searchResponse(apdu_req,
436 YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
437 "no HTTP response from backend");
442 void yf::SPARQL::Session::handle_z(mp::Package &package, Z_APDU *apdu_req)
445 Z_APDU *apdu_res = 0;
446 if (apdu_req->which == Z_APDU_initRequest)
448 apdu_res = odr.create_initResponse(apdu_req, 0, 0);
449 Z_InitRequest *req = apdu_req->u.initRequest;
450 Z_InitResponse *resp = apdu_res->u.initResponse;
452 resp->implementationName = odr_strdup(odr, "sparql");
453 if (ODR_MASK_GET(req->options, Z_Options_namedResultSets))
454 m_support_named_result_sets = true;
456 static const int masks[] = {
457 Z_Options_search, Z_Options_present,
458 Z_Options_namedResultSets, -1
460 for (i = 0; masks[i] != -1; i++)
461 if (ODR_MASK_GET(req->options, masks[i]))
462 ODR_MASK_SET(resp->options, masks[i]);
463 static const int versions[] = {
469 for (i = 0; versions[i] != -1; i++)
470 if (ODR_MASK_GET(req->protocolVersion, versions[i]))
471 ODR_MASK_SET(resp->protocolVersion, versions[i]);
474 *resp->preferredMessageSize = *req->preferredMessageSize;
475 *resp->maximumRecordSize = *req->maximumRecordSize;
477 else if (apdu_req->which == Z_APDU_close)
479 apdu_res = odr.create_close(apdu_req,
480 Z_Close_finished, 0);
481 package.session().close();
483 else if (apdu_req->which == Z_APDU_searchRequest)
485 Z_SearchRequest *req = apdu_req->u.searchRequest;
487 FrontendSets::iterator fset_it =
488 m_frontend_sets.find(req->resultSetName);
489 if (fset_it != m_frontend_sets.end())
491 // result set already exist
492 // if replace indicator is off: we return diagnostic if
493 // result set already exist.
494 if (*req->replaceIndicator == 0)
497 odr.create_searchResponse(
499 YAZ_BIB1_RESULT_SET_EXISTS_AND_REPLACE_INDICATOR_OFF,
501 package.response() = apdu_res;
503 m_frontend_sets.erase(fset_it);
505 if (req->query->which != Z_Query_type_1)
507 apdu_res = odr.create_searchResponse(
508 apdu_req, YAZ_BIB1_QUERY_TYPE_UNSUPP, 0);
510 else if (req->num_databaseNames != 1)
512 apdu_res = odr.create_searchResponse(
514 YAZ_BIB1_ACCESS_TO_SPECIFIED_DATABASE_DENIED, 0);
518 std::string db = req->databaseNames[0];
519 std::list<ConfPtr>::const_iterator it;
521 it = m_sparql->db_conf.begin();
522 for (; it != m_sparql->db_conf.end(); it++)
523 if (yaz_match_glob((*it)->db.c_str(), db.c_str()))
525 if (it == m_sparql->db_conf.end())
527 apdu_res = odr.create_searchResponse(
528 apdu_req, YAZ_BIB1_DATABASE_DOES_NOT_EXIST, db.c_str());
532 WRBUF addinfo_wr = wrbuf_alloc();
533 WRBUF sparql_wr = wrbuf_alloc();
535 yaz_sparql_from_rpn_wrbuf((*it)->s,
536 addinfo_wr, sparql_wr,
537 req->query->u.type_1);
540 apdu_res = odr.create_searchResponse(
542 wrbuf_len(addinfo_wr) ?
543 wrbuf_cstr(addinfo_wr) : 0);
547 apdu_res = run_sparql(package, apdu_req, odr,
548 wrbuf_cstr(sparql_wr),
551 wrbuf_destroy(addinfo_wr);
552 wrbuf_destroy(sparql_wr);
556 else if (apdu_req->which == Z_APDU_presentRequest)
558 Z_PresentRequest *req = apdu_req->u.presentRequest;
559 FrontendSets::iterator fset_it =
560 m_frontend_sets.find(req->resultSetId);
561 if (fset_it == m_frontend_sets.end())
564 odr.create_presentResponse(
565 apdu_req, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
567 package.response() = apdu_res;
570 int number_returned = 0;
571 int next_position = 0;
574 Z_ElementSetNames *esn = 0;
575 if (req->recordComposition)
577 if (req->recordComposition->which == Z_RecordComp_simple)
578 esn = req->recordComposition->u.simple;
582 odr.create_presentResponse(
584 YAZ_BIB1_ONLY_A_SINGLE_ELEMENT_SET_NAME_SUPPORTED,
586 package.response() = apdu_res;
590 Z_Records *records = fetch(
592 odr, req->preferredRecordSyntax, esn,
593 *req->resultSetStartPoint, *req->numberOfRecordsRequested,
600 odr.create_presentResponse(apdu_req, error_code,
606 odr.create_presentResponse(apdu_req, 0, 0);
607 Z_PresentResponse *resp = apdu_res->u.presentResponse;
608 resp->records = records;
609 *resp->numberOfRecordsReturned = number_returned;
610 *resp->nextResultSetPosition = next_position;
615 apdu_res = odr.create_close(apdu_req,
616 Z_Close_protocolError,
617 "sparql: unhandled APDU");
618 package.session().close();
622 package.response() = apdu_res;
625 void yf::SPARQL::process(mp::Package &package) const
628 SessionPtr p = get_session(package, &apdu);
631 p->handle_z(package, apdu);
635 release_session(package);
638 static mp::filter::Base* filter_creator()
640 return new mp::filter::SPARQL;
644 struct metaproxy_1_filter_struct metaproxy_1_filter_sparql = {
655 * c-file-style: "Stroustrup"
656 * indent-tabs-mode: nil
658 * vim: shiftwidth=4 tabstop=8 expandtab