metaproxy  1.21.0
filter_cql_rpn.cpp
Go to the documentation of this file.
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 {
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  Z_APDU *f_apdu =
166  odr.create_searchResponse(apdu_req, r, addinfo);
167  package.response() = f_apdu;
168  return;
169  }
170  else
171  {
172  Z_External *ext = (Z_External *)
173  odr_malloc(odr, sizeof(*ext));
174  ext->direct_reference = odr_oiddup(odr,
175  yaz_oid_userinfo_cql);
176  ext->indirect_reference = 0;
177  ext->descriptor = 0;
178  ext->which = Z_External_CQL;
179  ext->u.cql = odr_strdup(odr, wrbuf_cstr(cql));
180 
181  sr->query->which = Z_Query_type_104;
182  sr->query->u.type_104 = ext;
183 
184  package.request() = gdu;
185  }
186  wrbuf_destroy(cql);
187  }
188  if (!reverse && sr->query && sr->query->which == Z_Query_type_104 &&
189  sr->query->u.type_104->which == Z_External_CQL)
190  {
191  char *addinfo = 0;
192  Z_RPNQuery *rpnquery = 0;
193  mp::odr odr;
194 
195  int r = m_cql2rpn.query_transform(sr->query->u.type_104->u.cql,
196  &rpnquery, odr,
197  &addinfo);
198  if (r == -3)
199  {
200  Z_APDU *f_apdu =
201  odr.create_searchResponse(
202  apdu_req,
203  YAZ_BIB1_PERMANENT_SYSTEM_ERROR,
204  "cql_rpn: missing CQL to RPN configuration");
205  package.response() = f_apdu;
206  return;
207  }
208  else if (r)
209  {
210  int error_code = yaz_diag_srw_to_bib1(r);
211 
212  Z_APDU *f_apdu =
213  odr.create_searchResponse(apdu_req, error_code, addinfo);
214  package.response() = f_apdu;
215  return;
216  }
217  else
218  { // conversion OK
219 
220  sr->query->which = Z_Query_type_1;
221  sr->query->u.type_1 = rpnquery;
222  package.request() = gdu;
223  }
224  }
225  }
226  package.move();
227 }
228 
229 
230 static mp::filter::Base* filter_creator()
231 {
232  return new mp::filter::CQLtoRPN;
233 }
234 
235 extern "C" {
236  struct metaproxy_1_filter_struct metaproxy_1_filter_cql_rpn = {
237  0,
238  "cql_rpn",
240  };
241 }
242 
243 /*
244  * Local variables:
245  * c-basic-offset: 4
246  * c-file-style: "Stroustrup"
247  * indent-tabs-mode: nil
248  * End:
249  * vim: shiftwidth=4 tabstop=8 expandtab
250  */
251 
void process(metaproxy_1::Package &package)
void configure(const xmlNode *ptr, const char *path)
struct metaproxy_1_filter_struct metaproxy_1_filter_cql_rpn
static mp::filter::Base * filter_creator()