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