Windows: use Boost 1.59, msvc 14.0
[metaproxy-moved-to-github.git] / src / filter_sd_remove.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 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 <yaz/diagbib1.h>
25 #include <metaproxy/filter.hpp>
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 static void diagrec_to_sutrs(WRBUF b, Z_DiagRec *diag)
68 {
69     wrbuf_puts(b," ERROR ");
70     if (diag->which != Z_DiagRec_defaultFormat)
71         wrbuf_puts(b, "diag not in default format");
72     else
73     {
74         Z_DefaultDiagFormat *e = diag->u.defaultFormat;
75         wrbuf_printf(b, ODR_INT_PRINTF ": %s", *e->condition,
76                      diagbib1_str(*e->condition));
77         if (e->u.v2Addinfo && *e->u.v2Addinfo) // v3Addinfo is same data
78         {
79             wrbuf_puts(b, " -- ");
80             wrbuf_puts(b, e->u.v2Addinfo);
81         }
82     }
83     wrbuf_puts(b, "\n");
84 }
85
86 void yf::SD_Remove::process(mp::Package &package) const
87 {
88     package.move();
89
90     Z_GDU *gdu_res = package.response().get();
91     if (gdu_res && gdu_res->which == Z_GDU_Z3950)
92     {
93         Z_NamePlusRecordList *records = 0;
94         Z_APDU *apdu = gdu_res->u.z3950;
95         if (apdu->which == Z_APDU_presentResponse)
96         {
97             Z_PresentResponse * pr_res = apdu->u.presentResponse;
98             if (pr_res->numberOfRecordsReturned
99                 && *(pr_res->numberOfRecordsReturned) > 0
100                 && pr_res->records
101                 && pr_res->records->which == Z_Records_DBOSD)
102             {
103                 records = pr_res->records->u.databaseOrSurDiagnostics;
104             }
105         }
106         if (apdu->which == Z_APDU_searchResponse)
107         {
108             Z_SearchResponse *sr_res = apdu->u.searchResponse;
109             if (
110                 sr_res->numberOfRecordsReturned
111                 && *(sr_res->numberOfRecordsReturned) > 0
112                 && sr_res->records
113                 && sr_res->records->which == Z_Records_DBOSD)
114             {
115                 records = sr_res->records->u.databaseOrSurDiagnostics;
116             }
117         }
118         if (records)
119         {
120             mp::odr odr_en(ODR_ENCODE);
121             int i;
122             for (i = 0; i < records->num_records; i++)
123             {
124                 Z_NamePlusRecord *npr = records->records[i];
125                 if (npr->which == Z_NamePlusRecord_surrogateDiagnostic)
126                 {
127                     WRBUF w = wrbuf_alloc();
128                     diagrec_to_sutrs(w, npr->u.surrogateDiagnostic);
129                     npr->which = Z_NamePlusRecord_databaseRecord;
130                     npr->u.databaseRecord = z_ext_record_sutrs(odr_en,
131                                                                wrbuf_buf(w),
132                                                                wrbuf_len(w));
133                     wrbuf_destroy(w);
134                 }
135             }
136             package.response() = gdu_res;
137         }
138     }
139 }
140
141
142 static mp::filter::Base* filter_creator()
143 {
144     return new mp::filter::SD_Remove;
145 }
146
147 extern "C" {
148     struct metaproxy_1_filter_struct metaproxy_1_filter_sd_remove = {
149         0,
150         "sd_remove",
151         filter_creator
152     };
153 }
154
155
156 /*
157  * Local variables:
158  * c-basic-offset: 4
159  * c-file-style: "Stroustrup"
160  * indent-tabs-mode: nil
161  * End:
162  * vim: shiftwidth=4 tabstop=8 expandtab
163  */
164