factorizing http utils and sru utild code out of SRUtoZ3959 filter and into util...
[metaproxy-moved-to-github.git] / src / sru_util.cpp
1 /* $Id: sru_util.cpp,v 1.2 2006-10-02 13:44:48 marc Exp $
2    Copyright (c) 2005-2006, Index Data.
3
4    See the LICENSE file for details
5 */
6
7 #include "sru_util.hpp"
8 #include "util.hpp"
9
10 //#include <yaz/wrbuf.h>
11 //#include <yaz/querytowrbuf.h>
12
13 #include <iostream>
14 #include <string>
15
16 namespace mp = metaproxy_1;
17
18 // Doxygen doesn't like mp::gdu, so we use this instead
19 namespace mp_util = metaproxy_1::util;
20
21
22 mp_util::SRU::SRU_protocol_type
23 mp_util::SRU::protocol(const Z_HTTP_Request &http_req) const
24 {
25     const std::string mime_urlencoded("application/x-www-form-urlencoded");
26     const std::string mime_text_xml("text/xml");
27     const std::string mime_soap_xml("application/soap+xml");
28
29     const std::string http_method(http_req.method);
30     const std::string http_type 
31         =  mp_util::http_header_value(http_req.headers, "Content-Type");
32
33     if (http_method == "GET")
34         return SRU_GET;
35
36     if (http_method == "POST"
37               && http_type  == mime_urlencoded)
38         return SRU_POST;
39     
40     if ( http_method == "POST"
41          && (http_type  == mime_text_xml
42              || http_type  == mime_soap_xml))
43         return SRU_SOAP;
44
45     return SRU_NONE;
46 }
47
48
49 std::ostream& std::operator<<(std::ostream& os, Z_SRW_PDU& srw_pdu) 
50 {
51     os << "SRU";
52     
53     switch(srw_pdu.which) {
54     case  Z_SRW_searchRetrieve_request:
55         os << " " << "searchRetrieveRequest";
56         {
57             Z_SRW_searchRetrieveRequest *sr = srw_pdu.u.request;
58             if (sr)
59             {
60                 if (sr->database)
61                     os << " " << (sr->database);
62                 else
63                     os << " -";
64                 if (sr->startRecord)
65                     os << " " << *(sr->startRecord);
66                 else
67                     os << " -";
68                 if (sr->maximumRecords)
69                     os << " " << *(sr->maximumRecords);
70                 else
71                     os << " -";
72                 if (sr->recordPacking)
73                     os << " " << (sr->recordPacking);
74                 else
75                     os << " -";
76
77                 if (sr->recordSchema)
78                     os << " " << (sr->recordSchema);
79                 else
80                     os << " -";
81                 
82                 switch (sr->query_type){
83                 case Z_SRW_query_type_cql:
84                     os << " CQL";
85                     if (sr->query.cql)
86                         os << " " << sr->query.cql;
87                     break;
88                 case Z_SRW_query_type_xcql:
89                     os << " XCQL";
90                     break;
91                 case Z_SRW_query_type_pqf:
92                     os << " PQF";
93                     if (sr->query.pqf)
94                         os << " " << sr->query.pqf;
95                     break;
96                 }
97             }
98         }
99         break;
100     case  Z_SRW_searchRetrieve_response:
101         os << " " << "searchRetrieveResponse";
102         {
103             Z_SRW_searchRetrieveResponse *sr = srw_pdu.u.response;
104             if (sr)
105             {
106                 if (! (sr->num_diagnostics))
107                 {
108                     os << " OK";
109                     if (sr->numberOfRecords)
110                         os << " " << *(sr->numberOfRecords);
111                     else
112                         os << " -";
113                     //if (sr->num_records)
114                     os << " " << (sr->num_records);
115                     //else
116                     //os << " -";
117                     if (sr->nextRecordPosition)
118                         os << " " << *(sr->nextRecordPosition);
119                     else
120                         os << " -";
121                 } 
122                 else
123                 {
124                     os << " DIAG";
125                     if (sr->diagnostics && sr->diagnostics->uri)
126                         os << " " << (sr->diagnostics->uri);
127                     else
128                         os << " -";
129                     if (sr->diagnostics && sr->diagnostics->message)
130                         os << " " << (sr->diagnostics->message);
131                     else
132                         os << " -";
133                     if (sr->diagnostics && sr->diagnostics->details)
134                         os << " " << (sr->diagnostics->details);
135                     else
136                         os << " -";
137                 }
138                 
139                     
140             }
141         }
142         break;
143     case  Z_SRW_explain_request:
144         os << " " << "explainRequest";
145         break;
146     case  Z_SRW_explain_response:
147         os << " " << "explainResponse";
148         break;
149     case  Z_SRW_scan_request:
150         os << " " << "scanRequest";
151         break;
152     case  Z_SRW_scan_response:
153         os << " " << "scanResponse";
154         break;
155     case  Z_SRW_update_request:
156         os << " " << "updateRequest";
157         break;
158     case  Z_SRW_update_response:
159         os << " " << "updateResponse";
160         break;
161     default: 
162         os << " " << "UNKNOWN";    
163     }
164
165     return os;    
166 }
167
168
169
170 /*
171  * Local variables:
172  * c-basic-offset: 4
173  * indent-tabs-mode: nil
174  * c-file-style: "stroustrup"
175  * End:
176  * vim: shiftwidth=4 tabstop=8 expandtab
177  */