1 16 package org.apache.axis.components.net; 17 18 import org.apache.axis.components.logger.LogFactory; 19 import org.apache.axis.encoding.Base64; 20 import org.apache.axis.transport.http.HTTPConstants; 21 import org.apache.axis.utils.Messages; 22 import org.apache.commons.logging.Log; 23 24 import java.net.Socket ; 25 import java.util.Hashtable ; 26 import java.util.StringTokenizer ; 27 import java.lang.reflect.Constructor ; 28 import java.lang.reflect.Method ; 29 30 35 public class DefaultSocketFactory implements SocketFactory { 36 37 38 protected static Log log = 39 LogFactory.getLog(DefaultSocketFactory.class.getName()); 40 41 42 public static String CONNECT_TIMEOUT = "axis.client.connect.timeout"; 43 44 45 protected Hashtable attributes = null; 46 47 private static boolean plain; 48 private static Class inetClass; 49 private static Constructor inetConstructor; 50 private static Constructor socketConstructor; 51 private static Method connect; 52 53 static { 54 try { 55 inetClass = Class.forName("java.net.InetSocketAddress"); 56 plain = false; 57 inetConstructor = inetClass.getConstructor(new Class []{String .class, int.class}); 58 socketConstructor = Socket .class.getConstructor(new Class []{}); 59 connect = Socket .class.getMethod("connect", new Class []{inetClass.getSuperclass(), 60 int.class}); 61 } catch (Exception e) { 62 plain = true; 63 } 64 } 65 66 71 public DefaultSocketFactory(Hashtable attributes) { 72 this.attributes = attributes; 73 } 74 75 87 public Socket create( 88 String host, int port, StringBuffer otherHeaders, BooleanHolder useFullURL) 89 throws Exception { 90 91 int timeout = 0; 92 if (attributes != null) { 93 String value = (String )attributes.get(CONNECT_TIMEOUT); 94 timeout = (value != null) ? Integer.parseInt(value) : 0; 95 } 96 97 TransportClientProperties tcp = TransportClientPropertiesFactory.create("http"); 98 99 Socket sock = null; 100 boolean hostInNonProxyList = isHostInNonProxyList(host, tcp.getNonProxyHosts()); 101 102 if (tcp.getProxyUser().length() != 0) { 103 StringBuffer tmpBuf = new StringBuffer (); 104 105 tmpBuf.append(tcp.getProxyUser()) 106 .append(":") 107 .append(tcp.getProxyPassword()); 108 otherHeaders.append(HTTPConstants.HEADER_PROXY_AUTHORIZATION) 109 .append(": Basic ") 110 .append(Base64.encode(tmpBuf.toString().getBytes())) 111 .append("\r\n"); 112 } 113 if (port == -1) { 114 port = 80; 115 } 116 if ((tcp.getProxyHost().length() == 0) || 117 (tcp.getProxyPort().length() == 0) || 118 hostInNonProxyList) 119 { 120 sock = create(host, port, timeout); 121 if (log.isDebugEnabled()) { 122 log.debug(Messages.getMessage("createdHTTP00")); 123 } 124 } else { 125 sock = create(tcp.getProxyHost(), 126 new Integer (tcp.getProxyPort()).intValue(), 127 timeout); 128 if (log.isDebugEnabled()) { 129 log.debug(Messages.getMessage("createdHTTP01", tcp.getProxyHost(), 130 tcp.getProxyPort())); 131 } 132 useFullURL.value = true; 133 } 134 return sock; 135 } 136 137 146 private static Socket create(String host, int port, int timeout) throws Exception { 147 Socket sock = null; 148 if (plain || timeout == 0) { 149 sock = new Socket (host, port); 150 } else { 151 Object address = inetConstructor.newInstance(new Object []{host, new Integer (port)}); 152 sock = (Socket )socketConstructor.newInstance(new Object []{}); 153 connect.invoke(sock, new Object []{address, new Integer (timeout)}); 154 } 155 return sock; 156 } 157 158 166 protected boolean isHostInNonProxyList(String host, String nonProxyHosts) { 167 168 if ((nonProxyHosts == null) || (host == null)) { 169 return false; 170 } 171 172 176 StringTokenizer tokenizer = new StringTokenizer (nonProxyHosts, "|\""); 177 178 while (tokenizer.hasMoreTokens()) { 179 String pattern = tokenizer.nextToken(); 180 181 if (log.isDebugEnabled()) { 182 log.debug(Messages.getMessage("match00", 183 new String []{"HTTPSender", 184 host, 185 pattern})); 186 } 187 if (match(pattern, host, false)) { 188 return true; 189 } 190 } 191 return false; 192 } 193 194 207 protected static boolean match(String pattern, String str, 208 boolean isCaseSensitive) { 209 210 char[] patArr = pattern.toCharArray(); 211 char[] strArr = str.toCharArray(); 212 int patIdxStart = 0; 213 int patIdxEnd = patArr.length - 1; 214 int strIdxStart = 0; 215 int strIdxEnd = strArr.length - 1; 216 char ch; 217 boolean containsStar = false; 218 219 for (int i = 0; i < patArr.length; i++) { 220 if (patArr[i] == '*') { 221 containsStar = true; 222 break; 223 } 224 } 225 if (!containsStar) { 226 227 if (patIdxEnd != strIdxEnd) { 229 return false; } 231 for (int i = 0; i <= patIdxEnd; i++) { 232 ch = patArr[i]; 233 if (isCaseSensitive && (ch != strArr[i])) { 234 return false; } 236 if (!isCaseSensitive 237 && (Character.toUpperCase(ch) 238 != Character.toUpperCase(strArr[i]))) { 239 return false; } 241 } 242 return true; } 244 if (patIdxEnd == 0) { 245 return true; } 247 248 while ((ch = patArr[patIdxStart]) != '*' 250 && (strIdxStart <= strIdxEnd)) { 251 if (isCaseSensitive && (ch != strArr[strIdxStart])) { 252 return false; } 254 if (!isCaseSensitive 255 && (Character.toUpperCase(ch) 256 != Character.toUpperCase(strArr[strIdxStart]))) { 257 return false; } 259 patIdxStart++; 260 strIdxStart++; 261 } 262 if (strIdxStart > strIdxEnd) { 263 264 for (int i = patIdxStart; i <= patIdxEnd; i++) { 267 if (patArr[i] != '*') { 268 return false; 269 } 270 } 271 return true; 272 } 273 274 while ((ch = patArr[patIdxEnd]) != '*' && (strIdxStart <= strIdxEnd)) { 276 if (isCaseSensitive && (ch != strArr[strIdxEnd])) { 277 return false; } 279 if (!isCaseSensitive 280 && (Character.toUpperCase(ch) 281 != Character.toUpperCase(strArr[strIdxEnd]))) { 282 return false; } 284 patIdxEnd--; 285 strIdxEnd--; 286 } 287 if (strIdxStart > strIdxEnd) { 288 289 for (int i = patIdxStart; i <= patIdxEnd; i++) { 292 if (patArr[i] != '*') { 293 return false; 294 } 295 } 296 return true; 297 } 298 299 while ((patIdxStart != patIdxEnd) && (strIdxStart <= strIdxEnd)) { 302 int patIdxTmp = -1; 303 304 for (int i = patIdxStart + 1; i <= patIdxEnd; i++) { 305 if (patArr[i] == '*') { 306 patIdxTmp = i; 307 break; 308 } 309 } 310 if (patIdxTmp == patIdxStart + 1) { 311 312 patIdxStart++; 314 continue; 315 } 316 317 int patLength = (patIdxTmp - patIdxStart - 1); 320 int strLength = (strIdxEnd - strIdxStart + 1); 321 int foundIdx = -1; 322 323 strLoop: 324 for (int i = 0; i <= strLength - patLength; i++) { 325 for (int j = 0; j < patLength; j++) { 326 ch = patArr[patIdxStart + j + 1]; 327 if (isCaseSensitive 328 && (ch != strArr[strIdxStart + i + j])) { 329 continue strLoop; 330 } 331 if (!isCaseSensitive && (Character 332 .toUpperCase(ch) != Character 333 .toUpperCase(strArr[strIdxStart + i + j]))) { 334 continue strLoop; 335 } 336 } 337 foundIdx = strIdxStart + i; 338 break; 339 } 340 if (foundIdx == -1) { 341 return false; 342 } 343 patIdxStart = patIdxTmp; 344 strIdxStart = foundIdx + patLength; 345 } 346 347 for (int i = patIdxStart; i <= patIdxEnd; i++) { 350 if (patArr[i] != '*') { 351 return false; 352 } 353 } 354 return true; 355 } 356 } 357 | Popular Tags |