| 1 | #!/usr/bin/env perl | ||
| 2 | |||
| 3 | # Copyright (c) 2003, Cornell University | ||
| 4 | # See the file COPYING for the status of this software | ||
| 5 | |||
| 6 | # Try, | ||
| 7 | # | ||
| 8 | # ./wsdl-client.pl 'cgifile:./soap-server.cgi?wsdl' --usage | ||
| 9 | # ./wsdl-client.pl http://www.xmethods.net/sd/TemperatureService.wsdl --usage | ||
| 10 | # ./wsdl-client.pl http://www.xmethods.net/sd/TemperatureService.wsdl getTemp zipcode=14853 | ||
| 11 | # ./wsdl-client.pl http://ctcstager.tc.cornell.edu/Users/heber/Persistence/Persistence.asmx?WSDL --usage | ||
| 12 | # ./wsdl-client.pl http://ctcstager.tc.cornell.edu/Users/heber/Persistence/Persistence.asmx?WSDL TestMath x=10 y=12 | ||
| 13 | |||
| 14 | use strict; | ||
| 15 | use warnings; | ||
| 16 | |||
| 17 | use File::Basename; | ||
| 18 | |||
| 19 | #use lib dirname($0).'/..'; | ||
| 20 | |||
| 21 | use SOAP::Clean::Misc; | ||
| 22 | use SOAP::Clean::XML; | ||
| 23 | use SOAP::Clean::Client; | ||
| 24 | |||
| 25 | use Getopt::Long; | ||
| 26 | |||
| 27 | my $verbose = 0; | ||
| 28 | my $help = 0; | ||
| 29 | my $usage = 0; | ||
| 30 | |||
| 31 | my $status = GetOptions('verbose+' => \$verbose, | ||
| 32 | 'usage' => \$usage, | ||
| 33 | 'help' => \$help); | ||
| 34 | |||
| 35 | if ( !$status || $help ) { usage($status); } | ||
| 36 | |||
| 37 | my $wsdl_url = shift @ARGV; | ||
| 38 | if ( !defined($wsdl_url) ) { | ||
| 39 | print STDERR "No WSDL URL specified\n"; | ||
| 40 | usage(2); | ||
| 41 | } | ||
| 42 | |||
| 43 | my $c = new SOAP::Clean::Client($wsdl_url); | ||
| 44 | if ( $verbose ) { $c->verbose($verbose); } | ||
| 45 | |||
| 46 | my $usage_data = $c->usage(); | ||
| 47 | |||
| 48 | if ( $usage ) { | ||
| 49 | |||
| 50 | foreach my $method_name ( sort(keys %$usage_data) ) { | ||
| 51 | my $method_data = $$usage_data{$method_name}; | ||
| 52 | print "$method_name\n"; | ||
| 53 | foreach my $direction ( "input", "output" ) { | ||
| 54 | print " $direction:\n"; | ||
| 55 | my $args = $$method_data{$direction}; | ||
| 56 | foreach my $arg ( keys %$args ) { | ||
| 57 | print " $arg: ",$$args{$arg},"\n"; | ||
| 58 | } | ||
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 | goto EXIT; | ||
| 63 | } | ||
| 64 | |||
| 65 | my $method_name = shift @ARGV; | ||
| 66 | defined($method_name) || die "No method name specified"; | ||
| 67 | |||
| 68 | my $method_data = $$usage_data{$method_name}; | ||
| 69 | defined($method_data) || die "No such method. Try --usage to get a list"; | ||
| 70 | my $in_args = $$method_data{input}; | ||
| 71 | my $out_args = $$method_data{output}; | ||
| 72 | |||
| 73 | my %arg_data = (); | ||
| 74 | my %arg_mech = (); | ||
| 75 | foreach my $arg ( @ARGV ) { | ||
| 76 | if ( $arg =~ /^([^=]+)=(.*)$/ ) { | ||
| 77 | $arg_data{$1} = $2; | ||
| 78 | $arg_mech{$1} = 'value'; | ||
| 79 | } elsif ( $arg =~ /^([^:]+):(.*)$/ ) { | ||
| 80 | $arg_data{$1} = $2; | ||
| 81 | $arg_mech{$1} = 'file'; | ||
| 82 | } elsif ( $arg =~ /^([^:]+)\?$/ ) { | ||
| 83 | $arg_mech{$1} = 'status'; | ||
| 84 | } else { | ||
| 85 | die "Ill-formed argument: $arg"; | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | my $args = {}; | ||
| 90 | foreach my $in_arg_name ( keys %$in_args ) { | ||
| 91 | my $optional=0; | ||
| 92 | my $in_arg_type = $$in_args{$in_arg_name}; | ||
| 93 | if ( $in_arg_type =~ /^optional\s+(.*)/ ) { | ||
| 94 | $optional = 1; | ||
| 95 | $in_arg_type = $1; | ||
| 96 | } | ||
| 97 | my $mech = $arg_mech{$in_arg_name}; | ||
| 98 | assert($optional || defined($mech), | ||
| 99 | "Missing input argument: $in_arg_name"); | ||
| 100 | if ( !defined($mech) ) { | ||
| 101 | # Optional argument without a value. | ||
| 102 | } elsif ( $mech eq 'value' ) { | ||
| 103 | if ( $in_arg_type eq "xml" ) { | ||
| 104 | $$args{$in_arg_name} = xml_from_string($arg_data{$in_arg_name}); | ||
| 105 | } else { | ||
| 106 | $$args{$in_arg_name} = $arg_data{$in_arg_name}; | ||
| 107 | } | ||
| 108 | } elsif ( $mech eq 'file' ) { | ||
| 109 | my $filename = $arg_data{$in_arg_name}; | ||
| 110 | if ( ! ( -f $filename && -r $filename ) ) { | ||
| 111 | die "Cannot read file $filename"; | ||
| 112 | } | ||
| 113 | if ($in_arg_type eq "raw" ) { | ||
| 114 | open F, "<".$filename | ||
| 115 | || die("Can't open file $filename"); | ||
| 116 | binmode F; | ||
| 117 | my $out_len = sysseek F,0,2; # how long is the file. | ||
| 118 | sysseek F,0,0; # jump back to the beginning. | ||
| 119 | my $out_str; | ||
| 120 | sysread F,$out_str,$out_len; | ||
| 121 | $$args{$in_arg_name} = $out_str; | ||
| 122 | } elsif ( $in_arg_type eq "xml" ) { | ||
| 123 | $$args{$in_arg_name} = xml_from_file($filename); | ||
| 124 | } else { | ||
| 125 | $$args{$in_arg_name} = `cat $filename`; | ||
| 126 | } | ||
| 127 | } elsif ( $mech eq 'status' ) { | ||
| 128 | die "Input paramaters cannot be specified as status argument" | ||
| 129 | } else { | ||
| 130 | die "Bad mechanism - $mech"; | ||
| 131 | } | ||
| 132 | } | ||
| 133 | |||
| 134 | my $results = $c->invoke($method_name,$args); | ||
| 135 | |||
| 136 | my $exit_status; | ||
| 137 | |||
| 138 | # fixme: bools are printed as 0 and 1. Is that right? | ||
| 139 | foreach my $out_arg_name ( keys %$out_args ) { | ||
| 140 | my $out_arg_type = $$out_args{$out_arg_name}; | ||
| 141 | my $optional = 0; | ||
| 142 | if ( $out_arg_type =~ /^optional\s+(.*)/ ) { | ||
| 143 | $optional = 1; | ||
| 144 | $out_arg_type = $1; | ||
| 145 | } | ||
| 146 | my $data = $arg_data{$out_arg_name}; | ||
| 147 | my $mech = $arg_mech{$out_arg_name}; | ||
| 148 | defined($mech) || ( $mech = 'value' ); | ||
| 149 | my $result = $$results{$out_arg_name}; | ||
| 150 | |||
| 151 | if ( $mech eq 'value' ) { | ||
| 152 | if ( $out_arg_type eq "xml" ) { | ||
| 153 | print $out_arg_name,"=",xml_to_string($result),"\n"; | ||
| 154 | } else { | ||
| 155 | print $out_arg_name,"=",$result,"\n"; | ||
| 156 | } | ||
| 157 | } elsif ( $mech eq 'file' ) { | ||
| 158 | if ( $out_arg_type eq "raw" ) { | ||
| 159 | open F,">$data" || die; | ||
| 160 | binmode F; | ||
| 161 | syswrite F, $result; | ||
| 162 | close F || die; | ||
| 163 | } elsif ( $out_arg_type eq "xml" ) { | ||
| 164 | xml_to_file($result,$data); | ||
| 165 | } else { | ||
| 166 | open F,">$data" || die; | ||
| 167 | print F $result,"\n"; | ||
| 168 | close F; | ||
| 169 | } | ||
| 170 | } elsif ( $mech eq 'status' ) { | ||
| 171 | !defined($exit_status) | ||
| 172 | || die "More than one output parameter specified as the exit status"; | ||
| 173 | if ( $out_arg_type eq "bool" || $out_arg_type eq "int" ) { | ||
| 174 | # exit codes: true -> 0, false -> 1 | ||
| 175 | $exit_status = ( $result ? 0 : 1 ); | ||
| 176 | } else { | ||
| 177 | die "Cannot convert type $out_arg_type to exit status"; | ||
| 178 | } | ||
| 179 | } else { | ||
| 180 | die "Bad mechanism - $mech"; | ||
| 181 | } | ||
| 182 | } | ||
| 183 | |||
| 184 | ######################################################################## | ||
| 185 | |||
| 186 | EXIT: | ||
| 187 | |||
| 188 | if ( $verbose ) { | ||
| 189 | print "##################################################\n"; | ||
| 190 | print "HTTP Statistics:\n"; | ||
| 191 | my ($request_count,$response_count) = $c->counts(); | ||
| 192 | print "Request count = ",$request_count,"\n"; | ||
| 193 | print "Response count = ",$response_count,"\n"; | ||
| 194 | } | ||
| 195 | |||
| 196 | defined($exit_status) || ( $exit_status = 0 ); | ||
| 197 | exit($exit_status); | ||
| 198 | |||
| 199 | ######################################################################## | ||
| 200 | |||
| 201 | sub usage { | ||
| 202 | my ($stat) = @_; | ||
| 203 | print "Usage: $0 [--help] [--verbose] wsdl [--usage] params ...\n"; | ||
| 204 | exit($stat); | ||
| 205 | } | ||
| 206 |