4fa308f7139cb4040c03303db56ffac9e6b29708
[metaproxy-moved-to-github.git] / src / filter_cql_to_rpn.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 2005-2011 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/util.hpp>
21
22 #include <metaproxy/filter.hpp>
23 #include <metaproxy/package.hpp>
24
25 #include "filter_cql_to_rpn.hpp"
26
27 #include <yazpp/z-query.h>
28 #include <yaz/cql.h>
29 #include <yazpp/cql2rpn.h>
30 #include <yaz/zgdu.h>
31 #include <yaz/diagbib1.h>
32 #include <yaz/srw.h>
33
34
35 namespace mp = metaproxy_1;
36 namespace yf = metaproxy_1::filter;
37
38 namespace metaproxy_1 {
39     namespace filter {
40         class CQLtoRPN::Impl {
41         public:
42             Impl();
43             ~Impl();
44             void process(metaproxy_1::Package & package);
45             void configure(const xmlNode * ptr);
46         private:
47             yazpp_1::Yaz_cql2rpn m_cql2rpn;
48         };
49     }
50 }
51
52
53 // define Pimpl wrapper forwarding to Impl
54  
55 yf::CQLtoRPN::CQLtoRPN() : m_p(new Impl)
56 {
57 }
58
59 yf::CQLtoRPN::~CQLtoRPN()
60 {  // must have a destructor because of boost::scoped_ptr
61 }
62
63 void yf::CQLtoRPN::configure(const xmlNode *xmlnode, bool test_only)
64 {
65     m_p->configure(xmlnode);
66 }
67
68 void yf::CQLtoRPN::process(mp::Package &package) const
69 {
70     m_p->process(package);
71 }
72
73
74 // define Implementation stuff
75
76 yf::CQLtoRPN::Impl::Impl()
77 {
78 }
79
80 yf::CQLtoRPN::Impl::~Impl()
81
82 }
83
84 void yf::CQLtoRPN::Impl::configure(const xmlNode *xmlnode)
85 {
86
87     /*
88       <filter type="cql_rpn">
89       <conversion file="pqf.properties"/>
90       </filter>
91     */
92     
93     std::string fname;
94     for (xmlnode = xmlnode->children; xmlnode; xmlnode = xmlnode->next)
95     {
96         if (xmlnode->type != XML_ELEMENT_NODE)
97             continue;
98         if (!strcmp((const char *) xmlnode->name, "conversion"))
99         {
100             const struct _xmlAttr *attr;
101             for (attr = xmlnode->properties; attr; attr = attr->next)
102             {
103                 if (!strcmp((const char *) attr->name, "file"))
104                     fname = mp::xml::get_text(attr);
105                 else
106                     throw mp::filter::FilterException(
107                         "Bad attribute " + std::string((const char *)
108                                                        attr->name));
109             }
110         }
111         else
112         {
113             throw mp::filter::FilterException("Bad element " 
114                                                + std::string((const char *)
115                                                              xmlnode->name));
116         }
117     }
118     if (fname.length() == 0)
119     {
120         throw mp::filter::FilterException("Missing conversion configuration "
121                                           "for filter cql_rpn");
122     }
123
124     int error = 0;
125     if (!m_cql2rpn.parse_spec_file(fname.c_str(), &error))
126     {
127         throw mp::filter::FilterException("Bad or missing "
128                                           "CQL to RPN configuration "
129                                           + fname);
130     }
131 }
132
133 void yf::CQLtoRPN::Impl::process(mp::Package &package)
134 {
135     Z_GDU *gdu = package.request().get();
136
137     if (gdu && gdu->which == Z_GDU_Z3950 && gdu->u.z3950->which ==
138         Z_APDU_searchRequest)
139     {
140         Z_APDU *apdu_req = gdu->u.z3950;
141         Z_SearchRequest *sr = gdu->u.z3950->u.searchRequest;
142         if (sr->query && sr->query->which == Z_Query_type_104 &&
143             sr->query->u.type_104->which == Z_External_CQL)
144         {
145             char *addinfo = 0;
146             Z_RPNQuery *rpnquery = 0;
147             mp::odr odr;
148             
149             int r = m_cql2rpn.query_transform(sr->query->u.type_104->u.cql,
150                                                  &rpnquery, odr,
151                                                  &addinfo);
152             if (r == -3)
153             {
154                 Z_APDU *f_apdu = 
155                     odr.create_searchResponse(
156                         apdu_req, 
157                         YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
158                         "Missing CQL to RPN configuration");
159                 package.response() = f_apdu;
160                 return;
161             }
162             else if (r)
163             {
164                 int error_code = yaz_diag_srw_to_bib1(r);
165
166                 Z_APDU *f_apdu = 
167                     odr.create_searchResponse(apdu_req, error_code, addinfo);
168                 package.response() = f_apdu;
169                 return;
170             }
171             else
172             {   // conversion OK
173                 
174                 sr->query->which = Z_Query_type_1;
175                 sr->query->u.type_1 = rpnquery;
176                 package.request() = gdu;
177             }
178         }
179     }
180     package.move();
181 }
182
183
184 static mp::filter::Base* filter_creator()
185 {
186     return new mp::filter::CQLtoRPN;
187 }
188
189 extern "C" {
190     struct metaproxy_1_filter_struct metaproxy_1_filter_cql_to_rpn = {
191         0,
192         "cql_rpn",
193         filter_creator
194     };
195 }
196
197 /*
198  * Local variables:
199  * c-basic-offset: 4
200  * c-file-style: "Stroustrup"
201  * indent-tabs-mode: nil
202  * End:
203  * vim: shiftwidth=4 tabstop=8 expandtab
204  */
205