1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2011 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)
66 m_p->configure(xmlnode);
69 void yf::ZeeRexExplain::process(mp::Package &package) const
71 m_p->process(package);
74 void yf::ZeeRexExplain::Impl::configure(const xmlNode *confignode)
76 const xmlNode * dbnode;
78 for (dbnode = confignode->children; dbnode; dbnode = dbnode->next){
79 if (dbnode->type != XML_ELEMENT_NODE)
83 mp::xml::check_element_mp(dbnode, "database");
85 for (struct _xmlAttr *attr = dbnode->properties;
86 attr; attr = attr->next){
88 mp::xml::check_attribute(attr, "", "name");
89 database = mp::xml::get_text(attr);
91 std::cout << database << "\n";
93 const xmlNode *explainnode;
94 for (explainnode = dbnode->children;
95 explainnode; explainnode = explainnode->next){
96 if (explainnode->type != XML_ELEMENT_NODE)
101 // assigning explain node to database name - no check yet
102 m_database_explain.insert(std::make_pair(database, explainnode));
108 void yf::ZeeRexExplain::Impl::process(mp::Package &package)
110 Z_GDU *zgdu_req = package.request().get();
112 // ignoring all non HTTP_Request packages
113 if (!zgdu_req || !(zgdu_req->which == Z_GDU_HTTP_Request)){
118 // only working on HTTP_Request packages now
120 mp::odr odr_de(ODR_DECODE);
121 Z_SRW_PDU *sru_pdu_req = 0;
123 mp::odr odr_en(ODR_ENCODE);
124 //Z_SRW_PDU *sru_pdu_res = 0;
125 Z_SRW_PDU *sru_pdu_res = yaz_srw_get(odr_en, Z_SRW_explain_response);
127 // finding correct SRU database and explain XML node fragment from config
128 mp_util::SRUServerInfo sruinfo = mp_util::get_sru_server_info(package);
130 const xmlNode *explainnode = 0;
131 std::map<std::string, const xmlNode *>::iterator idbexp;
132 idbexp = m_database_explain.find(sruinfo.database);
134 //std::cout << "Finding " << sruinfo.database << "\n";
135 if (idbexp != m_database_explain.end()){
136 //std::cout << "Found " << idbexp->first << " " << idbexp->second << "\n";
137 explainnode = idbexp->second;
140 // need to emmit error ?? or just let package pass ??
141 //std::cout << "Missed " << sruinfo.database << "\n";
147 // if SRU package could not be decoded, send minimal explain and
152 char *stylesheet = 0;
153 if (! (sru_pdu_req = mp_util::decode_sru_request(package, odr_de, odr_en,
155 charset, stylesheet)))
157 mp_util::build_sru_explain(package, odr_en, sru_pdu_res,
158 sruinfo, explainnode);
159 mp_util::build_sru_response(package, odr_en, soap,
160 sru_pdu_res, charset, stylesheet);
161 package.session().close();
166 if (sru_pdu_req->which != Z_SRW_explain_request)
168 // Let pass all other SRU actions
172 // except valid SRU explain request, construct ZeeRex Explain response
175 Z_SRW_explainRequest *er_req = sru_pdu_req->u.explain_request;
176 //mp_util::build_simple_explain(package, odr_en, sru_pdu_res,
178 mp_util::build_sru_explain(package, odr_en, sru_pdu_res,
179 sruinfo, explainnode, er_req);
180 mp_util::build_sru_response(package, odr_en, soap,
181 sru_pdu_res, charset, stylesheet);
185 // should never arrive here
186 package.session().close();
192 static mp::filter::Base* filter_creator()
194 return new mp::filter::ZeeRexExplain;
198 struct metaproxy_1_filter_struct metaproxy_1_filter_zeerex_explain = {
209 * c-file-style: "Stroustrup"
210 * indent-tabs-mode: nil
212 * vim: shiftwidth=4 tabstop=8 expandtab