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