20 #include <metaproxy/util.hpp>
22 #include <metaproxy/filter.hpp>
23 #include <metaproxy/package.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>
45 void process(metaproxy_1::Package & package);
46 void configure(
const xmlNode *ptr,
const char *path);
57 yf::CQLtoRPN::CQLtoRPN() : m_p(new
Impl)
61 yf::CQLtoRPN::~CQLtoRPN()
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);
79 yf::CQLtoRPN::Impl::Impl() : reverse(false)
83 yf::CQLtoRPN::Impl::~Impl()
87 void yf::CQLtoRPN::Impl::configure(
const xmlNode *xmlnode,
const char *path)
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,
166 odr.create_searchResponse(apdu_req, r, addinfo);
167 package.response() = f_apdu;
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;
178 ext->which = Z_External_CQL;
179 ext->u.cql = odr_strdup(odr, wrbuf_cstr(cql));
181 sr->query->which = Z_Query_type_104;
182 sr->query->u.type_104 = ext;
184 package.request() = gdu;
188 if (!reverse && sr->query && sr->query->which == Z_Query_type_104 &&
189 sr->query->u.type_104->which == Z_External_CQL)
192 Z_RPNQuery *rpnquery = 0;
195 int r = m_cql2rpn.query_transform(sr->query->u.type_104->u.cql,
201 odr.create_searchResponse(
203 YAZ_BIB1_PERMANENT_SYSTEM_ERROR,
204 "cql_rpn: missing CQL to RPN configuration");
205 package.response() = f_apdu;
210 int error_code = yaz_diag_srw_to_bib1(r);
213 odr.create_searchResponse(apdu_req, error_code, addinfo);
214 package.response() = f_apdu;
220 sr->query->which = Z_Query_type_1;
221 sr->query->u.type_1 = rpnquery;
222 package.request() = gdu;
232 return new mp::filter::CQLtoRPN;
struct metaproxy_1_filter_struct metaproxy_1_filter_cql_rpn
static mp::filter::Base * filter_creator()