The configure method takes test_only flag so we can avoid
[metaproxy-moved-to-github.git] / src / filter_cql_to_rpn.cpp
1 /* $Id: filter_cql_to_rpn.cpp,v 1.8 2008-02-20 15:07:51 adam Exp $
2    Copyright (c) 2005-2007, Index Data.
3
4 This file is part of Metaproxy.
5
6 Metaproxy is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
10
11 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Metaproxy; see the file LICENSE.  If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA.
20  */
21 /* $Id: filter_cql_to_rpn.cpp,v 1.8 2008-02-20 15:07:51 adam Exp $
22    Copyright (c) 2005-2007, Index Data.
23
24    See the LICENSE file for details
25  */
26
27 #include "config.hpp"
28 #include "util.hpp"
29
30 #include "filter.hpp"
31 #include "package.hpp"
32
33 #include "filter_cql_to_rpn.hpp"
34
35 #include <yazpp/z-query.h>
36 #include <yaz/cql.h>
37 #include <yazpp/cql2rpn.h>
38 #include <yaz/zgdu.h>
39 #include <yaz/diagbib1.h>
40 #include <yaz/srw.h>
41
42
43 namespace mp = metaproxy_1;
44 namespace yf = metaproxy_1::filter;
45
46 namespace metaproxy_1 {
47     namespace filter {
48         class CQLtoRPN::Impl {
49         public:
50             Impl();
51             ~Impl();
52             void process(metaproxy_1::Package & package);
53             void configure(const xmlNode * ptr);
54         private:
55             yazpp_1::Yaz_cql2rpn m_cql2rpn;
56         };
57     }
58 }
59
60
61 // define Pimpl wrapper forwarding to Impl
62  
63 yf::CQLtoRPN::CQLtoRPN() : m_p(new Impl)
64 {
65 }
66
67 yf::CQLtoRPN::~CQLtoRPN()
68 {  // must have a destructor because of boost::scoped_ptr
69 }
70
71 void yf::CQLtoRPN::configure(const xmlNode *xmlnode, bool test_only)
72 {
73     m_p->configure(xmlnode);
74 }
75
76 void yf::CQLtoRPN::process(mp::Package &package) const
77 {
78     m_p->process(package);
79 }
80
81
82 // define Implementation stuff
83
84 yf::CQLtoRPN::Impl::Impl()
85 {
86 }
87
88 yf::CQLtoRPN::Impl::~Impl()
89
90 }
91
92 void yf::CQLtoRPN::Impl::configure(const xmlNode *xmlnode)
93 {
94
95     /*
96       <filter type="cql_rpn">
97       <conversion file="pqf.properties"/>
98       </filter>
99     */
100     
101     std::string fname;
102     for (xmlnode = xmlnode->children; xmlnode; xmlnode = xmlnode->next)
103     {
104         if (xmlnode->type != XML_ELEMENT_NODE)
105             continue;
106         if (!strcmp((const char *) xmlnode->name, "conversion"))
107         {
108             const struct _xmlAttr *attr;
109             for (attr = xmlnode->properties; attr; attr = attr->next)
110             {
111                 if (!strcmp((const char *) attr->name, "file"))
112                     fname = mp::xml::get_text(attr);
113                 else
114                     throw mp::filter::FilterException(
115                         "Bad attribute " + std::string((const char *)
116                                                        attr->name));
117             }
118         }
119         else
120         {
121             throw mp::filter::FilterException("Bad element " 
122                                                + std::string((const char *)
123                                                              xmlnode->name));
124         }
125     }
126     if (fname.length() == 0)
127     {
128         throw mp::filter::FilterException("Missing conversion configuration "
129                                           "for filter cql_rpn");
130     }
131
132     int error = 0;
133     if (!m_cql2rpn.parse_spec_file(fname.c_str(), &error))
134     {
135         throw mp::filter::FilterException("Bad or missing "
136                                           "CQL to RPN configuration "
137                                           + fname);
138     }
139 }
140
141 void yf::CQLtoRPN::Impl::process(mp::Package &package)
142 {
143     Z_GDU *gdu = package.request().get();
144
145     if (gdu && gdu->which == Z_GDU_Z3950 && gdu->u.z3950->which ==
146         Z_APDU_searchRequest)
147     {
148         Z_APDU *apdu_req = gdu->u.z3950;
149         Z_SearchRequest *sr = gdu->u.z3950->u.searchRequest;
150         if (sr->query && sr->query->which == Z_Query_type_104 &&
151             sr->query->u.type_104->which == Z_External_CQL)
152         {
153             char *addinfo = 0;
154             Z_RPNQuery *rpnquery = 0;
155             mp::odr odr;
156             
157             int r = m_cql2rpn.query_transform(sr->query->u.type_104->u.cql,
158                                                  &rpnquery, odr,
159                                                  &addinfo);
160             if (r == -3)
161             {
162                 Z_APDU *f_apdu = 
163                     odr.create_searchResponse(
164                         apdu_req, 
165                         YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
166                         "Missing CQL to RPN configuration");
167                 package.response() = f_apdu;
168                 return;
169             }
170             else if (r)
171             {
172                 int error_code = yaz_diag_srw_to_bib1(r);
173
174                 Z_APDU *f_apdu = 
175                     odr.create_searchResponse(apdu_req, error_code, addinfo);
176                 package.response() = f_apdu;
177                 return;
178             }
179             else
180             {   // conversion OK
181                 
182                 sr->query->which = Z_Query_type_1;
183                 sr->query->u.type_1 = rpnquery;
184                 package.request() = gdu;
185             }
186         }
187     }
188     package.move();
189 }
190
191
192 static mp::filter::Base* filter_creator()
193 {
194     return new mp::filter::CQLtoRPN;
195 }
196
197 extern "C" {
198     struct metaproxy_1_filter_struct metaproxy_1_filter_cql_to_rpn = {
199         0,
200         "cql_rpn",
201         filter_creator
202     };
203 }
204
205 /*
206  * Local variables:
207  * c-basic-offset: 4
208  * indent-tabs-mode: nil
209  * c-file-style: "stroustrup"
210  * End:
211  * vim: shiftwidth=4 tabstop=8 expandtab
212  */