1 55 package org.jboss.axis.components.net; 56 57 import java.io.BufferedWriter ; 58 import java.io.IOException ; 59 import java.io.InputStream ; 60 import java.io.OutputStream ; 61 import java.io.OutputStreamWriter ; 62 import java.io.PrintWriter ; 63 import java.net.Socket ; 64 import java.util.HashMap ; 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 82 public class JSSESocketFactory extends DefaultSocketFactory implements SecureSocketFactory 83 { 84 85 private static Logger log = Logger.getLogger(JSSESocketFactory.class.getName()); 86 87 90 protected SSLSocketFactory sslFactory = null; 91 92 97 public JSSESocketFactory(HashMap options) 98 { 99 super(options); 100 } 101 102 107 protected void initFactory() throws IOException 108 { 109 sslFactory = (SSLSocketFactory)SSLSocketFactory.getDefault(); 110 } 111 112 122 public Socket create(String host, int port, StringBuffer otherHeaders, BooleanHolder useFullURL) 123 throws Exception 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 sslSocket = null; 139 if (tcp.getProxyHost().length() == 0 || hostInNonProxyList) 140 { 141 sslSocket = sslFactory.createSocket(host, port); 143 } 144 else 145 { 146 147 int tunnelPort = (tcp.getProxyPort().length() != 0) 149 ? Integer.parseInt(tcp.getProxyPort()) 150 : 80; 151 if (tunnelPort < 0) 152 tunnelPort = 80; 153 154 Socket tunnel = new Socket (tcp.getProxyHost(), tunnelPort); 156 157 OutputStream tunnelOutputStream = tunnel.getOutputStream(); 159 PrintWriter out = new PrintWriter (new BufferedWriter (new OutputStreamWriter (tunnelOutputStream))); 160 161 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 String 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 tunnelInputStream = tunnel.getInputStream(); 191 192 if (log.isDebugEnabled()) 193 { 194 log.debug(Messages.getMessage("isNull00", "tunnelInputStream", 195 "" + (tunnelInputStream 196 == null))); 197 } 198 String replyStr = ""; 199 200 int newlinesSeen = 0; 203 boolean headerDone = false; 204 205 while (newlinesSeen < 2) 206 { 207 int i = tunnelInputStream.read(); 208 209 if (i < 0) 210 { 211 throw new IOException ("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 (Messages.getMessage("cantTunnel00", 231 new String []{ 232 tcp.getProxyHost(), 233 "" + tunnelPort, 234 replyStr})); 235 } 236 237 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 |