1 23 24 package com.sun.enterprise.admin.comm; 25 26 import com.sun.enterprise.admin.util.HostAndPort; 27 import java.io.IOException ; 28 import java.net.URL ; 29 import java.net.MalformedURLException ; 30 import java.net.URLConnection ; 31 import java.net.HttpURLConnection ; 32 import javax.net.ssl.HttpsURLConnection; 33 import javax.net.ssl.HostnameVerifier; 34 import javax.net.ssl.SSLSession; 35 import sun.misc.BASE64Encoder; 36 43 public final class HttpConnectorAddress implements GenericHttpConnectorAddress 44 { 45 private static final String HTTP_CONNECTOR = "http"; 46 private static final String HTTPS_CONNECTOR = "https"; 47 private static final String AUTHORIZATION_KEY = "Authorization"; 48 private static final String AUTHORIZATION_TYPE = "Basic "; 49 50 private String 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 host, int port){ 63 this(host, port, false); 64 } 65 73 public HttpConnectorAddress(String host, int port, boolean secure){ 74 this.host = host; 75 this.port = port; 76 this.secure = secure; 77 } 78 79 96 public URLConnection openConnection(String path) throws IOException { 97 return this.openConnection(this.toURL(path)); 98 } 99 100 101 107 public String getConnectorType() { 108 return this.isSecure() ? HTTPS_CONNECTOR : HTTP_CONNECTOR; 109 } 110 111 public String getHost() { 112 return host; 113 } 114 115 public void setHost(String 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 138 public void setSecure(boolean secure){ 139 this.secure = secure; 140 } 141 142 143 146 public boolean isSecure(){ 147 return secure; 148 } 149 150 private boolean secure; 151 152 private final String getUser(){ 153 return authInfo != null ? authInfo.getUser() : ""; 154 } 155 156 private final String getPassword(){ 157 return authInfo != null ? authInfo.getPassword() : ""; 158 } 159 160 168 private final String asURLSpec(String path){ 169 return this.getConnectorType() 170 +"://"+this.getAuthority() 171 +(path != null? path : ""); 172 173 } 174 175 178 private final String getAuthority(){ 179 return this.getHost() + ":" + this.getPort(); 180 } 181 182 183 private final URL toURL(String path) throws MalformedURLException { 184 return new URL (this.asURLSpec(path)); 185 } 186 187 188 private final URLConnection openConnection(URL url) throws IOException { 189 return this.setOptions(this.makeConnection(url)); 190 } 191 192 private final URLConnection makeConnection(URL url) throws IOException { 193 URLConnection uc = url.openConnection(); 194 if (uc instanceof HttpsURLConnection){ 195 setHostnameVerifier((HttpsURLConnection) uc); 196 } 197 return uc; 198 } 199 200 207 private final URLConnection setHostnameVerifier(HttpsURLConnection uc) { 208 uc.setHostnameVerifier( 209 new HostnameVerifier() { 210 private final String expected = host; 211 public boolean verify(String h, SSLSession s){ 212 return expected.equals(h); 213 } 214 } 215 ); 216 return uc; 217 } 218 219 private final URLConnection setOptions(URLConnection 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 setAuthentication(URLConnection uc){ 227 if (authInfo != null) { 228 uc.setRequestProperty(AUTHORIZATION_KEY, this.getBasicAuthString()); 229 } 230 return uc; 231 } 232 233 private final String getBasicAuthString(){ 234 return AUTHORIZATION_TYPE+ this.getBase64Encoded(this.getUser() + ":" + this.getPassword()); 235 } 236 237 238 private static final String getBase64Encoded(String clearString) { 239 return new BASE64Encoder().encode(clearString.getBytes()); 240 } 241 } 242 243 244 245 246 | Popular Tags |