KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > jmx > remote > 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 /* CVS information
25  * $Header: /cvs/glassfish/jmx-remote/rjmx-impl/src/java/com/sun/enterprise/admin/jmx/remote/comm/HttpConnectorAddress.java,v 1.4 2005/12/25 04:26:31 tcfujii Exp $
26  * $Revision: 1.4 $
27  * $Date: 2005/12/25 04:26:31 $
28 */

29
30 package com.sun.enterprise.admin.jmx.remote.comm;
31
32 import com.sun.enterprise.admin.jmx.remote.DefaultConfiguration;
33 import java.io.IOException JavaDoc;
34 import java.net.URL JavaDoc;
35 import java.net.MalformedURLException JavaDoc;
36 import java.net.URLConnection JavaDoc;
37 import java.net.HttpURLConnection JavaDoc;
38 /* BEGIN -- S1WS_MOD */
39 import java.util.logging.Logger JavaDoc;
40
41 /* END -- S1WS_MOD */
42 import javax.net.ssl.HttpsURLConnection;
43 import javax.net.ssl.HostnameVerifier;
44 import javax.net.ssl.SSLSession;
45 import sun.misc.BASE64Encoder;
46
47 /** This class abstracts the details of URLS from a client. allowing
48  * the client to set the host, port, security property and
49  * authorization informaiton. This information is then used to create
50  * an URLConnection which will only connect to the admin servlet using
51  * basic authorization (if authorization information is given).
52  * @author Kedar Mhaswade, Toby Ferguson
53  * @since S1AS7.0
54  * @version 1.0
55 */

