Sling/JsonQueryServlet.pm

filter: Statementstmt 76.8% 73 / 95 Branchbran 56.2% 9 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 84.2% 16 / 19 Podpod 55.5% 5 / 9 totaltotal 74.1% 103 / 139 CC 9 SCAR 9.5
CC
9
Cov
74%
all_nodes
6.0
run
5.6
new
3.0
CRAP 2.6
« Sling/URL.pm ↑ Summary Sling/GroupMemberUtil.pm »
1 #!/usr/bin/perl -w
2  
3 package Apache::Sling::JsonQueryServlet;
4  
5 1 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Getopt::Long qw(:config bundling);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Apache::Sling;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Apache::Sling::JsonQueryServletUtil;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Apache::Sling::Print;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Apache::Sling::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 require Exporter;
16  
17 1 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 our @EXPORT_OK = qw(command_line);
20  
21 1 our $VERSION = '0.27';
22  
23 #{{{sub new
24 2 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
25 2 my ( $class, $authn, $verbose, $log ) = @_;
26 2 ▶ if ( !defined $authn ) { croak 'no authn provided!'; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $authn) 1 1 2
27 1 my $response;
28 1 ▶ $verbose = ( defined $verbose ? $verbose : 0 );
Branch: 1 branch
BranchTrueFalseTotal
defined $verbose ? : 1 0 1
29 my $json_query_servlet = {
30 1 BaseURL => ${$authn}->{'BaseURL'},
31 Authn => $authn,
32 Message => q{},
33 Response => \$response,
34 Verbose => $verbose,
35 Log => $log
36 };
37 1 bless $json_query_servlet, $class;
38 1 return $json_query_servlet;
39 }
40  
41 #}}}
42  
43 #{{{sub set_results
44 1 ▶sub set_results {
Subroutine set_results: called CC=1 SCAR=0.0
Pod documented
45 1 my ( $json_query_servlet, $message, $response ) = @_;
46 1 $json_query_servlet->{'Message'} = $message;
47 1 $json_query_servlet->{'Response'} = $response;
48 1 return 1;
49 }
50  
51 #}}}
52  
53 #{{{sub all_nodes
54 0 ▶sub all_nodes {
Subroutine all_nodes: not called CC=2 SCAR=17.9
Pod documented
55 0 my ($json_query_servlet) = @_;
56 my $res = Apache::Sling::Request::request(
57 \$json_query_servlet,
58 Apache::Sling::JsonQueryServletUtil::all_nodes_setup(
59 0 $json_query_servlet->{'BaseURL'}
60 )
61 );
62 0 my $success = Apache::Sling::JsonQueryServletUtil::all_nodes_eval($res);
63 my $message = (
64 $success
65 0 ▶ ? ${$res}->content
Branch: 1 branch
BranchTrueFalseTotal
$success ? : 0 0 0
66 : "Problem fetching all nodes"
67 );
68 0 $json_query_servlet->set_results( "$message", $res );
69 0 return $success;
70 }
71  
72 #}}}
73  
74 #{{{ sub command_line
75 0 ▶sub command_line {
Subroutine command_line: not called CC=1 SCAR=6.9
Pod undocumented
76 0 my ( $json_query_servlet, @ARGV ) = @_;
77 0 my $sling = Apache::Sling->new;
78 0 my $config = $json_query_servlet->config( $sling, @ARGV );
79 0 return $json_query_servlet->run( $sling, $config );
80 }
81  
82 #}}}
83  
84 #{{{sub config
85  
86 1 ▶sub config {
Subroutine config: called CC=2 SCAR=7.0
Pod documented
87 1 my ( $json_query_servlet, $sling, @ARGV ) = @_;
88  
89 1 my $json_query_servlet_config = $json_query_servlet->config_hash( $sling, @ARGV );
90  
91 1 ▶ GetOptions(
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions($json_query_servlet_config, "auth=s", "help|?", "log|L=s", "man|M", "pass|p=s", "threads|t=s", "url|U=s", "user|u=s", "verbose|v+", "all_nodes|a") or $json_query_servlet->help 1 0 1
92 $json_query_servlet_config, 'auth=s',
93 'help|?', 'log|L=s',
94 'man|M', 'pass|p=s',
95 'threads|t=s', 'url|U=s',
96 'user|u=s', 'verbose|v+',
97 'all_nodes|a'
98 ) or $json_query_servlet->help();
99  
100 1 return $json_query_servlet_config;
101 }
102  
103 #}}}
104  
105 #{{{sub config_hash
106  
107 1 ▶sub config_hash {
Subroutine config_hash: called CC=1 SCAR=0.0
Pod undocumented
108 1 my ( $json_query_servlet, $sling, @ARGV ) = @_;
109 1 my $all_nodes;
110  
111 my %json_query_servlet_config = (
112 'auth' => \$sling->{'Auth'},
113 'help' => \$sling->{'Help'},
114 'log' => \$sling->{'Log'},
115 'man' => \$sling->{'Man'},
116 'pass' => \$sling->{'Pass'},
117 'threads' => \$sling->{'Threads'},
118 'url' => \$sling->{'URL'},
119 'user' => \$sling->{'User'},
120 1 'verbose' => \$sling->{'Verbose'},
121 'all_nodes' => \$all_nodes
122 );
123  
124 1 return \%json_query_servlet_config;
125 }
126  
127 #}}}
128  
129 #{{{ sub help
130 1 ▶sub help {
Subroutine help: called CC=1 SCAR=0.0
Pod undocumented
131  
132 1 print <<"EOF";
133 Usage: perl $0 [-OPTIONS [-MORE_OPTIONS]] [--] [PROGRAM_ARG1 ...]
134 The following options are accepted:
135  
136 --all_nodes or -a - Return a JSON representation of all nodes in the system.
137 --auth (type) - Specify auth type. If ommitted, default is used.
138 --help or -? - view the script synopsis and options.
139 --log or -L (log) - Log script output to specified log file.
140 --man or -M - view the full script documentation.
141 --pass or -p (password) - Password of user performing json queries.
142 --threads or -t (threads) - Used with -A, defines number of parallel
143 processes to have running through file.
144 --url or -U (URL) - URL for system being tested against.
145 --user or -u (username) - Name of user to perform queries as.
146 --verbose or -v or -vv or -vvv - Increase verbosity of output.
147  
148 Options may be merged together. -- stops processing of options.
149 Space is not required between options and their arguments.
150 For full details run: perl $0 --man
151 EOF
152  
153 1 return 1;
154 }
155  
156 #}}}
157  
158 #{{{ sub man
159 0 ▶sub man {
Subroutine man: not called CC=1 SCAR=6.9
Pod undocumented
160  
161 0 my ($json_query_servlet) = @_;
162  
163 0 print <<'EOF';
164 json_query_servlet perl script. Provides a means of querying content in sling
165 from the command line. The script also acts as a reference implementation for
166 the JSON Query Servlet perl library.
167  
168 EOF
169  
170 0 $json_query_servlet->help();
171  
172 0 print <<"EOF";
173 Example Usage
174  
175 * Query all nodes in the system:
176  
177 perl $0 -U http://localhost:8080 -a -u admin -p admin
178 EOF
179  
180 0 return 1;
181 }
182  
183 #}}}
184  
185 #{{{sub run
186 2 ▶sub run {
Subroutine run: called CC=5 SCAR=17.2
Pod documented
187 2 my ( $json_query_servlet, $sling, $config ) = @_;
188 2 ▶ if ( !defined $config ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $config) 1 1 2
189 1 croak 'No json query servlet config supplied!';
190 }
191 1 $sling->check_forks;
192 1 ${ $config->{'remote'} } =
193 1 Apache::Sling::URL::strip_leading_slash( ${ $config->{'remote'} } );
194 1 ${ $config->{'remote-source'} } = Apache::Sling::URL::strip_leading_slash(
195 1 ${ $config->{'remote-source'} } );
196  
197 1 my $authn = new Apache::Sling::Authn( \$sling );
198 1 $authn->login_user();
199 1 my $success = 1;
200 0 ▶ if ( $sling->{'Help'} ) { $json_query_servlet->help(); }
Branch: 3 branches
BranchTrueFalseTotal
if ($sling->{'Help'}) { } 0 1 1
elsif ($sling->{'Man'}) { } 0 1 1
elsif (defined ${$$config{'all_nodes'};}) { } 0 1 1
201 0 elsif ( $sling->{'Man'} ) { $json_query_servlet->man(); }
202 1 elsif ( defined ${ $config->{'all_nodes'} } ) {
203 $json_query_servlet =
204 new Apache::Sling::JsonQueryServlet( \$authn, $sling->{'Verbose'},
205 0 $sling->{'Log'} );
206 0 $success = $json_query_servlet->all_nodes();
207 }
208 else {
209 1 $json_query_servlet->help();
210 1 return 1;
211 }
212 0 Apache::Sling::Print::print_result($json_query_servlet);
213 0 return $success;
214 }
215  
216 #}}}
217  
218 1 1;
219  
220 __END__
« Sling/URL.pm ↑ Summary Sling/GroupMemberUtil.pm »