1 19 20 package com.sslexplorer.server.jetty; 21 22 import java.io.InputStream ; 23 import java.io.OutputStream ; 24 import java.lang.reflect.Field ; 25 import java.lang.reflect.Method ; 26 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 import org.mortbay.http.HttpTunnel; 30 31 import com.sslexplorer.boot.RequestHandlerTunnel; 32 33 class TunnelAdapter extends HttpTunnel { 34 35 static Log log = LogFactory.getLog(TunnelAdapter.class); 36 37 private RequestHandlerTunnel tunnel; 38 private int timeoutMs; 39 private OutputStream originalOutputStream; 40 41 public TunnelAdapter(RequestHandlerTunnel tunnel) { 42 this(tunnel, 0); 43 } 44 45 public TunnelAdapter(RequestHandlerTunnel tunnel, int timeoutMs) { 46 this.tunnel = tunnel; 47 this.timeoutMs = timeoutMs; 48 } 49 50 53 public void handle(InputStream in, OutputStream out) { 54 55 this.originalOutputStream = out; 56 57 if(timeoutMs > 0) 58 setSocketTimeout(out, timeoutMs); 59 60 tunnel.tunnel(in, out); 61 } 62 63 private void setSocketTimeout(Object obj, int timeoutMs) { 64 65 if(log.isDebugEnabled()) 66 log.debug("Looking for com.sun.net.ssl.internal.ssl.SSLSocketImpl to set timeout"); 67 68 Object ssl = getPrivateMember(obj, "com.sun.net.ssl.internal.ssl.SSLSocketImpl"); 69 70 if(ssl!=null) { 71 try { 72 Method m = ssl.getClass().getMethod("setSoTimeout", new Class [] { int.class}); 73 if(m!=null) { 74 m.setAccessible(true); 75 m.invoke(ssl, new Object [] { timeoutMs}); 76 } 77 } catch(Throwable t) { 78 log.error("Could not access setSoTimeout method", t); 79 } 80 } 81 } 82 83 private Object getPrivateMember(Object obj, String type) { 84 85 Class secretClass = obj.getClass(); 86 87 Field fields[] = secretClass.getDeclaredFields(); 89 90 if(log.isDebugEnabled()) 91 log.debug("Access all the fields on " 92 + obj.getClass().getName() 93 + " looking for type " 94 + type); 95 96 for (int i = 0; i < fields.length; i++) { 97 98 if(log.isDebugEnabled()) 99 log.debug("Field Name: " + fields[i].getName()); 100 101 fields[i].setAccessible(true); 102 103 try { 104 Object obj2 = fields[i].get(obj); 105 if (obj2!=null && obj2.getClass().getName().equals(type)) { 106 return obj2; 107 } 108 } catch (Throwable e) { 109 log.error("Failed to set timeout on SSLSocketImpl", e); 110 } 111 112 } 113 114 log.error("Could not find " + type + " on object " + obj.getClass().getName()); 115 116 return null; 117 } 118 } | Popular Tags |