/[aagtl_public1]/src/com/byarger/exchangeit/EasySSLSocketFactory.java
aagtl

Contents of /src/com/byarger/exchangeit/EasySSLSocketFactory.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2 - (show annotations) (download)
Sun Aug 5 13:48:36 2012 UTC (11 years, 7 months ago) by zoffadmin
File size: 4545 byte(s)
initial import of aagtl source code
1 package com.byarger.exchangeit;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.io.IOException;
23 import java.net.InetAddress;
24 import java.net.InetSocketAddress;
25 import java.net.Socket;
26 import java.net.UnknownHostException;
27
28 import javax.net.ssl.SSLContext;
29 import javax.net.ssl.SSLSocket;
30 import javax.net.ssl.TrustManager;
31
32 import org.apache.http.conn.ConnectTimeoutException;
33 import org.apache.http.conn.scheme.LayeredSocketFactory;
34 import org.apache.http.params.HttpConnectionParams;
35 import org.apache.http.params.HttpParams;
36
37 /**
38 * This socket factory will create ssl socket that accepts self signed
39 * certificate
40 *
41 * @author olamy
42 * @version $Id: EasySSLSocketFactory.java 765355 2009-04-15 20:59:07Z evenisse
43 * $
44 * @since 1.2.3
45 */
46 public class EasySSLSocketFactory implements LayeredSocketFactory
47 {
48
49 private SSLContext sslcontext = null;
50
51 private static SSLContext createEasySSLContext() throws IOException
52 {
53 try
54 {
55 SSLContext context = SSLContext.getInstance("TLS");
56 context.init(null, new TrustManager[] { new EasyX509TrustManager(null) }, null);
57 return context;
58 }
59 catch (Exception e)
60 {
61 throw new IOException(e.getMessage());
62 }
63 }
64
65 private SSLContext getSSLContext() throws IOException
66 {
67 if (this.sslcontext == null)
68 {
69 this.sslcontext = createEasySSLContext();
70 }
71 return this.sslcontext;
72 }
73
74 /**
75 * @see org.apache.http.conn.scheme.SocketFactory#connectSocket(java.net.Socket, java.lang.String, int, java.net.InetAddress, int, org.apache.http.params.HttpParams)
76 */
77 public Socket connectSocket(Socket sock, String host, int port, InetAddress localAddress, int localPort, HttpParams params) throws IOException, UnknownHostException, ConnectTimeoutException
78 {
79 int connTimeout = HttpConnectionParams.getConnectionTimeout(params);
80 int soTimeout = HttpConnectionParams.getSoTimeout(params);
81
82 InetSocketAddress remoteAddress = new InetSocketAddress(host, port);
83 SSLSocket sslsock = (SSLSocket) ((sock != null) ? sock : createSocket());
84
85 if ((localAddress != null) || (localPort > 0))
86 {
87 // we need to bind explicitly
88 if (localPort < 0)
89 {
90 localPort = 0; // indicates "any"
91 }
92 InetSocketAddress isa = new InetSocketAddress(localAddress, localPort);
93 sslsock.bind(isa);
94 }
95
96 sslsock.connect(remoteAddress, connTimeout);
97 sslsock.setSoTimeout(soTimeout);
98 return sslsock;
99
100 }
101
102 /**
103 * @see org.apache.http.conn.scheme.SocketFactory#createSocket()
104 */
105 public Socket createSocket() throws IOException
106 {
107 return getSSLContext().getSocketFactory().createSocket();
108 }
109
110 /**
111 * @see org.apache.http.conn.scheme.SocketFactory#isSecure(java.net.Socket)
112 */
113 public boolean isSecure(Socket socket) throws IllegalArgumentException
114 {
115 return true;
116 }
117
118 /**
119 * @see org.apache.http.conn.scheme.LayeredSocketFactory#createSocket(java.net.Socket, java.lang.String, int, boolean)
120 */
121 public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException, UnknownHostException
122 {
123 return getSSLContext().getSocketFactory().createSocket(socket, host, port, autoClose);
124 }
125
126 // -------------------------------------------------------------------
127 // javadoc in org.apache.http.conn.scheme.SocketFactory says :
128 // Both Object.equals() and Object.hashCode() must be overridden
129 // for the correct operation of some connection managers
130 // -------------------------------------------------------------------
131
132 public boolean equals(Object obj)
133 {
134 return ((obj != null) && obj.getClass().equals(EasySSLSocketFactory.class));
135 }
136
137 public int hashCode()
138 {
139 return EasySSLSocketFactory.class.hashCode();
140 }
141
142 }

   
Visit the aagtl Website