KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > requesthandler > test > TestTunnel


1 package com.sslexplorer.requesthandler.test;
2
3 import java.io.InputStream JavaDoc;
4 import java.io.InterruptedIOException JavaDoc;
5 import java.io.OutputStream JavaDoc;
6 import java.util.Random JavaDoc;
7
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10
11 import com.sslexplorer.boot.RequestHandlerTunnel;
12
13 public class TestTunnel implements RequestHandlerTunnel {
14
15     public TestTunnel() {
16         super();
17         // TODO Auto-generated constructor stub
18
}
19
20
21     private static Log log= LogFactory.getLog(TestTunnel.class);
22
23     private Thread JavaDoc _thread;
24     private int _timeoutMs;
25     private InputStream JavaDoc _in;
26     private OutputStream JavaDoc _out;
27
28
29     /* ------------------------------------------------------------ */
30     /** handle method.
31      * This method is called by the HttpConnection.handleNext() method if
32      * this HttpTunnel has been set on that connection.
33      * The default implementation of this method copies between the HTTP
34      * socket and the socket passed in the constructor.
35      * @param in
36      * @param out
37      */

38     public void tunnel(InputStream JavaDoc in, OutputStream JavaDoc out)
39     {
40
41         Copy copy= new Copy();
42         _in= in;
43         _out= out;
44         try
45         {
46             _thread= Thread.currentThread();
47             copy.start();
48
49             copydata(null, _out);
50         }
51         catch (Exception JavaDoc e)
52         {
53         }
54         finally
55         {
56             try
57             {
58                 _in.close();
59
60             }
61             catch (Exception JavaDoc e)
62             {
63                 if (log.isDebugEnabled())
64                     log.debug("Failed to close tunnel.", e);
65             }
66             copy.interrupt();
67
68         }
69     }
70
71     /* ------------------------------------------------------------ */
72     private void copydata(InputStream JavaDoc in, OutputStream JavaDoc out) throws java.io.IOException JavaDoc
73     {
74         long timestamp= 0;
75         while (true)
76         {
77             try
78             {
79                 byte[] buf = new byte[32768];
80                 int read;
81                 
82                 if(in!=null) {
83                     while((read = in.read(buf)) > -1) {
84                         // Do nothing?!?!?!
85
}
86                 }
87                 else if(out!=null) {
88                     Random JavaDoc rnd = new Random JavaDoc();
89                     while(true) {
90                         rnd.nextBytes(buf);
91                         out.write(buf);
92                     }
93                 }
94                 timestamp= 0;
95                 return;
96             }
97             catch (InterruptedIOException JavaDoc e)
98             {
99                 if (timestamp == 0)
100                     timestamp= System.currentTimeMillis();
101                 else if (_timeoutMs > 0 && (System.currentTimeMillis() - timestamp) > _timeoutMs)
102                     throw e;
103             }
104         }
105     }
106
107     /* ------------------------------------------------------------ */
108     /* ------------------------------------------------------------ */
109     /** Copy thread.
110      * Helper thread to copy from the HTTP input to the sockets output
111      */

112     private class Copy extends Thread JavaDoc
113     {
114         public void run()
115         {
116             try
117             {
118                 copydata(_in, null);
119             }
120             catch (Exception JavaDoc e)
121             {
122                 if (log.isDebugEnabled())
123                     log.debug("Failed to copy data." , e);
124             }
125             finally
126             {
127                 try
128                 {
129                     _out.close();
130                 }
131                 catch (Exception JavaDoc e)
132                 {
133                 }
134                 _thread.interrupt();
135             }
136         }
137     }
138
139     public void close() {
140
141     }
142 }
143
Popular Tags