1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2013 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 "filter_zeerex_explain.hpp"
21 #include <metaproxy/package.hpp>
22 #include <metaproxy/util.hpp>
23 #include "gduutil.hpp"
24 #include "sru_util.hpp"
27 #include <yaz/z-core.h>
29 #include <yaz/pquery.h>
31 #include <boost/thread/mutex.hpp>
39 namespace mp = metaproxy_1;
40 namespace mp_util = metaproxy_1::util;
41 namespace yf = mp::filter;
44 namespace metaproxy_1 {
46 class ZeeRexExplain::Impl {
48 void configure(const xmlNode *xmlnode);
49 void process(metaproxy_1::Package &package);
51 std::map<std::string, const xmlNode *> m_database_explain;
56 yf::ZeeRexExplain::ZeeRexExplain() : m_p(new Impl)
60 yf::ZeeRexExplain::~ZeeRexExplain()
61 { // must have a destructor because of boost::scoped_ptr
64 void yf::ZeeRexExplain::configure(const xmlNode *xmlnode, bool test_only,
67 m_p->configure(xmlnode);
70 void yf::ZeeRexExplain::process(mp::Package &package) const
72 m_p->process(package);
75 void yf::ZeeRexExplain::Impl::configure(const xmlNode *confignode)
77 const xmlNode * dbnode;
79 for (dbnode = confignode->children; dbnode; dbnode = dbnode->next){
80 if (dbnode->type != XML_ELEMENT_NODE)
84 mp::xml::check_element_mp(dbnode, "database");
86 for (struct _xmlAttr *attr = dbnode->properties;
87 attr; attr = attr->next){
89 mp::xml::check_attribute(attr, "", "name");
90 database = mp::xml::get_text(attr);
92 std::cout << database << "\n";
94 const xmlNode *explainnode;
95 for (explainnode = dbnode->children;
96 explainnode; explainnode = explainnode->next){
97 if (explainnode->type != XML_ELEMENT_NODE)
102 // assigning explain node to database name - no check yet
103 m_database_explain.insert(std::make_pair(database, explainnode));
109 void yf::ZeeRexExplain::Impl::process(mp::Package &package)
111 Z_GDU *zgdu_req = package.request().get();
113 // ignoring all non HTTP_Request packages
114 if (!zgdu_req || !(zgdu_req->which == Z_GDU_HTTP_Request)){
119 // only working on HTTP_Request packages now
121 mp::odr odr_de(ODR_DECODE);
122 Z_SRW_PDU *sru_pdu_req = 0;
124 mp::odr odr_en(ODR_ENCODE);
125 //Z_SRW_PDU *sru_pdu_res = 0;
126 Z_SRW_PDU *sru_pdu_res = yaz_srw_get(odr_en, Z_SRW_explain_response);
128 // finding correct SRU database and explain XML node fragment from config
129 mp_util::SRUServerInfo sruinfo = mp_util::get_sru_server_info(package);
131 const xmlNode *explainnode = 0;
132 std::map<std::string, const xmlNode *>::iterator idbexp;
133 idbexp = m_database_explain.find(sruinfo.database);
135 //std::cout << "Finding " << sruinfo.database << "\n";
136 if (idbexp != m_database_explain.end()){
137 //std::cout << "Found " << idbexp->first << " " << idbexp->second << "\n";
138 explainnode = idbexp->second;
141 // need to emmit error ?? or just let package pass ??
142 //std::cout << "Missed " << sruinfo.database << "\n";
148 // if SRU package could not be decoded, send minimal explain and
153 char *stylesheet = 0;
154 if (! (sru_pdu_req = mp_util::decode_sru_request(package, odr_de, odr_en,
156 charset, stylesheet)))
158 mp_util::build_sru_explain(package, odr_en, sru_pdu_res,
159 sruinfo, explainnode);
160 mp_util::build_sru_response(package, odr_en, soap,
161 sru_pdu_res, charset, stylesheet);
162 package.session().close();
167 if (sru_pdu_req->which != Z_SRW_explain_request)
169 // Let pass all other SRU actions
173 // except valid SRU explain request, construct ZeeRex Explain response
176 Z_SRW_explainRequest *er_req = sru_pdu_req->u.explain_request;
177 //mp_util::build_simple_explain(package, odr_en, sru_pdu_res,
179 mp_util::build_sru_explain(package, odr_en, sru_pdu_res,
180 sruinfo, explainnode, er_req);
181 mp_util::build_sru_response(package, odr_en, soap,
182 sru_pdu_res, charset, stylesheet);
186 // should never arrive here
187 package.session().close();
193 static mp::filter::Base* filter_creator()
195 return new mp::filter::ZeeRexExplain;
199 struct metaproxy_1_filter_struct metaproxy_1_filter_zeerex_explain = {
210 * c-file-style: "Stroustrup"
211 * indent-tabs-mode: nil
213 * vim: shiftwidth=4 tabstop=8 expandtab