X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=src%2Ffilter_query_rewrite.cpp;h=4828781d41329755165253fd9015be1dfb230d73;hb=586d78659d671683f33ec55f4a7d32b28e345ccd;hp=2111fc5ba0407ea138aa6319fd972f6e39c0fb27;hpb=d0123337d70b0fb97b578cc57467bb94980f1014;p=metaproxy-moved-to-github.git diff --git a/src/filter_query_rewrite.cpp b/src/filter_query_rewrite.cpp index 2111fc5..4828781 100644 --- a/src/filter_query_rewrite.cpp +++ b/src/filter_query_rewrite.cpp @@ -1,5 +1,5 @@ /* This file is part of Metaproxy. - Copyright (C) 2005-2009 Index Data + Copyright (C) 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 @@ -17,16 +17,18 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #include "config.hpp" -#include "filter.hpp" -#include "package.hpp" +#include +#include -#include "util.hpp" -#include "xmlutil.hpp" +#include #include "filter_query_rewrite.hpp" +#include #include #include #include +#include +#include #include #include @@ -41,16 +43,18 @@ namespace metaproxy_1 { Rep(); ~Rep(); void process(mp::Package &package) const; - void configure(const xmlNode * ptr); + void configure(const xmlNode * ptr, bool test_only, + const char *path); private: xsltStylesheetPtr m_stylesheet; - }; + std::string charset_from; + std::string charset_to; + }; } } -yf::QueryRewrite::Rep::Rep() +yf::QueryRewrite::Rep::Rep() : m_stylesheet(0), charset_from("UTF-8") { - m_stylesheet = 0; } yf::QueryRewrite::Rep::~Rep() @@ -67,20 +71,21 @@ yf::QueryRewrite::~QueryRewrite() { // must have a destructor because of boost::scoped_ptr } -void yf::QueryRewrite::process(mp::Package &package) const +void mp::filter::QueryRewrite::configure(const xmlNode *ptr, bool test_only, + const char *path) { - m_p->process(package); + m_p->configure(ptr, test_only, path); } -void mp::filter::QueryRewrite::configure(const xmlNode *ptr, bool test_only) +void yf::QueryRewrite::process(mp::Package &package) const { - m_p->configure(ptr); + m_p->process(package); } void yf::QueryRewrite::Rep::process(mp::Package &package) const { Z_GDU *gdu = package.request().get(); - + if (gdu && gdu->which == Z_GDU_Z3950) { Z_APDU *apdu_req = gdu->u.z3950; @@ -90,18 +95,13 @@ void yf::QueryRewrite::Rep::process(mp::Package &package) const 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) { - if (m_stylesheet) + xmlDocPtr doc_input = 0; + yaz_query2xml(req->query, &doc_input); + + if (doc_input) { xmlDocPtr doc_res = xsltApplyStylesheet(m_stylesheet, doc_input, 0); @@ -117,30 +117,49 @@ void yf::QueryRewrite::Rep::process(mp::Package &package) const &error_code, &addinfo); xmlFreeDoc(doc_res); } + xmlFreeDoc(doc_input); + } + } + if (!error_code && charset_to.length() && charset_from.length() && + (req->query->which == Z_Query_type_1 + || req->query->which == Z_Query_type_101)) + { + yaz_iconv_t cd = yaz_iconv_open(charset_to.c_str(), + charset_from.c_str()); + if (cd) + { + int r = yaz_query_charset_convert_rpnquery_check( + req->query->u.type_1, odr, cd); + yaz_iconv_close(cd); + if (r) + { /* query could not be char converted */ + error_code = YAZ_BIB1_MALFORMED_QUERY; + addinfo = "could not convert query to target charset"; + } } - xmlFreeDoc(doc_input); } - package.request() = gdu; if (error_code) { - Z_APDU *f_apdu = + Z_APDU *f_apdu = odr.create_searchResponse(apdu_req, error_code, addinfo); package.response() = f_apdu; return; } - } + package.request() = gdu; + } } package.move(); } -void mp::filter::QueryRewrite::Rep::configure(const xmlNode *ptr) +void mp::filter::QueryRewrite::Rep::configure(const xmlNode *ptr, + bool test_only, const char *path) { for (ptr = ptr->children; ptr; ptr = ptr->next) { if (ptr->type != XML_ELEMENT_NODE) continue; - if (mp::xml::check_element_mp(ptr, "xslt")) + if (mp::xml::is_element_mp(ptr, "xslt")) { if (m_stylesheet) { @@ -148,35 +167,61 @@ void mp::filter::QueryRewrite::Rep::configure(const xmlNode *ptr) ("Only one xslt element allowed in query_rewrite filter"); } - std::string fname;// = mp::xml::get_text(ptr); + std::string fname; - for (struct _xmlAttr *attr = ptr->properties; + for (struct _xmlAttr *attr = ptr->properties; attr; attr = attr->next) { mp::xml::check_attribute(attr, "", "stylesheet"); - fname = mp::xml::get_text(attr); + 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()); + + char fullpath[1024]; + char *cp = yaz_filepath_resolve(fname.c_str(), path, 0, fullpath); + if (!cp) + { + throw mp::filter::FilterException("Cannot read XSLT " + fname); + } + + m_stylesheet = xsltParseStylesheetFile(BAD_CAST cp); if (!m_stylesheet) { throw mp::filter::FilterException - ("Failed to read XSLT stylesheet '" + ("Failed to read XSLT stylesheet '" + fname + "' in query_rewrite filter"); } } + else if (mp::xml::is_element_mp(ptr, "charset")) + { + for (struct _xmlAttr *attr = ptr->properties; + attr; attr = attr->next) + { + if (!strcmp((const char *) attr->name, "from")) + { + charset_from = mp::xml::get_text(attr); + } + else if (!strcmp((const char *) attr->name, "to")) + { + charset_to = mp::xml::get_text(attr); + } + else + throw mp::filter::FilterException + ("Invalid attribute inside charset inside " + "query_rewrite filter"); + } + } else { throw mp::filter::FilterException - ("Bad element " + ("Bad element " + std::string((const char *) ptr->name) + " in query_rewrite filter"); }