| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package WebSource::Query; |
|
2
|
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
2547
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
93
|
|
|
4
|
1
|
|
|
1
|
|
7
|
use Carp; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
112
|
|
|
5
|
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
use WebSource::Module |
|
7
|
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
1532
|
our @ISA = ('WebSource::Module'); |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head1 NAME |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
WebSource::Query - Build a query (HTTP request) given a hash |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
A Query operator builds an HTTP query for each input. |
|
17
|
|
|
|
|
|
|
The declaration of a query operator is a DOM Node with the following format : |
|
18
|
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
http://somewhere/query.cgi |
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
... |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
=head1 METHODS |
|
29
|
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
See WebSource::Module |
|
31
|
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
=cut |
|
33
|
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub _init_ { |
|
35
|
|
|
|
|
|
|
my $self = shift; |
|
36
|
|
|
|
|
|
|
$self->SUPER::_init_; |
|
37
|
|
|
|
|
|
|
my $wsd = $self->{wsdnode} or croak("No description node given\n"); |
|
38
|
|
|
|
|
|
|
$self->{base_uri} = $wsd->findvalue('base'); |
|
39
|
|
|
|
|
|
|
$self->{method} = $wsd->getAttribute('method'); |
|
40
|
|
|
|
|
|
|
} |
|
41
|
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub handle { |
|
43
|
|
|
|
|
|
|
my $self = shift; |
|
44
|
|
|
|
|
|
|
my $env = shift; |
|
45
|
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
my ($query, $missing) = $self->build_query($env); |
|
47
|
|
|
|
|
|
|
if(@$missing) { |
|
48
|
|
|
|
|
|
|
croak "Missing query parameters :\n\t- '" . join("'\n\t- '",@$missing) . "'\n"; |
|
49
|
|
|
|
|
|
|
} |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
my $req = $self->make_request($query,$env); |
|
52
|
|
|
|
|
|
|
return WebSource::Envelope->new( |
|
53
|
|
|
|
|
|
|
type => "object/http-request", |
|
54
|
|
|
|
|
|
|
data => $req); |
|
55
|
|
|
|
|
|
|
} |
|
56
|
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub build_query { |
|
58
|
|
|
|
|
|
|
my ($self,$env) = @_; |
|
59
|
|
|
|
|
|
|
# $env->type eq "hash" or croak("Envelope didn't contain hash\n"); |
|
60
|
|
|
|
|
|
|
# my $qref = $env->data; |
|
61
|
|
|
|
|
|
|
my $qref = $env; |
|
62
|
|
|
|
|
|
|
my @params = $self->{wsdnode}->findnodes('parameters/param'); |
|
63
|
|
|
|
|
|
|
my @missing; |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
my %query; |
|
66
|
|
|
|
|
|
|
foreach $_ (@params) { |
|
67
|
|
|
|
|
|
|
my $name = $_->getAttribute("name"); |
|
68
|
|
|
|
|
|
|
my $value; |
|
69
|
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
$_->hasAttribute("rename") and |
|
71
|
|
|
|
|
|
|
croak("Deprecated use of attribute rename in query parameters"); |
|
72
|
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
if($_->hasAttribute("value-of")){ |
|
74
|
|
|
|
|
|
|
my $key = $_->getAttribute("value-of"); |
|
75
|
|
|
|
|
|
|
if (exists($qref->{$key})) { |
|
76
|
|
|
|
|
|
|
if($qref->{$key}) { |
|
77
|
|
|
|
|
|
|
$value = ($key eq 'data') ? |
|
78
|
|
|
|
|
|
|
$env->dataString : |
|
79
|
|
|
|
|
|
|
$qref->{$key}; |
|
80
|
|
|
|
|
|
|
} |
|
81
|
|
|
|
|
|
|
} else { |
|
82
|
|
|
|
|
|
|
$value = ""; |
|
83
|
|
|
|
|
|
|
} |
|
84
|
|
|
|
|
|
|
} |
|
85
|
|
|
|
|
|
|
if(!defined($value) && $_->hasAttribute("default")) { |
|
86
|
|
|
|
|
|
|
$value = $_->getAttribute("default"); |
|
87
|
|
|
|
|
|
|
} |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
if (defined($value)) { |
|
90
|
|
|
|
|
|
|
$query{$name} = $value; |
|
91
|
|
|
|
|
|
|
} else { |
|
92
|
|
|
|
|
|
|
push(@missing, ($name)); |
|
93
|
|
|
|
|
|
|
} |
|
94
|
|
|
|
|
|
|
}; |
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
return (\%query,\@missing); |
|
97
|
|
|
|
|
|
|
} |
|
98
|
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub make_request { |
|
100
|
|
|
|
|
|
|
my ($self,$query,$env) = @_; |
|
101
|
|
|
|
|
|
|
my $uri = URI->new($self->{base_uri} ? $self->{base_uri} : $env->dataAsURI); |
|
102
|
|
|
|
|
|
|
my $qstr; |
|
103
|
|
|
|
|
|
|
map { |
|
104
|
|
|
|
|
|
|
$qstr .= "\t". $_ . " => " . $query->{$_} ."\n"; |
|
105
|
|
|
|
|
|
|
} keys(%$query); |
|
106
|
|
|
|
|
|
|
$self->log(5,"Built query : \n",$qstr); |
|
107
|
|
|
|
|
|
|
$uri->query_form(%$query); |
|
108
|
|
|
|
|
|
|
my $method = $self->{method}; |
|
109
|
|
|
|
|
|
|
if($method ne "GET") { |
|
110
|
|
|
|
|
|
|
my $query = $uri->query; |
|
111
|
|
|
|
|
|
|
$uri->query(undef); |
|
112
|
|
|
|
|
|
|
my $req = HTTP::Request->new('POST',$uri); |
|
113
|
|
|
|
|
|
|
$req->content_type("application/x-www-form-urlencoded"); |
|
114
|
|
|
|
|
|
|
$req->content_length(length($query)); |
|
115
|
|
|
|
|
|
|
$req->content($query); |
|
116
|
|
|
|
|
|
|
return $req; |
|
117
|
|
|
|
|
|
|
} else { |
|
118
|
|
|
|
|
|
|
return HTTP::Request->new('GET',$uri); |
|
119
|
|
|
|
|
|
|
} |
|
120
|
|
|
|
|
|
|
} |
|
121
|
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
sub parameters { |
|
123
|
|
|
|
|
|
|
my ($self) = @_; |
|
124
|
|
|
|
|
|
|
return map { |
|
125
|
|
|
|
|
|
|
$_->getAttribute("name") => $_->getAttribute("default") |
|
126
|
|
|
|
|
|
|
} $self->{wsdnode}->findnodes('parameters/param'); |
|
127
|
|
|
|
|
|
|
} |
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
130
|
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
WebSource |
|
132
|
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=cut |
|
134
|
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
1; |