LS/Client/HTTP.pm untested

filter: Statementstmt 0.0% 0 / 133 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 0.0% 0 / 23 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 262 CC 21 SCAR 23.4
CC
21
Cov
0%
getContent
20.0
getMetadata
20.0
getMetadataSubset
20.0
CRAP 10.4
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::HTTP;
11  
12 use strict;
13 use warnings;
14  
15 use vars qw( $AGENT_IDENTIFIER $METHODS );
16  
17 use URI;
18 use File::Temp;
19 use LWP::UserAgent;
20  
21 use LS;
22 use LS::ID;
23 use LS::Client;
24 use LS::Service::Response;
25  
26 use base 'LS::Client';
27  
28  
29  
30 #
31 # $AGENT_IDENTIFIER -
32 #
33 $AGENT_IDENTIFIER = 'IBM LSID Resolver';
34  
35  
36 BEGIN {
37  
38 $METHODS = [
39 'url',
40 ];
41
42 LS::makeAccessorMethods($METHODS, __PACKAGE__);
43 }
44  
45  
46 #
47 # new( %options ) -
48 #
49 sub new {
50  
51 my ($self, %options) = @_;
52  
53 unless(exists($options{'url'}) && defined($options{'url'})) {
54 die('Missing URL parameter');
55 }
56
57 unless(ref $self) {
58  
59 $self = bless {
60 %options,
61 }, $self;
62  
63 }
64  
65 $self->url( $options{'url'} );
66 $self->{'userAgent'} = LWP::UserAgent->new(agent=> $AGENT_IDENTIFIER);
67  
68 return $self;
69 }
70  
71 #
72 # getContent( %options ) - Makes a generic HTTP request to the specified URL and returns the content
73 #
74 # Hash Parameters:
75 #
76 # $options{'url'} - The URL to retrieve data
77 #
78 # Returns:
79 #
80 # undef if there is an error, otherwise
81 # data from the web request
82 #
83 #
84 sub getContent {
85  
86 my ($self, %options) = @_;
87  
88 unless($options{'url'}) {
89  
90 $self->recordError( 'Missing URL in sub getContent' );
91 $self->addStackTrace();
92 die($self->errorDetails());
93 }
94  
95 my $url = $options{'url'};
96
97 # Method defaults to GET as per OMG spec
98 my $method = $options{'method'} ? $options{'method'} : 'GET';
99  
100 my $ua = $self->{'userAgent'};
101  
102 # Stream the request in to a temporary file
103 # so that we can store large files
104 my $fh = File::Temp->new(UNLINK=> 1);
105  
106 my $request = HTTP::Request->new($method, $url);
107  
108 # Using the filename is bad.. but how to get around it on all platforms?
109 my $response = $ua->request($request, $fh->filename());
110  
111 unless ($response->is_success()) {
112  
113 $self->recordError( 'HTTP ' . $method . ' unsuccessful: ' . $response->status_line() );
114 $self->addStackTrace();
115 die($self->errorDetails());
116 }
117  
118 return LS::Service::Response->new(response=> $fh);
119 }
120  
121  
122 #
123 # getServices -
124 #
125 sub getServices {
126  
127 }
128  
129  
130 #
131 # getAvailableServices( %options ) -
132 #
133 sub getAvailableServices {
134  
135 my ($self, %options) = @_;
136  
137 unless($options{'lsid'}) {
138  
139 $self->recordError( 'Missing parameters in sub getAvailableServices' );
140 $self->addStackTrace();
141 die($self->errorDetails());
142 }
143  
144 my $lsid = $options{'lsid'};
145  
146 my $url = (exists($options{'url'}) && defined($options{'url'})) ? $options{'url'} : $self->url();
147 my $request_url = $url . '?lsid=' . $lsid->as_string();
148  
149 return $self->getContent(url=> $request_url);
150 }
151  
152  
153 #
154 # getMetadata( %options ) -
155 #
156 sub getMetadata {
157  
158 my ($self, %options) = @_;
159  
160 unless($options{'lsid'}) {
161  
162 $self->recordError( 'Missing parameters in sub getMetadata' );
163 $self->addStackTrace();
164 die($self->errorDetails());
165 }
166  
167 my $lsid = $options{'lsid'};
168 my $acceptedFormats = $options{'acceptedFormats'} ? $options{'acceptedFormats'} : 'application/rdf+xml';
169  
170 my $url = (exists($options{'url'}) && defined($options{'url'})) ? $options{'url'} : $self->url();
171  
172 my $request_url = $url . '?acceptedFormats=' . URI::Escape::uri_escape($acceptedFormats) .
173 '&lsid=' . URI::Escape::uri_escape($lsid->as_string());
174  
175 return $self->getContent(url=> $request_url);
176 }
177  
178  
179 #
180 # getMetadataSubset( %options ) -
181 #
182 sub getMetadataSubset {
183  
184 my ($self, %options) = @_;
185  
186 unless($options{'lsid'}) {
187  
188 $self->recordError( 'Missing parameters in sub getMetadataSubset' );
189 $self->addStackTrace();
190 die($self->errorDetails());
191 }
192  
193 my $lsid = $options{'lsid'};
194 my $acceptedFormats = $options{'acceptedFormats'} ? $options{'acceptedFormats'} : 'application/rdf+xml';
195  
196 # FIXME: Add getMetadataSubset to URL parameters
197 my $url = (exists($options{'url'}) && defined($options{'url'})) ? $options{'url'} : $self->url();
198
199 my $request_url = $url . '?acceptedFormats=' . URI::Escape::uri_escape($acceptedFormats) .
200 '&lsid=' . URI::Escape::uri_escape($lsid->as_string);
201  
202 return $self->getContent(url=> $request_url);
203 }
204  
205  
206 #
207 # getData( %options ) -
208 #
209 sub getData {
210  
211 my ($self, %options) = @_;
212  
213 unless($options{'lsid'}) {
214  
215 $self->recordError( 'Missing parameters in sub getData' );
216 $self->addStackTrace();
217 die($self->errorDetails());
218 }
219  
220 my $lsid = $options{'lsid'};
221 my $url = (exists($options{'url'}) && defined($options{'url'})) ? $options{'url'} : $self->url();
222
223 my $request_url = $url . '?lsid=' . URI::Escape::uri_escape($lsid->as_string());
224  
225 return $self->getContent(url=> $request_url);
226 }
227  
228  
229 #
230 # getDataByRange( %options ) -
231 #
232 sub getDataByRange {
233  
234 my ($self, %options) = @_;
235  
236 unless($options{'lsid'} &&
237 $options{'start'} &&
238 $options{'length'}) {
239  
240 $self->recordError( 'Missing parameters in sub getDataByRange' );
241 $self->addStackTrace();
242 die($self->errorDetails());
243 }
244  
245 my $lsid = $options{'lsid'};
246 my $start = $options{'start'};
247 my $length = $options{'length'};
248 my $url = (exists($options{'url'}) && defined($options{'url'})) ? $options{'url'} : $self->url();
249  
250 my $request_url = $url . '?lsid=' . URI::Escape::uri_escape($lsid->as_string());
251 $request_url .= '&start=' . $start;
252 $request_url .= '&length=' . $length;
253  
254 return $self->getContent(url=> $request_url);
255 }
256  
257  
258  
259  
260 #
261 # FAN Stubs
262 # NOTE: This may change
263 #
264  
265  
266 #
267 # _fan_call( %options ) -
268 #
269 sub _fan_call {
270  
271 my ($self, %options) = @_;
272  
273 my $lsid = $options{'lsid'};
274 my $authorityName = $options{'authorityName'};
275  
276 my $url = (exists($options{'url'}) && defined($options{'url'})) ? $options{'url'} : $self->url();
277  
278 my $request_url = $url . '?lsid=' . URI::Escape::uri_escape($lsid->as_string());
279 $request_url .= '&authorityName=' . URI::Escape::uri_escape($authorityName);
280  
281 return $self->getContent(url=> $request_url);
282 }
283  
284  
285 #
286 #
287 # notifyForeignAuthority(String lsid, String authorityName)
288 #
289 sub notifyForeignAuthority {
290  
291 my ($self, %options) = @_;
292  
293 unless($options{'lsid'} &&
294 $options{'authorityName'}) {
295  
296 $self->recordError( 'Missing parameters in sub notifyForeignAuthority' );
297 $self->addStackTrace();
298 die($self->errorDetails());
299 }
300
301 return $self->_fan_call(%options);
302 }
303  
304  
305 #
306 #
307 # revokeNotifcationForeignAuthority(String lsid, String authorityName)
308 #
309 sub revokeNotificationForeignAuthority {
310  
311 my ($self, %options) = @_;
312  
313 unless($options{'lsid'} &&
314 $options{'authorityName'}) {
315  
316 $self->recordError( 'Missing parameters in sub revokeNotificationForeignAuthority' );
317 $self->addStackTrace();
318 die($self->errorDetails());
319 }
320  
321 return $self->_fan_call(%options);
322 }
323  
324 1;
325  
326 __END__