Index: main/trunk/greenstone2/perllib/servercontrol.pm
===================================================================
--- main/trunk/greenstone2/perllib/servercontrol.pm	(revision 30524)
+++ main/trunk/greenstone2/perllib/servercontrol.pm	(revision 30526)
@@ -33,13 +33,18 @@
 no strict 'subs'; # allow barewords (eg STDERR) as function arguments
 
+# Greenstone includes
+use printusage;
+use parse2;
+
+
+# The perl library imports below are used by deprecated methods config_old(), is_URL_active() and pingHost()
+# If the following library imports are not supported by your perl installation, comment out these
+# imports and move the methods config_old(), is_URL_active() and pingHost() out to a temporary file.
 use HTTP::Response;
 use LWP::Simple qw($ua !head); # import useragent object as $ua from the full LWP to use along with LWP::Simple
-		# don't import LWP::Simple's head function by name since it can conflict with CGI:head())			
+		# don't import LWP::Simple's head function by name since it can conflict with CGI:head())
 #use CGI qw(:standard);  # then only CGI.pm defines a head()
 use Net::Ping;
 use URI;
-
-use printusage;
-use parse2;
 
 
@@ -132,27 +137,52 @@
     }
     else {
-	$ua->timeout(5); # set LWP useragent to 5s max timeout for testing the URL
-	# Need to set this, else it takes I don't know how long to timeout 
-	# http://www.perlmonks.org/?node_id=618534
-	
-	# http://search.cpan.org/~gaas/libwww-perl-6.04/lib/LWP/UserAgent.pm
-	# use LWP::UserAgent's get($url) since it returns an HTTP::Response code
-	
-	my $response_obj = $ua->get($library_url.$command);
-	
-	# $response_obj->content stores the content and $response_obj->code the HTTP response code
-	my $response_code = $response_obj->code();
-	
-	if(LWP::Simple::is_success($response_code)) {# $response_code eq RC_OK) { # LWP::Simple::is_success($response_code)
+	# ampersands need to be escaped with single quotes
+	my $wgetCommand = $command;
+	$wgetCommand =~ s@&@'&'@g;
+
+	my $wget_file_path = &FileUtils::filenameConcatenate($ENV{'GSDLHOME'}, "bin", $ENV{'GSDLOS'}, "wget");
+
+	# https://www.gnu.org/software/wget/manual/wget.html
+	# output-document set to - (STDOUT), so page is streamed to STDOUT
+	# timeout: 5 seconds, tries: 1
+	# wget sends status information and response code to STDERR, so redirect it to STDOUT
+	# Searching for "perl backtick operator redirect stderr to stdout":
+	# http://www.perlmonks.org/?node=How%20can%20I%20capture%20STDERR%20from%20an%20external%20command%3F
+	$wgetCommand = "\"$wget_file_path\" --output-document=- -T 5 -t 1 $library_url$wgetCommand 2>&1";	
+	#$wgetCommand = "\"$wget_file_path\" --spider -T 5 -t 1 $library_url$wgetCommand 2>&1"; # won't save page
+	my $response_content = `$wgetCommand`;
+	my $response_code = undef;
+	my @lines = split( /\n/, $response_content );
+	foreach my $line (@lines) {
+	    #print STDERR "@@@@ LINE: $line\n";
+	    if($line =~ m@failed: Connection timed out.$@) {
+		$response_code = "failed: Connection timed out.";
+		last; # break keyword in perl = last
+	    }
+	    elsif($line =~ m@failed: Connection refused.$@) {
+		$response_code = "failed: Connection refused.";
+		last; # break keyword in perl = last
+	    }
+	    elsif($line =~ m@HTTP request sent, @) {
+		$response_code = $line;
+		$response_code =~ s@[^\d]*(.*)$@$1@;
+		last;
+	    }
+	}
+
+	if($command =~ m@ping@ && $response_code =~ m@failed: Connection refused@) {
+	    # server not running
+	    $self->print_msg("*** Server not running. $library_url$command\n", 3);
+	    return 0;
+	}
+	if($response_code && $response_code eq "200 OK") {
 	    $self->print_msg("*** Command $library_url$command\n", 3);
 	    $self->print_msg("*** HTTP Response Status: $response_code - Complete.", 3);
 	    
 	    # check the page content is as expected
-	    my $response_content = $response_obj->content;
 	    my $resultstr = $response_content;
 	    $resultstr =~ s@.*gs_content\"\>@@s;		
 	    $resultstr =~ s@</div>.*@@s;
-	    
-	    if($response_content =~ m/$check_message_against_regex/) {
+	    if($resultstr =~ m/$check_message_against_regex/) {
 		$self->print_msg(" Response as expected.\n", 3);
 		$self->print_msg("@@@@@@ Got result:\n$resultstr\n", 4);
@@ -164,5 +194,5 @@
 		# So we only suppress the ping col "did not succeed" response if we're in silent mode
 		# But if any message other than ping "did not succeed" is returned, we always print it
-		if($response_content !~ m/did not succeed/ || !$silent) {
+		if($resultstr !~ m/did not succeed/ || !$silent) {
 		    $self->print_msg("\n\tBUT: command $library_url$command response UNEXPECTED.\n", 3);
 		    $self->print_msg("*** Got message:\n$response_content.\n", 4);
@@ -171,20 +201,28 @@
 		return 0; # ping on a collection may "not succeed."
 	    }
-	} 
-	elsif(LWP::Simple::is_error($response_code)) { # method exported by LWP::Simple, along with HTTP::Status constants
+	}
+	elsif($response_code && $response_code =~ m@^(4|5)\d\d@) { # client side errors start with 4xx, server side with 5xx
 	    # check the page content is as expected
-	    if(defined $expected_error_code && $response_code == $expected_error_code) {
+	    if(defined $expected_error_code && $response_code =~ m@^$expected_error_code@) {
 		$self->print_msg(" Response status $response_code as expected.\n", 3);
 	    } else {
 		$self->print_msg("*** Command $library_url$command\n");
-		$self->print_msg("*** Unexpected error. HTTP Response Status: $response_code - Failed.\n");
+		$self->print_msg("*** Unexpected error type 1. HTTP Response Status: $response_code - Failed.\n");
 	    }
 	    return 0; # return false, since the response_code was an error, expected or not
-	}
-	else {
+	}	
+	else {  # also if response_code is still undefined, as can happen with connection timing out
 	    $self->print_msg("*** Command $library_url$command\n");
-	    $self->print_msg("*** Unexpected error. HTTP Response Status: $response_code - Failed.\n");
+	    if(defined $response_code) {
+		$self->print_msg("*** Unexpected error type 2. HTTP Response Status: $response_code - Failed.\n");
+	    } else {
+		$self->print_msg("*** Unexpected error type 3. Failed:\n\n$response_content\n\n");
+	    }
 	    return 0;
 	}
+	#print STDERR "********** WgetCommand: $wgetCommand\n\n";
+	#print STDERR "********** Response_content:\n$response_content\n\n";
+	#print STDERR "********** Response_CODE: $response_code\n";
+
     }	
 }
@@ -437,8 +475,8 @@
 
     # Can't do $self->{'library_url'}, because it may not yet be set
-    my $library_url = $self->get_library_URL(); # returns undef if no server is running
+    my $library_url = $self->get_library_URL(); # returns undef if no valid server URL
 
     if(!defined $library_url) { # undef if no valid server URL
-	return; # can't do any deactivation without a running GS server
+	return; # can't do any deactivation without a valid server URL
     }
 
@@ -492,5 +530,5 @@
 
     if(!defined $library_url) { # undef if no valid server URL
-	return; # nothing to activate if there's no running server
+	return; # nothing to activate if without valid server URL
     }
 
@@ -549,4 +587,89 @@
 ### UNUSED METHODS - CAN BE HANDY
 
+
+# This method uses the perl libraries we're advised to use in place of wget for pinging and retrieving web 
+# pages. The problem is that not all perl installations may support these libraries. So we now use the new
+# config() method further above, which uses the wget included in Greenstone binary installations.
+# If the library imports at page top conflict, comment out those imports and move the methods config_old(),
+# is_URL_active() and pingHost() out to a temporary file.
+# 
+# If for some reason you can't use wget, then rename the config() method to config_old(), and rename the
+# method below to config() and things should work as before.
+sub config_old {
+    my $self = shift(@_);
+    my ($command, $check_message_against_regex, $expected_error_code, $silent) = @_;
+
+    my $library_url = $self->get_library_URL(); #$self->{'library_url'};
+
+
+    # Gatherer.java's configGS3Server doesn't use the site variable
+    # so we don't have to either
+    
+    # for GS2, getting the HTTP status isn't enough, we need to read the output
+    # since this is what CollectionManager.config() stipulates.
+    # Using LWP::UserAgent::get($url) for this	
+    
+    if(!defined $library_url) {
+	return 0;
+    }
+    else {
+	$ua->timeout(5); # set LWP useragent to 5s max timeout for testing the URL
+	# Need to set this, else it takes I don't know how long to timeout 
+	# http://www.perlmonks.org/?node_id=618534
+	
+	# http://search.cpan.org/~gaas/libwww-perl-6.04/lib/LWP/UserAgent.pm
+	# use LWP::UserAgent's get($url) since it returns an HTTP::Response code
+	
+	my $response_obj = $ua->get($library_url.$command);
+	
+	# $response_obj->content stores the content and $response_obj->code the HTTP response code
+	my $response_code = $response_obj->code();
+	
+	if(LWP::Simple::is_success($response_code)) {# $response_code eq RC_OK) { # LWP::Simple::is_success($response_code)
+	    $self->print_msg("*** Command $library_url$command\n", 3);
+	    $self->print_msg("*** HTTP Response Status: $response_code - Complete.", 3);
+	    
+	    # check the page content is as expected
+	    my $response_content = $response_obj->content;
+	    my $resultstr = $response_content;
+	    $resultstr =~ s@.*gs_content\"\>@@s;		
+	    $resultstr =~ s@</div>.*@@s;
+	    
+	    if($resultstr =~ m/$check_message_against_regex/) {#if($response_content =~ m/$check_message_against_regex/) {
+		$self->print_msg(" Response as expected.\n", 3);
+		$self->print_msg("@@@@@@ Got result:\n$resultstr\n", 4);
+		return 1;
+	    } else {
+		# if we expect the collection to be inactive, then we'd be in silent mode: if so,
+		# don't print out the "ping did not succeed" response, but print out any other messages
+		
+		# So we only suppress the ping col "did not succeed" response if we're in silent mode
+		# But if any message other than ping "did not succeed" is returned, we always print it
+		if($resultstr !~ m/did not succeed/ || !$silent) {#if($response_content !~ m/did not succeed/ || !$silent) {
+		    $self->print_msg("\n\tBUT: command $library_url$command response UNEXPECTED.\n", 3);
+		    $self->print_msg("*** Got message:\n$response_content.\n", 4);
+		    $self->print_msg("*** Got result:\n$resultstr\n", 3);
+		}
+		return 0; # ping on a collection may "not succeed."
+	    }
+	} 
+	elsif(LWP::Simple::is_error($response_code)) { # method exported by LWP::Simple, along with HTTP::Status constants
+	    # check the page content is as expected
+	    if(defined $expected_error_code && $response_code == $expected_error_code) {
+		$self->print_msg(" Response status $response_code as expected.\n", 3);
+	    } else {
+		$self->print_msg("*** Command $library_url$command\n");
+		$self->print_msg("*** Unexpected error. HTTP Response Status: $response_code - Failed.\n");
+	    }
+	    return 0; # return false, since the response_code was an error, expected or not
+	}
+	else {
+	    $self->print_msg("*** Command $library_url$command\n");
+	    $self->print_msg("*** Unexpected error. HTTP Response Status: $response_code - Failed.\n");
+	    return 0;
+	}
+    }	
+}
+
 # This method is now unused. Using ping_library instead to send the ping action to a
 # GS2/GS3 server. This method can be used more generally to test whether a URL is alive.
