| 1 | #!/usr/bin/perl | ||||||||||||||||||||||||||||
| 2 | package JMX::Jmx4Perl::Agent; | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use JSON; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use URI::Escape qw(uri_escape_utf8); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use HTTP::Request; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use vars qw($VERSION $DEBUG); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use base qw(JMX::Jmx4Perl); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use JMX::Jmx4Perl::Request; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use JMX::Jmx4Perl::Response; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use JMX::Jmx4Perl::Agent::UserAgent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 3 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 3 | $VERSION = $JMX::Jmx4Perl::VERSION; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | =head1 NAME | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | JMX::Jmx4Perl::Agent - JSON-HTTP based acess to a remote JMX agent | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | my $agent = new JMX::Jmx4Perl(mode=>"agent", url => "http://jeeserver/j4p"); | ||||||||||||||||||||||||||||
| 26 | my $answer = $agent->get_attribute("java.lang:type=Memory","HeapMemoryUsage"); | ||||||||||||||||||||||||||||
| 27 | print Dumper($answer); | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | { | ||||||||||||||||||||||||||||
| 30 | request => { | ||||||||||||||||||||||||||||
| 31 | attribute => "HeapMemoryUsage", | ||||||||||||||||||||||||||||
| 32 | name => "java.lang:type=Memory" | ||||||||||||||||||||||||||||
| 33 | }, | ||||||||||||||||||||||||||||
| 34 | status => 200, | ||||||||||||||||||||||||||||
| 35 | value => { | ||||||||||||||||||||||||||||
| 36 | committed => 18292736, | ||||||||||||||||||||||||||||
| 37 | init => 0, | ||||||||||||||||||||||||||||
| 38 | max => 532742144, | ||||||||||||||||||||||||||||
| 39 | used => 15348352 | ||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | This module is not used directly, but via L<JMX::Jmx4Perl>, which acts as a | ||||||||||||||||||||||||||||
| 46 | proxy to this module. You can think of L<JMX::Jmx4Perl> as the interface which | ||||||||||||||||||||||||||||
| 47 | is backed up by this module. Other implementations (e.g. | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =head1 METHODS | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | =over 4 | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | =item $jjagent = JMX::Jmx4Perl::Agent->new(url => $url, ....) | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | Creates a new local agent for a given url | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | =over | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | =item url => <url to JEE server> | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | The url where the agent is deployed. This is a mandatory parameter. The url | ||||||||||||||||||||||||||||
| 62 | must include the context within the server, which is typically based on the | ||||||||||||||||||||||||||||
| 63 | name of the war archive. Example: C<http://localhost:8080/j4p> for a drop | ||||||||||||||||||||||||||||
| 64 | in deployment of the agent in a standard Tomcat's webapp directory. | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | =item timeout => <timeout> | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | Timeout in seconds after which a request should be stopped if it not suceeds | ||||||||||||||||||||||||||||
| 69 | within this time. This parameter is given through directly to the underlying | ||||||||||||||||||||||||||||
| 70 | L<LWP::UserAgent> | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | =item user => <user>, password => <password> | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | Credentials to use for the HTTP request | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | =item method => <method> | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | The HTTP method to use for contacting the agent. Must be either "GET" or | ||||||||||||||||||||||||||||
| 79 | "POST". This method is used, if the request to send dosen't specify the method | ||||||||||||||||||||||||||||
| 80 | and no other parameters forces a POST context. | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | =item proxy => { http => '<http_proxy>', https => '<https_proxy>', ... } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | =item proxy => <http_proxy> | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | =item proxy => { url => <http_proxy> } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | Optional proxy to use | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | =item proxy_user => <user>, proxy_password => <password> | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | Credentials to use for accessing the proxy | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | =item target | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | Add a target which is used for any request served by this object if not already | ||||||||||||||||||||||||||||
| 97 | a target is present in the request. This way you can setup the default target | ||||||||||||||||||||||||||||
| 98 | configuration if you are using the agent servlet as a proxy, e.g. | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | ... target => { url => "service:jmx:...", user => "...", password => "..." } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | =item legacy-escape | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | Before version 1.0 a quite strange escaping scheme is used, when the part of a | ||||||||||||||||||||||||||||
| 105 | GET requests contains a slash (/). Starting with 1.0 this scheme has changed, | ||||||||||||||||||||||||||||
| 106 | but in order to allow post 1.0 Jmx4perl clients acess pre 1.0 Jolokia agents, | ||||||||||||||||||||||||||||
| 107 | this option can be set to true to switch to the old escape mechanism. | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | =back | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | =cut | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | # HTTP Parameters to be used for transmitting the request | ||||||||||||||||||||||||||||
| 114 | 3 | my @PARAMS = ("maxDepth","maxCollectionSize","maxObjects","ignoreErrors"); | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | # Regexp for detecting invalid chars which can not be used securily in pathinfos | ||||||||||||||||||||||||||||
| 117 | 3 | my $INVALID_PATH_CHARS = qr/%(5C|3F|3B|2F)/i; # \ ? ; / | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | # Init called by parent package within 'new' for specific initialization. See | ||||||||||||||||||||||||||||
| 120 | # above for the parameters recognized | ||||||||||||||||||||||||||||
| 121 | 5 | ▶ | sub init { | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=8 SCAR=29.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 122 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 5 | ▶ | croak "No URL provided" unless $self->cfg('url'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 5 | my $ua = JMX::Jmx4Perl::Agent::UserAgent->new(); | |||||||||||||||||||||||||||
| 126 | 5 | $ua->jjagent_config($self->{cfg}); | |||||||||||||||||||||||||||
| 127 | #push @{ $ua->requests_redirectable }, 'POST'; | ||||||||||||||||||||||||||||
| 128 | 5 | ▶ | $ua->timeout($self->cfg('timeout')) if $self->cfg('timeout'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | #print "TO: ",$ua->timeout(),"\n"; | ||||||||||||||||||||||||||||
| 130 | 5 | $ua->agent("JMX::Jmx4Perl::Agent $VERSION"); | |||||||||||||||||||||||||||
| 131 | # $ua->env_proxy; | ||||||||||||||||||||||||||||
| 132 | 5 | my $proxy = $self->cfg('proxy'); | |||||||||||||||||||||||||||
| 133 | 5 | ▶ | if ($proxy) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 0 | ▶ | my $url = ref($proxy) eq "HASH" ? $proxy->{url} : $proxy; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 0 | ▶ | if (ref($url) eq "HASH") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 0 | for my $k (keys %$url) { | |||||||||||||||||||||||||||
| 137 | 0 | $ua->proxy($k,$url->{$k}); | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | } else { | ||||||||||||||||||||||||||||
| 140 | 0 | ▶ | if ($self->cfg('url') =~ m|^(.*?)://|) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | # Set proxy for URL scheme used | ||||||||||||||||||||||||||||
| 142 | 0 | $ua->proxy($1,$url); | |||||||||||||||||||||||||||
| 143 | } else { | ||||||||||||||||||||||||||||
| 144 | 0 | $ua->proxy('http',$proxy); | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | 5 | $self->{ua} = $ua; | |||||||||||||||||||||||||||
| 149 | 5 | return $self; | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | =item $url = $agent->url() | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | Get the base URL for connecting to the agent. You cannot change the URL via this | ||||||||||||||||||||||||||||
| 155 | method, it is immutable for a given agent. | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | =cut | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 0 | ▶ | sub url { | ||||||||||||||||||||||||||
|
Subroutine
url: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 160 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 161 | 0 | return $self->cfg('url'); | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | =item $resp = $agent->request($request) | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | Implementation of the JMX request as specified in L<JMX::Jmx4Perl>. It uses a | ||||||||||||||||||||||||||||
| 167 | L<HTTP::Request> sent via an L<LWP::UserAgent> for posting a JSON representation | ||||||||||||||||||||||||||||
| 168 | of the request. This method shouldn't be called directly but via | ||||||||||||||||||||||||||||
| 169 | L<JMX::Jmx4Perl>->request(). | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | =cut | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 0 | ▶ | sub request { | ||||||||||||||||||||||||||
|
Subroutine
request: not called CC=13 SCAR=52.0
Pod
documented
| |||||||||||||||||||||||||||||
| 174 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 175 | 0 | ▶ | my @jmx_requests = $self->cfg('target') ? $self->_update_targets(@_) : @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | 0 | my $ua = $self->{ua}; | |||||||||||||||||||||||||||
| 177 | 0 | my $http_req = $self->_to_http_request(@jmx_requests); | |||||||||||||||||||||||||||
| 178 | 0 | ▶ | if ($self->{cfg}->{verbose}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 179 | 0 | print $http_req->as_string; | |||||||||||||||||||||||||||
| 180 | 0 | print "===========================================================\n"; | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | #print Dumper($http_req); | ||||||||||||||||||||||||||||
| 183 | 0 | my $http_resp = $ua->request($http_req); | |||||||||||||||||||||||||||
| 184 | 0 | my $json_resp = {}; | |||||||||||||||||||||||||||
| 185 | 0 | ▶ | if ($self->{cfg}->{verbose}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 186 | 0 | print $http_resp->as_string,"\n"; | |||||||||||||||||||||||||||
| 187 | 0 | print "===========================================================\n"; | |||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | 0 | eval { | |||||||||||||||||||||||||||
| 190 | 0 | $json_resp = from_json($http_resp->content()); | |||||||||||||||||||||||||||
| 191 | }; | ||||||||||||||||||||||||||||
| 192 | 0 | my $json_error = $@; | |||||||||||||||||||||||||||
| 193 | 0 | ▶ | if ($http_resp->is_error) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 194 | return JMX::Jmx4Perl::Response->new | ||||||||||||||||||||||||||||
| 195 | ( | ||||||||||||||||||||||||||||
| 196 | status => $http_resp->code, | ||||||||||||||||||||||||||||
| 197 | value => $json_error ? $http_resp->content : $json_resp, | ||||||||||||||||||||||||||||
| 198 | error => $json_error ? $self->_prepare_http_error_text($http_resp) : | ||||||||||||||||||||||||||||
| 199 | 0 | ▶ | ref($json_resp) eq "ARRAY" ? join "\n", map { $_->{error} } grep { $_->{error} } @$json_resp : $json_resp->{error}, | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 200 | stacktrace => ref($json_resp) eq "ARRAY" ? $self->_extract_stacktraces($json_resp) : $json_resp->{stacktrace}, | ||||||||||||||||||||||||||||
| 201 | 0 | ▶ | request => @jmx_requests == 1 ? $jmx_requests[0] : \@jmx_requests | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 202 | ); | ||||||||||||||||||||||||||||
| 203 | } elsif ($json_error) { | ||||||||||||||||||||||||||||
| 204 | # If is not an HTTP-Error and deserialization fails, then we | ||||||||||||||||||||||||||||
| 205 | # probably got a wrong URL and get delivered some server side | ||||||||||||||||||||||||||||
| 206 | # document (with HTTP code 200) | ||||||||||||||||||||||||||||
| 207 | 0 | my $e = $json_error; | |||||||||||||||||||||||||||
| 208 | 0 | $e =~ s/(.*)at .*?line.*$/$1/; | |||||||||||||||||||||||||||
| 209 | 0 | return JMX::Jmx4Perl::Response->new | |||||||||||||||||||||||||||
| 210 | ( | ||||||||||||||||||||||||||||
| 211 | status => 400, | ||||||||||||||||||||||||||||
| 212 | error => | ||||||||||||||||||||||||||||
| 213 | "Error while deserializing JSON answer (Wrong URL ?)\n" . $e, | ||||||||||||||||||||||||||||
| 214 | value => $http_resp->content | ||||||||||||||||||||||||||||
| 215 | ); | ||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | 0 | my @responses = ($self->_from_http_response($json_resp,@jmx_requests)); | |||||||||||||||||||||||||||
| 219 | 0 | ▶ | if (!wantarray && scalar(@responses) == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not wantarray and scalar @responses == 1
MC/DC0%0/2
not wantarray and scalar @responses == 1
not wantarray
scalar @responses == 1
Truth table0%0/3
not(A) and B
A= not wantarrayB= scalar @responses == 1
| |||||||||||||||||||||||||||||
| 220 | 0 | return shift @responses; | |||||||||||||||||||||||||||
| 221 | } else { | ||||||||||||||||||||||||||||
| 222 | 0 | return @responses; | |||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | =item $encrypted = $agent->encrypt($plain) | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | Encrypt a password which can be used in configuration files in order to | ||||||||||||||||||||||||||||
| 229 | obfuscate the clear text password. | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | =cut | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | 0 | ▶ | sub encrypt { | ||||||||||||||||||||||||||
|
Subroutine
encrypt: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 234 | 0 | return "[[" . &JMX::Jmx4Perl::Agent::UserAgent::encrypt(shift) . "]]"; | |||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | # Create an HTTP-Request for calling the server | ||||||||||||||||||||||||||||
| 239 | 0 | ▶ | sub _to_http_request { | ||||||||||||||||||||||||||
|
Subroutine
_to_http_request: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 240 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 241 | 0 | my @reqs = @_; | |||||||||||||||||||||||||||
| 242 | 0 | ▶ | if ($self->_use_GET_request(\@reqs)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 243 | # Old, rest-style | ||||||||||||||||||||||||||||
| 244 | 0 | my $url = $self->request_url($reqs[0]); | |||||||||||||||||||||||||||
| 245 | 0 | return HTTP::Request->new(GET => $url); | |||||||||||||||||||||||||||
| 246 | } else { | ||||||||||||||||||||||||||||
| 247 | 0 | ▶ | my $url = $self->cfg('url') || croak "No URL provided"; | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->cfg('url') || croak('No URL provided')
MC/DC0%0/1
$self->cfg('url') || croak('No URL provided')
$self->cfg('url')
Truth table0%0/2
A
A= $self->cfg('url')
| |||||||||||||||||||||||||||||
| 248 | 0 | ▶ | $url .= "/" unless $url =~ m|/$|; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 249 | 0 | my $request = HTTP::Request->new(POST => $url); | |||||||||||||||||||||||||||
| 250 | 0 | ▶ | my $content = to_json(@reqs > 1 ? \@reqs : $reqs[0], { convert_blessed => 1 }); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 251 | #print Dumper($reqs[0],$content); | ||||||||||||||||||||||||||||
| 252 | 0 | $request->content($content); | |||||||||||||||||||||||||||
| 253 | 0 | return $request; | |||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | 0 | ▶ | sub _use_GET_request { | ||||||||||||||||||||||||||
|
Subroutine
_use_GET_request: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 258 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 259 | 0 | my $reqs = shift; | |||||||||||||||||||||||||||
| 260 | 0 | ▶ | if (@$reqs == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 261 | 0 | my $req = $reqs->[0]; | |||||||||||||||||||||||||||
| 262 | # For proxy configs and explicite set POST request, get can not be | ||||||||||||||||||||||||||||
| 263 | # used | ||||||||||||||||||||||||||||
| 264 | 0 | ▶ | return 0 if defined($req->get("target")); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 265 | #print Dumper($req); | ||||||||||||||||||||||||||||
| 266 | 0 | for my $r ($req->method,$self->cfg('method')) { | |||||||||||||||||||||||||||
| 267 | 0 | ▶ | return lc($r) eq "get" if defined($r); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||
| 269 | # GET by default | ||||||||||||||||||||||||||||
| 270 | 0 | return 1; | |||||||||||||||||||||||||||
| 271 | } else { | ||||||||||||||||||||||||||||
| 272 | 0 | return 0; | |||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | # Create one or more response objects for a given request | ||||||||||||||||||||||||||||
| 277 | 0 | ▶ | sub _from_http_response { | ||||||||||||||||||||||||||
|
Subroutine
_from_http_response: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||
| 278 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 279 | 0 | my $json_resp = shift; | |||||||||||||||||||||||||||
| 280 | 0 | my @reqs = @_; | |||||||||||||||||||||||||||
| 281 | 0 | ▶ | if (ref($json_resp) eq "HASH") { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 282 | 0 | return JMX::Jmx4Perl::Response->new(%{$json_resp},request => $reqs[0]); | |||||||||||||||||||||||||||
| 283 | } elsif (ref($json_resp) eq "ARRAY") { | ||||||||||||||||||||||||||||
| 284 | 0 | ▶ | die "Internal: Number of request and responses doesn't match (",scalar(@reqs)," vs. ",scalar(@$json_resp) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 285 | unless scalar(@reqs) == scalar(@$json_resp); | ||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | 0 | my @ret = (); | |||||||||||||||||||||||||||
| 288 | 0 | ▶ | for (my $i=0;$i<@reqs;$i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 289 | 0 | ▶ | die "Internal: Not a hash --> ",$json_resp->[$i] unless ref($json_resp->[$i]) eq "HASH"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 290 | 0 | my $response = JMX::Jmx4Perl::Response->new(%{$json_resp->[$i]},request => $reqs[$i]); | |||||||||||||||||||||||||||
| 291 | 0 | push @ret,$response; | |||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||
| 293 | 0 | return @ret; | |||||||||||||||||||||||||||
| 294 | } else { | ||||||||||||||||||||||||||||
| 295 | 0 | ▶ | die "Internal: Not a hash nor an array but ",ref($json_resp) ? ref($json_resp) : $json_resp; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | # Update targets if not set in request. | ||||||||||||||||||||||||||||
| 300 | 0 | ▶ | sub _update_targets { | ||||||||||||||||||||||||||
|
Subroutine
_update_targets: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 301 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 302 | 0 | my @requests = @_; | |||||||||||||||||||||||||||
| 303 | 0 | my $target = $self->_clone_target; | |||||||||||||||||||||||||||
| 304 | 0 | for my $req (@requests) { | |||||||||||||||||||||||||||
| 305 | 0 | ▶ | $req->{target} = $target unless exists($req->{target}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 306 | # A request with existing but undefined target removes | ||||||||||||||||||||||||||||
| 307 | # any default | ||||||||||||||||||||||||||||
| 308 | 0 | ▶ | delete $req->{target} unless defined($req->{target}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||
| 310 | 0 | return @requests; | |||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | 0 | ▶ | sub _clone_target { | ||||||||||||||||||||||||||
|
Subroutine
_clone_target: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 314 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 315 | 0 | ▶ | die "Internal: No target set" unless $self->cfg('target'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 316 | 0 | my $target = { %{$self->cfg('target')} }; | |||||||||||||||||||||||||||
| 317 | 0 | ▶ | if ($target->{env}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 318 | 0 | $target->{env} = { %{$target->{env}}}; | |||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||
| 320 | 0 | return $target; | |||||||||||||||||||||||||||
| 321 | } | ||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | =item $url = $agent->request_url($request) | ||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | Generate the URL for accessing the java agent based on a given request. | ||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | =cut | ||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | 4 | ▶ | sub request_url { | ||||||||||||||||||||||||||
|
Subroutine
request_url: called CC=16 SCAR=40.1
Pod
documented
| |||||||||||||||||||||||||||||
| 330 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 331 | 4 | my $request = shift; | |||||||||||||||||||||||||||
| 332 | 4 | ▶ | my $url = $self->cfg('url') || croak "No base url given in configuration"; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->cfg('url') || croak('No base url given in configuration')
MC/DC0%0/1
$self->cfg('url') || croak('No base url given in configuration')
$self->cfg('url')
Truth table50%1/2
A
A= $self->cfg('url')
| |||||||||||||||||||||||||||||
| 333 | 4 | ▶ | $url .= "/" unless $url =~ m|/$|; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | 4 | my $type = $request->get("type"); | |||||||||||||||||||||||||||
| 336 | 4 | my $req = $type . "/"; | |||||||||||||||||||||||||||
| 337 | 4 | $req .= $self->_escape($request->get("mbean")); | |||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | 4 | ▶ | if ($type eq READ) { | ||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||
| 340 | 0 | $req .= "/" . $self->_escape($request->get("attribute")); | |||||||||||||||||||||||||||
| 341 | 0 | $req .= $self->_extract_path($request->get("path")); | |||||||||||||||||||||||||||
| 342 | } elsif ($type eq WRITE) { | ||||||||||||||||||||||||||||
| 343 | 0 | $req .= "/" . $self->_escape($request->get("attribute")); | |||||||||||||||||||||||||||
| 344 | 0 | $req .= "/" . $self->_escape($self->_null_escape($request->get("value"))); | |||||||||||||||||||||||||||
| 345 | 0 | $req .= $self->_extract_path($request->get("path")); | |||||||||||||||||||||||||||
| 346 | } elsif ($type eq LIST) { | ||||||||||||||||||||||||||||
| 347 | # The (URI escaped) colon after the <domain> must be transformed into a slash | ||||||||||||||||||||||||||||
| 348 | 4 | $req =~ s|%3A|/|i; | |||||||||||||||||||||||||||
| 349 | 4 | $req .= $self->_extract_path($request->get("path")); | |||||||||||||||||||||||||||
| 350 | } elsif ($type eq EXEC) { | ||||||||||||||||||||||||||||
| 351 | 0 | $req .= "/" . $self->_escape($request->get("operation")); | |||||||||||||||||||||||||||
| 352 | 0 | for my $arg (@{$request->get("arguments")}) { | |||||||||||||||||||||||||||
| 353 | # Array refs are sticked together via "," | ||||||||||||||||||||||||||||
| 354 | 0 | ▶ | my $a = ref($arg) eq "ARRAY" ? join ",",@{$arg} : $arg; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 355 | 0 | $req .= "/" . $self->_escape($self->_null_escape($a)); | |||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||
| 357 | } elsif ($type eq SEARCH) { | ||||||||||||||||||||||||||||
| 358 | # Nothing further to append. | ||||||||||||||||||||||||||||
| 359 | } | ||||||||||||||||||||||||||||
| 360 | # Squeeze multiple slashes | ||||||||||||||||||||||||||||
| 361 | 4 | $req =~ s|((?:!/)?/)/*|$1|g; | |||||||||||||||||||||||||||
| 362 | #print "R: $req\n"; | ||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | 4 | ▶ | if ($req =~ $INVALID_PATH_CHARS || $request->{use_query}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$req =~ /$INVALID_PATH_CHARS/ or $request->{"use_query"}
MC/DC0%0/2
$req =~ /$INVALID_PATH_CHARS/ or $request->{"use_query"}
$req =~ /$INVALID_PATH_CHARS/
$request->{"use_query"}
Truth table33%1/3
A or B
A= $req =~ /$INVALID_PATH_CHARS/B= $request->{"use_query"}
| |||||||||||||||||||||||||||||
| 365 | 0 | $req = "?p=$req"; | |||||||||||||||||||||||||||
| 366 | } | ||||||||||||||||||||||||||||
| 367 | 4 | my @params; | |||||||||||||||||||||||||||
| 368 | 4 | for my $k (@PARAMS) { | |||||||||||||||||||||||||||
| 369 | 16 | ▶ | push @params, $k . "=" . $request->get($k) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 370 | if $request->get($k); | ||||||||||||||||||||||||||||
| 371 | } | ||||||||||||||||||||||||||||
| 372 | 4 | ▶ | $req .= ($req =~ /\?/ ? "&" : "?") . join("&",@params) if @params; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 373 | 4 | return $url . $req; | |||||||||||||||||||||||||||
| 374 | } | ||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | # ============================================================================= | ||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | # Return an (optional) path which must already be escaped | ||||||||||||||||||||||||||||
| 381 | 4 | ▶ | sub _extract_path { | ||||||||||||||||||||||||||
|
Subroutine
_extract_path: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 382 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 383 | 4 | my $path = shift; | |||||||||||||||||||||||||||
| 384 | 4 | ▶ | return $path ? "/" . $path : ""; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 385 | } | ||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | # Escaping is simple: | ||||||||||||||||||||||||||||
| 389 | # ! --> !! | ||||||||||||||||||||||||||||
| 390 | # / --> !/ | ||||||||||||||||||||||||||||
| 391 | # It is not done by backslashes '\' since often they get magically get | ||||||||||||||||||||||||||||
| 392 | # translated into / when part of an URL | ||||||||||||||||||||||||||||
| 393 | 4 | ▶ | sub _escape { | ||||||||||||||||||||||||||
|
Subroutine
_escape: called CC=2 SCAR=8.3
| |||||||||||||||||||||||||||||
| 394 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 395 | 4 | my $input = shift; | |||||||||||||||||||||||||||
| 396 | 4 | ▶ | if ($self->cfg('legacy-escape')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 397 | # Pre 1.0 escaping: | ||||||||||||||||||||||||||||
| 398 | 0 | $input =~ s|(/+)|"/" . ('-' x length($1)) . "/"|eg; | |||||||||||||||||||||||||||
| 399 | 0 | $input =~ s|^/-|/^|; # The first slash needs to be escaped (first) | |||||||||||||||||||||||||||
| 400 | 0 | $input =~ s|-/$|+/|; # as well as last slash. They need a special | |||||||||||||||||||||||||||
| 401 | # escape, because two subsequent slashes get | ||||||||||||||||||||||||||||
| 402 | # squeezed to one on the server side | ||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||
| 404 | } else { | ||||||||||||||||||||||||||||
| 405 | # Simpler escaping since 1.0: | ||||||||||||||||||||||||||||
| 406 | 4 | $input =~ s/!/!!/g; | |||||||||||||||||||||||||||
| 407 | 4 | $input =~ s/\//!\//g; | |||||||||||||||||||||||||||
| 408 | } | ||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||
| 410 | 4 | return URI::Escape::uri_escape_utf8($input,"^A-Za-z0-9\-_.!~*'()/"); # Added "/" to | |||||||||||||||||||||||||||
| 411 | # default | ||||||||||||||||||||||||||||
| 412 | # set. See L<URI> | ||||||||||||||||||||||||||||
| 413 | } | ||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||
| 415 | # Escape empty and undef values so that they can be detangled | ||||||||||||||||||||||||||||
| 416 | # on the server side | ||||||||||||||||||||||||||||
| 417 | 0 | ▶ | sub _null_escape { | ||||||||||||||||||||||||||
|
Subroutine
_null_escape: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 418 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 419 | 0 | my $value = shift; | |||||||||||||||||||||||||||
| 420 | 0 | ▶ | if (!defined($value)) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 421 | 0 | return "[null]"; | |||||||||||||||||||||||||||
| 422 | } elsif (! length($value)) { | ||||||||||||||||||||||||||||
| 423 | 0 | return "\"\""; | |||||||||||||||||||||||||||
| 424 | } else { | ||||||||||||||||||||||||||||
| 425 | 0 | return $value; | |||||||||||||||||||||||||||
| 426 | } | ||||||||||||||||||||||||||||
| 427 | } | ||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||
| 429 | # Prepare some readable error text | ||||||||||||||||||||||||||||
| 430 | 0 | ▶ | sub _prepare_http_error_text { | ||||||||||||||||||||||||||
|
Subroutine
_prepare_http_error_text: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 431 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 432 | 0 | my $http_resp = shift; | |||||||||||||||||||||||||||
| 433 | 0 | my $content = $http_resp->content; | |||||||||||||||||||||||||||
| 434 | 0 | my $error = "Error while fetching ".$http_resp->request->uri." :\n\n" . $http_resp->status_line . "\n"; | |||||||||||||||||||||||||||
| 435 | 0 | chomp $content; | |||||||||||||||||||||||||||
| 436 | 0 | ▶ | if ($content && $content ne $http_resp->status_line) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$content and $content ne $http_resp->status_line
MC/DC0%0/2
$content and $content ne $http_resp->status_line
$content
$content ne $http_resp->status_line
Truth table0%0/3
A and B
A= $contentB= $content ne $http_resp->status_line
| |||||||||||||||||||||||||||||
| 437 | 0 | my $error .= "=" x length($http_resp->status_line) . "\n\n"; | |||||||||||||||||||||||||||
| 438 | 0 | my $short = substr($content,0,600); | |||||||||||||||||||||||||||
| 439 | 0 | ▶ | $error .= $short . (length($short) < length($content) ? "\n\n... [truncated] ...\n\n" : "") . "\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 440 | } | ||||||||||||||||||||||||||||
| 441 | 0 | return $error; | |||||||||||||||||||||||||||
| 442 | } | ||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||
| 444 | # Extract all stacktraces stored in the given array ref of json responses | ||||||||||||||||||||||||||||
| 445 | 0 | ▶ | sub _extract_stacktraces { | ||||||||||||||||||||||||||
|
Subroutine
_extract_stacktraces: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 446 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 447 | 0 | my $json_resp = shift; | |||||||||||||||||||||||||||
| 448 | 0 | my @ret = (); | |||||||||||||||||||||||||||
| 449 | 0 | for my $j (@$json_resp) { | |||||||||||||||||||||||||||
| 450 | 0 | ▶ | push @ret,$j->{stacktrace} if $j->{stacktrace}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 451 | } | ||||||||||||||||||||||||||||
| 452 | 0 | ▶ | return @ret ? (scalar(@ret) == 1 ? $ret[0] : \@ret) : undef; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 453 | } | ||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||
| 455 | =back | ||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||
| 457 | =cut | ||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||
| 459 | # =================================================================== | ||||||||||||||||||||||||||||
| 460 | # Specialized UserAgent for passing in credentials: | ||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||
| 462 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||
| 464 | This file is part of jmx4perl. | ||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||
| 466 | Jmx4perl is free software: you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 467 | it under the terms of the GNU General Public License as published by | ||||||||||||||||||||||||||||
| 468 | the Free Software Foundation, either version 2 of the License, or | ||||||||||||||||||||||||||||
| 469 | (at your option) any later version. | ||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||
| 471 | jmx4perl is distributed in the hope that it will be useful, | ||||||||||||||||||||||||||||
| 472 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||||||||||||||||||||
| 473 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||||||||||||||||||||||||||
| 474 | GNU General Public License for more details. | ||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||
| 476 | You should have received a copy of the GNU General Public License | ||||||||||||||||||||||||||||
| 477 | along with jmx4perl. If not, see <http://www.gnu.org/licenses/>. | ||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||
| 479 | A commercial license is available as well. Please contact roland@cpan.org for | ||||||||||||||||||||||||||||
| 480 | further details. | ||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||
| 482 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||
| 484 | roland@cpan.org | ||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||
| 486 | =cut | ||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||
| 488 | 3 | 1; | |||||||||||||||||||||||||||