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