KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > comm > HttpConnectorAddress


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.admin.comm;
25
26 import com.sun.enterprise.admin.util.HostAndPort;
27 import java.io.IOException JavaDoc;
28 import java.net.URL JavaDoc;
29 import java.net.MalformedURLException JavaDoc;
30 import java.net.URLConnection JavaDoc;
31 import java.net.HttpURLConnection JavaDoc;
32 import javax.net.ssl.HttpsURLConnection;
33 import javax.net.ssl.HostnameVerifier;
34 import javax.net.ssl.SSLSession;
35 import sun.misc.BASE64Encoder;
36 /**
37    This class abstracts the details of URLS from a client. allowing
38    the client to set the host, port, security property and
39    authorization informaiton. This information is then used to create
40    an URLConnection which will only connect to the admin servlet using
41    basic authorization (if authorization information is given).
42 */

43 public final class HttpConnectorAddress implements GenericHttpConnectorAddress
44 {
45   private static final String JavaDoc HTTP_CONNECTOR = "http";
46   private static final String JavaDoc HTTPS_CONNECTOR = "https";
47   private static final String JavaDoc AUTHORIZATION_KEY = "Authorization";
48   private static final String JavaDoc AUTHORIZATION_TYPE = "Basic ";
49
50   private String JavaDoc host;
51   private int port;
52   private AuthenticationInfo authInfo;
53
54   public HttpConnectorAddress() {
55   }
56
57
58   public HttpConnectorAddress(HostAndPort h){
59     this(h.getHost(), h.getPort(), h.isSecure());
60   }
61   
62   public HttpConnectorAddress(String JavaDoc host, int port){
63     this(host, port, false);
64   }
65       /**
66        * construct an address which indicates the host, port and
67        * security attributes desired.
68        * @param host a host address
69        * @param port a port number
70        * @secure an indication of whether the connection should be
71        * secure (i.e. confidential) or not
72        */

73   public HttpConnectorAddress(String JavaDoc host, int port, boolean secure){
74     this.host = host;
75     this.port = port;
76     this.secure = secure;
77   }
78
79       /**
80          Open a connection using the reciever and the given path
81          @param path the path to the required resource (path here is
82          the portion after the <code>hostname:port</code> portion of a URL)
83          @returns a connection to the required resource. The
84          connection returned may be a sub-class of
85          <code>URLConnection</code> including
86          <code>HttpsURLConnection</code>. If the sub-class is a
87          <code>HttpsURLConnection</code> then this connection will
88          accept any certificate from any server where the server's
89          name matches the host name of this object. Specifically we
90          allows the certificate <em>not</em> to contain the name of
91          the server. This is a potential security hole, but is also a
92          usability enhancement.
93          @throws IOException if there's a problem in connecting to the
94          resource
95       */

96   public URLConnection JavaDoc openConnection(String JavaDoc path) throws IOException JavaDoc {
97     return this.openConnection(this.toURL(path));
98   }
99
100
101       /**
102        * get the protocol prefix to be used for a connection for the
103        * receiver
104        * @return the protocol prefix - one of <code>http</code> or
105        *<code>https</code> depending upon the security setting.
106        */

107   public String JavaDoc getConnectorType() {
108     return this.isSecure() ? HTTPS_CONNECTOR : HTTP_CONNECTOR;
109   }
110
111   public String JavaDoc getHost() {
112     return host;
113   }
114
115   public void setHost(String JavaDoc host) {
116     this.host = host;
117   }
118
119   public int getPort() {
120     return port;
121   }
122
123   public void setPort(int port) {
124     this.port = port;
125   }
126
127   public AuthenticationInfo getAuthenticationInfo() {
128     return authInfo;
129   }
130
131   public void setAuthenticationInfo(AuthenticationInfo authInfo) {
132     this.authInfo = authInfo;
133   }
134
135       /**
136        * Set the security attibute
137        */

138   public void setSecure(boolean secure){
139     this.secure = secure;
140   }
141   
142
143       /**
144        * Indicate if the reciever represents a secure address
145        */

146   public boolean isSecure(){
147     return secure;
148   }
149
150   private boolean secure;
151
152   private final String JavaDoc getUser(){
153     return authInfo != null ? authInfo.getUser() : "";
154   }
155
156   private final String JavaDoc getPassword(){
157     return authInfo != null ? authInfo.getPassword() : "";
158   }
159
160       /**
161        * Return a string which can be used as the specification to
162        * form an URL.
163        * @return a string which can be used as the specification to
164        *form an URL. This string is in the form of
165        *<code>&gt;protocol>://&gt;host>:&gtport>/</code> with the
166        *appropriate substitutions
167        */

168   private final String JavaDoc asURLSpec(String JavaDoc path){
169     return this.getConnectorType()
170     +"://"+this.getAuthority()
171     +(path != null? path : "");
172   
173   }
174
175       /**
176          Return the authority portion of the URL spec
177       */

178   private final String JavaDoc getAuthority(){
179     return this.getHost() + ":" + this.getPort();
180   }
181   
182
183   private final URL JavaDoc toURL(String JavaDoc path) throws MalformedURLException JavaDoc{
184     return new URL JavaDoc(this.asURLSpec(path));
185   }
186   
187
188   private final URLConnection JavaDoc openConnection(URL JavaDoc url) throws IOException JavaDoc {
189     return this.setOptions(this.makeConnection(url));
190   }
191
192   private final URLConnection JavaDoc makeConnection(URL JavaDoc url) throws IOException JavaDoc {
193     URLConnection JavaDoc uc = url.openConnection();
194     if (uc instanceof HttpsURLConnection){
195       setHostnameVerifier((HttpsURLConnection) uc);
196     }
197     return uc;
198   }
199
200       /**
201        * Set the hostname verifier on the given connection so that a
202        peer which appears to be the one we want to connect to is accepted.
203        * @param uc the non-null URL connection whose hostname verifier may be
204        * set.
205        * @returns the URLConnection argument
206        */

207   private final URLConnection JavaDoc setHostnameVerifier(HttpsURLConnection uc) {
208     uc.setHostnameVerifier(
209       new HostnameVerifier() {
210         private final String JavaDoc expected = host;
211         public boolean verify(String JavaDoc h, SSLSession s){
212           return expected.equals(h);
213         }
214       }
215       );
216     return uc;
217   }
218   
219   private final URLConnection JavaDoc setOptions(URLConnection JavaDoc uc){
220     uc.setDoOutput(true);
221     uc.setUseCaches(false);
222     uc.setRequestProperty("Content-type", "application/octet-stream");
223     return this.setAuthentication(uc);
224   }
225
226   private final URLConnection JavaDoc setAuthentication(URLConnection JavaDoc uc){
227     if (authInfo != null) {
228       uc.setRequestProperty(AUTHORIZATION_KEY, this.getBasicAuthString());
229     }
230     return uc;
231   }
232
233   private final String JavaDoc getBasicAuthString(){
234     return AUTHORIZATION_TYPE+ this.getBase64Encoded(this.getUser() + ":" + this.getPassword());
235   }
236   
237
238   private static final String JavaDoc getBase64Encoded(String JavaDoc clearString) {
239     return new BASE64Encoder().encode(clearString.getBytes());
240   }
241 }
242
243       
244
245   
246
Popular Tags