1 23 24 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 ; 34 import java.net.URL ; 35 import java.net.MalformedURLException ; 36 import java.net.URLConnection ; 37 import java.net.HttpURLConnection ; 38 39 import java.util.logging.Logger ; 40 41 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 56 public final class HttpConnectorAddress implements GenericHttpConnectorAddress 57 { 58 private static final String HTTP_CONNECTOR = "http"; 59 private static final String HTTPS_CONNECTOR = "https"; 60 private static final String AUTHORIZATION_KEY = "Authorization"; 61 private static final String AUTHORIZATION_TYPE = "Basic "; 62 63 private String host; 64 private int port; 65 66 private String path; 67 68 private AuthenticationInfo authInfo; 69 70 71 private static final Logger logger = Logger.getLogger( 72 DefaultConfiguration.JMXCONNECTOR_LOGGER); 74 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 host, int port){ 85 this(host, port, false); 86 } 87 95 public HttpConnectorAddress(String host, int port, boolean secure){ 96 97 102 this(host, port, secure, null); 103 104 } 105 106 107 public HttpConnectorAddress(String host, int port, boolean secure, String path) { 108 this.host = host; 109 this.port = port; 110 this.secure = secure; 111 this.path = path; 112 } 113 114 115 132 public URLConnection openConnection(String path) throws IOException { 133 134 if (path == null || path.trim().length() == 0) 135 path = this.path; 136 137 return this.openConnection(this.toURL(path)); 138 } 139 140 141 147 public String getConnectorType() { 148 return this.isSecure() ? HTTPS_CONNECTOR : HTTP_CONNECTOR; 149 } 150 151 public String getHost() { 152 return host; 153 } 154 155 public void setHost(String 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 168 public String getPath() { 169 return path; 170 } 171 172 public void setPath(String path) { 173 this.path = path; 174 } 175 176 177 public AuthenticationInfo getAuthenticationInfo() { 178 return authInfo; 179 } 180 181 public void setAuthenticationInfo(AuthenticationInfo authInfo) { 182 this.authInfo = authInfo; 183 } 184 185 188 public void setSecure(boolean secure){ 189 this.secure = secure; 190 } 191 192 193 196 public boolean isSecure(){ 197 return secure; 198 } 199 200 private boolean secure; 201 202 private final String getUser(){ 203 return authInfo != null ? authInfo.getUser() : ""; 204 } 205 206 private final String getPassword(){ 207 return authInfo != null ? authInfo.getPassword() : ""; 208 } 209 210 218 private final String asURLSpec(String path){ 219 return this.getConnectorType() 220 +"://"+this.getAuthority() 221 +(path != null? path : ""); 222 223 } 224 225 228 private final String getAuthority(){ 229 return this.getHost() + ":" + this.getPort(); 230 } 231 232 233 private final URL toURL(String path) throws MalformedURLException { 234 return new URL (this.asURLSpec(path)); 235 } 236 237 238 private final URLConnection openConnection(URL url) throws IOException { 239 return this.setOptions(this.makeConnection(url)); 240 } 241 242 private final URLConnection makeConnection(URL url) throws IOException { 243 return ( url.openConnection() ); 244 } 245 246 private final URLConnection setOptions(URLConnection 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 setAuthentication(URLConnection uc){ 255 if (authInfo != null) { 256 uc.setRequestProperty(AUTHORIZATION_KEY, this.getBasicAuthString()); 257 } 258 return uc; 259 } 260 261 private final String getBasicAuthString(){ 262 272 String enc = this.getBase64Encoded(this.getUser() + ":" + this.getPassword()); 273 281 enc = enc.replaceAll(System.getProperty("line.separator"), ""); 282 return ( AUTHORIZATION_TYPE + enc ); 283 } 284 285 286 private static final String getBase64Encoded(String clearString) { 287 return new BASE64Encoder().encode(clearString.getBytes()); 288 } 289 } 290 | Popular Tags |