View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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 Https Url. Given the pain involved in getting Https working
32   * under Java 1.3, Java 1.4+ is recommended.
33   * 
34   * @author jmccrindle
35   */
36  public class HttpsUrlDiagnostic extends DiagnosticUnit {
37  
38      /***
39       * @param name
40       */
41      public HttpsUrlDiagnostic(String name) {
42          super(name);
43      }
44  
45      private String url = null;
46      private String protocolHandler = "com.sun.net.ssl.internal.www.protocol";
47      private HttpProxyConfig proxyConfig = null;
48      private String method = "GET";
49      private Properties parameters = new Properties();
50  
51      /***
52       * Creates a default https url diagnostic. URL is a required property
53       */
54      public HttpsUrlDiagnostic() {
55          super();
56      }
57  
58      /***
59       * Diagnose a particular url
60       * @throws IOException if there was a problem connecting
61       */
62      public void diagnoseUrl() throws IOException {
63          if(protocolHandler != null) {
64              System.setProperty("java.protocol.handler.pkgs", protocolHandler);
65          }
66          if(proxyConfig != null) {
67              System.setProperty("https.proxyHost", proxyConfig.getProxyHost());
68              System.setProperty("https.proxyPort", "8080");
69              if(proxyConfig.getProxyUsername() != null
70                  && proxyConfig.getProxyPassword() != null) {
71                  Authenticator.setDefault(new Authenticator() {
72                      protected PasswordAuthentication getPasswordAuthentication() {
73                          return new PasswordAuthentication(proxyConfig.getProxyUsername(), 
74                              proxyConfig.getProxyPassword().toCharArray());
75                      }
76                  });
77              }
78          }
79          assertNotNull("url property MUST be set", url);
80          URL fullUrl = new URL(url);
81          URLConnection connection = fullUrl.openConnection();
82          assertTrue(url + " is a " + connection.getClass(), connection instanceof HttpURLConnection);
83          HttpURLConnection urlConnnection = (HttpURLConnection) connection;
84          urlConnnection.setRequestMethod(method);
85          if(parameters != null && parameters.size() > 0) {
86              for (Iterator entryIterator = parameters.entrySet().iterator(); entryIterator.hasNext();) {
87                  Map.Entry entry = (Map.Entry) entryIterator.next();
88                  urlConnnection.setRequestProperty((String) entry.getKey(), (String) entry.getValue());
89              }
90          }
91          assertTrue("Expected response code of 200 but recieved " + urlConnnection.getResponseCode(), urlConnnection.getResponseCode() == 200);
92      }
93  
94      /***
95       * @return
96       */
97      public String getUrl() {
98          return url;
99      }
100 
101     /***
102      * @param string
103      */
104     public void setUrl(String string) {
105         url = string;
106     }
107 
108     /***
109      * @return
110      */
111     public String getProtocolHandler() {
112         return protocolHandler;
113     }
114 
115     /***
116      * @return
117      */
118     public HttpProxyConfig getProxyConfig() {
119         return proxyConfig;
120     }
121 
122     /***
123      * @param string
124      */
125     public void setProtocolHandler(String string) {
126         protocolHandler = string;
127     }
128 
129     /***
130      * @param config
131      */
132     public void setProxyConfig(HttpProxyConfig config) {
133         proxyConfig = config;
134     }
135 
136     public String getMethod() {
137         return method;
138     }
139     public void setMethod(String method) {
140         this.method = method;
141     }
142     public Properties getParameters() {
143         return parameters;
144     }
145     public void setParameters(Properties parameters) {
146         this.parameters = parameters;
147     }
148 }