New filter sd_remove
[metaproxy-moved-to-github.git] / src / filter_sd_remove.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 2005-2013 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 "config.hpp"
20 #include "filter_sd_remove.hpp"
21 #include <metaproxy/package.hpp>
22 #include <metaproxy/util.hpp>
23 #include <yaz/zgdu.h>
24 #include <metaproxy/filter.hpp>
25 #include <yaz/querytowrbuf.h>
26
27 namespace metaproxy_1 {
28     namespace filter {
29         class SD_Remove : public Base {
30         public:
31             SD_Remove();
32             ~SD_Remove();
33             void process(metaproxy_1::Package & package) const;
34             void configure(const xmlNode * ptr, bool test_only,
35                            const char *path);
36         };
37     }
38 }
39
40 namespace mp = metaproxy_1;
41 namespace yf = mp::filter;
42
43 yf::SD_Remove::SD_Remove()
44 {
45 }
46
47 yf::SD_Remove::~SD_Remove()
48 {
49 }
50
51 void yf::SD_Remove::configure(const xmlNode *xmlnode, bool test_only,
52                              const char *path)
53 {
54     if (xmlnode)
55     {
56         xmlNode *ptr;
57         for (ptr = xmlnode->children; ptr; ptr = ptr->next)
58         {
59             if (ptr->type == XML_ELEMENT_NODE)
60                 throw mp::filter::FilterException("Bad element "
61                                                   + std::string((const char *)
62                                                                 ptr->name));
63         }
64     }
65 }
66
67 void yf::SD_Remove::process(mp::Package &package) const
68 {
69     package.move();
70
71     Z_GDU *gdu_res = package.response().get();
72     if (gdu_res && gdu_res->which == Z_GDU_Z3950)
73     {
74         Z_NamePlusRecordList *records = 0;
75         Z_APDU *apdu = gdu_res->u.z3950;
76         if (apdu->which == Z_APDU_presentResponse)
77         {
78             Z_PresentResponse * pr_res = apdu->u.presentResponse;
79             if (pr_res->numberOfRecordsReturned
80                 && *(pr_res->numberOfRecordsReturned) > 0
81                 && pr_res->records
82                 && pr_res->records->which == Z_Records_DBOSD)
83             {
84                 records = pr_res->records->u.databaseOrSurDiagnostics;
85             }
86         }
87         if (apdu->which == Z_APDU_searchResponse)
88         {
89             Z_SearchResponse *sr_res = apdu->u.searchResponse;
90             if (
91                 sr_res->numberOfRecordsReturned
92                 && *(sr_res->numberOfRecordsReturned) > 0
93                 && sr_res->records
94                 && sr_res->records->which == Z_Records_DBOSD)
95             {
96                 records = sr_res->records->u.databaseOrSurDiagnostics;
97             }
98         }
99         if (records)
100         {
101             mp::odr odr_en(ODR_ENCODE);
102             int i;
103             for (i = 0; i < records->num_records; i++)
104             {
105                 Z_NamePlusRecord *npr = records->records[i];
106                 if (npr->which == Z_NamePlusRecord_surrogateDiagnostic)
107                 {
108                     WRBUF w = wrbuf_alloc();
109                     wrbuf_diags(w, 1, &npr->u.surrogateDiagnostic);
110                     npr->which = Z_NamePlusRecord_databaseRecord;
111                     npr->u.databaseRecord = z_ext_record_sutrs(odr_en,
112                                                                wrbuf_buf(w),
113                                                                wrbuf_len(w));
114                     wrbuf_destroy(w);
115                 }
116             }
117             package.response() = gdu_res;
118         }
119     }
120 }
121
122
123 static mp::filter::Base* filter_creator()
124 {
125     return new mp::filter::SD_Remove;
126 }
127
128 extern "C" {
129     struct metaproxy_1_filter_struct metaproxy_1_filter_sd_remove = {
130         0,
131         "sd_remove",
132         filter_creator
133     };
134 }
135
136
137 /*
138  * Local variables:
139  * c-basic-offset: 4
140  * c-file-style: "Stroustrup"
141  * indent-tabs-mode: nil
142  * End:
143  * vim: shiftwidth=4 tabstop=8 expandtab
144  */
145