KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > vfs > QSocketWrapper


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.vfs;
30
31 import com.caucho.log.Log;
32 import com.caucho.util.IntMap;
33
34 import javax.net.ssl.SSLPeerUnverifiedException;
35 import javax.net.ssl.SSLSession;
36 import javax.net.ssl.SSLSocket;
37 import java.io.IOException JavaDoc;
38 import java.io.InputStream JavaDoc;
39 import java.io.OutputStream JavaDoc;
40 import java.net.InetAddress JavaDoc;
41 import java.net.Socket JavaDoc;
42 import java.nio.channels.SelectableChannel JavaDoc;
43 import java.security.cert.CertificateException JavaDoc;
44 import java.security.cert.X509Certificate JavaDoc;
45 import java.util.logging.Level JavaDoc;
46 import java.util.logging.Logger JavaDoc;
47
48 /**
49  * Abstract socket to handle both normal sockets and bin/resin sockets.
50  */

51 public class QSocketWrapper extends QSocket {
52   private static final Logger JavaDoc log = Log.open(QSocketWrapper.class);
53   private static Class JavaDoc sslSocketClass;
54   private static IntMap sslKeySizes;
55   
56   private Socket _s;
57   private InputStream JavaDoc _is;
58   private OutputStream JavaDoc _os;
59   private SocketStream _streamImpl;
60
61   public QSocketWrapper()
62   {
63   }
64
65   public QSocketWrapper(Socket s)
66   {
67     init(s);
68   }
69
70   public void init(Socket s)
71   {
72     _s = s;
73     _is = null;
74     _os = null;
75   }
76
77   /**
78    * Sets the socket timeout.
79    */

80   public void setReadTimeout(int ms)
81     throws IOException JavaDoc
82   {
83     _s.setSoTimeout(ms);
84   }
85
86   /**
87    * Returns the server inet address that accepted the request.
88    */

89   public InetAddress JavaDoc getLocalAddress()
90   {
91     return _s.getLocalAddress();
92   }
93   
94   /**
95    * Returns the server port that accepted the request.
96    */

97   public int getLocalPort()
98   {
99     return _s.getLocalPort();
100   }
101
102   /**
103    * Returns the remote client's inet address.
104    */

105   public long getRemoteIP()
106   {
107     InetAddress JavaDoc addr = _s.getInetAddress();
108     byte []bytes = addr.getAddress();
109
110     long address = 0;
111     for (int i = 0; i < bytes.length; i++)
112       address = 256 * address + (bytes[i] & 0xff);
113     
114     return address;
115   }
116
117   /**
118    * Returns the remote client's inet address.
119    */

120   public InetAddress JavaDoc getRemoteAddress()
121   {
122     return _s.getInetAddress();
123   }
124   
125   /**
126    * Returns the remote client's port.
127    */

128   public int getRemotePort()
129   {
130     return _s.getPort();
131   }
132
133   /**
134    * Returns true if the connection is secure.
135    */

136   public boolean isSecure()
137   {
138     if (_s == null || sslSocketClass == null)
139       return false;
140     else
141       return sslSocketClass.isAssignableFrom(_s.getClass());
142   }
143   /**
144    * Returns the secure cipher algorithm.
145    */

146   public String JavaDoc getCipherSuite()
147   {
148     if (! (_s instanceof SSLSocket))
149       return super.getCipherSuite();
150
151     SSLSocket sslSocket = (SSLSocket) _s;
152     
153     SSLSession sslSession = sslSocket.getSession();
154     
155     if (sslSession != null)
156       return sslSession.getCipherSuite();
157     else
158       return null;
159   }
160
161   /**
162    * Returns the bits in the socket.
163    */

164   public int getCipherBits()
165   {
166     if (! (_s instanceof SSLSocket))
167       return super.getCipherBits();
168     
169     SSLSocket sslSocket = (SSLSocket) _s;
170     
171     SSLSession sslSession = sslSocket.getSession();
172     
173     if (sslSession != null)
174       return sslKeySizes.get(sslSession.getCipherSuite());
175     else
176       return 0;
177   }
178   
179   /**
180    * Returns the client certificate.
181    */

182   public X509Certificate JavaDoc getClientCertificate()
183     throws CertificateException JavaDoc
184   {
185     X509Certificate JavaDoc []certs = getClientCertificates();
186
187     if (certs == null || certs.length == 0)
188       return null;
189     else
190       return certs[0];
191   }
192
193   /**
194    * Returns the client certificate.
195    */

196   public X509Certificate JavaDoc []getClientCertificates()
197     throws CertificateException JavaDoc
198   {
199     if (sslSocketClass == null)
200       return null;
201     else
202       return getClientCertificatesImpl();
203   }
204   
205   /**
206    * Returns the client certificate.
207    */

208   private X509Certificate JavaDoc []getClientCertificatesImpl()
209     throws CertificateException JavaDoc
210   {
211     if (! (_s instanceof SSLSocket))
212       return null;
213     
214     SSLSocket sslSocket = (SSLSocket) _s;
215
216     SSLSession sslSession = sslSocket.getSession();
217     if (sslSession == null)
218       return null;
219
220     String JavaDoc cipherSuite = sslSession.getCipherSuite();
221
222     try {
223       return (X509Certificate JavaDoc []) sslSession.getPeerCertificates();
224     } catch (SSLPeerUnverifiedException e) {
225       if (log.isLoggable(Level.FINEST))
226     log.log(Level.FINEST, e.toString(), e);
227       
228       return null;
229     } catch (Throwable JavaDoc e) {
230       log.log(Level.FINER, e.toString(), e);
231     }
232     
233     return null;
234   }
235
236   /**
237    * Returns the selectable channel.
238    */

239   public SelectableChannel JavaDoc getSelectableChannel()
240   {
241     if (_s != null)
242       return _s.getChannel();
243     else
244       return null;
245   }
246   
247   /**
248    * Returns the socket's input stream.
249    */

250   public StreamImpl getStream()
251     throws IOException JavaDoc
252   {
253     if (_streamImpl == null)
254       _streamImpl = new SocketStream();
255
256     _streamImpl.init(getInputStream(), getOutputStream());
257
258     return _streamImpl;
259   }
260   
261   /**
262    * Returns the socket's input stream.
263    */

264   private InputStream JavaDoc getInputStream()
265     throws IOException JavaDoc
266   {
267     if (_is == null)
268       _is = _s.getInputStream();
269
270     return _is;
271   }
272   
273   /**
274    * Returns the socket's output stream.
275    */

276   private OutputStream JavaDoc getOutputStream()
277     throws IOException JavaDoc
278   {
279     if (_os == null)
280       _os = _s.getOutputStream();
281
282     return _os;
283   }
284
285   public void resetTotalBytes()
286   {
287     if (_streamImpl != null)
288       _streamImpl.resetTotalBytes();
289   }
290
291   public long getTotalReadBytes()
292   {
293     return (_streamImpl == null) ? 0 : _streamImpl.getTotalReadBytes();
294   }
295
296   public long getTotalWriteBytes()
297   {
298     return (_streamImpl == null) ? 0 : _streamImpl.getTotalWriteBytes();
299   }
300
301   /**
302    * Returns true for closes.
303    */

304   public boolean isClosed()
305   {
306     return _s == null;
307   }
308
309   /**
310    * Closes the underlying socket.
311    */

312   public void close()
313     throws IOException JavaDoc
314   {
315     Socket s = _s;
316     _s = null;
317     
318     InputStream JavaDoc is = _is;
319     _is = null;
320     
321     OutputStream JavaDoc os = _os;
322     _os = null;
323
324     if (os != null) {
325       try {
326         os.close();
327       } catch (Exception JavaDoc e) {
328       }
329     }
330
331     if (is != null) {
332       try {
333         is.close();
334       } catch (Exception JavaDoc e) {
335       }
336     }
337
338     if (s != null) {
339       try {
340         s.close();
341       } catch (Exception JavaDoc e) {
342       }
343     }
344   }
345
346   public String JavaDoc toString()
347   {
348     return "QSocketWrapper[" + _s + "]";
349   }
350
351   static {
352     try {
353       sslSocketClass = Class.forName("javax.net.ssl.SSLSocket");
354     } catch (Throwable JavaDoc e) {
355     }
356
357     sslKeySizes = new IntMap();
358     sslKeySizes.put("SSL_DH_anon_WITH_DES_CBC_SHA", 56);
359     sslKeySizes.put("SSL_DH_anon_WITH_3DES_EDE_CBC_SHA", 168);
360     sslKeySizes.put("SSL_DH_anon_WITH_RC4_128_MD5", 128);
361     sslKeySizes.put("SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA", 40);
362     sslKeySizes.put("SSL_DH_anon_EXPORT_WITH_RC4_40_MD5", 40);
363     sslKeySizes.put("SSL_DHE_DSS_WITH_DES_CBC_SHA", 56);
364     sslKeySizes.put("SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA", 40);
365     sslKeySizes.put("SSL_RSA_WITH_RC4_128_MD5", 128);
366     sslKeySizes.put("SSL_RSA_WITH_RC4_128_SHA", 128);
367     sslKeySizes.put("SSL_RSA_WITH_DES_CBC_SHA", 56);
368     sslKeySizes.put("SSL_RSA_WITH_3DES_EDE_CBC_SHA", 168);
369     sslKeySizes.put("SSL_RSA_EXPORT_WITH_RC4_40_MD5", 40);
370     sslKeySizes.put("SSL_RSA_WITH_NULL_MD5", 0);
371     sslKeySizes.put("SSL_RSA_WITH_NULL_SHA", 0);
372     sslKeySizes.put("SSL_DSA_WITH_RC4_128_MD5", 128);
373     sslKeySizes.put("SSL_DSA_WITH_RC4_128_SHA", 128);
374     sslKeySizes.put("SSL_DSA_WITH_DES_CBC_SHA", 56);
375     sslKeySizes.put("SSL_DSA_WITH_3DES_EDE_CBC_SHA", 168);
376     sslKeySizes.put("SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA", 168);
377     sslKeySizes.put("SSL_DSA_EXPORT_WITH_RC4_40_MD5", 40);
378     sslKeySizes.put("SSL_DSA_WITH_NULL_MD5", 0);
379     sslKeySizes.put("SSL_DSA_WITH_NULL_SHA", 0);
380   }
381 }
382
383
Popular Tags