Public MP headers in include/metaproxy
[metaproxy-moved-to-github.git] / src / filter_query_rewrite.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 2005-2010 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/zgdu.h>
27 #include <yaz/xmlquery.h>
28 #include <yaz/diagbib1.h>
29
30 #include <libxslt/xsltutils.h>
31 #include <libxslt/transform.h>
32
33 namespace mp = metaproxy_1;
34 namespace yf = mp::filter;
35
36 namespace metaproxy_1 {
37     namespace filter {
38         class QueryRewrite::Rep {
39         public:
40             Rep();
41             ~Rep();
42             void process(mp::Package &package) const;
43             void configure(const xmlNode * ptr);
44         private:
45             xsltStylesheetPtr m_stylesheet;
46         };
47     }
48 }
49
50 yf::QueryRewrite::Rep::Rep()
51 {
52     m_stylesheet = 0;
53 }
54
55 yf::QueryRewrite::Rep::~Rep()
56 {
57     if (m_stylesheet)
58         xsltFreeStylesheet(m_stylesheet);
59 }
60
61 yf::QueryRewrite::QueryRewrite() : m_p(new Rep)
62 {
63 }
64
65 yf::QueryRewrite::~QueryRewrite()
66 {  // must have a destructor because of boost::scoped_ptr
67 }
68
69 void yf::QueryRewrite::process(mp::Package &package) const
70 {
71     m_p->process(package);
72 }
73
74 void mp::filter::QueryRewrite::configure(const xmlNode *ptr, bool test_only)
75 {
76     m_p->configure(ptr);
77 }
78
79 void yf::QueryRewrite::Rep::process(mp::Package &package) const
80 {
81     Z_GDU *gdu = package.request().get();
82     
83     if (gdu && gdu->which == Z_GDU_Z3950)
84     {
85         Z_APDU *apdu_req = gdu->u.z3950;
86         if (apdu_req->which == Z_APDU_searchRequest)
87         {
88             int error_code = 0;
89             const char *addinfo = 0;
90             mp::odr odr;
91             Z_SearchRequest *req = apdu_req->u.searchRequest;
92             
93             xmlDocPtr doc_input = 0;
94             yaz_query2xml(req->query, &doc_input);
95             
96             if (!doc_input)
97             {
98                 error_code = YAZ_BIB1_MALFORMED_QUERY;
99                 addinfo = "converion from Query to XML failed";
100             }
101             else
102             {
103                 if (m_stylesheet)
104                 {
105                     xmlDocPtr doc_res = xsltApplyStylesheet(m_stylesheet,
106                                                             doc_input, 0);
107                     if (!doc_res)
108                     {
109                         error_code = YAZ_BIB1_MALFORMED_QUERY;
110                         addinfo = "XSLT transform failed for query";
111                     }
112                     else
113                     {
114                         const xmlNode *root_element = xmlDocGetRootElement(doc_res);
115                         yaz_xml2query(root_element, &req->query, odr,
116                                       &error_code, &addinfo);
117                         xmlFreeDoc(doc_res);
118                     }
119                 }
120                 xmlFreeDoc(doc_input);
121             }
122             package.request() = gdu;
123             if (error_code)
124             {
125                 Z_APDU *f_apdu = 
126                     odr.create_searchResponse(apdu_req, error_code, addinfo);
127                 package.response() = f_apdu;
128                 return;
129             }
130         } 
131     }
132     package.move();
133 }
134
135 void mp::filter::QueryRewrite::Rep::configure(const xmlNode *ptr)
136 {
137     for (ptr = ptr->children; ptr; ptr = ptr->next)
138     {
139         if (ptr->type != XML_ELEMENT_NODE)
140             continue;
141
142         if (mp::xml::check_element_mp(ptr, "xslt"))
143         {
144             if (m_stylesheet)
145             {
146                 throw mp::filter::FilterException
147                     ("Only one xslt element allowed in query_rewrite filter");
148             }
149
150             std::string fname;// = mp::xml::get_text(ptr);
151
152             for (struct _xmlAttr *attr = ptr->properties; 
153                  attr; attr = attr->next)
154             {
155                 mp::xml::check_attribute(attr, "", "stylesheet");
156                 fname = mp::xml::get_text(attr);            
157             }
158
159             if (0 == fname.size())
160                 throw mp::filter::FilterException
161                     ("Attribute <xslt stylesheet=\"" 
162                      + fname
163                      + "\"> needs XSLT stylesheet path content"
164                      + " in query_rewrite filter");
165             
166             m_stylesheet = xsltParseStylesheetFile(BAD_CAST fname.c_str());
167             if (!m_stylesheet)
168             {
169                 throw mp::filter::FilterException
170                     ("Failed to read XSLT stylesheet '" 
171                      + fname
172                      + "' in query_rewrite filter");
173             }
174         }
175         else
176         {
177             throw mp::filter::FilterException
178                 ("Bad element " 
179                  + std::string((const char *) ptr->name)
180                  + " in query_rewrite filter");
181         }
182     }
183 }
184
185 static mp::filter::Base* filter_creator()
186 {
187     return new mp::filter::QueryRewrite;
188 }
189
190 extern "C" {
191     struct metaproxy_1_filter_struct metaproxy_1_filter_query_rewrite = {
192         0,
193         "query_rewrite",
194         filter_creator
195     };
196 }
197
198 /*
199  * Local variables:
200  * c-basic-offset: 4
201  * c-file-style: "Stroustrup"
202  * indent-tabs-mode: nil
203  * End:
204  * vim: shiftwidth=4 tabstop=8 expandtab
205  */
206