1 |
//---PACKAGE DECLARATION--- |
2 |
package uk.org.iscream.cms.server.filter.plugins; |
3 |
|
4 |
//---IMPORTS--- |
5 |
import uk.org.iscream.cms.server.filter.PluginServiceCheck; |
6 |
import java.net.URL; |
7 |
import java.net.HttpURLConnection; |
8 |
|
9 |
/** |
10 |
* Tests whether a webserver is responding on port 80. |
11 |
* Connects on port 80 and performs a simple HTTP HEAD request. |
12 |
* Currently it sends back 200 for OK, anything else indicates a failure. |
13 |
* |
14 |
* @author $Author: tdb1 $ |
15 |
* @version $Id: HTTP__ServiceCheck.java,v 1.8 2001/05/29 17:02:35 tdb1 Exp $ |
16 |
*/ |
17 |
public class HTTP__ServiceCheck implements PluginServiceCheck { |
18 |
|
19 |
//---FINAL ATTRIBUTES--- |
20 |
|
21 |
/** |
22 |
* The current CVS revision of this class |
23 |
*/ |
24 |
public final String REVISION = "$Revision: 1.8 $"; |
25 |
|
26 |
public final String DESC = "Checks that a webserver is responding to requests."; |
27 |
|
28 |
//---STATIC METHODS--- |
29 |
|
30 |
//---CONSTRUCTORS--- |
31 |
|
32 |
//---PUBLIC METHODS--- |
33 |
|
34 |
/** |
35 |
* Performs the service check on a given host. |
36 |
* |
37 |
* @param hostname the host to check |
38 |
* @return XML data representing the result of the test |
39 |
*/ |
40 |
public String runServiceCheck(String hostname){ |
41 |
String status = ""; |
42 |
String message = ""; |
43 |
try { |
44 |
HttpURLConnection connection = (HttpURLConnection) ((new URL("HTTP://" + hostname)).openConnection()); |
45 |
// only get the head as we don't want any real content |
46 |
connection.setRequestMethod("HEAD"); |
47 |
// let the server know who we are, just to be nice, and get our name about ;p |
48 |
connection.setRequestProperty("User-Agent", "i-scream HTTP Service Checker v" + REVISION.substring(11, REVISION.length() - 2)); |
49 |
// connect and do the request |
50 |
connection.connect(); |
51 |
if (connection.getResponseCode() == 200 ) { |
52 |
status = "0"; |
53 |
} else { |
54 |
status = "1"; |
55 |
} |
56 |
message = connection.getResponseMessage(); |
57 |
connection.disconnect(); |
58 |
} catch (Exception e) { |
59 |
status = "1"; |
60 |
message = "Service check failed to establish connection to host:" + e.getMessage(); |
61 |
} |
62 |
// send the results back |
63 |
return "<HTTP status=\"" + status + "\" message=\"" + message + "\"></HTTP>"; |
64 |
} |
65 |
|
66 |
/** |
67 |
* return the String representation of what the filter does |
68 |
*/ |
69 |
public String getDescription(){ |
70 |
return DESC; |
71 |
} |
72 |
|
73 |
//---PRIVATE METHODS--- |
74 |
|
75 |
//---ACCESSOR/MUTATOR METHODS--- |
76 |
|
77 |
//---ATTRIBUTES--- |
78 |
|
79 |
//---STATIC ATTRIBUTES--- |
80 |
|
81 |
} |