1 /* $Id: filter_zeerex_explain.cpp,v 1.6 2007-05-09 21:23:09 adam Exp $
2 Copyright (c) 2005-2007, Index Data.
4 This file is part of Metaproxy.
6 Metaproxy is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
11 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with Metaproxy; see the file LICENSE. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 #include "package.hpp"
26 #include "gduutil.hpp"
27 #include "sru_util.hpp"
28 #include "filter_zeerex_explain.hpp"
31 #include <yaz/z-core.h>
33 #include <yaz/pquery.h>
35 #include <boost/thread/mutex.hpp>
43 namespace mp = metaproxy_1;
44 namespace mp_util = metaproxy_1::util;
45 namespace yf = mp::filter;
48 namespace metaproxy_1 {
50 class ZeeRexExplain::Impl {
52 void configure(const xmlNode *xmlnode);
53 void process(metaproxy_1::Package &package);
55 std::map<std::string, const xmlNode *> m_database_explain;
60 yf::ZeeRexExplain::ZeeRexExplain() : m_p(new Impl)
64 yf::ZeeRexExplain::~ZeeRexExplain()
65 { // must have a destructor because of boost::scoped_ptr
68 void yf::ZeeRexExplain::configure(const xmlNode *xmlnode)
70 m_p->configure(xmlnode);
73 void yf::ZeeRexExplain::process(mp::Package &package) const
75 m_p->process(package);
78 void yf::ZeeRexExplain::Impl::configure(const xmlNode *confignode)
80 const xmlNode * dbnode;
82 for (dbnode = confignode->children; dbnode; dbnode = dbnode->next){
83 if (dbnode->type != XML_ELEMENT_NODE)
87 mp::xml::check_element_mp(dbnode, "database");
89 for (struct _xmlAttr *attr = dbnode->properties;
90 attr; attr = attr->next){
92 mp::xml::check_attribute(attr, "", "name");
93 database = mp::xml::get_text(attr);
95 std::cout << database << "\n";
97 const xmlNode *explainnode;
98 for (explainnode = dbnode->children;
99 explainnode; explainnode = explainnode->next){
100 if (explainnode->type != XML_ELEMENT_NODE)
105 // assigning explain node to database name - no check yet
106 m_database_explain.insert(std::make_pair(database, explainnode));
112 void yf::ZeeRexExplain::Impl::process(mp::Package &package)
114 Z_GDU *zgdu_req = package.request().get();
116 // ignoring all non HTTP_Request packages
117 if (!zgdu_req || !(zgdu_req->which == Z_GDU_HTTP_Request)){
122 // only working on HTTP_Request packages now
124 mp::odr odr_de(ODR_DECODE);
125 Z_SRW_PDU *sru_pdu_req = 0;
127 mp::odr odr_en(ODR_ENCODE);
128 //Z_SRW_PDU *sru_pdu_res = 0;
129 Z_SRW_PDU *sru_pdu_res = yaz_srw_get(odr_en, Z_SRW_explain_response);
131 // finding correct SRU database and explain XML node fragment from config
132 mp_util::SRUServerInfo sruinfo = mp_util::get_sru_server_info(package);
134 const xmlNode *explainnode = 0;
135 std::map<std::string, const xmlNode *>::iterator idbexp;
136 idbexp = m_database_explain.find(sruinfo.database);
138 //std::cout << "Finding " << sruinfo.database << "\n";
139 if (idbexp != m_database_explain.end()){
140 //std::cout << "Found " << idbexp->first << " " << idbexp->second << "\n";
141 explainnode = idbexp->second;
144 // need to emmit error ?? or just let package pass ??
145 //std::cout << "Missed " << sruinfo.database << "\n";
151 // if SRU package could not be decoded, send minimal explain and
156 char *stylesheet = 0;
157 if (! (sru_pdu_req = mp_util::decode_sru_request(package, odr_de, odr_en,
159 charset, stylesheet)))
161 mp_util::build_sru_explain(package, odr_en, sru_pdu_res,
162 sruinfo, explainnode);
163 mp_util::build_sru_response(package, odr_en, soap,
164 sru_pdu_res, charset, stylesheet);
165 package.session().close();
170 if (sru_pdu_req->which != Z_SRW_explain_request)
172 // Let pass all other SRU actions
176 // except valid SRU explain request, construct ZeeRex Explain response
179 Z_SRW_explainRequest *er_req = sru_pdu_req->u.explain_request;
180 //mp_util::build_simple_explain(package, odr_en, sru_pdu_res,
182 mp_util::build_sru_explain(package, odr_en, sru_pdu_res,
183 sruinfo, explainnode, er_req);
184 mp_util::build_sru_response(package, odr_en, soap,
185 sru_pdu_res, charset, stylesheet);
189 // should never arrive here
190 package.session().close();
196 static mp::filter::Base* filter_creator()
198 return new mp::filter::ZeeRexExplain;
202 struct metaproxy_1_filter_struct metaproxy_1_filter_zeerex_explain = {
213 * indent-tabs-mode: nil
214 * c-file-style: "stroustrup"
216 * vim: shiftwidth=4 tabstop=8 expandtab