7fd718f7db0402d21205cf61b1e231274ef60584
[metaproxy-moved-to-github.git] / src / gduutil.cpp
1 /* $Id: gduutil.cpp,v 1.22 2007-05-22 13:03:32 adam Exp $
2    Copyright (c) 2005-2007, Index Data.
3
4 This file is part of Metaproxy.
5
6 Metaproxy is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
10
11 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Metaproxy; see the file LICENSE.  If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA.
20  */
21
22 #include "gduutil.hpp"
23 #include "util.hpp"
24
25 #include <yaz/wrbuf.h>
26 #include <yaz/oid_db.h>
27 #include <yaz/querytowrbuf.h>
28
29 #include <iostream>
30 #include <list>
31
32 namespace mp = metaproxy_1;
33
34 // Doxygen doesn't like mp::gdu, so we use this instead
35 namespace mp_util = metaproxy_1::util;
36
37
38
39
40 std::ostream& std::operator<<(std::ostream& os,  Z_GDU& zgdu)
41 {
42     if (zgdu.which == Z_GDU_Z3950)
43     {
44         os << "Z3950";
45         if (zgdu.u.z3950)
46             os << *(zgdu.u.z3950);
47     }
48     else if (zgdu.which == Z_GDU_HTTP_Request)
49     {
50         os << "HTTP_Request";
51         if (zgdu.u.HTTP_Request)
52             os << " " << *(zgdu.u.HTTP_Request);
53     }
54     else if (zgdu.which == Z_GDU_HTTP_Response)
55     {
56         os << "HTTP_Response";
57         if (zgdu.u.HTTP_Response)
58             os << " " << *(zgdu.u.HTTP_Response);
59     }
60     else
61         os << "Z_GDU";
62     return os;
63 }
64
65 std::ostream& std::operator<<(std::ostream& os, Z_HTTP_Request& httpreq)
66 {
67     os << httpreq.method << " ";
68     os << httpreq.path;    
69     return os;
70 }
71
72
73 std::ostream& std::operator<<(std::ostream& os, Z_HTTP_Response& httpres)
74 {
75     os << httpres.code << " ";
76     os << httpres.content_len;   
77     return os;
78 }
79
80 std::ostream& std::operator<<(std::ostream& os, Z_Records & rs)
81 {
82     switch(rs.which) {
83     case Z_Records_DBOSD :
84         break;
85     case Z_Records_NSD:
86         if (rs.u.nonSurrogateDiagnostic)
87             os << *(rs.u.nonSurrogateDiagnostic);
88         break;
89     case Z_Records_multipleNSD:
90         os << "Z_Records_multipleNSD";
91         //os << *(rs.u.multipleNonSurDiagnostics);
92         break;
93     default:
94         os << "Z_Records" ;
95     }
96     
97     return os;
98 }
99
100 std::ostream& std::operator<<(std::ostream& os, Z_DiagRec& dr)
101 {
102     switch(dr.which) {
103     case Z_DiagRec_defaultFormat:
104         if (dr.u.defaultFormat)
105             os << *(dr.u.defaultFormat);
106         break;
107     case Z_DiagRec_externallyDefined :
108         os << "Z_DiagRec_externallyDefined";
109         break;
110     default:
111         os << "Z_DiagRec" ;
112     }
113     
114     return os;
115 }
116
117 std::ostream& std::operator<<(std::ostream& os, Z_DefaultDiagFormat& ddf)
118 {
119     if (ddf.condition)
120         os << *(ddf.condition) << " ";
121
122     switch(ddf.which) {
123     case Z_DefaultDiagFormat_v2Addinfo:
124         os << ddf.u.v2Addinfo;
125         break;
126     case Z_DefaultDiagFormat_v3Addinfo:
127         os << ddf.u.v3Addinfo;
128         break;
129     default:
130         os << "Z_DefaultDiagFormat" ;
131     }
132     
133     return os;
134 }
135
136 static void dump_opt_string(std::ostream& os, const char *s)
137 {
138     os << " ";
139     if (s)
140         os << s;
141     else
142         os << "-";
143 }
144
145 static void dump_opt_int(std::ostream& os, const int *i)
146 {
147     os << " ";
148     if (i)
149         os << *i;
150     else
151         os << "-";
152 }
153
154 std::ostream& std::operator<<(std::ostream& os,  Z_APDU& zapdu)
155 {
156     switch(zapdu.which) {
157
158     case Z_APDU_initRequest:
159         os << " " << "initRequest";
160                         
161         {
162             Z_InitRequest *ir 
163                 = zapdu.u.initRequest;
164
165             Z_IdAuthentication *a = ir->idAuthentication;
166             if (a && a->which == Z_IdAuthentication_idPass)
167                 dump_opt_string(os, a->u.idPass->userId);
168             else if (a && a->which == Z_IdAuthentication_open)
169                 dump_opt_string(os, a->u.open);
170             else
171                 dump_opt_string(os, 0);
172             
173             os << " ";
174             std::list<std::string> vhosts;
175             mp::util::get_vhost_otherinfo(ir->otherInfo, vhosts);
176             if (vhosts.size()){
177                 copy(vhosts.begin(), vhosts.end(), 
178                      ostream_iterator<string>(os, " "));
179             }
180             else
181                 os << "-" ;
182             
183             dump_opt_string(os, ir->implementationId);
184             dump_opt_string(os, ir->implementationName);
185             dump_opt_string(os, ir->implementationVersion);
186         }
187         break;
188     case Z_APDU_initResponse:
189         os << " " << "initResponse ";
190         {
191             Z_InitResponse *ir 
192                 = zapdu.u.initResponse;
193             if (ir->result && *(ir->result))
194             {
195                 os << "OK";
196             }
197             else
198             {
199                 os << "FAIL";
200             }
201             dump_opt_string(os, ir->implementationId);
202             dump_opt_string(os, ir->implementationName);
203             dump_opt_string(os, ir->implementationVersion);
204         }
205         break;
206     case Z_APDU_searchRequest:
207         os << " " << "searchRequest" << " ";
208         { 
209             Z_SearchRequest *sr 
210                 = zapdu.u.searchRequest;
211                             
212             for (int i = 0; i < sr->num_databaseNames; i++)
213             {
214                 os << sr->databaseNames[i];
215                 if (i+1 !=  sr->num_databaseNames)
216                     os << "+";
217             }
218
219             dump_opt_string(os, sr->resultSetName);
220
221             os << " ";
222             if (sr->preferredRecordSyntax)
223             {
224                 char oid_name_str[OID_STR_MAX];
225                 const char *oid_name = yaz_oid_to_string_buf(
226                     sr->preferredRecordSyntax, 0, oid_name_str);
227                 
228                 os << oid_name;
229             }
230             else
231                 os << "-";
232
233             os << " ";
234             WRBUF wr = wrbuf_alloc();
235             yaz_query_to_wrbuf(wr, sr->query);
236             os << wrbuf_cstr(wr);
237             wrbuf_destroy(wr);
238         }
239         break;
240     case Z_APDU_searchResponse:
241         os << " " << "searchResponse ";
242         {
243             Z_SearchResponse *sr 
244                 = zapdu.u.searchResponse;
245             if (sr->searchStatus && *(sr->searchStatus))
246             {
247                 os << "OK";
248                 dump_opt_int(os, sr->resultCount);
249                 dump_opt_int(os, sr->numberOfRecordsReturned);
250                 dump_opt_int(os, sr->nextResultSetPosition);
251             }
252             else 
253                 if (sr->records)
254                     os << "DIAG " << *(sr->records);
255                 else
256                     os << "ERROR";
257         }
258         break;
259     case Z_APDU_presentRequest:
260         os << " " << "presentRequest";
261         {
262             Z_PresentRequest *pr = zapdu.u.presentRequest;
263             dump_opt_string(os, pr->resultSetId);
264             dump_opt_int(os, pr->resultSetStartPoint);
265             dump_opt_int(os, pr->numberOfRecordsRequested);
266             if (pr->preferredRecordSyntax)
267             {
268                 char oid_name_str[OID_STR_MAX];
269                 const char *oid_name = yaz_oid_to_string_buf(
270                     pr->preferredRecordSyntax, 0, oid_name_str);
271                     
272                 os << " " << oid_name;
273             }
274             else
275                 os << " -";
276             const char * msg = 0;
277             if (pr->recordComposition)
278                 msg = mp_util::record_composition_to_esn(pr->recordComposition);
279             dump_opt_string(os, msg);
280         }
281         break;
282     case Z_APDU_presentResponse:
283         os << " " << "presentResponse" << " ";
284         {
285             Z_PresentResponse *pr 
286                 = zapdu.u.presentResponse;
287             if ((pr->presentStatus) && !*(pr->presentStatus))
288             {
289                 os << "OK";
290                 //<< pr->referenceId << " "
291                 if (pr->numberOfRecordsReturned)
292                     os << " " << *(pr->numberOfRecordsReturned);
293                 else
294                     os << " -";
295                 if (pr->nextResultSetPosition)
296                     os << " " << *(pr->nextResultSetPosition);
297                 else
298                     os << " -";
299             }
300             else
301                 if (pr->records)
302                     os << "DIAG " << *(pr->records);
303                 else
304                     os << "ERROR";
305
306             //os << "DIAG" << " "
307             //<< "-" << " "
308             //<< pr->referenceId << " "
309             //<< *(pr->numberOfRecordsReturned) << " "
310             //<< *(pr->nextResultSetPosition);
311         }
312         break;
313     case Z_APDU_deleteResultSetRequest:
314         os << " " << "deleteResultSetRequest";
315         break;
316     case Z_APDU_deleteResultSetResponse:
317         os << " " << "deleteResultSetResponse";
318         break;
319     case Z_APDU_accessControlRequest:
320         os << " " << "accessControlRequest";
321         break;
322     case Z_APDU_accessControlResponse:
323         os << " " << "accessControlResponse";
324         break;
325     case Z_APDU_resourceControlRequest:
326         os << " " << "resourceControlRequest";
327         break;
328     case Z_APDU_resourceControlResponse:
329         os << " " << "resourceControlResponse";
330         break;
331     case Z_APDU_triggerResourceControlRequest:
332         os << " " << "triggerResourceControlRequest";
333         break;
334     case Z_APDU_resourceReportRequest:
335         os << " " << "resourceReportRequest";
336         break;
337     case Z_APDU_resourceReportResponse:
338         os << " " << "resourceReportResponse";
339         break;
340     case Z_APDU_scanRequest:
341         os << " " << "scanRequest" << " ";
342         { 
343             Z_ScanRequest *sr 
344                 = zapdu.u.scanRequest;
345                         
346             if (sr)
347             {
348                 for (int i = 0; i < sr->num_databaseNames; i++)
349                 {
350                     os << sr->databaseNames[i];
351                     if (i+1 !=  sr->num_databaseNames)
352                         os << "+";
353                 }
354                 dump_opt_int(os, sr->numberOfTermsRequested);
355                 dump_opt_int(os, sr->preferredPositionInResponse);
356                 dump_opt_int(os, sr->stepSize);
357
358                 os << " ";
359                 if (sr->termListAndStartPoint)
360                 {
361                     WRBUF wr = wrbuf_alloc();
362                     yaz_scan_to_wrbuf(wr, sr->termListAndStartPoint, 
363                                       sr->attributeSet);
364                     os << wrbuf_cstr(wr);
365                     wrbuf_destroy(wr);
366                 }
367                 else
368                     os << "-";
369             }
370         }
371         break;
372     case Z_APDU_scanResponse:
373         os << " " << "scanResponse" << " ";
374         {
375             Z_ScanResponse *sr 
376                 = zapdu.u.scanResponse;
377             if (sr)
378             {
379                 if (!sr->scanStatus)
380                 {
381                     os << "OK";
382                 }
383                 else
384                 {
385                     switch (*(sr->scanStatus)){
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                      case Z_ExtendedServicesResponse_done:
484                          os << "OK";
485                          break;
486                      case Z_ExtendedServicesResponse_accepted:
487                          os << "ACCEPT";
488                          break;
489                      case Z_ExtendedServicesResponse_failure:
490                          if (er->num_diagnostics)
491                              os << "DIAG " << **(er->diagnostics);
492                          else
493                              os << "ERROR";
494                          break;
495                      default:
496                          os << "unknown";
497                      }
498                  }
499                  else
500                      os << " -";
501              }
502          }
503         break;
504     case Z_APDU_close:
505         os  << " " << "close" << " ";
506         { 
507             Z_Close  *c 
508                 = zapdu.u.close;
509             if (c)
510             {
511                 if (c->closeReason)
512                 {
513                     os << *(c->closeReason) << " ";
514
515                     switch (*(c->closeReason)) {
516                     case Z_Close_finished:
517                         os << "finished";
518                         break;
519                     case Z_Close_shutdown:
520                         os << "shutdown";
521                         break;
522                     case Z_Close_systemProblem:
523                         os << "systemProblem";
524                         break;
525                     case Z_Close_costLimit:
526                         os << "costLimit";
527                         break;
528                     case Z_Close_resources:
529                         os << "resources";
530                         break;
531                     case Z_Close_securityViolation:
532                         os << "securityViolation";
533                         break;
534                     case Z_Close_protocolError:
535                         os << "protocolError";
536                         break;
537                     case Z_Close_lackOfActivity:
538                         os << "lackOfActivity";
539                         break;
540                     case Z_Close_peerAbort:
541                         os << "peerAbort";
542                         break;
543                     case Z_Close_unspecified:
544                         os << "unspecified";
545                         break;
546                     default:
547                         os << "unknown";
548                     }
549                 }
550                 
551                 if (c->diagnosticInformation)
552                     os << " " << c->diagnosticInformation;
553             }
554         }
555         break;
556     case Z_APDU_duplicateDetectionRequest:
557         os << " " << "duplicateDetectionRequest";
558         break;
559     case Z_APDU_duplicateDetectionResponse:
560         os << " " << "duplicateDetectionResponse";
561         break;
562     default: 
563         os << " " << "Z_APDU " << "UNKNOWN";
564     }
565
566     return os;
567 }
568
569
570
571
572 /*
573  * Local variables:
574  * c-basic-offset: 4
575  * indent-tabs-mode: nil
576  * c-file-style: "stroustrup"
577  * End:
578  * vim: shiftwidth=4 tabstop=8 expandtab
579  */