1 22 package org.jboss.invocation.http.interfaces; 23 24 import java.io.InputStream ; 25 import java.io.ObjectInputStream ; 26 import java.io.ObjectStreamException ; 27 import java.io.OutputStream ; 28 import java.io.ObjectOutputStream ; 29 import java.lang.reflect.InvocationTargetException ; 30 import java.lang.reflect.Method ; 31 import java.net.Authenticator ; 32 import java.net.HttpURLConnection ; 33 import java.net.MalformedURLException ; 34 import java.net.URL ; 35 import java.security.PrivilegedAction ; 36 import java.security.AccessController ; 37 import java.util.zip.GZIPInputStream ; 38 39 import javax.net.ssl.HttpsURLConnection; 40 import javax.net.ssl.SSLSocketFactory; 41 42 import org.jboss.invocation.Invocation; 43 import org.jboss.invocation.InvocationException; 44 import org.jboss.invocation.MarshalledValue; 45 import org.jboss.logging.Logger; 46 import org.jboss.security.SecurityAssociationAuthenticator; 47 import org.jboss.net.ssl.SSLSocketFactoryBuilder; 48 49 54 public class Util 55 { 56 57 public static final String IGNORE_HTTPS_HOST = "org.jboss.security.ignoreHttpsHost"; 58 59 public static final String SSL_FACTORY_BUILDER = "org.jboss.security.httpInvoker.sslSocketFactoryBuilder"; 60 63 private static String REQUEST_CONTENT_TYPE = 64 "application/x-java-serialized-object; class=org.jboss.invocation.MarshalledInvocation"; 65 private static Logger log = Logger.getLogger(Util.class); 66 67 private static SSLSocketFactoryBuilder sslSocketFactoryBuilder; 68 69 static class SetAuthenticator implements PrivilegedAction 70 { 71 public Object run() 72 { 73 Authenticator.setDefault(new SecurityAssociationAuthenticator()); 74 return null; 75 } 76 77 } 78 static class ReadSSLBuilder implements PrivilegedAction 79 { 80 public Object run() 81 { 82 String value = System.getProperty(SSL_FACTORY_BUILDER); 83 return value; 84 } 85 } 86 87 static 88 { 89 try 91 { 92 SetAuthenticator action = new SetAuthenticator(); 93 AccessController.doPrivileged(action); 94 } 95 catch(Exception e) 96 { 97 log.warn("Failed to install SecurityAssociationAuthenticator", e); 98 } 99 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 100 101 String factoryFactoryFQCN = null; 102 try 103 { 104 ReadSSLBuilder action = new ReadSSLBuilder(); 105 factoryFactoryFQCN = (String ) AccessController.doPrivileged(action); 106 } 107 catch(Exception e) 108 { 109 log.warn("Failed to read "+SSL_FACTORY_BUILDER, e); 110 } 111 112 if (factoryFactoryFQCN != null) 113 { 114 try 115 { 116 Class clazz = loader.loadClass(factoryFactoryFQCN); 117 sslSocketFactoryBuilder = (SSLSocketFactoryBuilder) clazz.newInstance(); 118 } 119 catch (Exception e) 120 { 121 log.warn("Could not instantiate SSLSocketFactoryFactory", e); 122 } 123 } 124 } 125 126 129 public static void init() 130 { 131 try 132 { 133 SetAuthenticator action = new SetAuthenticator(); 134 AccessController.doPrivileged(action); 135 } 136 catch(Exception e) 137 { 138 log.warn("Failed to install SecurityAssociationAuthenticator", e); 139 } 140 } 141 142 146 public static Object invoke(URL externalURL, Invocation mi) 147 throws Exception 148 { 149 if( log.isTraceEnabled() ) 150 log.trace("invoke, externalURL="+externalURL); 151 155 HttpURLConnection conn = (HttpURLConnection ) externalURL.openConnection(); 156 configureHttpsHostVerifier(conn); 157 conn.setDoInput(true); 158 conn.setDoOutput(true); 159 conn.setRequestProperty("ContentType", REQUEST_CONTENT_TYPE); 160 conn.setRequestMethod("POST"); 161 conn.setRequestProperty("Accept-Encoding", "x-gzip,x-deflate,gzip,deflate"); 163 OutputStream os = conn.getOutputStream(); 164 ObjectOutputStream oos = new ObjectOutputStream (os); 165 try 166 { 167 oos.writeObject(mi); 168 oos.flush(); 169 } 170 catch (ObjectStreamException e) 171 { 172 throw new InvocationException(e); 175 } 176 177 InputStream is = conn.getInputStream(); 179 String encoding = conn.getHeaderField("Content-Encoding"); 181 if( encoding != null && encoding.indexOf("gzip") >= 0 ) 182 is = new GZIPInputStream (is); 183 ObjectInputStream ois = new ObjectInputStream (is); 184 MarshalledValue mv = (MarshalledValue) ois.readObject(); 185 ois.read(); 187 ois.close(); 188 oos.close(); 189 190 Object value = mv.get(); 192 if( value instanceof Exception ) 193 { 194 throw (Exception ) value; 195 } 196 197 return value; 198 } 199 200 208 public static void configureHttpsHostVerifier(HttpURLConnection conn) 209 { 210 if ( conn instanceof HttpsURLConnection ) 211 { 212 if (Boolean.getBoolean(IGNORE_HTTPS_HOST) == true) 214 { 215 AnyhostVerifier.setHostnameVerifier(conn); 216 } 217 } 218 } 219 220 228 public static void configureSSLSocketFactory(HttpURLConnection conn) 229 throws InvocationTargetException 230 { 231 Class connClass = conn.getClass(); 232 if ( conn instanceof HttpsURLConnection && sslSocketFactoryBuilder != null) 233 { 234 try 235 { 236 SSLSocketFactory socketFactory = sslSocketFactoryBuilder.getSocketFactory(); 237 Class [] sig = {SSLSocketFactory.class}; 238 Method method = connClass.getMethod("setSSLSocketFactory", sig); 239 Object [] args = {socketFactory}; 240 method.invoke(conn, args); 241 log.trace("Socket factory set on connection"); 242 } 243 catch(Exception e) 244 { 245 throw new InvocationTargetException (e); 246 } 247 } 248 } 249 250 258 public static URL resolveURL(String urlValue) throws MalformedURLException 259 { 260 if( urlValue == null ) 261 return null; 262 263 URL externalURL = null; 264 try 265 { 266 externalURL = new URL (urlValue); 267 } 268 catch(MalformedURLException e) 269 { 270 String urlProperty = System.getProperty(urlValue); 272 if( urlProperty == null ) 273 throw e; 274 externalURL = new URL (urlProperty); 275 } 276 return externalURL; 277 } 278 } 279 | Popular Tags |