z3950_client: client_ip controls client-IP presence
[metaproxy-moved-to-github.git] / src / filter_query_rewrite.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) Index Data
3
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
7 version.
8
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
12 for more details.
13
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
17 */
18
19 #include "config.hpp"
20 #include <metaproxy/filter.hpp>
21 #include <metaproxy/package.hpp>
22
23 #include <metaproxy/util.hpp>
24 #include "filter_query_rewrite.hpp"
25
26 #include <yaz/log.h>
27 #include <yaz/zgdu.h>
28 #include <yaz/xmlquery.h>
29 #include <yaz/diagbib1.h>
30 #include <yaz/query-charset.h>
31 #include <yaz/tpath.h>
32
33 #include <libxslt/xsltutils.h>
34 #include <libxslt/transform.h>
35
36 namespace mp = metaproxy_1;
37 namespace yf = mp::filter;
38
39 namespace metaproxy_1 {
40     namespace filter {
41         class QueryRewrite::Rep {
42         public:
43             Rep();
44             ~Rep();
45             void process(mp::Package &package) const;
46             void configure(const xmlNode * ptr, bool test_only,
47                            const char *path);
48         private:
49             xsltStylesheetPtr m_stylesheet;
50             std::string charset_from;
51             std::string charset_to;
52        };
53     }
54 }
55
56 yf::QueryRewrite::Rep::Rep() : m_stylesheet(0), charset_from("UTF-8")
57 {
58 }
59
60 yf::QueryRewrite::Rep::~Rep()
61 {
62     if (m_stylesheet)
63         xsltFreeStylesheet(m_stylesheet);
64 }
65
66 yf::QueryRewrite::QueryRewrite() : m_p(new Rep)
67 {
68 }
69
70 yf::QueryRewrite::~QueryRewrite()
71 {  // must have a destructor because of boost::scoped_ptr
72 }
73
74 void mp::filter::QueryRewrite::configure(const xmlNode *ptr, bool test_only,
75                                          const char *path)
76 {
77     m_p->configure(ptr, test_only, path);
78 }
79
80 void yf::QueryRewrite::process(mp::Package &package) const
81 {
82     m_p->process(package);
83 }
84
85 void yf::QueryRewrite::Rep::process(mp::Package &package) const
86 {
87     Z_GDU *gdu = package.request().get();
88
89     if (gdu && gdu->which == Z_GDU_Z3950)
90     {
91         Z_APDU *apdu_req = gdu->u.z3950;
92         if (apdu_req->which == Z_APDU_searchRequest)
93         {
94             int error_code = 0;
95             const char *addinfo = 0;
96             mp::odr odr;
97             Z_SearchRequest *req = apdu_req->u.searchRequest;
98
99             if (m_stylesheet)
100             {
101                 xmlDocPtr doc_input = 0;
102                 yaz_query2xml(req->query, &doc_input);
103
104                 if (!doc_input)
105                 {
106                     error_code = YAZ_BIB1_MALFORMED_QUERY;
107                     addinfo = "converion from Query to XML failed";
108                 }
109                 else
110                 {
111                     xmlDocPtr doc_res = xsltApplyStylesheet(m_stylesheet,
112                                                             doc_input, 0);
113                     if (!doc_res)
114                     {
115                         error_code = YAZ_BIB1_MALFORMED_QUERY;
116                         addinfo = "XSLT transform failed for query";
117                     }
118                     else
119                     {
120                         const xmlNode *root_element = xmlDocGetRootElement(doc_res);
121                         yaz_xml2query(root_element, &req->query, odr,
122                                       &error_code, &addinfo);
123                         xmlFreeDoc(doc_res);
124                     }
125                     xmlFreeDoc(doc_input);
126                 }
127             }
128             if (!error_code && charset_to.length() && charset_from.length() &&
129                 (req->query->which == Z_Query_type_1
130                  || req->query->which == Z_Query_type_101))
131             {
132                 yaz_iconv_t cd = yaz_iconv_open(charset_to.c_str(),
133                                                 charset_from.c_str());
134                 if (cd)
135                 {
136                     int r = yaz_query_charset_convert_rpnquery_check(
137                         req->query->u.type_1, odr, cd);
138                     yaz_iconv_close(cd);
139                     if (r)
140                     {  /* query could not be char converted */
141                         error_code = YAZ_BIB1_MALFORMED_QUERY;
142                         addinfo = "could not convert query to target charset";
143                     }
144                 }
145             }
146             if (error_code)
147             {
148                 Z_APDU *f_apdu =
149                     odr.create_searchResponse(apdu_req, error_code, addinfo);
150                 package.response() = f_apdu;
151                 return;
152             }
153             package.request() = gdu;
154         }
155     }
156     package.move();
157 }
158
159 void mp::filter::QueryRewrite::Rep::configure(const xmlNode *ptr,
160                                               bool test_only, const char *path)
161 {
162     for (ptr = ptr->children; ptr; ptr = ptr->next)
163     {
164         if (ptr->type != XML_ELEMENT_NODE)
165             continue;
166
167         if (mp::xml::is_element_mp(ptr, "xslt"))
168         {
169             if (m_stylesheet)
170             {
171                 throw mp::filter::FilterException
172                     ("Only one xslt element allowed in query_rewrite filter");
173             }
174
175             std::string fname;
176
177             for (struct _xmlAttr *attr = ptr->properties;
178                  attr; attr = attr->next)
179             {
180                 mp::xml::check_attribute(attr, "", "stylesheet");
181                 fname = mp::xml::get_text(attr);
182             }
183
184             if (0 == fname.size())
185                 throw mp::filter::FilterException
186                     ("Attribute <xslt stylesheet=\""
187                      + fname
188                      + "\"> needs XSLT stylesheet path content"
189                      + " in query_rewrite filter");
190
191             char fullpath[1024];
192             char *cp = yaz_filepath_resolve(fname.c_str(), path, 0, fullpath);
193             if (!cp)
194             {
195                 throw mp::filter::FilterException("Cannot read XSLT " + fname);
196             }
197
198             m_stylesheet = xsltParseStylesheetFile(BAD_CAST cp);
199             if (!m_stylesheet)
200             {
201                 throw mp::filter::FilterException
202                     ("Failed to read XSLT stylesheet '"
203                      + fname
204                      + "' in query_rewrite filter");
205             }
206         }
207         else if (mp::xml::is_element_mp(ptr, "charset"))
208         {
209             for (struct _xmlAttr *attr = ptr->properties;
210                  attr; attr = attr->next)
211             {
212                 if (!strcmp((const char *) attr->name, "from"))
213                 {
214                     charset_from = mp::xml::get_text(attr);
215                 }
216                 else if (!strcmp((const char *) attr->name, "to"))
217                 {
218                     charset_to = mp::xml::get_text(attr);
219                 }
220                 else
221                     throw mp::filter::FilterException
222                         ("Invalid attribute inside charset inside "
223                          "query_rewrite filter");
224             }
225         }
226         else
227         {
228             throw mp::filter::FilterException
229                 ("Bad element "
230                  + std::string((const char *) ptr->name)
231                  + " in query_rewrite filter");
232         }
233     }
234 }
235
236 static mp::filter::Base* filter_creator()
237 {
238     return new mp::filter::QueryRewrite;
239 }
240
241 extern "C" {
242     struct metaproxy_1_filter_struct metaproxy_1_filter_query_rewrite = {
243         0,
244         "query_rewrite",
245         filter_creator
246     };
247 }
248
249 /*
250  * Local variables:
251  * c-basic-offset: 4
252  * c-file-style: "Stroustrup"
253  * indent-tabs-mode: nil
254  * End:
255  * vim: shiftwidth=4 tabstop=8 expandtab
256  */
257