| 1 | # ===================================================================== | ||
| 2 | # Copyright (c) 2002,2003 IBM Corporation | ||
| 3 | # All rights reserved. This program and the accompanying materials | ||
| 4 | # are made available under the terms of the Common Public License v1.0 | ||
| 5 | # which accompanies this distribution, and is available at | ||
| 6 | # http://www.opensource.org/licenses/cpl.php | ||
| 7 | # | ||
| 8 | # ===================================================================== | ||
| 9 | |||
| 10 | package LS::Client::BasicResolver; | ||
| 11 | |||
| 12 | use strict; | ||
| 13 | use warnings; | ||
| 14 | |||
| 15 | use LS::ID; | ||
| 16 | use LS::Client; | ||
| 17 | |||
| 18 | use base 'LS::Client'; | ||
| 19 | |||
| 20 | |||
| 21 | |||
| 22 | # | ||
| 23 | # new( %options ) - Creates a new instance of an LS::Client::BasicResolver | ||
| 24 | # | ||
| 25 | sub new { | ||
| 26 | |||
| 27 | my ($self, %options) = @_; | ||
| 28 | |||
| 29 | unless(ref $self) { | ||
| 30 | |||
| 31 | $self = bless { | ||
| 32 | |||
| 33 | authorities=> {}, | ||
| 34 | resources=> {}, | ||
| 35 | }, $self; | ||
| 36 | |||
| 37 | } | ||
| 38 | |||
| 39 | return $self; | ||
| 40 | } | ||
| 41 | |||
| 42 | |||
| 43 | # | ||
| 44 | # cacheAuthority( $lsid, [ $authority ] ) - Sets or retrieves an LS::Authority object for the given LSID. | ||
| 45 | # | ||
| 46 | # If the $authority parameter is left unspecified, the LS::Authority | ||
| 47 | # object for $lsid is returned. | ||
| 48 | # | ||
| 49 | # Parameters - $lsid, Required. The LSID used to retrieve the cached authority. | ||
| 50 | # $authority, Optional. The LS::Authority object that will be cached for $lsid. | ||
| 51 | # | ||
| 52 | # Returns - The LS::Authority object for the specified LSID. | ||
| 53 | # | ||
| 54 | sub cacheAuthority { | ||
| 55 | |||
| 56 | my $self = shift; | ||
| 57 | my $lsid = shift; | ||
| 58 | |||
| 59 | unless(UNIVERSAL::isa($lsid, 'LS::ID')) { | ||
| 60 | |||
| 61 | $self->recordError( 'LSID not specified' ); | ||
| 62 | $self->addStackTrace(); | ||
| 63 | die($self->errorDetails()); | ||
| 64 | } | ||
| 65 | |||
| 66 | my $stringLSID = $lsid->as_string(); | ||
| 67 | |||
| 68 | @_ ? $self->{'authorities'}->{$stringLSID} = shift : return $self->{'authorities'}->{$stringLSID}; | ||
| 69 | } | ||
| 70 | |||
| 71 | |||
| 72 | # | ||
| 73 | # cacheResource( $lsid ) - Sets or retrieves an LS::Resource object for the given LSID. | ||
| 74 | # | ||
| 75 | # Parameters - $lsid, Required. The LSID used to lookup a particular LS::Resource. | ||
| 76 | # | ||
| 77 | # Returns - An LS::Resource for the specified LSID. | ||
| 78 | # | ||
| 79 | sub cacheResource { | ||
| 80 | |||
| 81 | my $self = shift; | ||
| 82 | my $lsid = shift; | ||
| 83 | |||
| 84 | unless(UNIVERSAL::isa($lsid, 'LS::ID') ) { | ||
| 85 | |||
| 86 | $self->recordError( 'LSID not specified' ); | ||
| 87 | $self->addStackTrace(); | ||
| 88 | die($self->errorDetails()); | ||
| 89 | } | ||
| 90 | |||
| 91 | my $stringLSID = $lsid->as_string(); | ||
| 92 | |||
| 93 | @_ ? $self->{'resources'}->{$stringLSID} = shift : return $self->{'resources'}->{$stringLSID}; | ||
| 94 | } | ||
| 95 | |||
| 96 | |||
| 97 | # | ||
| 98 | # resolve( %options ) - Retrieves the authority WSDL for the specified LSID in the form | ||
| 99 | # of an LS::Authority object. | ||
| 100 | # | ||
| 101 | # This method performs the necessary steps to resolve an LSID if it is not | ||
| 102 | # present in LS::Client::BasicResolver's cache. If the LSID has already | ||
| 103 | # been resolved, then the cached copy of the LS::Authority object is | ||
| 104 | # returned without querying any external resources. | ||
| 105 | # | ||
| 106 | # Parameters - An options hash with the following keys: | ||
| 107 | # | ||
| 108 | # lsid - Required. The LSID to resolve. | ||
| 109 | # | ||
| 110 | # Returns - An LS::Authority for the specified LSID. | ||
| 111 | # | ||
| 112 | sub resolve { | ||
| 113 | |||
| 114 | my ($self, %options) = @_; | ||
| 115 | |||
| 116 | my $lsid = $options{'lsid'}; | ||
| 117 | |||
| 118 | unless(UNIVERSAL::isa($lsid, 'LS::ID')) { | ||
| 119 | |||
| 120 | $lsid = LS::ID->new($lsid); | ||
| 121 | |||
| 122 | unless($lsid) { | ||
| 123 | |||
| 124 | $self->recordError( 'LSID not specified' ); | ||
| 125 | $self->addStackTrace(); | ||
| 126 | die($self->errorDetails()); | ||
| 127 | } | ||
| 128 | } | ||
| 129 | |||
| 130 | my $authority; | ||
| 131 | |||
| 132 | unless( ($authority = $self->cacheAuthority($lsid)) ) { | ||
| 133 | |||
| 134 | require LS::Locator; | ||
| 135 | |||
| 136 | my $locator = LS::Locator->new(); | ||
| 137 | |||
| 138 | unless( ($authority = $locator->resolveAuthority($lsid)) ) { | ||
| 139 | |||
| 140 | $self->recordError( "Unable to resolve authority." ); | ||
| 141 | $self->appendError( ($locator->errorString() || 'No details from LS::Locator') ); | ||
| 142 | |||
| 143 | $self->addStackTrace( $locator->getStackTrace() ); | ||
| 144 | |||
| 145 | die($self->errorDetails()); | ||
| 146 | } | ||
| 147 | |||
| 148 | $self->cacheAuthority($lsid, $authority); | ||
| 149 | } | ||
| 150 | |||
| 151 | return $authority; | ||
| 152 | } | ||
| 153 | |||
| 154 | |||
| 155 | # | ||
| 156 | # getResource( %options ) - Retrieves an LS::Resource for the specified LSID. | ||
| 157 | # | ||
| 158 | # This method returns an LS::Resource object which can later be used to | ||
| 159 | # invoke LSID service routines such as getData and getMetadata. | ||
| 160 | # | ||
| 161 | # Parameters - A hash of the following keys: | ||
| 162 | # | ||
| 163 | # credentials - Optional. The LS::Client::Credentials object containing the | ||
| 164 | # user's credentials for the authority. | ||
| 165 | # | ||
| 166 | # protocol - Optional. The protocol (such as 'soap' or 'http') used for transport. | ||
| 167 | # | ||
| 168 | # lsid - Required. The LSID that the LS::Resource will represent. | ||
| 169 | # | ||
| 170 | # | ||
| 171 | # Returns - An LS::Resource object for the specified LSID | ||
| 172 | # | ||
| 173 | sub getResource { | ||
| 174 | |||
| 175 | my ($self, %options) = @_; | ||
| 176 | |||
| 177 | my $credentials = $options{'credentials'}; | ||
| 178 | |||
| 179 | my $lsid = $options{'lsid'}; | ||
| 180 | |||
| 181 | unless(UNIVERSAL::isa($lsid, 'LS::ID')) { | ||
| 182 | |||
| 183 | $lsid = LS::ID->new($lsid); | ||
| 184 | |||
| 185 | unless($lsid) { | ||
| 186 | |||
| 187 | $self->recordError( 'LSID not specified' ); | ||
| 188 | $self->addStackTrace(); | ||
| 189 | |||
| 190 | die($self->errorDetails()); | ||
| 191 | } | ||
| 192 | } | ||
| 193 | |||
| 194 | my $authority; | ||
| 195 | |||
| 196 | unless(($authority = $self->cacheAuthority($lsid)) || | ||
| 197 | ($authority = $self->resolve(lsid=> $lsid)) ) { | ||
| 198 | |||
| 199 | $self->appendError( 'Unable to resolve authority for LSID: ' . $lsid->as_string() ); | ||
| 200 | die($self->errorDetails()); | ||
| 201 | } | ||
| 202 | |||
| 203 | # Authenticate to the authority | ||
| 204 | if(UNIVERSAL::isa($credentials, 'LS::Client::Credentials')) { | ||
| 205 | |||
| 206 | $authority->authenticate(credentials=> $credentials, | ||
| 207 | username=> $credentials->username(), | ||
| 208 | password=> $credentials->password()); | ||
| 209 | } | ||
| 210 | |||
| 211 | my $resource; | ||
| 212 | |||
| 213 | unless($resource = $authority->getResource($lsid)) { | ||
| 214 | |||
| 215 | $self->recordError( "Unable to retrieve resource for specified LSID: " . $lsid->as_string() ); | ||
| 216 | $self->appendError( ($authority->errorString() || 'No details from LS::Authority') ); | ||
| 217 | |||
| 218 | $self->addStackTrace( $authority->getStackTrace() ); | ||
| 219 | |||
| 220 | die($self->errorDetails()); | ||
| 221 | } | ||
| 222 | |||
| 223 | $self->cacheResource($lsid, $resource); | ||
| 224 | |||
| 225 | return $resource; | ||
| 226 | } | ||
| 227 | |||
| 228 | |||
| 229 | # | ||
| 230 | # _service_call( %options ) - A generic method to invoke the protocol specific client backend and deal | ||
| 231 | # with errors etc. | ||
| 232 | # | ||
| 233 | # Parameters - A hash with the following keys: | ||
| 234 | # | ||
| 235 | # method - Required. The name of the remote method to invoke. | ||
| 236 | # | ||
| 237 | # All other hash keys will be passed to getResource( ) | ||
| 238 | # | ||
| 239 | # Returns - undef in the case of an error or, | ||
| 240 | # The result of the remote method call. | ||
| 241 | # | ||
| 242 | sub _service_call { | ||
| 243 | |||
| 244 | my ($self, %options) = @_; | ||
| 245 | |||
| 246 | my $method = $options{'method'}; | ||
| 247 | |||
| 248 | unless($method) { | ||
| 249 | |||
| 250 | $self->recordError( 'SOAP Method not specified' ); | ||
| 251 | $self->addStackTrace(); | ||
| 252 | die($self->errorDetails()); | ||
| 253 | } | ||
| 254 | |||
| 255 | # Get the LS::Resource for the request | ||
| 256 | my $resource = $self->getResource(%options); | ||
| 257 | |||
| 258 | return undef | ||
| 259 | unless($resource); | ||
| 260 | |||
| 261 | my $ret; | ||
| 262 | unless( ($ret = $resource->$method())) { | ||
| 263 | |||
| 264 | $self->recordError( "Error in method: $method." ); | ||
| 265 | $self->appendError( ($resource->errorString() || 'No details available from LS::Resource')); | ||
| 266 | |||
| 267 | $self->addStackTrace( $resource->getStackTrace() ); | ||
| 268 | |||
| 269 | die($self->errorDetails()); | ||
| 270 | } | ||
| 271 | |||
| 272 | return $ret; | ||
| 273 | } | ||
| 274 | |||
| 275 | |||
| 276 | # | ||
| 277 | # getMetadata( %options ) - | ||
| 278 | # | ||
| 279 | sub getMetadata { | ||
| 280 | |||
| 281 | my ($self, %options) = @_; | ||
| 282 | |||
| 283 | return $self->_service_call(method=> 'getMetadata', | ||
| 284 | %options); | ||
| 285 | } | ||
| 286 | |||
| 287 | |||
| 288 | # | ||
| 289 | # getMetadataSubset( %options ) - | ||
| 290 | # | ||
| 291 | sub getMetadataSubset { | ||
| 292 | |||
| 293 | my ($self, %options) = @_; | ||
| 294 | |||
| 295 | return $self->_service_call(method=> 'getMetadataSubset', | ||
| 296 | %options); | ||
| 297 | } | ||
| 298 | |||
| 299 | |||
| 300 | # | ||
| 301 | # getData( %options ) - | ||
| 302 | # | ||
| 303 | sub getData { | ||
| 304 | |||
| 305 | my ($self, %options) = @_; | ||
| 306 | |||
| 307 | return $self->_service_call(method=> 'getData', | ||
| 308 | %options); | ||
| 309 | } | ||
| 310 | |||
| 311 | |||
| 312 | # | ||
| 313 | # getDataByRange( %options ) - | ||
| 314 | # | ||
| 315 | sub getDataByRange { | ||
| 316 | |||
| 317 | my ($self, %options) = @_; | ||
| 318 | |||
| 319 | return $self->_service_call(method=> 'getDataByRange', | ||
| 320 | %options); | ||
| 321 | } | ||
| 322 | |||
| 323 | |||
| 324 | 1; | ||
| 325 | |||
| 326 | __END__ |