GRNOC-WebService-Client

 view release on metacpan or  search on metacpan

lib/GRNOC/WebService/Client.pm  view on Meta::CPAN

    The list of methods available is dependent upon the web service you have bound to.  Use get_methods() to retrieve
    the list of available methods. Only the methods implemented in the client library are listed here.


=head2 new()

    constructor that takes four named parameters: .

=over

=item url

    the url that directly indentifies a servcie

=cut


=item service_name

    the GlobalNOC service identifier, with this client will consult the service
    naming service to resolve, best URL to use.

=cut

=item service_cache_file

    the location of the service cache file to use on disk (if not specified does direct nameservice queries)

=cut

=item name_services

    array containing the locations of nameservices to use

=cut

=item uid

    user id for authentication

=cut

=item passwd

    user password

=cut

=item timeout

    timeout value in seconds for the connection, if no activity observed in this time period LWP will abort.

=cut

=item usePost

    boolean value for whether or not we are using http POST style or not

=cut

=item use_keep_alive

    boolean value for whether or not to try and use keep_alives

=cut

=item use_pagination

    boolean value for wether or not to use a GRNOC::WebService::Client::Paginator object to iterate through results

=cut

=item user_agent

    string to use as the User-Agent string in request headers, defaults to $0

=cut

=item verify_hostname

    If set to 1 then ssl certs are validated. Set to 0 when working with untrusted or self-signed certs. Defaults to 1.

=cut

=item retry_error_codes

    hash of http error codes to retry the request on if the request fails

=cut

=back

=cut

sub new {

    my $that  = shift;
    my $class =ref($that) || $that;

    my %args = (
        debug          => 0,
        timeout        => 15,
        usePost        => 0,
        use_keep_alive => 1,
        raw_output     => 0,
        timing         => 0,
        user_agent     => $0,
        oldstyle_urls  => 0,
        cookieJar        => undef,
        method_parameter => "method",
        use_pagination   => 0,
        verify_hostname  => 1,
        retry_error_codes => { '408' => 1,
                               '503' => 1,
                               '502' => 1,
                               '500' => 1,
                               '504' => 1},
        config_file    => "/etc/grnoc/webservice_client/config.xml",
        default_realm  => undef,
        @_,
        );


    my $self = \%args;
    bless $self,$class;

    if (!defined $self->{'url'} && defined $self->{'service_name'}) {
        #ISSUE=3454
        my $t0;
        if ($self->{timing}) {
            $t0 = [gettimeofday];
            print "URL is not provided, start looking up the URL to be requested...\n";
        }

        #first check to see if either name_services or service_cache_file
        if (defined($self->{'service_cache_file'})) {
            #--- load the client config
            if (!$self->_load_config()) {
                #--- cant find the config?
                return $self;
            }

            if (! $self->_setup_urls($self->{'service_name'})) {
                #-- no url provided and none resolved from service name
                $self->_set_error("Unable to find a usable URL for URN = " . $self->{'service_name'} . " in cache file \"" . $self->{'service_cache_file'} . "\"\n");
                return $self;
            }

            if ($self->{timing}) {
                my $elapsed = tv_interval ($t0, [gettimeofday]);
                print "Took $elapsed seconds to look up from the config file\n"
            }
        }
        elsif (defined($self->{'name_services'})) {
            # get the NameService locations
            $self->_ns_service_lookup();

            if ($self->{timing}) {
                my $elapsed = tv_interval ($t0, [gettimeofday]);
                print "Took $elapsed seconds to look up from the Name Service\n"
            }

            if (! $self->_setup_urls($self->{'service_name'})) {
                #-- no url provided and none resolved from service name
                $self->_set_error("Unable to find a usable URL for URN = " . $self->{'service_name'} . " in name services: " . Dumper($self->{'name_services'}) . "\n");
                return $self;
            }
        }
        else {
            $self->_set_error("Unable to find a usable URL: Neither name_services or service_cache_file were specified\n");
        }

        #ISSUE=3454
        if ($self->{timing}) {
            print "URLs:\n";
            foreach my $weight (sort {$a <=> $b} keys %{$self->{'urls'}}) {
                foreach my $base (@{$self->{'urls'}{$weight}}) {
                    print "$base\n";
                }
            }
            print "\n";
        }

    }
    else {
        #--- defined url input means we dont do service lookup
        $self->{'urls'}{'0'}[0] = $self->{'url'};
    }

    {
        # In older versions of LWP::UserAgent::Determined the ssl_opts
        # parameter is not defined, and a warning is logged to the
        # command line. Setting $^W to zero surpresses these warnings
        # within this block.
        local ($^W) = 0;

        $self->{'ua'} = LWP::UserAgent::Determined->new(
            agent      => $self->{'user_agent'},
            ssl_opts   => {verify_hostname => $self->{'verify_hostname'}},
            keep_alive => $self->{'use_keep_alive'}
        );
    }

    #---- check to see if we need to use old style urls. This allows us to use web services that don't parse semicolons the same as ampersands.
    if($self->{'oldstyle_urls'}) {
        CGI->import(qw/ -oldstyle_urls /);
    }

    #---- set the timeout
    $self->{'ua'}->timeout($self->{'timeout'});

    #---- cookies to be automatically dealt with
    $self->set_cookie_jar($self->{'cookieJar'});

    #---- turn on auto redirects
    $self->{'ua'}->requests_redirectable(['GET', 'HEAD', 'POST', 'OPTIONS']);

    if ($self->{'timing'}){
        if ($self->{'ua'}->can("add_handler")){
            $self->{'ua'}->add_handler("response_redirect", $self->_redirect_timing());
        }
    }

    #--- verify error handler
    my $callback = $self->{'error_callback'};
    if (defined $callback && (!ref($callback) || ref $callback ne "CODE")){
        $self->{'error_callback'} = undef;
        $self->_set_error("error_callback argument must be a code ref");
    }

    #set retries to 0 initially
    $self->set_retries( 0 );

    #set retry interval to 5s by default
    $self->set_retry_interval( 5 );

    #set the retry http error codes
    my $retry_codes = dclone $self->{'retry_error_codes'};
    $self->{'ua'}->codes_to_determinate( $retry_codes );

    # XML processors for ECP
    $self->{'xmlparser'} = XML::LibXML->new();
    $self->{'xpath'} = XML::LibXML::XPathContext->new();
    $self->{'xpath'}->registerNs('S' => 'http://schemas.xmlsoap.org/soap/envelope/');
    $self->{'xpath'}->registerNs('paos' => 'urn:liberty:paos:2003-08');
    $self->{'xpath'}->registerNs('ecp' => 'urn:oasis:names:tc:SAML:2.0:profiles:SSO:ecp');
    $self->{'xpath'}->registerNs('saml' => 'urn:oasis:names:tc:SAML:2.0:assertion');
    $self->{'xpath'}->registerNs('saml2p' => 'urn:oasis:names:tc:SAML:2.0:protocol');

    #load the default realm from config file             
    $self->_load_default_realm() if( -e $self->{'config_file'} );

    return $self;
}



sub DESTROY{

}



( run in 2.836 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )