| 1 | package SOAP::UMRA; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use HTTP::Cookies; | ||
| 7 | use SOAP::Lite; | ||
| 8 | |||
| 9 | our $VERSION = "1.0"; | ||
| 10 | |||
| 11 | sub new { | ||
| 12 | my ($class, $url, $opts) = @_; | ||
| 13 | my $self = { | ||
| 14 | verbose => $opts->{verbose} || 0, | ||
| 15 | umra_uri => 'http://tools4ever.com/products/user-management-resource-administrator/', | ||
| 16 | host => $url, | ||
| 17 | }; | ||
| 18 | $self->{host} =~ s!https?://([^/]*)(/.*)?!$1!; | ||
| 19 | |||
| 20 | $self->{umra} = SOAP::Lite | ||
| 21 | -> service($url . '?WSDL') | ||
| 22 | -> uri($self->{umra_uri}) | ||
| 23 | -> proxy($url); | ||
| 24 | |||
| 25 | # Sessions are maintained with cookies in this .NET service | ||
| 26 | my $cookies = HTTP::Cookies->new(ignore_discard => 1); | ||
| 27 | $self->{umra}->transport->cookie_jar($cookies); | ||
| 28 | |||
| 29 | bless($self, $class); | ||
| 30 | return $self; | ||
| 31 | } | ||
| 32 | |||
| 33 | sub error { | ||
| 34 | my ($self) = @_; | ||
| 35 | return delete $self->{__error}; | ||
| 36 | } | ||
| 37 | |||
| 38 | sub connect { | ||
| 39 | my $self = shift; | ||
| 40 | my $umra_port = 56814; | ||
| 41 | return $self->_call('Connect', [ | ||
| 42 | SOAP::Data->name(HostName => $self->{host}), | ||
| 43 | SOAP::Data->name(PortNumber => $umra_port) | ||
| 44 | ]); | ||
| 45 | } | ||
| 46 | |||
| 47 | sub disconnect { | ||
| 48 | my ($self) = @_; | ||
| 49 | return $self->_call('Close'); | ||
| 50 | } | ||
| 51 | |||
| 52 | sub _call { | ||
| 53 | my ($self, $function, $args) = @_; | ||
| 54 | my $r = $self->{umra} | ||
| 55 | # It's a .NET service, so specify the action manually | ||
| 56 | -> on_action( sub { $self->{umra_uri} . $function } ) | ||
| 57 | -> call($function, @$args); | ||
| 58 | $self->_print_result($r) if $self->{verbose}; | ||
| 59 | return $r; | ||
| 60 | } | ||
| 61 | |||
| 62 | sub execute { | ||
| 63 | my ($self, $project, $variables) = @_; | ||
| 64 | |||
| 65 | my $r = $self->connect(); | ||
| 66 | if(defined($r->result) && ($r->result != 0)) { | ||
| 67 | $self->{__error} = $r; | ||
| 68 | return 0; | ||
| 69 | } | ||
| 70 | |||
| 71 | while(my($key, $val) = each %$variables) { | ||
| 72 | $r = $self->_call('SetVariableText', [ | ||
| 73 | SOAP::Data->name(VariableName => '%'.$key.'%'), | ||
| 74 | SOAP::Data->name(VariableValue => $val) | ||
| 75 | ]); | ||
| 76 | if(defined($r->result) && ($r->result != 0)) { | ||
| 77 | $self->{__error} = $r; | ||
| 78 | $self->disconnect(); | ||
| 79 | return 0; | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | $r = $self->_call('ExecuteProjectScript', [SOAP::Data->name(ProjectName => $project)]); | ||
| 84 | |||
| 85 | $self->disconnect(); | ||
| 86 | |||
| 87 | if(defined($r->result) && ($r->result != 0)) { | ||
| 88 | $self->{__error} = $r; | ||
| 89 | return 0; | ||
| 90 | } | ||
| 91 | return 1; | ||
| 92 | } | ||
| 93 | |||
| 94 | sub project { | ||
| 95 | my ($self, $name) = @_; | ||
| 96 | my $project = sub { | ||
| 97 | my ($variables) = @_; | ||
| 98 | return $self->execute($name, $variables); | ||
| 99 | }; | ||
| 100 | return $project; | ||
| 101 | } | ||
| 102 | |||
| 103 | sub _print_result { | ||
| 104 | require Data::Dumper; | ||
| 105 | Data::Dumper->import; | ||
| 106 | my ($self, $r) = @_; | ||
| 107 | my $r2 = $r->{_context}->{_transport}->{_proxy}->{_http_response}; | ||
| 108 | printf STDERR "\nRequest: \n%s\n", $r2->{_request}->{_content}; | ||
| 109 | printf STDERR "Response: \n%s\n", $r2->{_content}; | ||
| 110 | printf STDERR "Result: %s\n", (defined($r->result) ? $r->result : 'undef'); | ||
| 111 | print STDERR "Fault: \n"; | ||
| 112 | print STDERR Dumper($r->fault); | ||
| 113 | } | ||
| 114 | |||
| 115 | 1; | ||
| 116 | |||
| 117 | __END__ |