322eb4628b43a36e865cd9170b66697f36108e80
[metaproxy-moved-to-github.git] / src / filter_cql_to_rpn.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 2005-2013 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 #include <yaz/tpath.h>
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, const char *path);
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                              const char *path)
65 {
66     m_p->configure(xmlnode, path);
67 }
68
69 void yf::CQLtoRPN::process(mp::Package &package) const
70 {
71     m_p->process(package);
72 }
73
74
75 // define Implementation stuff
76
77 yf::CQLtoRPN::Impl::Impl()
78 {
79 }
80
81 yf::CQLtoRPN::Impl::~Impl()
82 {
83 }
84
85 void yf::CQLtoRPN::Impl::configure(const xmlNode *xmlnode, const char *path)
86 {
87
88     /*
89       <filter type="cql_rpn">
90       <conversion file="pqf.properties"/>
91       </filter>
92     */
93
94     std::string fname;
95     for (xmlnode = xmlnode->children; xmlnode; xmlnode = xmlnode->next)
96     {
97         if (xmlnode->type != XML_ELEMENT_NODE)
98             continue;
99         if (!strcmp((const char *) xmlnode->name, "conversion"))
100         {
101             const struct _xmlAttr *attr;
102             for (attr = xmlnode->properties; attr; attr = attr->next)
103             {
104                 if (!strcmp((const char *) attr->name, "file"))
105                     fname = mp::xml::get_text(attr);
106                 else
107                     throw mp::filter::FilterException(
108                         "Bad attribute " + std::string((const char *)
109                                                        attr->name));
110             }
111         }
112         else
113         {
114             throw mp::filter::FilterException("Bad element "
115                                                + std::string((const char *)
116                                                              xmlnode->name));
117         }
118     }
119     if (fname.length() == 0)
120     {
121         throw mp::filter::FilterException("Missing conversion configuration "
122                                           "for filter cql_rpn");
123     }
124
125
126     char fullpath[1024];
127     if (!yaz_filepath_resolve(fname.c_str(), path, 0, fullpath))
128     {
129         throw mp::filter::FilterException("Could not open " + fname);
130     }
131     int error = 0;
132     if (!m_cql2rpn.parse_spec_file(fullpath, &error))
133     {
134         throw mp::filter::FilterException("Bad or missing "
135                                           "CQL to RPN configuration "
136                                           + fname);
137     }
138 }
139
140 void yf::CQLtoRPN::Impl::process(mp::Package &package)
141 {
142     Z_GDU *gdu = package.request().get();
143
144     if (gdu && gdu->which == Z_GDU_Z3950 && gdu->u.z3950->which ==
145         Z_APDU_searchRequest)
146     {
147         Z_APDU *apdu_req = gdu->u.z3950;
148         Z_SearchRequest *sr = gdu->u.z3950->u.searchRequest;
149         if (sr->query && sr->query->which == Z_Query_type_104 &&
150             sr->query->u.type_104->which == Z_External_CQL)
151         {
152             char *addinfo = 0;
153             Z_RPNQuery *rpnquery = 0;
154             mp::odr odr;
155
156             int r = m_cql2rpn.query_transform(sr->query->u.type_104->u.cql,
157                                                  &rpnquery, odr,
158                                                  &addinfo);
159             if (r == -3)
160             {
161                 Z_APDU *f_apdu =
162                     odr.create_searchResponse(
163                         apdu_req,
164                         YAZ_BIB1_PERMANENT_SYSTEM_ERROR,
165                         "cql_rpn: missing CQL to RPN configuration");
166                 package.response() = f_apdu;
167                 return;
168             }
169             else if (r)
170             {
171                 int error_code = yaz_diag_srw_to_bib1(r);
172
173                 Z_APDU *f_apdu =
174                     odr.create_searchResponse(apdu_req, error_code, addinfo);
175                 package.response() = f_apdu;
176                 return;
177             }
178             else
179             {   // conversion OK
180
181                 sr->query->which = Z_Query_type_1;
182                 sr->query->u.type_1 = rpnquery;
183                 package.request() = gdu;
184             }
185         }
186     }
187     package.move();
188 }
189
190
191 static mp::filter::Base* filter_creator()
192 {
193     return new mp::filter::CQLtoRPN;
194 }
195
196 extern "C" {
197     struct metaproxy_1_filter_struct metaproxy_1_filter_cql_rpn = {
198         0,
199         "cql_rpn",
200         filter_creator
201     };
202 }
203
204 /*
205  * Local variables:
206  * c-basic-offset: 4
207  * c-file-style: "Stroustrup"
208  * indent-tabs-mode: nil
209  * End:
210  * vim: shiftwidth=4 tabstop=8 expandtab
211  */
212