1 16 package org.apache.axis.components.net; 17 18 import org.apache.axis.utils.Messages; 19 import org.apache.axis.utils.XMLUtils; 20 import org.apache.axis.utils.StringUtils; 21 22 import javax.net.ssl.SSLSocket; 23 import javax.net.ssl.SSLSocketFactory; 24 import java.io.BufferedWriter ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.io.OutputStream ; 28 import java.io.OutputStreamWriter ; 29 import java.io.PrintWriter ; 30 import java.net.Socket ; 31 import java.util.Hashtable ; 32 33 34 42 public class JSSESocketFactory extends DefaultSocketFactory implements SecureSocketFactory { 43 44 45 protected SSLSocketFactory sslFactory = null; 46 47 52 public JSSESocketFactory(Hashtable attributes) { 53 super(attributes); 54 } 55 56 60 protected void initFactory() throws IOException { 61 sslFactory = (SSLSocketFactory)SSLSocketFactory.getDefault(); 62 } 63 64 75 public Socket create( 76 String host, int port, StringBuffer otherHeaders, BooleanHolder useFullURL) 77 throws Exception { 78 if (sslFactory == null) { 79 initFactory(); 80 } 81 if (port == -1) { 82 port = 443; 83 } 84 85 TransportClientProperties tcp = TransportClientPropertiesFactory.create("https"); 86 87 boolean hostInNonProxyList = isHostInNonProxyList(host, tcp.getNonProxyHosts()); 88 89 Socket sslSocket = null; 90 if (tcp.getProxyHost().length() == 0 || hostInNonProxyList) { 91 sslSocket = sslFactory.createSocket(host, port); 93 } else { 94 95 int tunnelPort = (tcp.getProxyPort().length() != 0) 97 ? Integer.parseInt(tcp.getProxyPort()) 98 : 80; 99 if (tunnelPort < 0) 100 tunnelPort = 80; 101 102 Socket tunnel = new Socket(tcp.getProxyHost(), tunnelPort); 104 105 OutputStream tunnelOutputStream = tunnel.getOutputStream(); 107 PrintWriter out = new PrintWriter ( 108 new BufferedWriter (new OutputStreamWriter (tunnelOutputStream))); 109 110 out.print("CONNECT " + host + ":" + port + " HTTP/1.0\r\n" 123 + "User-Agent: AxisClient"); 124 if (tcp.getProxyUser().length() != 0 && 125 tcp.getProxyPassword().length() != 0) { 126 127 String encodedPassword = XMLUtils.base64encode((tcp.getProxyUser() 129 + ":" 130 + tcp.getProxyPassword()).getBytes()); 131 132 out.print("\nProxy-Authorization: Basic " + encodedPassword); 133 } 134 out.print("\nContent-Length: 0"); 135 out.print("\nPragma: no-cache"); 136 out.print("\r\n\r\n"); 137 out.flush(); 138 InputStream tunnelInputStream = tunnel.getInputStream(); 139 140 if (log.isDebugEnabled()) { 141 log.debug(Messages.getMessage("isNull00", "tunnelInputStream", 142 "" + (tunnelInputStream 143 == null))); 144 } 145 String replyStr = ""; 146 147 int newlinesSeen = 0; 150 boolean headerDone = false; 151 152 while (newlinesSeen < 2) { 153 int i = tunnelInputStream.read(); 154 155 if (i < 0) { 156 throw new IOException ("Unexpected EOF from proxy"); 157 } 158 if (i == '\n') { 159 headerDone = true; 160 ++newlinesSeen; 161 } else if (i != '\r') { 162 newlinesSeen = 0; 163 if (!headerDone) { 164 replyStr += String.valueOf((char) i); 165 } 166 } 167 } 168 if (StringUtils.startsWithIgnoreWhitespaces("HTTP/1.0 200", replyStr) && 169 StringUtils.startsWithIgnoreWhitespaces("HTTP/1.1 200", replyStr)) { 170 throw new IOException (Messages.getMessage("cantTunnel00", 171 new String []{ 172 tcp.getProxyHost(), 173 "" + tunnelPort, 174 replyStr})); 175 } 176 177 sslSocket = sslFactory.createSocket(tunnel, host, port, true); 179 if (log.isDebugEnabled()) { 180 log.debug(Messages.getMessage("setupTunnel00", 181 tcp.getProxyHost(), 182 "" + tunnelPort)); 183 } 184 } 185 186 ((SSLSocket) sslSocket).startHandshake(); 187 if (log.isDebugEnabled()) { 188 log.debug(Messages.getMessage("createdSSL00")); 189 } 190 return sslSocket; 191 } 192 } 193 | Popular Tags |