KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > components > net > JSSESocketFactory


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Axis" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation. For more
52  * information on the Apache Software Foundation, please see
53  * <http://www.apache.org/>.
54  */

55 package org.jboss.axis.components.net;
56
57 import java.io.BufferedWriter JavaDoc;
58 import java.io.IOException JavaDoc;
59 import java.io.InputStream JavaDoc;
60 import java.io.OutputStream JavaDoc;
61 import java.io.OutputStreamWriter JavaDoc;
62 import java.io.PrintWriter JavaDoc;
63 import java.net.Socket JavaDoc;
64 import java.util.HashMap JavaDoc;
65
66 import javax.net.ssl.SSLSocket;
67 import javax.net.ssl.SSLSocketFactory;
68
69 import org.jboss.axis.utils.Messages;
70 import org.jboss.axis.utils.XMLUtils;
71 import org.jboss.logging.Logger;
72
73
74 /**
75  * SSL socket factory. It _requires_ a valid RSA key and
76  * JSSE. (borrowed code from tomcat)
77  * <p/>
78  * THIS CODE STILL HAS DEPENDENCIES ON sun.* and com.sun.*
79  *
80  * @author Davanum Srinivas (dims@yahoo.com)
81  */

82 public class JSSESocketFactory extends DefaultSocketFactory implements SecureSocketFactory
83 {
84
85    private static Logger log = Logger.getLogger(JSSESocketFactory.class.getName());
86
87    /**
88     * Field sslFactory
89     */

90    protected SSLSocketFactory sslFactory = null;
91
92    /**
93     * Constructor JSSESocketFactory
94     *
95     * @param attributes
96     */

97    public JSSESocketFactory(HashMap JavaDoc options)
98    {
99       super(options);
100    }
101
102    /**
103     * Initialize the SSLSocketFactory
104     *
105     * @throws IOException
106     */

107    protected void initFactory() throws IOException JavaDoc
108    {
109       sslFactory = (SSLSocketFactory)SSLSocketFactory.getDefault();
110    }
111
112    /**
113     * creates a secure socket
114     *
115     * @param host
116     * @param port
117     * @param otherHeaders
118     * @param useFullURL
119     * @return Socket
120     * @throws Exception
121     */

122    public Socket JavaDoc create(String JavaDoc host, int port, StringBuffer JavaDoc otherHeaders, BooleanHolder useFullURL)
123            throws Exception JavaDoc
124    {
125       if (sslFactory == null)
126       {
127          initFactory();
128       }
129       if (port == -1)
130       {
131          port = 443;
132       }
133
134       TransportClientProperties tcp = TransportClientPropertiesFactory.create("https");
135
136       boolean hostInNonProxyList = isHostInNonProxyList(host, tcp.getNonProxyHosts());
137
138       Socket JavaDoc sslSocket = null;
139       if (tcp.getProxyHost().length() == 0 || hostInNonProxyList)
140       {
141          // direct SSL connection
142
sslSocket = sslFactory.createSocket(host, port);
143       }
144       else
145       {
146
147          // Default proxy port is 80, even for https
148
int tunnelPort = (tcp.getProxyPort().length() != 0)
149                  ? Integer.parseInt(tcp.getProxyPort())
150                  : 80;
151          if (tunnelPort < 0)
152             tunnelPort = 80;
153
154          // Create the regular socket connection to the proxy
155
Socket JavaDoc tunnel = new Socket JavaDoc(tcp.getProxyHost(), tunnelPort);
156
157          // The tunnel handshake method (condensed and made reflexive)
158
OutputStream JavaDoc tunnelOutputStream = tunnel.getOutputStream();
159          PrintWriter JavaDoc out = new PrintWriter JavaDoc(new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(tunnelOutputStream)));
160
161          // More secure version... engage later?
162
// PasswordAuthentication pa =
163
// Authenticator.requestPasswordAuthentication(
164
// InetAddress.getByName(tunnelHost),
165
// tunnelPort, "SOCK", "Proxy","HTTP");
166
// if(pa == null){
167
// printDebug("No Authenticator set.");
168
// }else{
169
// printDebug("Using Authenticator.");
170
// tunnelUser = pa.getUserName();
171
// tunnelPassword = new String(pa.getPassword());
172
// }
173
out.print("CONNECT " + host + ":" + port + " HTTP/1.0\r\n"
174                  + "User-Agent: AxisClient");
175          if (tcp.getProxyUser().length() != 0 &&
176                  tcp.getProxyPassword().length() != 0)
177          {
178
179             // add basic authentication header for the proxy
180
String JavaDoc encodedPassword = XMLUtils.base64encode((tcp.getProxyUser()
181                     + ":"
182                     + tcp.getProxyPassword()).getBytes());
183
184             out.print("\nProxy-Authorization: Basic " + encodedPassword);
185          }
186          out.print("\nContent-Length: 0");
187          out.print("\nPragma: no-cache");
188          out.print("\r\n\r\n");
189          out.flush();
190          InputStream JavaDoc tunnelInputStream = tunnel.getInputStream();
191
192          if (log.isDebugEnabled())
193          {
194             log.debug(Messages.getMessage("isNull00", "tunnelInputStream",
195                     "" + (tunnelInputStream
196                     == null)));
197          }
198          String JavaDoc replyStr = "";
199
200          // Make sure to read all the response from the proxy to prevent SSL negotiation failure
201
// Response message terminated by two sequential newlines
202
int newlinesSeen = 0;
203          boolean headerDone = false; /* Done on first newline */
204
205          while (newlinesSeen < 2)
206          {
207             int i = tunnelInputStream.read();
208
209             if (i < 0)
210             {
211                throw new IOException JavaDoc("Unexpected EOF from proxy");
212             }
213             if (i == '\n')
214             {
215                headerDone = true;
216                ++newlinesSeen;
217             }
218             else if (i != '\r')
219             {
220                newlinesSeen = 0;
221                if (!headerDone)
222                {
223                   replyStr += String.valueOf((char)i);
224                }
225             }
226          }
227          if (!replyStr.startsWith("HTTP/1.0 200")
228                  && !replyStr.startsWith("HTTP/1.1 200"))
229          {
230             throw new IOException JavaDoc(Messages.getMessage("cantTunnel00",
231                     new String JavaDoc[]{
232                        tcp.getProxyHost(),
233                        "" + tunnelPort,
234                        replyStr}));
235          }
236
237          // End of condensed reflective tunnel handshake method
238
sslSocket = sslFactory.createSocket(tunnel, host, port, true);
239          if (log.isDebugEnabled())
240          {
241             log.debug(Messages.getMessage("setupTunnel00",
242                     tcp.getProxyHost(),
243                     "" + tunnelPort));
244          }
245       }
246
247       ((SSLSocket)sslSocket).startHandshake();
248       if (log.isDebugEnabled())
249       {
250          log.debug(Messages.getMessage("createdSSL00"));
251       }
252       return sslSocket;
253    }
254 }
255
Popular Tags