56 public final class HttpConnectorAddress implements GenericHttpConnectorAddress
57 {
58   private static final String JavaDoc HTTP_CONNECTOR = "http";
59   private static final String JavaDoc HTTPS_CONNECTOR = "https";
60   private static final String JavaDoc AUTHORIZATION_KEY = "Authorization";
61   private static final String JavaDoc AUTHORIZATION_TYPE = "Basic ";
62
63   private String JavaDoc host;
64   private int port;
65 /* BEGIN -- S1WS_MOD */
66   private String JavaDoc path;
67 /* END -- S1WS_MOD */
68   private AuthenticationInfo authInfo;
69
70 /* BEGIN -- S1WS_MOD */
71     private static final Logger JavaDoc logger = Logger.getLogger(
72         DefaultConfiguration.JMXCONNECTOR_LOGGER);/*,
73         DefaultConfiguration.LOGGER_RESOURCE_BUNDLE_NAME );*/

74 /* END -- S1WS_MOD */
75
76   public HttpConnectorAddress() {
77   }
78
79
80   public HttpConnectorAddress(HostAndPort h){
81     this(h.getHost(), h.getPort(), h.isSecure());
82   }
83   
84   public HttpConnectorAddress(String JavaDoc host, int port){
85     this(host, port, false);
86   }
87       /**
88        * construct an address which indicates the host, port and
89        * security attributes desired.
90        * @param host a host address
91        * @param port a port number
92        * @secure an indication of whether the connection should be
93        * secure (i.e. confidential) or not
94        */

95   public HttpConnectorAddress(String JavaDoc host, int port, boolean secure){
96 /* BEGIN -- S1WS_MOD */
97 /*
98     this.host = host;
99     this.port = port;
100     this.secure = secure;
101 */

102     this(host, port, secure, null);
103 /* END -- S1WS_MOD */
104   }
105
106 /* BEGIN -- S1WS_MOD */
107   public HttpConnectorAddress(String JavaDoc host, int port, boolean secure, String JavaDoc path) {
108     this.host = host;
109     this.port = port;
110     this.secure = secure;
111     this.path = path;
112   }
113 /* END -- S1WS_MOD */
114
115       /**
116          Open a connection using the reciever and the given path
117          @param path the path to the required resource (path here is
118          the portion after the <code>hostname:port</code> portion of a URL)
119          @returns a connection to the required resource. The
120          connection returned may be a sub-class of
121          <code>URLConnection</code> including
122          <code>HttpsURLConnection</code>. If the sub-class is a
123          <code>HttpsURLConnection</code> then this connection will
124          accept any certificate from any server where the server's
125          name matches the host name of this object. Specifically we
126          allows the certificate <em>not</em> to contain the name of
127          the server. This is a potential security hole, but is also a
128          usability enhancement.
129          @throws IOException if there's a problem in connecting to the
130          resource
131       */

132   public URLConnection JavaDoc openConnection(String JavaDoc path) throws IOException JavaDoc {
133 /* BEGIN -- S1WS_MOD */
134     if (path == null || path.trim().length() == 0)
135         path = this.path;
136 /* END -- S1WS_MOD */
137     return this.openConnection(this.toURL(path));
138   }
139
140
141       /**
142        * get the protocol prefix to be used for a connection for the
143        * receiver
144        * @return the protocol prefix - one of <code>http</code> or
145        *<code>https</code> depending upon the security setting.
146        */

147   public String JavaDoc getConnectorType() {
148     return this.isSecure() ? HTTPS_CONNECTOR : HTTP_CONNECTOR;
149   }
150
151   public String JavaDoc getHost() {
152     return host;
153   }
154
155   public void setHost(String JavaDoc host) {
156     this.host = host;
157   }
158
159   public int getPort() {
160     return port;
161   }
162
163   public void setPort(int port) {
164     this.port = port;
165   }
166
167 /* BEGIN -- S1WS_MOD */
168   public String JavaDoc getPath() {
169     return path;
170   }
171
172   public void setPath(String JavaDoc path) {
173     this.path = path;
174   }
175 /* END -- S1WS_MOD */
176
177   public AuthenticationInfo getAuthenticationInfo() {
178     return authInfo;
179   }
180
181   public void setAuthenticationInfo(AuthenticationInfo authInfo) {
182     this.authInfo = authInfo;
183   }
184
185       /**
186        * Set the security attibute
187        */

188   public void setSecure(boolean secure){
189     this.secure = secure;
190   }
191   
192
193       /**
194        * Indicate if the reciever represents a secure address
195        */

196   public boolean isSecure(){
197     return secure;
198   }
199
200   private boolean secure;
201
202   private final String JavaDoc getUser(){
203     return authInfo != null ? authInfo.getUser() : "";
204   }
205
206   private final String JavaDoc getPassword(){
207     return authInfo != null ? authInfo.getPassword() : "";
208   }
209
210       /**
211        * Return a string which can be used as the specification to
212        * form an URL.
213        * @return a string which can be used as the specification to
214        *form an URL. This string is in the form of
215        *<code>&gt;protocol>://&gt;host>:&gtport>/</code> with the
216        *appropriate substitutions
217        */

218   private final String JavaDoc asURLSpec(String JavaDoc path){
219     return this.getConnectorType()
220     +"://"+this.getAuthority()
221     +(path != null? path : "");
222   
223   }
224
225       /**
226          Return the authority portion of the URL spec
227       */

228   private final String JavaDoc getAuthority(){
229     return this.getHost() + ":" + this.getPort();
230   }
231   
232
233   private final URL JavaDoc toURL(String JavaDoc path) throws MalformedURLException JavaDoc{
234     return new URL JavaDoc(this.asURLSpec(path));
235   }
236   
237
238   private final URLConnection JavaDoc openConnection(URL JavaDoc url) throws IOException JavaDoc {
239     return this.setOptions(this.makeConnection(url));
240   }
241
242   private final URLConnection JavaDoc makeConnection(URL JavaDoc url) throws IOException JavaDoc {
243     return ( url.openConnection() );
244   }
245
246   private final URLConnection JavaDoc setOptions(URLConnection JavaDoc uc){
247     uc.setDoOutput(true);
248     uc.setUseCaches(false);
249     uc.setRequestProperty("Content-type", "application/octet-stream");
250     uc.setRequestProperty("Connection", "Keep-Alive");
251     return this.setAuthentication(uc);
252   }
253
254   private final URLConnection JavaDoc setAuthentication(URLConnection JavaDoc uc){
255     if (authInfo != null) {
256       uc.setRequestProperty(AUTHORIZATION_KEY, this.getBasicAuthString());
257     }
258     return uc;
259   }
260
261   private final String JavaDoc getBasicAuthString(){
262     /* taking care of the descripancies in the Base64Encoder, for very
263        large lengths of passwords and/or usernames.
264        Abhijit did the analysis and as per his suggestion, replacing
265        a newline in Base64 encoded String by newline followed by a space
266        should work for any length of password, independent of the
267        web server buffer length. That investigation is still on, but
268        in the meanwhile, it was found that the replacement of newline
269        character with empty string "" works. Hence implementing the same.
270        Date: 10/10/2003.
271     */

272     String JavaDoc enc = this.getBase64Encoded(this.getUser() + ":" + this.getPassword());
273     /*
274     String f = "\n"; // System.getProperty("line.separator");
275     String t = f + " " ;
276     enc = enc.replaceAll(f, t);
277     f = "\r\n";
278     t = f + " ";
279     enc = enc.replaceAll(f, t);
280     */

281     enc = enc.replaceAll(System.getProperty("line.separator"), "");
282     return ( AUTHORIZATION_TYPE + enc );
283   }
284   
285
286   private static final String JavaDoc getBase64Encoded(String JavaDoc clearString) {
287     return new BASE64Encoder().encode(clearString.getBytes());
288   }
289 }
290
Popular Tags