1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jdiagnose.library;
17
18 import java.io.IOException;
19 import java.net.Authenticator;
20 import java.net.HttpURLConnection;
21 import java.net.PasswordAuthentication;
22 import java.net.URL;
23 import java.net.URLConnection;
24 import java.util.Iterator;
25 import java.util.Map;
26 import java.util.Properties;
27
28 import org.jdiagnose.DiagnosticUnit;
29
30 /***
31 * Check a Http URL.
32 *
33 * @see HttpProxyConfig
34 * @author jmccrindle
35 */
36 public class HttpUrlDiagnostic extends DiagnosticUnit {
37
38 private String url = null;
39 private HttpProxyConfig proxyConfig = null;
40 private String method = "GET";
41 private Properties parameters = new Properties();
42
43 /***
44 * Create an empty HttpUrlDiagnostic. Url property
45 * must be set.
46 */
47 public HttpUrlDiagnostic() {
48 super();
49 }
50
51 /***
52 * Creates an HttpUrlDiagnostic with a fully qualified
53 * name. The url property must be set for this class
54 * @param name the fqn for this diagnostic
55 */
56 public HttpUrlDiagnostic(String name) {
57 super(name);
58 }
59
60 /***
61 * Diagnose a particular URL
62 * @throws IOException
63 */
64 public void diagnoseUrl() throws IOException {
65 if(proxyConfig != null) {
66 System.setProperty("http.proxyHost", proxyConfig.getProxyHost());
67 System.setProperty("http.proxyPort", proxyConfig.getProxyPort());
68 if(proxyConfig.getProxyUsername() != null
69 && proxyConfig.getProxyPassword() != null) {
70 Authenticator.setDefault(new Authenticator() {
71 protected PasswordAuthentication getPasswordAuthentication() {
72 return new PasswordAuthentication(proxyConfig.getProxyUsername(),
73 proxyConfig.getProxyPassword().toCharArray());
74 }
75 });
76 }
77 }
78 assertNotNull("url property MUST be set", url);
79 URL fullUrl = new URL(url);
80 URLConnection connection = fullUrl.openConnection();
81 assertTrue(url + " is a " + connection.getClass(), connection instanceof HttpURLConnection);
82 HttpURLConnection urlConnnection = (HttpURLConnection) connection;
83 urlConnnection.setRequestMethod(method);
84 if(parameters != null && parameters.size() > 0) {
85 for (Iterator entryIterator = parameters.entrySet().iterator(); entryIterator.hasNext();) {
86 Map.Entry entry = (Map.Entry) entryIterator.next();
87 urlConnnection.setRequestProperty((String) entry.getKey(), (String) entry.getValue());
88 }
89 }
90 assertTrue("Expected response code of 200 but recieved " + urlConnnection.getResponseCode(), urlConnnection.getResponseCode() == 200);
91 }
92
93 public String getUrl() {
94 return url;
95 }
96
97 public void setUrl(String string) {
98 url = string;
99 }
100
101 public String getMethod() {
102 return method;
103 }
104 public void setMethod(String method) {
105 this.method = method;
106 }
107 public Properties getParameters() {
108 return parameters;
109 }
110 public void setParameters(Properties parameters) {
111 this.parameters = parameters;
112 }
113 public HttpProxyConfig getProxyConfig() {
114 return proxyConfig;
115 }
116 public void setProxyConfig(HttpProxyConfig proxyConfig) {
117 this.proxyConfig = proxyConfig;
118 }
119 }