2b22813a24431c26a71716056aa08ed3afe570a8
[metaproxy-moved-to-github.git] / src / filter_bounce.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 2005-2011 Index Data
3
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
7 version.
8
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
12 for more details.
13
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
17 */
18
19 #include "filter_bounce.hpp"
20 #include <metaproxy/package.hpp>
21 #include <metaproxy/util.hpp>
22 #include "gduutil.hpp"
23
24 #include <yaz/zgdu.h>
25
26 #include <sstream>
27
28 //#include "config.hpp"
29 //#include "filter.hpp"
30
31 //#include <boost/thread/mutex.hpp>
32
33
34
35
36
37 namespace mp = metaproxy_1;
38 namespace yf = mp::filter;
39
40 namespace metaproxy_1 {
41     namespace filter {
42         class Bounce::Rep {
43             friend class Bounce;
44             bool bounce;
45         };
46     }
47 }
48
49 yf::Bounce::Bounce() : m_p(new Rep)
50 {
51     m_p->bounce = true;
52 }
53
54 yf::Bounce::~Bounce()
55 {  // must have a destructor because of boost::scoped_ptr
56 }
57
58 void yf::Bounce::process(mp::Package &package) const
59 {
60     if (! m_p->bounce )
61     {
62         package.move();
63         return;
64     }
65     package.session().close();
66     
67     Z_GDU *zgdu = package.request().get();
68     
69     if (!zgdu)
70         return;
71     
72     //std::string message("BOUNCE ");
73     std::ostringstream message;    
74     message << "BOUNCE " << *zgdu;
75     
76     metaproxy_1::odr odr; 
77     
78     if (zgdu->which == Z_GDU_Z3950)
79     {
80         Z_APDU *apdu_res = 0;
81         apdu_res = odr.create_close(zgdu->u.z3950,
82                                     Z_Close_systemProblem,
83                                     message.str().c_str());
84         package.response() = apdu_res;
85     }
86     else if (zgdu->which == Z_GDU_HTTP_Request)
87     {
88         Z_GDU *zgdu_res = 0;
89         zgdu_res 
90             = odr.create_HTTP_Response(package.session(), 
91                                        zgdu->u.HTTP_Request, 400);
92         
93         package.response() = zgdu_res;
94     }
95     else if (zgdu->which == Z_GDU_HTTP_Response)
96     {
97     }
98     
99
100     return;
101 }
102
103 static mp::filter::Base* filter_creator()
104 {
105     return new mp::filter::Bounce;
106 }
107
108 extern "C" {
109     struct metaproxy_1_filter_struct metaproxy_1_filter_bounce = {
110         0,
111         "bounce",
112         filter_creator
113     };
114 }
115
116
117 /*
118  * Local variables:
119  * c-basic-offset: 4
120  * c-file-style: "Stroustrup"
121  * indent-tabs-mode: nil
122  * End:
123  * vim: shiftwidth=4 tabstop=8 expandtab
124  */
125