Fix Metaproxy stops logging after check config failed MP-590
[metaproxy-moved-to-github.git] / src / filter_cql_rpn.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/util.hpp>
21
22 #include <metaproxy/filter.hpp>
23 #include <metaproxy/package.hpp>
24
25 #include "filter_cql_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 #include <yaz/tpath.h>
34 #include <yaz/oid_std.h>
35
36 namespace mp = metaproxy_1;
37 namespace yf = metaproxy_1::filter;
38
39 namespace metaproxy_1 {
40     namespace filter {
41         class CQLtoRPN::Impl {
42         public:
43             Impl();
44             ~Impl();
45             void process(metaproxy_1::Package & package);
46             void configure(const xmlNode *ptr, const char *path);
47         private:
48             yazpp_1::Yaz_cql2rpn m_cql2rpn;
49             bool reverse;
50         };
51     }
52 }
53
54
55 // define Pimpl wrapper forwarding to Impl
56
57 yf::CQLtoRPN::CQLtoRPN() : m_p(new Impl)
58 {
59 }
60
61 yf::CQLtoRPN::~CQLtoRPN()
62 {  // must have a destructor because of boost::scoped_ptr
63 }
64
65 void yf::CQLtoRPN::configure(const xmlNode *xmlnode, bool test_only,
66                              const char *path)
67 {
68     m_p->configure(xmlnode, path);
69 }
70
71 void yf::CQLtoRPN::process(mp::Package &package) const
72 {
73     m_p->process(package);
74 }
75
76
77 // define Implementation stuff
78
79 yf::CQLtoRPN::Impl::Impl() : reverse(false)
80 {
81 }
82
83 yf::CQLtoRPN::Impl::~Impl()
84 {
85 }
86
87 void yf::CQLtoRPN::Impl::configure(const xmlNode *xmlnode, const char *path)
88 {
89
90     /*
91       <filter type="cql_rpn">
92       <conversion file="pqf.properties"/>
93       </filter>
94     */
95
96     std::string fname;
97     for (xmlnode = xmlnode->children; xmlnode; xmlnode = xmlnode->next)
98     {
99         if (xmlnode->type != XML_ELEMENT_NODE)
100             continue;
101         if (!strcmp((const char *) xmlnode->name, "conversion"))
102         {
103             const struct _xmlAttr *attr;
104             for (attr = xmlnode->properties; attr; attr = attr->next)
105             {
106                 if (!strcmp((const char *) attr->name, "file"))
107                     fname = mp::xml::get_text(attr);
108                 else if (!strcmp((const char *) attr->name, "reverse"))
109                 {
110                     reverse = mp::xml::get_bool(attr->children, 0);
111                 }
112                 else
113                     throw mp::filter::FilterException(
114                         "Bad attribute " + std::string((const char *)
115                                                        attr->name));
116             }
117         }
118         else
119         {
120             throw mp::filter::FilterException("Bad element "
121                                                + std::string((const char *)
122                                                              xmlnode->name));
123         }
124     }
125     if (fname.length() == 0)
126     {
127         throw mp::filter::FilterException("Missing conversion configuration "
128                                           "for filter cql_rpn");
129     }
130
131
132     char fullpath[1024];
133     if (!yaz_filepath_resolve(fname.c_str(), path, 0, fullpath))
134     {
135         throw mp::filter::FilterException("Could not open " + fname);
136     }
137     int error = 0;
138     if (!m_cql2rpn.parse_spec_file(fullpath, &error))
139     {
140         throw mp::filter::FilterException("Bad or missing "
141                                           "CQL to RPN configuration "
142                                           + fname);
143     }
144 }
145
146 void yf::CQLtoRPN::Impl::process(mp::Package &package)
147 {
148     Z_GDU *gdu = package.request().get();
149
150     if (gdu && gdu->which == Z_GDU_Z3950 && gdu->u.z3950->which ==
151         Z_APDU_searchRequest)
152     {
153         Z_APDU *apdu_req = gdu->u.z3950;
154         Z_SearchRequest *sr = gdu->u.z3950->u.searchRequest;
155         if (reverse && sr->query && sr->query->which == Z_Query_type_1)
156         {
157             char *addinfo = 0;
158             mp::odr odr;
159             WRBUF cql = wrbuf_alloc();
160
161             int r = m_cql2rpn.rpn2cql_transform(sr->query->u.type_1, cql,
162                                                 odr, &addinfo);
163             if (r)
164             {
165                 int error_code = yaz_diag_srw_to_bib1(r);
166
167                 Z_APDU *f_apdu =
168                     odr.create_searchResponse(apdu_req, error_code, addinfo);
169                 package.response() = f_apdu;
170                 return;
171             }
172             else
173             {
174                 Z_External *ext = (Z_External *)
175                     odr_malloc(odr, sizeof(*ext));
176                 ext->direct_reference = odr_oiddup(odr,
177                                                    yaz_oid_userinfo_cql);
178                 ext->indirect_reference = 0;
179                 ext->descriptor = 0;
180                 ext->which = Z_External_CQL;
181                 ext->u.cql = odr_strdup(odr, wrbuf_cstr(cql));
182
183                 sr->query->which = Z_Query_type_104;
184                 sr->query->u.type_104 = ext;
185
186                 package.request() = gdu;
187             }
188             wrbuf_destroy(cql);
189         }
190         if (!reverse && sr->query && sr->query->which == Z_Query_type_104 &&
191             sr->query->u.type_104->which == Z_External_CQL)
192         {
193             char *addinfo = 0;
194             Z_RPNQuery *rpnquery = 0;
195             mp::odr odr;
196
197             int r = m_cql2rpn.query_transform(sr->query->u.type_104->u.cql,
198                                                  &rpnquery, odr,
199                                                  &addinfo);
200             if (r == -3)
201             {
202                 Z_APDU *f_apdu =
203                     odr.create_searchResponse(
204                         apdu_req,
205                         YAZ_BIB1_PERMANENT_SYSTEM_ERROR,
206                         "cql_rpn: missing CQL to RPN configuration");
207                 package.response() = f_apdu;
208                 return;
209             }
210             else if (r)
211             {
212                 int error_code = yaz_diag_srw_to_bib1(r);
213
214                 Z_APDU *f_apdu =
215                     odr.create_searchResponse(apdu_req, error_code, addinfo);
216                 package.response() = f_apdu;
217                 return;
218             }
219             else
220             {   // conversion OK
221
222                 sr->query->which = Z_Query_type_1;
223                 sr->query->u.type_1 = rpnquery;
224                 package.request() = gdu;
225             }
226         }
227     }
228     package.move();
229 }
230
231
232 static mp::filter::Base* filter_creator()
233 {
234     return new mp::filter::CQLtoRPN;
235 }
236
237 extern "C" {
238     struct metaproxy_1_filter_struct metaproxy_1_filter_cql_rpn = {
239         0,
240         "cql_rpn",
241         filter_creator
242     };
243 }
244
245 /*
246  * Local variables:
247  * c-basic-offset: 4
248  * c-file-style: "Stroustrup"
249  * indent-tabs-mode: nil
250  * End:
251  * vim: shiftwidth=4 tabstop=8 expandtab
252  */
253