1 /* This file is part of Metaproxy.
2 Copyright (C) Index Data
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
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
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
20 #include <metaproxy/util.hpp>
22 #include <metaproxy/filter.hpp>
23 #include <metaproxy/package.hpp>
25 #include "filter_cql_rpn.hpp"
27 #include <yazpp/z-query.h>
29 #include <yazpp/cql2rpn.h>
31 #include <yaz/diagbib1.h>
33 #include <yaz/tpath.h>
34 #include <yaz/oid_std.h>
36 namespace mp = metaproxy_1;
37 namespace yf = metaproxy_1::filter;
39 namespace metaproxy_1 {
41 class CQLtoRPN::Impl {
45 void process(metaproxy_1::Package & package);
46 void configure(const xmlNode *ptr, const char *path);
48 yazpp_1::Yaz_cql2rpn m_cql2rpn;
55 // define Pimpl wrapper forwarding to Impl
57 yf::CQLtoRPN::CQLtoRPN() : m_p(new Impl)
61 yf::CQLtoRPN::~CQLtoRPN()
62 { // must have a destructor because of boost::scoped_ptr
65 void yf::CQLtoRPN::configure(const xmlNode *xmlnode, bool test_only,
68 m_p->configure(xmlnode, path);
71 void yf::CQLtoRPN::process(mp::Package &package) const
73 m_p->process(package);
77 // define Implementation stuff
79 yf::CQLtoRPN::Impl::Impl() : reverse(false)
83 yf::CQLtoRPN::Impl::~Impl()
87 void yf::CQLtoRPN::Impl::configure(const xmlNode *xmlnode, const char *path)
91 <filter type="cql_rpn">
92 <conversion file="pqf.properties"/>
97 for (xmlnode = xmlnode->children; xmlnode; xmlnode = xmlnode->next)
99 if (xmlnode->type != XML_ELEMENT_NODE)
101 if (!strcmp((const char *) xmlnode->name, "conversion"))
103 const struct _xmlAttr *attr;
104 for (attr = xmlnode->properties; attr; attr = attr->next)
106 if (!strcmp((const char *) attr->name, "file"))
107 fname = mp::xml::get_text(attr);
108 else if (!strcmp((const char *) attr->name, "reverse"))
110 reverse = mp::xml::get_bool(attr->children, 0);
113 throw mp::filter::FilterException(
114 "Bad attribute " + std::string((const char *)
120 throw mp::filter::FilterException("Bad element "
121 + std::string((const char *)
125 if (fname.length() == 0)
127 throw mp::filter::FilterException("Missing conversion configuration "
128 "for filter cql_rpn");
133 if (!yaz_filepath_resolve(fname.c_str(), path, 0, fullpath))
135 throw mp::filter::FilterException("Could not open " + fname);
138 if (!m_cql2rpn.parse_spec_file(fullpath, &error))
140 throw mp::filter::FilterException("Bad or missing "
141 "CQL to RPN configuration "
146 void yf::CQLtoRPN::Impl::process(mp::Package &package)
148 Z_GDU *gdu = package.request().get();
150 if (gdu && gdu->which == Z_GDU_Z3950 && gdu->u.z3950->which ==
151 Z_APDU_searchRequest)
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)
159 WRBUF cql = wrbuf_alloc();
161 int r = m_cql2rpn.rpn2cql_transform(sr->query->u.type_1, cql,
165 int error_code = yaz_diag_srw_to_bib1(r);
168 odr.create_searchResponse(apdu_req, error_code, addinfo);
169 package.response() = f_apdu;
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;
180 ext->which = Z_External_CQL;
181 ext->u.cql = odr_strdup(odr, wrbuf_cstr(cql));
183 sr->query->which = Z_Query_type_104;
184 sr->query->u.type_104 = ext;
186 package.request() = gdu;
190 if (!reverse && sr->query && sr->query->which == Z_Query_type_104 &&
191 sr->query->u.type_104->which == Z_External_CQL)
194 Z_RPNQuery *rpnquery = 0;
197 int r = m_cql2rpn.query_transform(sr->query->u.type_104->u.cql,
203 odr.create_searchResponse(
205 YAZ_BIB1_PERMANENT_SYSTEM_ERROR,
206 "cql_rpn: missing CQL to RPN configuration");
207 package.response() = f_apdu;
212 int error_code = yaz_diag_srw_to_bib1(r);
215 odr.create_searchResponse(apdu_req, error_code, addinfo);
216 package.response() = f_apdu;
222 sr->query->which = Z_Query_type_1;
223 sr->query->u.type_1 = rpnquery;
224 package.request() = gdu;
232 static mp::filter::Base* filter_creator()
234 return new mp::filter::CQLtoRPN;
238 struct metaproxy_1_filter_struct metaproxy_1_filter_cql_rpn = {
248 * c-file-style: "Stroustrup"
249 * indent-tabs-mode: nil
251 * vim: shiftwidth=4 tabstop=8 expandtab