1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2012 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
20 #include <metaproxy/filter.hpp>
21 #include <metaproxy/package.hpp>
23 #include <metaproxy/util.hpp>
24 #include "filter_query_rewrite.hpp"
28 #include <yaz/xmlquery.h>
29 #include <yaz/diagbib1.h>
30 #include <yaz/query-charset.h>
32 #include <libxslt/xsltutils.h>
33 #include <libxslt/transform.h>
35 namespace mp = metaproxy_1;
36 namespace yf = mp::filter;
38 namespace metaproxy_1 {
40 class QueryRewrite::Rep {
44 void process(mp::Package &package) const;
45 void configure(const xmlNode * ptr);
47 xsltStylesheetPtr m_stylesheet;
48 std::string charset_from;
49 std::string charset_to;
54 yf::QueryRewrite::Rep::Rep() : m_stylesheet(0), charset_from("UTF-8")
58 yf::QueryRewrite::Rep::~Rep()
61 xsltFreeStylesheet(m_stylesheet);
64 yf::QueryRewrite::QueryRewrite() : m_p(new Rep)
68 yf::QueryRewrite::~QueryRewrite()
69 { // must have a destructor because of boost::scoped_ptr
72 void yf::QueryRewrite::process(mp::Package &package) const
74 m_p->process(package);
77 void mp::filter::QueryRewrite::configure(const xmlNode *ptr, bool test_only,
83 void yf::QueryRewrite::Rep::process(mp::Package &package) const
85 Z_GDU *gdu = package.request().get();
87 if (gdu && gdu->which == Z_GDU_Z3950)
89 Z_APDU *apdu_req = gdu->u.z3950;
90 if (apdu_req->which == Z_APDU_searchRequest)
93 const char *addinfo = 0;
95 Z_SearchRequest *req = apdu_req->u.searchRequest;
99 xmlDocPtr doc_input = 0;
100 yaz_query2xml(req->query, &doc_input);
104 error_code = YAZ_BIB1_MALFORMED_QUERY;
105 addinfo = "converion from Query to XML failed";
109 xmlDocPtr doc_res = xsltApplyStylesheet(m_stylesheet,
113 error_code = YAZ_BIB1_MALFORMED_QUERY;
114 addinfo = "XSLT transform failed for query";
118 const xmlNode *root_element = xmlDocGetRootElement(doc_res);
119 yaz_xml2query(root_element, &req->query, odr,
120 &error_code, &addinfo);
123 xmlFreeDoc(doc_input);
126 if (charset_to.length() && charset_from.length() &&
127 (req->query->which == Z_Query_type_1
128 || req->query->which == Z_Query_type_101))
130 yaz_iconv_t cd = yaz_iconv_open(charset_to.c_str(),
131 charset_from.c_str());
134 int r = yaz_query_charset_convert_rpnquery_check(
135 req->query->u.type_1, odr, cd);
138 { /* query could not be char converted */
139 error_code = YAZ_BIB1_MALFORMED_QUERY;
140 addinfo = "could not convert query to target charset";
147 odr.create_searchResponse(apdu_req, error_code, addinfo);
148 package.response() = f_apdu;
151 package.request() = gdu;
157 void mp::filter::QueryRewrite::Rep::configure(const xmlNode *ptr)
159 for (ptr = ptr->children; ptr; ptr = ptr->next)
161 if (ptr->type != XML_ELEMENT_NODE)
164 if (mp::xml::is_element_mp(ptr, "xslt"))
168 throw mp::filter::FilterException
169 ("Only one xslt element allowed in query_rewrite filter");
172 std::string fname;// = mp::xml::get_text(ptr);
174 for (struct _xmlAttr *attr = ptr->properties;
175 attr; attr = attr->next)
177 mp::xml::check_attribute(attr, "", "stylesheet");
178 fname = mp::xml::get_text(attr);
181 if (0 == fname.size())
182 throw mp::filter::FilterException
183 ("Attribute <xslt stylesheet=\""
185 + "\"> needs XSLT stylesheet path content"
186 + " in query_rewrite filter");
188 m_stylesheet = xsltParseStylesheetFile(BAD_CAST fname.c_str());
191 throw mp::filter::FilterException
192 ("Failed to read XSLT stylesheet '"
194 + "' in query_rewrite filter");
197 else if (mp::xml::is_element_mp(ptr, "charset"))
199 for (struct _xmlAttr *attr = ptr->properties;
200 attr; attr = attr->next)
202 if (!strcmp((const char *) attr->name, "from"))
204 charset_from = mp::xml::get_text(attr);
206 else if (!strcmp((const char *) attr->name, "to"))
208 charset_to = mp::xml::get_text(attr);
211 throw mp::filter::FilterException
212 ("Invalid attribute inside charset inside "
213 "query_rewrite filter");
218 throw mp::filter::FilterException
220 + std::string((const char *) ptr->name)
221 + " in query_rewrite filter");
226 static mp::filter::Base* filter_creator()
228 return new mp::filter::QueryRewrite;
232 struct metaproxy_1_filter_struct metaproxy_1_filter_query_rewrite = {
242 * c-file-style: "Stroustrup"
243 * indent-tabs-mode: nil
245 * vim: shiftwidth=4 tabstop=8 expandtab