| 1 | package SOAP::Transport::HTTP::AutoInvoke::Client; | ||
| 2 | |||
| 3 | |||
| 4 | BEGIN | ||
| 5 | { | ||
| 6 | |||
| 7 | use strict; | ||
| 8 | use vars qw ( $VERSION $AUTOLOAD $DEFAULT_HOST $DEFAULT_PORT $DEFAULT_ENDPOINT $DEFAULT_METHOD_URI ); | ||
| 9 | |||
| 10 | $VERSION = '0.25'; | ||
| 11 | |||
| 12 | require 5.000; | ||
| 13 | |||
| 14 | use SOAP::EnvelopeMaker; | ||
| 15 | use SOAP::Struct; | ||
| 16 | use SOAP::Transport::HTTP::Client; | ||
| 17 | use SOAP::Parser; | ||
| 18 | use Data::Dumper; | ||
| 19 | |||
| 20 | $DEFAULT_HOST = "localhost"; | ||
| 21 | $DEFAULT_PORT = 80; | ||
| 22 | $DEFAULT_ENDPOINT = "/soap?class="; | ||
| 23 | $DEFAULT_METHOD_URI = "urn:com-name-your"; | ||
| 24 | |||
| 25 | } | ||
| 26 | |||
| 27 | |||
| 28 | |||
| 29 | sub new | ||
| 30 | { | ||
| 31 | my $class = shift; | ||
| 32 | my $self = {}; | ||
| 33 | |||
| 34 | my $blessing = bless ( $self, $class ); | ||
| 35 | |||
| 36 | $self->{_soap_host} = $DEFAULT_HOST; | ||
| 37 | $self->{_soap_port} = $DEFAULT_PORT; | ||
| 38 | $self->{_soap_endpoint} = $DEFAULT_ENDPOINT.$class; | ||
| 39 | $self->{_soap_method_uri} = $DEFAULT_METHOD_URI; | ||
| 40 | $self->{_soap_new_method} = "new"; | ||
| 41 | $self->{_soap_new_args} = (); | ||
| 42 | |||
| 43 | # | ||
| 44 | # override defaults if arguments passed. | ||
| 45 | # | ||
| 46 | if (@_) { | ||
| 47 | while ( my $arg = shift ) { | ||
| 48 | if ( $arg eq "_soap_host" | ||
| 49 | || $arg eq "_soap_port" | ||
| 50 | || $arg eq "_soap_endpoint" | ||
| 51 | || $arg eq "_soap_method_uri" | ||
| 52 | || $arg eq "_soap_new_method" ) | ||
| 53 | { | ||
| 54 | $self->{$arg} = shift; | ||
| 55 | } | ||
| 56 | else { | ||
| 57 | push ( @{$self->{_soap_new_args}}, $arg ); | ||
| 58 | } | ||
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 | $blessing; | ||
| 63 | } | ||
| 64 | |||
| 65 | |||
| 66 | |||
| 67 | sub _soap_get_set_args | ||
| 68 | { | ||
| 69 | my ($self, $method) = (shift,shift); | ||
| 70 | |||
| 71 | |||
| 72 | $method =~ s/_([gs]et)//; | ||
| 73 | my $get_set = $1; | ||
| 74 | |||
| 75 | if ( $method =~ /_soap_host$/ | ||
| 76 | || $method =~ /_soap_port$/ | ||
| 77 | || $method =~ /_soap_endpoint$/ | ||
| 78 | || $method =~ /_soap_method_uri$/ | ||
| 79 | || $method =~ /_soap_new_method$/ ) | ||
| 80 | { | ||
| 81 | print "GETSET = $get_set\n"; | ||
| 82 | return ( $self->{$method} ) if ( $get_set eq "get" ); | ||
| 83 | |||
| 84 | $self->{$method} = shift; | ||
| 85 | } | ||
| 86 | else { | ||
| 87 | warn ( "Skipping get/set of unknown parameter: $method" ); | ||
| 88 | } | ||
| 89 | |||
| 90 | } | ||
| 91 | |||
| 92 | |||
| 93 | |||
| 94 | sub _soap_set_new_args | ||
| 95 | { | ||
| 96 | $self->{_soap_new_args} = (); | ||
| 97 | |||
| 98 | if (@_) { | ||
| 99 | while ( my $arg = shift ) { | ||
| 100 | push ( @{$self->{_soap_new_args}}, $arg ); | ||
| 101 | } | ||
| 102 | } | ||
| 103 | } | ||
| 104 | |||
| 105 | |||
| 106 | |||
| 107 | sub _soap_get_new_args | ||
| 108 | { | ||
| 109 | (wantarray) | ||
| 110 | ? @{$self->{_soap_new_args}} | ||
| 111 | : $self->{_soap_new_args} | ||
| 112 | ; | ||
| 113 | } | ||
| 114 | |||
| 115 | |||
| 116 | |||
| 117 | sub _soap_deliver_request | ||
| 118 | { | ||
| 119 | my ($self, $method_name) = (shift, shift); | ||
| 120 | |||
| 121 | # | ||
| 122 | # Convert any arguments into a hash for send SOAP::Struct. | ||
| 123 | # | ||
| 124 | my %ARGV; | ||
| 125 | my $arg = 0; | ||
| 126 | |||
| 127 | foreach (@_) { | ||
| 128 | if ( ref ($_) eq "ARRAY" ) { | ||
| 129 | $_ = Dumper ( $_ ); | ||
| 130 | s/^\$VAR1 = /_soap_array::/g; | ||
| 131 | } | ||
| 132 | $ARGV{"ARG$arg"} = $_; | ||
| 133 | $arg++; | ||
| 134 | } | ||
| 135 | $arg = 0; | ||
| 136 | if ( $self->{_soap_new_args} ) { | ||
| 137 | foreach (@{$self->{_soap_new_args}}) { | ||
| 138 | if ( ref ($_) eq "ARRAY" ) { | ||
| 139 | $_ = Dumper ( $_ ); | ||
| 140 | s/^\$VAR1 = /_soap_array::/g; | ||
| 141 | } | ||
| 142 | $ARGV{"NewARG$arg"} = $_; | ||
| 143 | $arg++; | ||
| 144 | } | ||
| 145 | } | ||
| 146 | |||
| 147 | # | ||
| 148 | # For some reason I feel compelled to do this.. | ||
| 149 | # | ||
| 150 | $ARGV{ARGC} = scalar @_; | ||
| 151 | |||
| 152 | $ARGV{_is_soap_autoload} = 1; | ||
| 153 | $ARGV{_soap_new_method} = ( $self->{_soap_new_method} ) | ||
| 154 | ? $self->{_soap_new_method} | ||
| 155 | : '' | ||
| 156 | ; | ||
| 157 | |||
| 158 | |||
| 159 | # | ||
| 160 | # Now set and send our request to the server. | ||
| 161 | # | ||
| 162 | my $soap_request = ''; | ||
| 163 | my $output_fcn = sub { $soap_request .= shift; }; | ||
| 164 | my $em = SOAP::EnvelopeMaker->new ( $output_fcn ); | ||
| 165 | |||
| 166 | my $body = SOAP::Struct->new ( | ||
| 167 | %ARGV | ||
| 168 | ); | ||
| 169 | |||
| 170 | $em->set_body( $self->{method_uri}, $method_name, 0, $body ); | ||
| 171 | |||
| 172 | |||
| 173 | my $soap_on_http = SOAP::Transport::HTTP::Client->new(); | ||
| 174 | |||
| 175 | my $soap_response = $soap_on_http->send_receive ( | ||
| 176 | $self->{_soap_host}, | ||
| 177 | $self->{_soap_port}, | ||
| 178 | $self->{_soap_endpoint}, | ||
| 179 | $self->{_soap_method_uri}, | ||
| 180 | $method_name, | ||
| 181 | $soap_request | ||
| 182 | ); | ||
| 183 | |||
| 184 | my $soap_parser = SOAP::Parser->new(); | ||
| 185 | |||
| 186 | $soap_parser->parsestring($soap_response); | ||
| 187 | |||
| 188 | $body = $soap_parser->get_body; | ||
| 189 | |||
| 190 | # | ||
| 191 | # Convert any return arguments into a return list. | ||
| 192 | # | ||
| 193 | $arg = 0; | ||
| 194 | @_ = (); | ||
| 195 | while ( $_ = $body->{"ARG$arg"} ) { | ||
| 196 | if ( /^_soap_array::/ ) { | ||
| 197 | s/^_soap_array:://; | ||
| 198 | $_ = eval ( $_ ); | ||
| 199 | } | ||
| 200 | push ( @_, $_ ); | ||
| 201 | $arg++; | ||
| 202 | } | ||
| 203 | |||
| 204 | @_; | ||
| 205 | } | ||
| 206 | |||
| 207 | |||
| 208 | |||
| 209 | DESTROY | ||
| 210 | { | ||
| 211 | $_[0] = undef; | ||
| 212 | } | ||
| 213 | |||
| 214 | |||
| 215 | |||
| 216 | sub AUTOLOAD | ||
| 217 | { | ||
| 218 | my($self) = shift; | ||
| 219 | my($method) = ($AUTOLOAD =~ /::([^:]+)$/); | ||
| 220 | return unless ($method); | ||
| 221 | |||
| 222 | return $self->_soap_get_set_args ( $method, @_ ) | ||
| 223 | if ( $method =~ "^_soap_" ); | ||
| 224 | |||
| 225 | |||
| 226 | $self->_soap_deliver_request ( $method, @_ ); | ||
| 227 | } | ||
| 228 | |||
| 229 | |||
| 230 | |||
| 231 | 1; | ||
| 232 | |||
| 233 | __END__ |