Spell fixes for urlRecipe description
[metaproxy-moved-to-github.git] / src / gduutil.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 "gduutil.hpp"
20 #include <metaproxy/util.hpp>
21
22 #include <yaz/wrbuf.h>
23 #include <yaz/oid_db.h>
24 #include <yaz/querytowrbuf.h>
25
26 #include <iostream>
27
28 namespace mp = metaproxy_1;
29
30 // Doxygen doesn't like mp::gdu, so we use this instead
31 namespace mp_util = metaproxy_1::util;
32
33 std::ostream& std::operator<<(std::ostream& os,  Z_GDU& zgdu)
34 {
35     if (zgdu.which == Z_GDU_Z3950)
36     {
37         os << "Z3950";
38         if (zgdu.u.z3950)
39             os << *(zgdu.u.z3950);
40     }
41     else if (zgdu.which == Z_GDU_HTTP_Request)
42     {
43         os << "HTTP_Request";
44         if (zgdu.u.HTTP_Request)
45             os << " " << *(zgdu.u.HTTP_Request);
46     }
47     else if (zgdu.which == Z_GDU_HTTP_Response)
48     {
49         os << "HTTP_Response";
50         if (zgdu.u.HTTP_Response)
51             os << " " << *(zgdu.u.HTTP_Response);
52     }
53     else
54         os << "Z_GDU";
55     return os;
56 }
57
58 std::ostream& std::operator<<(std::ostream& os, Z_HTTP_Request& httpreq)
59 {
60     os << httpreq.method << " ";
61     os << httpreq.path;    
62     return os;
63 }
64
65 std::ostream& std::operator<<(std::ostream& os, Z_HTTP_Response& httpres)
66 {
67     os << httpres.code << " ";
68     os << httpres.content_len;   
69     return os;
70 }
71
72 std::ostream& std::operator<<(std::ostream& os, Z_Records & rs)
73 {
74     switch (rs.which)
75     {
76     case Z_Records_DBOSD :
77         break;
78     case Z_Records_NSD:
79         if (rs.u.nonSurrogateDiagnostic)
80             os << *(rs.u.nonSurrogateDiagnostic);
81         break;
82     case Z_Records_multipleNSD:
83         os << "Z_Records_multipleNSD";
84         //os << *(rs.u.multipleNonSurDiagnostics);
85         break;
86     default:
87         os << "Z_Records" ;
88     }
89     
90     return os;
91 }
92
93 std::ostream& std::operator<<(std::ostream& os, Z_DiagRec& dr)
94 {
95     switch (dr.which)
96     {
97     case Z_DiagRec_defaultFormat:
98         if (dr.u.defaultFormat)
99             os << *(dr.u.defaultFormat);
100         break;
101     case Z_DiagRec_externallyDefined :
102         os << "Z_DiagRec_externallyDefined";
103         break;
104     default:
105         os << "Z_DiagRec" ;
106     }
107     
108     return os;
109 }
110
111 std::ostream& std::operator<<(std::ostream& os, Z_DefaultDiagFormat& ddf)
112 {
113     if (ddf.condition)
114         os << *(ddf.condition) << " ";
115
116     switch (ddf.which)
117     {
118     case Z_DefaultDiagFormat_v2Addinfo:
119         os << ddf.u.v2Addinfo;
120         break;
121     case Z_DefaultDiagFormat_v3Addinfo:
122         os << ddf.u.v3Addinfo;
123         break;
124     default:
125         os << "Z_DefaultDiagFormat" ;
126     }
127     
128     return os;
129 }
130
131 static void dump_opt_string(std::ostream& os, const char *s)
132 {
133     os << " ";
134     if (s)
135         os << s;
136     else
137         os << "-";
138 }
139
140 static void dump_opt_int(std::ostream& os, const Odr_int *i)
141 {
142     os << " ";
143     if (i)
144         os << *i;
145     else
146         os << "-";
147 }
148
149 std::ostream& std::operator<<(std::ostream& os,  Z_APDU& zapdu)
150 {
151     switch(zapdu.which) {
152
153     case Z_APDU_initRequest:
154         os << " " << "initRequest";
155                         
156         {
157             Z_InitRequest *ir 
158                 = zapdu.u.initRequest;
159
160             Z_IdAuthentication *a = ir->idAuthentication;
161             if (a && a->which == Z_IdAuthentication_idPass)
162                 dump_opt_string(os, a->u.idPass->userId);
163             else if (a && a->which == Z_IdAuthentication_open)
164                 dump_opt_string(os, a->u.open);
165             else
166                 dump_opt_string(os, 0);
167             
168             os << " ";
169             std::list<std::string> vhosts;
170             mp::util::get_vhost_otherinfo(ir->otherInfo, vhosts);
171             if (vhosts.size()){
172                 copy(vhosts.begin(), vhosts.end(), 
173                      ostream_iterator<string>(os, " "));
174             }
175             else
176                 os << "-" ;
177             
178             dump_opt_string(os, ir->implementationId);
179             dump_opt_string(os, ir->implementationName);
180             dump_opt_string(os, ir->implementationVersion);
181         }
182         break;
183     case Z_APDU_initResponse:
184         os << " " << "initResponse ";
185         {
186             Z_InitResponse *ir 
187                 = zapdu.u.initResponse;
188             if (ir->result && *(ir->result))
189             {
190                 os << "OK";
191             }
192             else
193             {
194                 os << "FAIL";
195             }
196             dump_opt_string(os, ir->implementationId);
197             dump_opt_string(os, ir->implementationName);
198             dump_opt_string(os, ir->implementationVersion);
199         }
200         break;
201     case Z_APDU_searchRequest:
202         os << " " << "searchRequest" << " ";
203         { 
204             Z_SearchRequest *sr 
205                 = zapdu.u.searchRequest;
206                             
207             for (int i = 0; i < sr->num_databaseNames; i++)
208             {
209                 os << sr->databaseNames[i];
210                 if (i+1 !=  sr->num_databaseNames)
211                     os << "+";
212             }
213
214             dump_opt_string(os, sr->resultSetName);
215
216             os << " ";
217             if (sr->preferredRecordSyntax)
218             {
219                 char oid_name_str[OID_STR_MAX];
220                 const char *oid_name = yaz_oid_to_string_buf(
221                     sr->preferredRecordSyntax, 0, oid_name_str);
222                 
223                 os << oid_name;
224             }
225             else
226                 os << "-";
227
228             os << " ";
229             WRBUF wr = wrbuf_alloc();
230             yaz_query_to_wrbuf(wr, sr->query);
231             os << wrbuf_cstr(wr);
232             wrbuf_destroy(wr);
233         }
234         break;
235     case Z_APDU_searchResponse:
236         os << " " << "searchResponse ";
237         {
238             Z_SearchResponse *sr = zapdu.u.searchResponse;
239             if (!sr->searchStatus)
240                 os << "Unknown";
241             else if (*sr->searchStatus)
242                 os << "OK";
243             else
244                 os << "Failure";
245             if (sr->records && sr->records->which != Z_Records_DBOSD)
246             {
247                 os << " DIAG " << *sr->records;
248             }
249             else
250             {
251                 dump_opt_int(os, sr->resultCount);
252                 dump_opt_int(os, sr->numberOfRecordsReturned);
253                 dump_opt_int(os, sr->nextResultSetPosition);
254             }
255         }
256         break;
257     case Z_APDU_presentRequest:
258         os << " " << "presentRequest";
259         {
260             Z_PresentRequest *pr = zapdu.u.presentRequest;
261             dump_opt_string(os, pr->resultSetId);
262             dump_opt_int(os, pr->resultSetStartPoint);
263             dump_opt_int(os, pr->numberOfRecordsRequested);
264             if (pr->preferredRecordSyntax)
265             {
266                 char oid_name_str[OID_STR_MAX];
267                 const char *oid_name = yaz_oid_to_string_buf(
268                     pr->preferredRecordSyntax, 0, oid_name_str);
269                     
270                 os << " " << oid_name;
271             }
272             else
273                 os << " -";
274             const char * msg = 0;
275             if (pr->recordComposition)
276                 msg = mp_util::record_composition_to_esn(pr->recordComposition);
277             dump_opt_string(os, msg);
278         }
279         break;
280     case Z_APDU_presentResponse:
281         os << " " << "presentResponse" << " ";
282         {
283             Z_PresentResponse *pr 
284                 = zapdu.u.presentResponse;
285             if (!pr->presentStatus)
286                 os << "Unknown";
287             else
288             {
289                 switch (*pr->presentStatus)
290                 {
291                 case Z_PresentStatus_success:
292                     os << "OK"; break;
293                 case Z_PresentStatus_partial_1:
294                     os << "Partial-1"; break;
295                 case Z_PresentStatus_partial_2:
296                     os << "Partial-2"; break;
297                 case Z_PresentStatus_partial_3:
298                     os << "Partial-3"; break;
299                 case Z_PresentStatus_partial_4:
300                     os << "Partial-4"; break;
301                 case Z_PresentStatus_failure:
302                     os << "Failure"; break;
303                 default:
304                     os << "Unknown"; break;
305                 }
306             }
307             if (pr->records && pr->records->which != Z_Records_DBOSD)
308                 os << " DIAG " << *pr->records;
309             else
310             {
311                 dump_opt_int(os, pr->numberOfRecordsReturned);
312                 dump_opt_int(os, pr->nextResultSetPosition);
313             }
314         }
315         break;
316     case Z_APDU_deleteResultSetRequest:
317         os << " " << "deleteResultSetRequest";
318         break;
319     case Z_APDU_deleteResultSetResponse:
320         os << " " << "deleteResultSetResponse";
321         break;
322     case Z_APDU_accessControlRequest:
323         os << " " << "accessControlRequest";
324         break;
325     case Z_APDU_accessControlResponse:
326         os << " " << "accessControlResponse";
327         break;
328     case Z_APDU_resourceControlRequest:
329         os << " " << "resourceControlRequest";
330         break;
331     case Z_APDU_resourceControlResponse:
332         os << " " << "resourceControlResponse";
333         break;
334     case Z_APDU_triggerResourceControlRequest:
335         os << " " << "triggerResourceControlRequest";
336         break;
337     case Z_APDU_resourceReportRequest:
338         os << " " << "resourceReportRequest";
339         break;
340     case Z_APDU_resourceReportResponse:
341         os << " " << "resourceReportResponse";
342         break;
343     case Z_APDU_scanRequest:
344         os << " " << "scanRequest" << " ";
345         { 
346             Z_ScanRequest *sr = zapdu.u.scanRequest;
347                         
348             if (sr)
349             {
350                 for (int i = 0; i < sr->num_databaseNames; i++)
351                 {
352                     os << sr->databaseNames[i];
353                     if (i+1 !=  sr->num_databaseNames)
354                         os << "+";
355                 }
356                 dump_opt_int(os, sr->numberOfTermsRequested);
357                 dump_opt_int(os, sr->preferredPositionInResponse);
358                 dump_opt_int(os, sr->stepSize);
359
360                 os << " ";
361                 if (sr->termListAndStartPoint)
362                 {
363                     WRBUF wr = wrbuf_alloc();
364                     yaz_scan_to_wrbuf(wr, sr->termListAndStartPoint, 
365                                       sr->attributeSet);
366                     os << wrbuf_cstr(wr);
367                     wrbuf_destroy(wr);
368                 }
369                 else
370                     os << "-";
371             }
372         }
373         break;
374     case Z_APDU_scanResponse:
375         os << " " << "scanResponse" << " ";
376         {
377             Z_ScanResponse *sr = zapdu.u.scanResponse;
378             if (sr)
379             {
380                 if (!sr->scanStatus)
381                     os << "OK";
382                 else
383                 {
384                     switch (*sr->scanStatus)
385                     {
386                     case Z_Scan_success:
387                         os << "OK";
388                         break;
389                     case Z_Scan_partial_1:
390                         os << "partial_1";
391                         break;
392                     case Z_Scan_partial_2:
393                         os << "partial_2";
394                         break;
395                     case Z_Scan_partial_3:
396                         os << "partial_3";
397                         break;
398                     case Z_Scan_partial_4:
399                         os << "partial_4";
400                         break;
401                     case Z_Scan_partial_5:
402                         os << "partial_5";
403                         break;
404                     case Z_Scan_failure:
405                         os << "failure";
406                         break;
407                     default:
408                         os << "unknown";
409                     }
410                 }
411                 dump_opt_int(os, sr->numberOfEntriesReturned);
412                 dump_opt_int(os, sr->positionOfTerm);
413                 dump_opt_int(os, sr->stepSize);
414             }
415         }
416         break;
417     case Z_APDU_sortRequest:
418         os << " " << "sortRequest" << " ";
419         break;
420     case Z_APDU_sortResponse:
421         os << " " << "sortResponse" << " ";
422         break;
423     case Z_APDU_segmentRequest:
424         os << " " << "segmentRequest" << " ";
425         break;
426     case Z_APDU_extendedServicesRequest:
427         os << " " << "extendedServicesRequest";
428         { 
429             Z_ExtendedServicesRequest *er 
430                 = zapdu.u.extendedServicesRequest;
431             if (er)
432             {
433                 if (er->function)
434                 {
435                     os << " ";
436                     switch(*(er->function))
437                     {
438                     case Z_ExtendedServicesRequest_create:
439                         os << "create";
440                         break;
441                     case Z_ExtendedServicesRequest_delete:
442                         os << "delete";
443                         break;
444                     case Z_ExtendedServicesRequest_modify:
445                         os << "modify";
446                         break;
447                     default:
448                         os << "unknown";
449                     }
450                 }
451                 else
452                     os << " -";
453                     
454                 
455                 if (er->userId)
456                     os << " " << er->userId ;
457                 else
458                     os << " -";
459                 
460                 if (er->packageName)
461                     os << " " << er->packageName;
462                 else
463                     os << " -";
464                 
465                 if (er->description)
466                     os << " " << er->description;
467                 else
468                     os << " -";
469             }
470         }
471         break;
472     case Z_APDU_extendedServicesResponse:
473         os << " " << "extendedServicesResponse";
474         { 
475             Z_ExtendedServicesResponse *er 
476                 = zapdu.u.extendedServicesResponse;
477             if (er)
478             {
479                 if (er->operationStatus)
480                 {
481                     os << " ";
482                     switch (*er->operationStatus)
483                     {
484                     case Z_ExtendedServicesResponse_done:
485                         os << "OK";
486                         break;
487                     case Z_ExtendedServicesResponse_accepted:
488                         os << "ACCEPT";
489                         break;
490                     case Z_ExtendedServicesResponse_failure:
491                         if (er->num_diagnostics)
492                             os << "DIAG " << **(er->diagnostics);
493                         else
494                             os << "ERROR";
495                         break;
496                     default:
497                         os << "unknown";
498                     }
499                 }
500                 else
501                     os << " -";
502             }
503         }
504         break;
505     case Z_APDU_close:
506         os  << " " << "close" << " ";
507         { 
508             Z_Close *c = zapdu.u.close;
509             if (c)
510             {
511                 if (c->closeReason)
512                 {
513                     os << *(c->closeReason) << " ";
514
515                     switch (*c->closeReason)
516                     {
517                     case Z_Close_finished:
518                         os << "finished";
519                         break;
520                     case Z_Close_shutdown:
521                         os << "shutdown";
522                         break;
523                     case Z_Close_systemProblem:
524                         os << "systemProblem";
525                         break;
526                     case Z_Close_costLimit:
527                         os << "costLimit";
528                         break;
529                     case Z_Close_resources:
530                         os << "resources";
531                         break;
532                     case Z_Close_securityViolation:
533                         os << "securityViolation";
534                         break;
535                     case Z_Close_protocolError:
536                         os << "protocolError";
537                         break;
538                     case Z_Close_lackOfActivity:
539                         os << "lackOfActivity";
540                         break;
541                     case Z_Close_peerAbort:
542                         os << "peerAbort";
543                         break;
544                     case Z_Close_unspecified:
545                         os << "unspecified";
546                         break;
547                     default:
548                         os << "unknown";
549                     }
550                 }
551                 
552                 if (c->diagnosticInformation)
553                     os << " " << c->diagnosticInformation;
554             }
555         }
556         break;
557     case Z_APDU_duplicateDetectionRequest:
558         os << " " << "duplicateDetectionRequest";
559         break;
560     case Z_APDU_duplicateDetectionResponse:
561         os << " " << "duplicateDetectionResponse";
562         break;
563     default: 
564         os << " " << "Z_APDU " << "UNKNOWN";
565     }
566
567     return os;
568 }
569
570
571
572
573 /*
574  * Local variables:
575  * c-basic-offset: 4
576  * c-file-style: "Stroustrup"
577  * indent-tabs-mode: nil
578  * End:
579  * vim: shiftwidth=4 tabstop=8 expandtab
580  */
581