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 {
42 typedef boost::shared_ptr<Session> SessionPtr;
43 typedef boost::shared_ptr<Conf> ConfPtr;
47 void process(metaproxy_1::Package & package) const;
48 void configure(const xmlNode * ptr, bool test_only,
50 SessionPtr get_session(Package &package, Z_APDU **apdu) const;
51 void release_session(Package &package) const;
52 boost::scoped_ptr<Rep> m_p;
53 std::list<ConfPtr> db_conf;
64 boost::condition m_cond_session_ready;
66 std::map<mp::Session,SessionPtr> m_clients;
68 class SPARQL::Session {
70 Session(const SPARQL *);
72 void handle_z(Package &package, Z_APDU *apdu);
73 Z_APDU *run_sparql(mp::Package &package,
76 const char *sparql_query,
80 bool m_support_named_result_sets;
81 const SPARQL *m_sparql;
86 yf::SPARQL::SPARQL() : m_p(new Rep)
94 void yf::SPARQL::configure(const xmlNode *xmlnode, bool test_only,
97 const xmlNode *ptr = xmlnode->children;
99 for (; ptr; ptr = ptr->next)
101 if (ptr->type != XML_ELEMENT_NODE)
103 if (!strcmp((const char *) ptr->name, "db"))
105 yaz_sparql_t s = yaz_sparql_create();
106 ConfPtr conf(new Conf);
109 const struct _xmlAttr *attr;
110 for (attr = ptr->properties; attr; attr = attr->next)
112 if (!strcmp((const char *) attr->name, "path"))
113 conf->db = mp::xml::get_text(attr->children);
114 else if (!strcmp((const char *) attr->name, "uri"))
115 conf->uri = mp::xml::get_text(attr->children);
117 throw mp::filter::FilterException(
118 "Bad attribute " + std::string((const char *)
121 xmlNode *p = ptr->children;
122 for (; p; p = p->next)
124 if (p->type != XML_ELEMENT_NODE)
126 std::string name = (const char *) p->name;
127 const struct _xmlAttr *attr;
128 for (attr = p->properties; attr; attr = attr->next)
130 if (!strcmp((const char *) attr->name, "type"))
133 name.append(mp::xml::get_text(attr->children));
136 throw mp::filter::FilterException(
137 "Bad attribute " + std::string((const char *)
140 std::string value = mp::xml::get_text(p);
141 if (yaz_sparql_add_pattern(s, name.c_str(), value.c_str()))
143 throw mp::filter::FilterException(
144 "Bad SPARQL config " + name);
147 if (!conf->uri.length())
149 throw mp::filter::FilterException("Missing uri");
151 if (!conf->db.length())
153 throw mp::filter::FilterException("Missing path");
155 db_conf.push_back(conf);
159 throw mp::filter::FilterException
161 + std::string((const char *) ptr->name)
162 + " in sparql filter");
167 yf::SPARQL::Conf::~Conf()
169 yaz_sparql_destroy(s);
172 yf::SPARQL::Session::Session(const SPARQL *sparql) :
174 m_support_named_result_sets(false),
179 yf::SPARQL::Session::~Session()
183 yf::SPARQL::SessionPtr yf::SPARQL::get_session(Package & package,
188 Z_GDU *gdu = package.request().get();
190 boost::mutex::scoped_lock lock(m_p->m_mutex);
192 std::map<mp::Session,SPARQL::SessionPtr>::iterator it;
194 if (gdu && gdu->which == Z_GDU_Z3950)
195 *apdu = gdu->u.z3950;
201 it = m_p->m_clients.find(package.session());
202 if (it == m_p->m_clients.end())
204 if (!it->second->m_in_use)
206 it->second->m_in_use = true;
209 m_p->m_cond_session_ready.wait(lock);
214 // new Z39.50 session ..
215 SessionPtr p(new Session(this));
216 m_p->m_clients[package.session()] = p;
220 void yf::SPARQL::release_session(Package &package) const
222 boost::mutex::scoped_lock lock(m_p->m_mutex);
223 std::map<mp::Session,SessionPtr>::iterator it;
225 it = m_p->m_clients.find(package.session());
226 if (it != m_p->m_clients.end())
228 it->second->m_in_use = false;
230 if (package.session().is_closed())
231 m_p->m_clients.erase(it);
232 m_p->m_cond_session_ready.notify_all();
236 Z_APDU *yf::SPARQL::Session::run_sparql(mp::Package &package,
239 const char *sparql_query,
242 Package http_package(package.session(), package.origin());
244 http_package.copy_filter(package);
245 Z_GDU *gdu = z_get_HTTP_Request_uri(odr, uri, 0, 1);
247 z_HTTP_header_add(odr, &gdu->u.HTTP_Request->headers,
248 "Content-Type", "application/x-www-form-urlencoded");
249 const char *names[2];
252 const char *values[1];
253 values[0] = sparql_query;
255 yaz_array_to_uri(&path, odr, (char **) names, (char **) values);
257 gdu->u.HTTP_Request->content_buf = path;
258 gdu->u.HTTP_Request->content_len = strlen(path);
261 yaz_log(YLOG_LOG, "sparql: HTTP request\n%s", sparql_query);
263 http_package.request() = gdu;
266 Z_GDU *gdu_resp = http_package.response().get();
267 if (gdu_resp && gdu_resp->which == Z_GDU_HTTP_Response)
269 Z_HTTP_Response *resp = gdu_resp->u.HTTP_Response;
273 yaz_log(YLOG_LOG, "sparql: no HTTP response");
275 Z_APDU *apdu_res = odr.create_searchResponse(apdu_req, 0, 0);
279 void yf::SPARQL::Session::handle_z(mp::Package &package, Z_APDU *apdu_req)
282 Z_APDU *apdu_res = 0;
283 if (apdu_req->which == Z_APDU_initRequest)
285 apdu_res = odr.create_initResponse(apdu_req, 0, 0);
286 Z_InitRequest *req = apdu_req->u.initRequest;
287 Z_InitResponse *resp = apdu_res->u.initResponse;
289 resp->implementationName = odr_strdup(odr, "sparql");
290 if (ODR_MASK_GET(req->options, Z_Options_namedResultSets))
291 m_support_named_result_sets = true;
293 static const int masks[] = {
294 Z_Options_search, Z_Options_present,
295 Z_Options_namedResultSets, -1
297 for (i = 0; masks[i] != -1; i++)
298 if (ODR_MASK_GET(req->options, masks[i]))
299 ODR_MASK_SET(resp->options, masks[i]);
300 static const int versions[] = {
306 for (i = 0; versions[i] != -1; i++)
307 if (ODR_MASK_GET(req->protocolVersion, versions[i]))
308 ODR_MASK_SET(resp->protocolVersion, versions[i]);
311 *resp->preferredMessageSize = *req->preferredMessageSize;
312 *resp->maximumRecordSize = *req->maximumRecordSize;
314 else if (apdu_req->which == Z_APDU_close)
316 apdu_res = odr.create_close(apdu_req,
317 Z_Close_finished, 0);
318 package.session().close();
320 else if (apdu_req->which == Z_APDU_searchRequest)
322 Z_SearchRequest *req = apdu_req->u.searchRequest;
324 if (req->query->which != Z_Query_type_1)
326 apdu_res = odr.create_searchResponse(
327 apdu_req, YAZ_BIB1_QUERY_TYPE_UNSUPP, 0);
329 else if (req->num_databaseNames != 1)
331 apdu_res = odr.create_searchResponse(
333 YAZ_BIB1_ACCESS_TO_SPECIFIED_DATABASE_DENIED, 0);
337 std::string db = req->databaseNames[0];
338 std::list<ConfPtr>::const_iterator it;
340 it = m_sparql->db_conf.begin();
341 for (; it != m_sparql->db_conf.end(); it++)
342 if (yaz_match_glob((*it)->db.c_str(), db.c_str()))
344 if (it == m_sparql->db_conf.end())
346 apdu_res = odr.create_searchResponse(
347 apdu_req, YAZ_BIB1_DATABASE_DOES_NOT_EXIST, db.c_str());
351 WRBUF addinfo_wr = wrbuf_alloc();
352 WRBUF sparql_wr = wrbuf_alloc();
354 yaz_sparql_from_rpn_wrbuf((*it)->s,
355 addinfo_wr, sparql_wr,
356 req->query->u.type_1);
359 apdu_res = odr.create_searchResponse(
361 wrbuf_len(addinfo_wr) ?
362 wrbuf_cstr(addinfo_wr) : 0);
366 apdu_res = run_sparql(package, apdu_req, odr,
367 wrbuf_cstr(sparql_wr),
370 wrbuf_destroy(addinfo_wr);
371 wrbuf_destroy(sparql_wr);
377 apdu_res = odr.create_close(apdu_req,
378 Z_Close_protocolError,
379 "sparql: unhandled APDU");
380 package.session().close();
384 package.response() = apdu_res;
387 void yf::SPARQL::process(mp::Package &package) const
390 SessionPtr p = get_session(package, &apdu);
393 p->handle_z(package, apdu);
397 release_session(package);
400 static mp::filter::Base* filter_creator()
402 return new mp::filter::SPARQL;
406 struct metaproxy_1_filter_struct metaproxy_1_filter_sparql = {
417 * c-file-style: "Stroustrup"
418 * indent-tabs-mode: nil
420 * vim: shiftwidth=4 tabstop=8 expandtab