KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > server > jetty > TunnelAdapter


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.server.jetty;
21
22 import java.io.InputStream JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import java.lang.reflect.Field JavaDoc;
25 import java.lang.reflect.Method JavaDoc;
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 JavaDoc 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     /* (non-Javadoc)
51      * @see org.mortbay.http.HttpTunnel#handle(java.io.InputStream, java.io.OutputStream)
52      */

53     public void handle(InputStream JavaDoc in, OutputStream JavaDoc 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 JavaDoc 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 JavaDoc ssl = getPrivateMember(obj, "com.sun.net.ssl.internal.ssl.SSLSocketImpl");
69         
70         if(ssl!=null) {
71             try {
72                 Method JavaDoc m = ssl.getClass().getMethod("setSoTimeout", new Class JavaDoc[] { int.class});
73                 if(m!=null) {
74                     m.setAccessible(true);
75                     m.invoke(ssl, new Object JavaDoc[] { timeoutMs});
76                 }
77             } catch(Throwable JavaDoc t) {
78                 log.error("Could not access setSoTimeout method", t);
79             }
80         }
81     }
82
83     private Object JavaDoc getPrivateMember(Object JavaDoc obj, String JavaDoc type) {
84
85         Class JavaDoc secretClass = obj.getClass();
86
87         // Print all the field names & values
88
Field JavaDoc 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 JavaDoc obj2 = fields[i].get(obj);
105                 if (obj2!=null && obj2.getClass().getName().equals(type)) {
106                     return obj2;
107                 }
108             } catch (Throwable JavaDoc 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