X-Git-Url: http://sru.miketaylor.org.uk/?a=blobdiff_plain;f=src%2Ffilter_query_rewrite.cpp;h=2111fc5ba0407ea138aa6319fd972f6e39c0fb27;hb=d0123337d70b0fb97b578cc57467bb94980f1014;hp=8ae8400afe8108effdaf2ed4fd093ea4d2461bf8;hpb=22c6fb6df0d4b56f388ea8fdd09550a5f3410ee2;p=metaproxy-moved-to-github.git diff --git a/src/filter_query_rewrite.cpp b/src/filter_query_rewrite.cpp index 8ae8400..2111fc5 100644 --- a/src/filter_query_rewrite.cpp +++ b/src/filter_query_rewrite.cpp @@ -1,98 +1,207 @@ -/* $Id: filter_query_rewrite.cpp,v 1.1 2006-01-19 12:18:09 marc Exp $ - Copyright (c) 2005, Index Data. +/* This file is part of Metaproxy. + Copyright (C) 2005-2009 Index Data -%LICENSE% - */ +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 this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ #include "config.hpp" #include "filter.hpp" #include "package.hpp" -//#include - #include "util.hpp" +#include "xmlutil.hpp" #include "filter_query_rewrite.hpp" #include +#include +#include + +#include +#include -namespace yf = yp2::filter; +namespace mp = metaproxy_1; +namespace yf = mp::filter; -namespace yp2 { +namespace metaproxy_1 { namespace filter { class QueryRewrite::Rep { - friend class QueryRewrite; - int dummy; + public: + Rep(); + ~Rep(); + void process(mp::Package &package) const; + void configure(const xmlNode * ptr); + private: + xsltStylesheetPtr m_stylesheet; }; } } +yf::QueryRewrite::Rep::Rep() +{ + m_stylesheet = 0; +} + +yf::QueryRewrite::Rep::~Rep() +{ + if (m_stylesheet) + xsltFreeStylesheet(m_stylesheet); +} + yf::QueryRewrite::QueryRewrite() : m_p(new Rep) { - m_p->dummy = 1; } yf::QueryRewrite::~QueryRewrite() { // must have a destructor because of boost::scoped_ptr } -void yf::QueryRewrite::process(yp2::Package &package) const +void yf::QueryRewrite::process(mp::Package &package) const { + m_p->process(package); +} - if (package.session().is_closed()) - { - std::cout << "Got Close.\n"; - } - +void mp::filter::QueryRewrite::configure(const xmlNode *ptr, bool test_only) +{ + m_p->configure(ptr); +} + +void yf::QueryRewrite::Rep::process(mp::Package &package) const +{ Z_GDU *gdu = package.request().get(); - if (gdu && gdu->which == Z_GDU_Z3950 && gdu->u.z3950->which == - Z_APDU_initRequest) - { - std::cout << "Got Z3950 Init PDU\n"; - //Z_InitRequest *req = gdu->u.z3950->u.initRequest; - //package.request() = gdu; - } - else if (gdu && gdu->which == Z_GDU_Z3950 && gdu->u.z3950->which == - Z_APDU_searchRequest) - { - std::cout << "Got Z3950 Search PDU\n"; - //Z_SearchRequest *req = gdu->u.z3950->u.searchRequest; - //package.request() = gdu; - } - else if (gdu && gdu->which == Z_GDU_Z3950 && gdu->u.z3950->which == - Z_APDU_scanRequest) + if (gdu && gdu->which == Z_GDU_Z3950) { - std::cout << "Got Z3950 Scan PDU\n"; - //Z_ScanRequest *req = gdu->u.z3950->u.scanRequest; - //package.request() = gdu; - } + Z_APDU *apdu_req = gdu->u.z3950; + if (apdu_req->which == Z_APDU_searchRequest) + { + int error_code = 0; + const char *addinfo = 0; + mp::odr odr; + Z_SearchRequest *req = apdu_req->u.searchRequest; + + xmlDocPtr doc_input = 0; + yaz_query2xml(req->query, &doc_input); + + if (!doc_input) + { + error_code = YAZ_BIB1_MALFORMED_QUERY; + addinfo = "converion from Query to XML failed"; + } + else + { + if (m_stylesheet) + { + xmlDocPtr doc_res = xsltApplyStylesheet(m_stylesheet, + doc_input, 0); + if (!doc_res) + { + error_code = YAZ_BIB1_MALFORMED_QUERY; + addinfo = "XSLT transform failed for query"; + } + else + { + const xmlNode *root_element = xmlDocGetRootElement(doc_res); + yaz_xml2query(root_element, &req->query, odr, + &error_code, &addinfo); + xmlFreeDoc(doc_res); + } + } + xmlFreeDoc(doc_input); + } + package.request() = gdu; + if (error_code) + { + Z_APDU *f_apdu = + odr.create_searchResponse(apdu_req, error_code, addinfo); + package.response() = f_apdu; + return; + } + } + } package.move(); } -static yp2::filter::Base* filter_creator() +void mp::filter::QueryRewrite::Rep::configure(const xmlNode *ptr) { - return new yp2::filter::QueryRewrite; + for (ptr = ptr->children; ptr; ptr = ptr->next) + { + if (ptr->type != XML_ELEMENT_NODE) + continue; + + if (mp::xml::check_element_mp(ptr, "xslt")) + { + if (m_stylesheet) + { + throw mp::filter::FilterException + ("Only one xslt element allowed in query_rewrite filter"); + } + + std::string fname;// = mp::xml::get_text(ptr); + + for (struct _xmlAttr *attr = ptr->properties; + attr; attr = attr->next) + { + mp::xml::check_attribute(attr, "", "stylesheet"); + fname = mp::xml::get_text(attr); + } + + if (0 == fname.size()) + throw mp::filter::FilterException + ("Attribute needs XSLT stylesheet path content" + + " in query_rewrite filter"); + + m_stylesheet = xsltParseStylesheetFile(BAD_CAST fname.c_str()); + if (!m_stylesheet) + { + throw mp::filter::FilterException + ("Failed to read XSLT stylesheet '" + + fname + + "' in query_rewrite filter"); + } + } + else + { + throw mp::filter::FilterException + ("Bad element " + + std::string((const char *) ptr->name) + + " in query_rewrite filter"); + } + } +} + +static mp::filter::Base* filter_creator() +{ + return new mp::filter::QueryRewrite; } extern "C" { - struct yp2_filter_struct yp2_filter_query_rewrite = { + struct metaproxy_1_filter_struct metaproxy_1_filter_query_rewrite = { 0, - "query-rewrite", + "query_rewrite", filter_creator }; } -extern "C" { - extern struct yp2_filter_struct yp2_filter_query_rewrite; -} - - /* * Local variables: * c-basic-offset: 4 + * c-file-style: "Stroustrup" * indent-tabs-mode: nil - * c-file-style: "stroustrup" * End: * vim: shiftwidth=4 tabstop=8 expandtab */ +