KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > httpclient > contrib > ssl > SocketFactoryWrapper


1 /*
2  * $Header$
3  * $Revision: 480424 $
4  * $Date: 2006-11-29 05:56:49 +0000 (Wed, 29 Nov 2006) $
5  *
6  * ====================================================================
7  *
8  * Licensed to the Apache Software Foundation (ASF) under one or more
9  * contributor license agreements. See the NOTICE file distributed with
10  * this work for additional information regarding copyright ownership.
11  * The ASF licenses this file to You under the Apache License, Version 2.0
12  * (the "License"); you may not use this file except in compliance with
13  * the License. You may obtain a copy of the License at
14  *
15  * http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * ====================================================================
23  *
24  * This software consists of voluntary contributions made by many
25  * individuals on behalf of the Apache Software Foundation. For more
26  * information on the Apache Software Foundation, please see
27  * <http://www.apache.org/>.
28  *
29  */

30 package org.apache.commons.httpclient.contrib.ssl;
31
32 import java.io.IOException JavaDoc;
33 import java.net.InetAddress JavaDoc;
34 import java.net.InetSocketAddress JavaDoc;
35 import java.net.Socket JavaDoc;
36 import java.net.SocketAddress JavaDoc;
37 import java.net.UnknownHostException JavaDoc;
38 import javax.net.ssl.SSLSocketFactory;
39 import org.apache.commons.httpclient.ConnectTimeoutException;
40 import org.apache.commons.httpclient.params.HttpConnectionParams;
41 import org.apache.commons.httpclient.protocol.SecureProtocolSocketFactory;
42
43 /**
44  * Author: Mark Claassen
45  * <p>
46  * Uses some code from EasySSLProtocolSocketFactory.java
47  *
48  * <p>
49  * Wraps a SSLSocketFactory with a SecureProtocolSocketFactory.
50  * <p>
51  * This was designed to make HttpClient work in situations where an application is being deployed by
52  * Java Web Start. In these cases, SSL connections are negotiated by webstart implementations of the
53  * KeyManager and TrustManager. Wrapping the socket factory obtained from
54  * HttpsURLConnection.getDefaultSocketFactory allows the use of HttpClient while still leveraging
55  * Java Web Start's handling of SSL certificates
56  */

57 public class SocketFactoryWrapper implements SecureProtocolSocketFactory {
58
59     private SSLSocketFactory socketFactory;
60
61     public SocketFactoryWrapper(SSLSocketFactory socketFactory) {
62         this.socketFactory = socketFactory;
63     }
64
65     public Socket JavaDoc createSocket(String JavaDoc host, int port) throws IOException JavaDoc, UnknownHostException JavaDoc {
66         return socketFactory.createSocket(host, port);
67     }
68
69     public Socket JavaDoc createSocket(String JavaDoc host, int port, InetAddress JavaDoc localAddress, int localPort)
70             throws IOException JavaDoc, UnknownHostException JavaDoc {
71         return socketFactory.createSocket(host, port, localAddress, localPort);
72     }
73
74     public Socket JavaDoc createSocket(
75             String JavaDoc host,
76             int port, InetAddress JavaDoc localAddress, int localPort,
77             HttpConnectionParams params) throws IOException JavaDoc, UnknownHostException JavaDoc,
78             ConnectTimeoutException {
79         // Based on code from EasySSLProtocolSocketFactory.java
80
Socket JavaDoc rval;
81         if (params == null) {
82             throw new IllegalArgumentException JavaDoc("Parameters may not be null");
83         }
84         int timeout = params.getConnectionTimeout();
85         if (timeout == 0) {
86             rval = socketFactory.createSocket(host, port, localAddress, localPort);
87         } else {
88             rval = socketFactory.createSocket();
89             SocketAddress JavaDoc localaddr = new InetSocketAddress JavaDoc(localAddress, localPort);
90             SocketAddress JavaDoc remoteaddr = new InetSocketAddress JavaDoc(host, port);
91             rval.bind(localaddr);
92             rval.connect(remoteaddr, timeout);
93         }
94         return rval;
95     }
96
97     public Socket JavaDoc createSocket(Socket JavaDoc socket, String JavaDoc host, int port, boolean autoClose)
98             throws IOException JavaDoc, UnknownHostException JavaDoc {
99         return socketFactory.createSocket(socket, host, port, autoClose);
100     }
101
102 }
103
Popular Tags