source: main/trunk/greenstone2/perllib/servercontrol.pm

Last change on this file was 36114, checked in by kjdon, 2 years ago

allow passing in of oaiserver_name to activate.pl, which then gets passed to servercontrol - use this instead of 'oaiserver' to activate/deactivate a collection in the oai sevrer

File size: 28.0 KB
Line 
1#############################################################################
2#
3# activate.pm -- functions to get the GS library URL, ping the library URL,
4# activate and deactivate a collection.
5#
6# A component of the Greenstone digital library software
7# from the New Zealand Digital Library Project at the
8# University of Waikato, New Zealand.
9#
10# Copyright (C) 1999 New Zealand Digital Library Project
11#
12# This program is free software; you can redistribute it and/or modify
13# it under the terms of the GNU General Public License as published by
14# the Free Software Foundation; either version 2 of the License, or
15# (at your option) any later version.
16#
17# This program is distributed in the hope that it will be useful,
18# but WITHOUT ANY WARRANTY; without even the implied warranty of
19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20# GNU General Public License for more details.
21#
22# You should have received a copy of the GNU General Public License
23# along with this program; if not, write to the Free Software
24# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25#
26###############################################################################
27
28package servercontrol;
29
30
31use strict;
32no strict 'refs'; # allow filehandles to be variables and vice versa
33no strict 'subs'; # allow barewords (eg STDERR) as function arguments
34
35# Greenstone includes
36use printusage;
37use parse2;
38
39
40# The perl library imports below are used by deprecated methods config_old(), is_URL_active() and pingHost()
41# If the following library imports are not supported by your perl installation, comment out these
42# imports and move the methods config_old(), is_URL_active() and pingHost() out to a temporary file.
43use HTTP::Response;
44use LWP::Simple qw($ua !head); # import useragent object as $ua from the full LWP to use along with LWP::Simple
45 # don't import LWP::Simple's head function by name since it can conflict with CGI:head())
46#use CGI qw(:standard); # then only CGI.pm defines a head()
47use Net::Ping;
48use URI;
49
50
51sub new
52{
53 my $class = shift(@_);
54
55 my ($qualified_collection, $site, $verbosity, $build_dir, $index_dir, $collect_dir, $library_url, $library_name, $oaiserver_name) = @_;
56
57 # library_url: to be specified on the cmdline if not using a GS-included web server
58 # the GSDL_LIBRARY_URL env var is useful when running cmdline buildcol.pl in the linux package manager versions of GS3
59
60 my $self = {'build_dir' => $build_dir,
61 'index_dir' => $index_dir,
62 'collect_dir' => $collect_dir,
63 'site' => $site,
64 'qualified_collection' => $qualified_collection,
65 #'is_persistent_server' => undef,
66 'library_url' => $library_url || $ENV{'GSDL_LIBRARY_URL'} || undef, # to be specified on the cmdline if not using a GS-included web server
67 'library_name' => $library_name,
68 'oaiserver_name' => $oaiserver_name || "oaiserver",
69 #'gs_mode' => "gs2",
70 'verbosity' => $verbosity || 2
71 };
72
73 if ((defined $site) && ($site ne "")) { # GS3
74 $self->{'gs_mode'} = "gs3";
75 } else {
76 $self->{'gs_mode'} = "gs2";
77 }
78
79 return bless($self, $class);
80}
81
82## TODO: gsprintf to $self->{'out'} in these 2 print functions
83## See buildcolutils.pm new() for setting up $out
84
85sub print_task_msg {
86 my $self = shift(@_);
87 my ($task_msg, $verbosity_setting) = @_;
88
89 $verbosity_setting = $self->{'verbosity'} unless $verbosity_setting;
90 #$verbosity_setting = 1 unless defined $verbosity;
91 if($verbosity_setting >= 1) {
92 print STDERR "\n";
93 print STDERR "************************\n";
94 print STDERR "* $task_msg\n";
95 print STDERR "************************\n";
96 }
97}
98
99# Prints messages if the verbosity is right. Does not add new lines.
100sub print_msg {
101 my $self = shift(@_);
102 my ($msg, $min_verbosity, $verbosity_setting) = @_;
103
104 # only display error messages if the current
105 # verbosity setting >= the minimum verbosity level
106 # needed for that message to be displayed.
107
108 $verbosity_setting = $self->{'verbosity'} unless defined $verbosity_setting;
109 $min_verbosity = 1 unless defined $min_verbosity;
110 if($verbosity_setting >= $min_verbosity) { # by default display all 1 messages
111 print STDERR "$msg";
112 }
113}
114
115# Method to send a command to a GS2 or GS3 library_URL
116# the commands used in this script can be activate, deactivate, ping,
117# and is-persistent (is-persistent only implemented for GS2).
118sub config {
119 my $self = shift(@_);
120 my ($command, $check_message_against_regex, $expected_error_code, $silent, $oai_servlet) = @_;
121
122 my $library_url = $self->get_library_URL(); #$self->{'library_url'};
123 if($oai_servlet) { # if asked to contact the oaiserver servlet, then
124 # replace the library servlet name with oaiserver servlet name
125 $library_url =~ s@([^/]*?)$@$oai_servlet@;
126 }
127
128 # Gatherer.java's configGS3Server doesn't use the site variable
129 # so we don't have to either
130
131 # for GS2, getting the HTTP status isn't enough, we need to read the output
132 # since this is what CollectionManager.config() stipulates.
133 # Using LWP::UserAgent::get($url) for this
134
135 if(!defined $library_url) {
136 return 0;
137 }
138 else {
139 # ampersands need to be escaped
140 # - with single quotes around it for linux for the cmd to run in bash subshell
141 # - with a ^ before it on windows for the cmd to run in a DOS prompt subshell
142 # - or the entire wget command should be nested in double quotes (single quotes don't work on windows)
143 my $wgetCommand = $command;
144
145 my $wget_file_path = &FileUtils::filenameConcatenate($ENV{'GSDLHOME'}, "bin", $ENV{'GSDLOS'}, "wget");
146 my $tmpfilename = &util::get_tmp_filename(".html"); # random file name with html extension in tmp location in which we'll store the HTML page retrieved by wget
147
148 # https://www.gnu.org/software/wget/manual/wget.html
149 # output-document set to - (STDOUT), so page is streamed to STDOUT
150 # timeout: 5 seconds, tries: 1
151 # wget sends status information and response code to STDERR, so redirect it to STDOUT
152 # Searching for "perl backtick operator redirect stderr to stdout":
153 # http://www.perlmonks.org/?node=How%20can%20I%20capture%20STDERR%20from%20an%20external%20command%3F
154 ##$wgetCommand = "\"$wget_file_path\" --spider -T 5 -t 1 \"$library_url$wgetCommand\" 2>&1"; # won't save page
155 #$wgetCommand = "\"$wget_file_path\" --output-document=- -T 5 -t 1 \"$library_url$wgetCommand\" 2>&1"; # THIS CAN MIX UP STDERR WITH STDOUT IN THE VERY LINE WE REGEX TEST AGAINST EXPECTED OUTPUT!!
156 $wgetCommand = "\"$wget_file_path\" --output-document=\"$tmpfilename\" -T 5 -t 1 \"$library_url$wgetCommand\" 2>&1"; # keep stderr (response code, response_content) separate from html page content
157
158 ##print STDERR "@@@@ $wgetCommand\n";
159
160 my $response_content = "";
161 my $response_code = undef;
162 #my $response_content = `$wgetCommand`; # Dr Bainbridge advises against using backticks for running a process. If capturing std output, use open():
163 if (open(PIN, "$wgetCommand |")) {
164 while (defined (my $perl_output_line = <PIN>)) {
165 $response_content = $response_content . $perl_output_line;
166 }
167 close(PIN);
168 } else {
169 print STDERR "servercontrol.pm::config() failed to run $wgetCommand\n";
170 }
171
172
173 my @lines = split( /\n/, $response_content );
174 foreach my $line (@lines) {
175 #print STDERR "@@@@ LINE: $line\n";
176 if($line =~ m@failed: Connection timed out.$@) { # linux
177 $response_code = "failed: Connection timed out.";
178 last; # break keyword in perl = last
179 }
180 elsif($line =~ m@Giving up.$@) { # windows (unless -T 5 -t 1 is not passed in)
181 $response_code = "failed: Giving up.";
182 last; # break keyword in perl = last
183 }
184 elsif($line =~ m@failed: Connection refused.$@) {
185 $response_code = "failed: Connection refused.";
186 last; # break keyword in perl = last
187 }
188 elsif($line =~ m@HTTP request sent, @) {
189 $response_code = $line;
190 $response_code =~ s@[^\d]*(.*)$@$1@;
191 last;
192 }
193 }
194
195 if($command =~ m@ping@ && $response_code =~ m@failed: (Connection refused|Giving up)@) {
196 # server not running
197 $self->print_msg("*** Server not running. $library_url$command\n", 3);
198 &FileUtils::removeFiles($tmpfilename); # get rid of the ping response's temporary html file we downloaded
199 return 0;
200 }
201 if($response_code && (index($response_code, "200") != -1)) {
202 $self->print_msg("*** Command $library_url$command\n", 3);
203 $self->print_msg("*** HTTP Response Status: $response_code - Complete.", 3);
204
205 # check the page content is as expected
206 #my $resultstr = $response_content;
207
208 # The following file reading section is a candidate to use FileUtils::readUTF8File()
209 # in place of calling sysread() directly. But only if we can reason we'd be working with UTF8
210 open(FIN,"<$tmpfilename") or die "servercontrol.pm: Unable to open $tmpfilename to read ping response page...ERROR: $!\n";
211 my $resultstr;
212 # Read in the entire contents of the file in one hit
213 ##sysread(FIN, $resultstr, -s FIN);
214 # GLI GBuildProgressMonitor.hasSignalledStop() removes any lines that start with elements it doesn't recognise
215 # but we want to see the lines with HTML tags as they are the most interesting: they embed success/failure messages
216 # So read line by line from the downloaded web page and replace <> with [] so that GLI can display these lines too
217 while (my $line = <FIN>) {
218 chomp $line;
219 $line =~ s@<@[@g;
220 $line =~ s@>@]@g;
221 $resultstr .= "$line\n";
222 }
223 close(FIN);
224 &FileUtils::removeFiles($tmpfilename); # get rid of the ping response's temporary html file we downloaded
225
226
227 #$resultstr =~ s@.*gs_content\"\>@@s; ## only true for default library servlet
228 #$resultstr =~ s@</div>.*@@s;
229 if($resultstr =~ m/$check_message_against_regex/) {
230 $self->print_msg(" Response as expected.\n", 3);
231 $self->print_msg("@@@@@@ Got result:\n$resultstr\n", 4);
232 return 1;
233 } else {
234 # if we expect the collection to be inactive, then we'd be in silent mode: if so,
235 # don't print out the "ping did not succeed" response, but print out any other messages
236
237 # So we only suppress the ping col "did not succeed" response if we're in silent mode
238 # But if any message other than ping "did not succeed" is returned, we always print it
239 if($resultstr !~ m/did not succeed/ || !$silent) {
240 $self->print_msg("\n\tBUT: command $library_url$command response UNEXPECTED.\n", 3);
241 $self->print_msg("*** Got message:\n$response_content.\n", 4);
242 $self->print_msg("*** Got result:\n$resultstr\n", 3);
243 }
244 return 0; # ping on a collection may "not succeed."
245 }
246 }
247 elsif($response_code && $response_code =~ m@^(4|5)\d\d@) { # client side errors start with 4xx, server side with 5xx
248 # check the page content is as expected
249 if(defined $expected_error_code && $response_code =~ m@^$expected_error_code@) {
250 $self->print_msg(" Response status $response_code as expected.\n", 3);
251 } else {
252 $self->print_msg("*** Command $library_url$command\n");
253 $self->print_msg("*** Unexpected error type 1. HTTP Response Status: $response_code - Failed.\n");
254 }
255 return 0; # return false, since the response_code was an error, expected or not
256 }
257 else { # also if response_code is still undefined, as can happen with connection timing out
258 $self->print_msg("*** Command $library_url$command\n");
259 if(defined $response_code) {
260 $self->print_msg("*** Unexpected error type 2. HTTP Response Status: $response_code - Failed.\n");
261 } else {
262 $self->print_msg("*** Unexpected error type 3. Failed:\n\n$response_content\n\n");
263 }
264 return 0;
265 }
266 #print STDERR "********** WgetCommand: $wgetCommand\n\n";
267 #print STDERR "********** Response_content:\n$response_content\n\n";
268 #print STDERR "********** Response_CODE: $response_code\n";
269
270 }
271}
272
273sub deactivate_collection {
274 my $self = shift(@_);
275
276 my $gs_mode = $self->{'gs_mode'};
277 my $qualified_collection = $self->{'qualified_collection'};
278
279 if($gs_mode eq "gs2") {
280 my $DEACTIVATE_COMMAND = "?a=config&cmd=release-collection&c=";
281 my $check_message_against_regex = q/configured release-collection/;
282 $self->config($DEACTIVATE_COMMAND.$qualified_collection, $check_message_against_regex);
283 }
284 elsif ($gs_mode eq "gs3") {
285 $self->print_msg("\t- Main library servlet\n");
286 my $DEACTIVATE_COMMAND = "?excerptid=gs_content&a=s&sa=d&st=collection&sn=";
287 my $check_message_against_regex = "collection: $qualified_collection deactivated";
288 $self->config($DEACTIVATE_COMMAND.$qualified_collection, $check_message_against_regex);
289
290 # and deactivate the collection on OAIserver url too.
291 # NOTE: if it's not an OAI collection, then the message that the collection is "not enabled for OAI" is EXPECTED. Another possible valid outcome.
292 $self->print_msg("\t- OAI servlet\n");
293 $DEACTIVATE_COMMAND = "?deactivate=";
294 $check_message_against_regex = "(collection\: $qualified_collection deactivated|collection\: $qualified_collection is not enabled for OAI.)";
295 $self->config($DEACTIVATE_COMMAND.$qualified_collection, $check_message_against_regex, undef, undef, $self->{'oaiserver_name'});
296 }
297}
298
299sub activate_collection {
300 my $self = shift(@_);
301
302 my $gs_mode = $self->{'gs_mode'};
303 my $qualified_collection = $self->{'qualified_collection'};
304
305 if($gs_mode eq "gs2") {
306 my $ACTIVATE_COMMAND = "?a=config&cmd=add-collection&c=";
307 my $check_message_against_regex = q/configured add-collection/;
308 $self->config($ACTIVATE_COMMAND.$qualified_collection, $check_message_against_regex);
309 }
310 elsif ($gs_mode eq "gs3") {
311 $self->print_msg("\t- Main library servlet\n");
312 my $ACTIVATE_COMMAND = "?excerptid=gs_content&a=s&sa=a&st=collection&sn=";
313 my $check_message_against_regex = "collection: $qualified_collection activated";
314 $self->config($ACTIVATE_COMMAND.$qualified_collection, $check_message_against_regex);
315
316 # and activate the collection on OAIserver url too.
317 # NOTE: if it's not an OAI collection, then the message that the collection is "not enabled for OAI" is EXPECTED. Another possible valid outcome.
318 $self->print_msg("\t- OAI servlet\n");
319 $ACTIVATE_COMMAND = "?activate=";
320 $check_message_against_regex = "(collection\: $qualified_collection activated|collection\: $qualified_collection is not enabled for OAI.)";
321 $self->config($ACTIVATE_COMMAND.$qualified_collection, $check_message_against_regex, undef, undef, $self->{'oaiserver_name'});
322 }
323}
324
325sub ping {
326 my $self = shift(@_);
327 my $command = shift(@_);
328 my $silent = shift(@_);
329
330 # If the GS server is not running, we *expect* to see a "500" status code.
331 # If the GS server is running, then "Ping" ... "succeeded" is expected on success.
332 # When pinging an inactive collection, it will say it did "not succeed". This is
333 # a message of interest to return.
334 my $check_responsemsg_against_regex = q/(succeeded)/;
335 my $expected_error_code = 500;
336
337 $self->print_msg("*** COMMAND WAS: |$command| ***\n", 4);
338
339 return $self->config($command, $check_responsemsg_against_regex, $expected_error_code, $silent);
340}
341
342# send a pingaction to the GS library. General server-level ping.
343sub ping_library {
344 my $self = shift(@_);
345
346 my $gs_mode = $self->{'gs_mode'};
347
348 my $command = "";
349 if($gs_mode eq "gs2") {
350 $command = "?a=ping";
351 }
352 elsif ($gs_mode eq "gs3") {
353 $command = "?a=s&sa=ping&excerptid=gs_content";
354 }
355 return $self->ping($command);
356}
357
358
359# send a pingaction to a collection in GS library to check if it's active
360sub ping_library_collection {
361 my $self = shift(@_);
362 my $silent = shift(@_);
363
364 my $gs_mode = $self->{'gs_mode'};
365 my $qualified_collection = $self->{'qualified_collection'};
366
367 my $command = "";
368 if($gs_mode eq "gs2") {
369 $command = "?a=ping&c=$qualified_collection";
370 }
371 elsif ($gs_mode eq "gs3") {
372 $command = "?a=s&sa=ping&st=collection&sn=$qualified_collection&excerptid=gs_content";
373 }
374 return $self->ping($command, $silent);
375}
376
377# return true if server is persistent, by calling is-persistent on library_url
378# this is only for GS2, since the GS3 server is always persistent
379sub is_persistent {
380 my $self = shift(@_);
381
382 if($self->{'gs_mode'} eq "gs3") { # GS3 server is always persistent
383 return 1;
384 }
385
386 my $command = "?a=is-persistent";
387 my $check_responsemsg_against_regex = q/true/; # isPersistent: true versus isPersistent: false
388 return $self->config($command, $check_responsemsg_against_regex);
389}
390
391sub set_library_URL {
392 my $self = shift(@_);
393 my $library_url = shift(@_);
394 $self->{'library_url'} = $library_url;
395}
396
397sub get_library_URL {
398 my $self = shift(@_);
399
400 # For web servers that are external to a Greenstone installation,
401 # the user can pass in their web server's library URL.
402 if($self->{'library_url'}) {
403 return $self->{'library_url'};
404 }
405
406 # For web servers included with GS (like tomcat for GS3 and server.exe
407 # and apache for GS2), we work out the library URL:
408 my ($gs_mode, $lib_name); # gs_mode can be gs3 or gs2, lib_name is the custom servlet name
409 $gs_mode = $self->{'gs_mode'};
410 $lib_name = $self->{'library_name'};
411
412 # If we get here, we are dealing with a server included with GS.
413 # For GS3, we ask ant for the library URL.
414 # For GS2, we derive the URL from the llssite.cfg file.
415
416 # note that unless we pass in $get_public_url=1, we now get the local http URL
417 # by default (e.g. http://127.0.0.1:httpPort/greenstone/library)
418 my $url = &util::get_full_greenstone_url_prefix($gs_mode, $lib_name); # found largely identical method copied
419 # into util.pm. Don't want duplicates, so calling that from here.
420
421 # either the url is still undef or it is now set
422 #print STDERR "\n@@@@@ final URL:|$url|\n" if $url;
423 #print STDERR "\n@@@@@ URL still undef\n" if !$url;
424
425 if (defined $url) {
426 $self->{'library_url'} = $url;
427 }
428
429 return $url;
430}
431
432sub do_deactivate {
433 my $self = shift(@_);
434
435 # 1. Get library URL
436
437 # For web servers that are external to a Greenstone installation,
438 # the user can pass in their web server's library URL.
439 # For web servers included with GS (like tomcat for GS3 and server.exe
440 # and apache for GS2), we work out the library URL:
441
442 # Can't do $self->{'library_url'}, because it may not yet be set
443 my $library_url = $self->get_library_URL(); # returns undef if no valid server URL
444
445 if(!defined $library_url) { # undef if no valid server URL
446 return; # can't do any deactivation without a valid server URL
447 }
448
449 my $is_persistent_server = $self->{'is_persistent_server'};
450 my $qualified_collection = $self->{'qualified_collection'};
451
452 # CollectionManager's installCollection phase in GLI
453 # 2. Ping the library URL, and if it's a persistent server and running, release the collection
454
455 $self->print_msg("Pinging $library_url\n");
456 if ($self->ping_library()) { # server running
457
458 # server is running, so release the collection if
459 # the server is persistent and the collection is active
460
461 # don't need to work out persistency of server more than once, since the libraryURL hasn't changed
462 if (!defined $is_persistent_server) {
463 $self->print_msg("Checking if Greenstone server is persistent\n");
464 $is_persistent_server = $self->is_persistent();
465 $self->{'is_persistent_server'} = $is_persistent_server;
466 }
467
468 if ($is_persistent_server) { # only makes sense to issue activate and deactivate cmds to a persistent server
469
470 $self->print_msg("Checking if the collection $qualified_collection is already active\n");
471 my $collection_active = $self->ping_library_collection();
472
473 if ($collection_active) {
474 $self->print_msg("De-activating collection $qualified_collection\n");
475 $self->deactivate_collection();
476 }
477 else {
478 $self->print_msg("Collection is not active => No need to deactivate\n");
479 }
480 }
481 else {
482 $self->print_msg("Server is not persistent => No need to deactivate collection\n");
483 }
484 }
485 else {
486 $self->print_msg("No response to Ping => Taken to mean server is not running\n");
487 }
488
489 return $is_persistent_server;
490}
491
492sub do_activate {
493 my $self = shift @_;
494
495 my $library_url = $self->get_library_URL(); # Can't do $self->{'library_url'}; as it may not be set yet
496
497 if(!defined $library_url) { # undef if no valid server URL
498 return; # nothing to activate if without valid server URL
499 }
500
501 my $is_persistent_server = $self->{'is_persistent_server'};
502 my $qualified_collection = $self->{'qualified_collection'};
503
504 $self->print_msg("Pinging $library_url\n");
505 if ($self->ping_library()) { # server running
506
507 # don't need to work out persistency of server more than once, since the libraryURL hasn't changed
508 if (!defined $is_persistent_server) {
509 $self->print_msg("Checking if Greenstone server is persistent\n");
510 $is_persistent_server = $self->is_persistent();
511 $self->{'is_persistent_server'} = $is_persistent_server;
512 }
513
514 if ($is_persistent_server) { # persistent server, so can try activating collection
515
516 $self->print_msg("Checking if the collection $qualified_collection is not already active\n");
517
518 # Since we could have deactivated the collection at this point,
519 # it is likely that it is not yet active. When pinging the collection
520 # a "ping did not succeed" message is expected, therefore tell the ping
521 # to proceed silently
522 my $silent = 1;
523 my $collection_active = $self->ping_library_collection($silent);
524
525 if (!$collection_active) {
526 $self->print_msg(" Collection is not active.\n");
527 $self->print_msg("Activating collection $qualified_collection\n");
528 $self->activate_collection();
529
530 # unless an error occurred, the collection should now be active:
531 $collection_active = $self->ping_library_collection(); # not silent if ping did not succeed
532 if(!$collection_active) {
533 $self->print_msg("ERROR: collection $qualified_collection did not get activated\n");
534 $self->print_msg(" (if the collection's not in the default library, you must pass\n");
535 $self->print_msg(" in the -library_name flag with the correct name of the library.)\n");
536 }
537 }
538 else {
539 $self->print_msg("Collection is already active => No need to activate\n");
540 }
541 }
542 else {
543 $self->print_msg("Server is not persistent => No need to activate collection\n");
544 }
545 }
546 else {
547 $self->print_msg("No response to Ping => Taken to mean server is not running\n");
548 }
549
550 return $is_persistent_server;
551}
552
553
554#########################################################
555### UNUSED METHODS - CAN BE HANDY
556
557
558# This method uses the perl libraries we're advised to use in place of wget for pinging and retrieving web
559# pages. The problem is that not all perl installations may support these libraries. So we now use the new
560# config() method further above, which uses the wget included in Greenstone binary installations.
561# If the library imports at page top conflict, comment out those imports and move the methods config_old(),
562# is_URL_active() and pingHost() out to a temporary file.
563#
564# If for some reason you can't use wget, then rename the config() method to config_old(), and rename the
565# method below to config() and things should work as before.
566sub config_old {
567 my $self = shift(@_);
568 my ($command, $check_message_against_regex, $expected_error_code, $silent, $oai_servlet) = @_;
569
570 my $library_url = $self->get_library_URL(); #$self->{'library_url'};
571 if($oai_servlet) { # if asked to contact the oaiserver servlet, then
572 # replace the library servlet name with oaiserver servlet name
573 $library_url =~ s@([^/]*?)$@$oai_servlet@;
574 }
575
576 # Gatherer.java's configGS3Server doesn't use the site variable
577 # so we don't have to either
578
579 # for GS2, getting the HTTP status isn't enough, we need to read the output
580 # since this is what CollectionManager.config() stipulates.
581 # Using LWP::UserAgent::get($url) for this
582
583 if(!defined $library_url) {
584 return 0;
585 }
586 else {
587 $ua->timeout(5); # set LWP useragent to 5s max timeout for testing the URL
588 # Need to set this, else it takes I don't know how long to timeout
589 # http://www.perlmonks.org/?node_id=618534
590
591 # http://search.cpan.org/~gaas/libwww-perl-6.04/lib/LWP/UserAgent.pm
592 # use LWP::UserAgent's get($url) since it returns an HTTP::Response code
593
594 my $response_obj = $ua->get($library_url.$command);
595
596 # $response_obj->content stores the content and $response_obj->code the HTTP response code
597 my $response_code = $response_obj->code();
598
599 if(LWP::Simple::is_success($response_code)) {# $response_code eq RC_OK) { # LWP::Simple::is_success($response_code)
600 $self->print_msg("*** Command $library_url$command\n", 3);
601 $self->print_msg("*** HTTP Response Status: $response_code - Complete.", 3);
602
603 # check the page content is as expected
604 my $response_content = $response_obj->content;
605 my $resultstr = $response_content;
606 $resultstr =~ s@.*gs_content\"\>@@s;
607 $resultstr =~ s@</div>.*@@s;
608
609 if($resultstr =~ m/$check_message_against_regex/) {#if($response_content =~ m/$check_message_against_regex/) {
610 $self->print_msg(" Response as expected.\n", 3);
611 $self->print_msg("@@@@@@ Got result:\n$resultstr\n", 4);
612 return 1;
613 } else {
614 # if we expect the collection to be inactive, then we'd be in silent mode: if so,
615 # don't print out the "ping did not succeed" response, but print out any other messages
616
617 # So we only suppress the ping col "did not succeed" response if we're in silent mode
618 # But if any message other than ping "did not succeed" is returned, we always print it
619 if($resultstr !~ m/did not succeed/ || !$silent) {#if($response_content !~ m/did not succeed/ || !$silent) {
620 $self->print_msg("\n\tBUT: command $library_url$command response UNEXPECTED.\n", 3);
621 $self->print_msg("*** Got message:\n$response_content.\n", 4);
622 $self->print_msg("*** Got result:\n$resultstr\n", 3);
623 }
624 return 0; # ping on a collection may "not succeed."
625 }
626 }
627 elsif(LWP::Simple::is_error($response_code)) { # method exported by LWP::Simple, along with HTTP::Status constants
628 # check the page content is as expected
629 if(defined $expected_error_code && $response_code == $expected_error_code) {
630 $self->print_msg(" Response status $response_code as expected.\n", 3);
631 } else {
632 $self->print_msg("*** Command $library_url$command\n");
633 $self->print_msg("*** Unexpected error. HTTP Response Status: $response_code - Failed.\n");
634 }
635 return 0; # return false, since the response_code was an error, expected or not
636 }
637 else {
638 $self->print_msg("*** Command $library_url$command\n");
639 $self->print_msg("*** Unexpected error. HTTP Response Status: $response_code - Failed.\n");
640 return 0;
641 }
642 }
643}
644
645# This method is now unused. Using ping_library instead to send the ping action to a
646# GS2/GS3 server. This method can be used more generally to test whether a URL is alive.
647# http://search.cpan.org/dist/libwww-perl/lib/LWP/Simple.pm
648# and http://www.perlmonks.org/?node_id=618534
649sub is_URL_active {
650 my $url = shift(@_); # gs3 or gs2 URL
651
652 my $status = 0;
653 if(defined $url) {
654 $ua->timeout(10); # set LWP useragent to 5s max timeout for testing the URL
655 # Need to set this, else it takes I don't know how long to timeout
656 # http://www.perlmonks.org/?node_id=618534
657
658 $status = LWP::Simple::head($url); # returns empty list of headers if it fails
659 # LWP::Simple::get($url) is more intensive, so don't need to do that
660 #print STDERR "**** $url is alive.\n" if $status;
661 }
662 return $status;
663}
664
665# Pinging seems to always return true, so this method doesn't work
666sub pingHost {
667 my $url = shift(@_); # gs3 or gs2 URL
668
669 my $status = 0;
670 if(defined $url) {
671 # Get just the domain. "http://localhost/gsdl?uq=332033495" becomes "localhost"
672 # "http://localhost/greenstone/cgi-bin/library.cgi" becomes "localhost" too
673
674 #my $host = $url;
675 #$host =~ s@^https?:\/\/(www.)?@@;
676 #$host =~ s@\/.*@@;
677 #print STDERR "**** HOST: $host\n";
678
679 # More robust way
680 # http://stackoverflow.com/questions/827024/how-do-i-extract-the-domain-out-of-an-url
681 my $uri = URI->new( $url );
682 my $host = $uri->host;
683
684 # Ping the host. http://perldoc.perl.org/Net/Ping.html
685 my $p = Net::Ping->new();
686 $status = $p->ping($host); # || 0. Appears to set to undef rather than 0
687 print STDERR "**** $host is alive.\n" if $status; #print "$host is alive.\n" if $p->ping($host);
688 $p->close();
689 }
690 # return whether pinging was a success or failure
691 return $status;
692}
693
6941;
Note: See TracBrowser for help on using the repository browser.