1 55 package org.jboss.axis.components.net; 56 57 import java.net.Socket ; 58 import java.util.HashMap ; 59 import java.util.StringTokenizer ; 60 61 import org.jboss.axis.encoding.Base64; 62 import org.jboss.axis.transport.http.HTTPConstants; 63 import org.jboss.axis.utils.Messages; 64 import org.jboss.logging.Logger; 65 66 71 public class DefaultSocketFactory implements SocketFactory 72 { 73 74 77 private static Logger log = Logger.getLogger(DefaultSocketFactory.class.getName()); 78 79 82 protected HashMap options = null; 83 84 89 public DefaultSocketFactory(HashMap options) 90 { 91 this.options = options; 92 } 93 94 104 public Socket create(String host, int port, StringBuffer otherHeaders, BooleanHolder useFullURL) 105 throws Exception 106 { 107 108 TransportClientProperties tcp = TransportClientPropertiesFactory.create("http"); 109 110 Socket sock = null; 111 boolean hostInNonProxyList = isHostInNonProxyList(host, tcp.getNonProxyHosts()); 112 113 if (tcp.getProxyUser().length() != 0) 114 { 115 StringBuffer tmpBuf = new StringBuffer (); 116 117 tmpBuf.append(tcp.getProxyUser()) 118 .append(":") 119 .append(tcp.getProxyPassword()); 120 otherHeaders.append(HTTPConstants.HEADER_PROXY_AUTHORIZATION) 121 .append(": Basic ") 122 .append(Base64.encode(tmpBuf.toString().getBytes())) 123 .append("\r\n"); 124 } 125 if (port == -1) 126 { 127 port = 80; 128 } 129 if ((tcp.getProxyHost().length() == 0) || 130 (tcp.getProxyPort().length() == 0) || 131 hostInNonProxyList) 132 { 133 sock = new Socket (host, port); 134 if (log.isDebugEnabled()) 135 { 136 log.debug(Messages.getMessage("createdHTTP00")); 137 } 138 } 139 else 140 { 141 sock = new Socket (tcp.getProxyHost(), 142 new Integer (tcp.getProxyPort()).intValue()); 143 if (log.isDebugEnabled()) 144 { 145 log.debug(Messages.getMessage("createdHTTP01", tcp.getProxyHost(), 146 tcp.getProxyPort())); 147 } 148 useFullURL.value = true; 149 } 150 return sock; 151 } 152 153 160 protected boolean isHostInNonProxyList(String host, String nonProxyHosts) 161 { 162 163 if ((nonProxyHosts == null) || (host == null)) 164 { 165 return false; 166 } 167 168 172 StringTokenizer tokenizer = new StringTokenizer (nonProxyHosts, "|\""); 173 174 while (tokenizer.hasMoreTokens()) 175 { 176 String pattern = tokenizer.nextToken(); 177 178 if (log.isDebugEnabled()) 179 { 180 log.debug(Messages.getMessage("match00", 181 new String []{"HTTPSender", 182 host, 183 pattern})); 184 } 185 if (match(pattern, host, false)) 186 { 187 return true; 188 } 189 } 190 return false; 191 } 192 193 205 protected static boolean match(String pattern, String str, 206 boolean isCaseSensitive) 207 { 208 209 char[] patArr = pattern.toCharArray(); 210 char[] strArr = str.toCharArray(); 211 int patIdxStart = 0; 212 int patIdxEnd = patArr.length - 1; 213 int strIdxStart = 0; 214 int strIdxEnd = strArr.length - 1; 215 char ch; 216 boolean containsStar = false; 217 218 for (int i = 0; i < patArr.length; i++) 219 { 220 if (patArr[i] == '*') 221 { 222 containsStar = true; 223 break; 224 } 225 } 226 if (!containsStar) 227 { 228 229 if (patIdxEnd != strIdxEnd) 231 { 232 return false; } 234 for (int i = 0; i <= patIdxEnd; i++) 235 { 236 ch = patArr[i]; 237 if (isCaseSensitive && (ch != strArr[i])) 238 { 239 return false; } 241 if (!isCaseSensitive 242 && (Character.toUpperCase(ch) 243 != Character.toUpperCase(strArr[i]))) 244 { 245 return false; } 247 } 248 return true; } 250 if (patIdxEnd == 0) 251 { 252 return true; } 254 255 while ((ch = patArr[patIdxStart]) != '*' 257 && (strIdxStart <= strIdxEnd)) 258 { 259 if (isCaseSensitive && (ch != strArr[strIdxStart])) 260 { 261 return false; } 263 if (!isCaseSensitive 264 && (Character.toUpperCase(ch) 265 != Character.toUpperCase(strArr[strIdxStart]))) 266 { 267 return false; } 269 patIdxStart++; 270 strIdxStart++; 271 } 272 if (strIdxStart > strIdxEnd) 273 { 274 275 for (int i = patIdxStart; i <= patIdxEnd; i++) 278 { 279 if (patArr[i] != '*') 280 { 281 return false; 282 } 283 } 284 return true; 285 } 286 287 while ((ch = patArr[patIdxEnd]) != '*' && (strIdxStart <= strIdxEnd)) 289 { 290 if (isCaseSensitive && (ch != strArr[strIdxEnd])) 291 { 292 return false; } 294 if (!isCaseSensitive 295 && (Character.toUpperCase(ch) 296 != Character.toUpperCase(strArr[strIdxEnd]))) 297 { 298 return false; } 300 patIdxEnd--; 301 strIdxEnd--; 302 } 303 if (strIdxStart > strIdxEnd) 304 { 305 306 for (int i = patIdxStart; i <= patIdxEnd; i++) 309 { 310 if (patArr[i] != '*') 311 { 312 return false; 313 } 314 } 315 return true; 316 } 317 318 while ((patIdxStart != patIdxEnd) && (strIdxStart <= strIdxEnd)) 321 { 322 int patIdxTmp = -1; 323 324 for (int i = patIdxStart + 1; i <= patIdxEnd; i++) 325 { 326 if (patArr[i] == '*') 327 { 328 patIdxTmp = i; 329 break; 330 } 331 } 332 if (patIdxTmp == patIdxStart + 1) 333 { 334 335 patIdxStart++; 337 continue; 338 } 339 340 int patLength = (patIdxTmp - patIdxStart - 1); 343 int strLength = (strIdxEnd - strIdxStart + 1); 344 int foundIdx = -1; 345 346 strLoop: 347 for (int i = 0; i <= strLength - patLength; i++) 348 { 349 for (int j = 0; j < patLength; j++) 350 { 351 ch = patArr[patIdxStart + j + 1]; 352 if (isCaseSensitive 353 && (ch != strArr[strIdxStart + i + j])) 354 { 355 continue strLoop; 356 } 357 if (!isCaseSensitive && (Character 358 .toUpperCase(ch) != Character 359 .toUpperCase(strArr[strIdxStart + i + j]))) 360 { 361 continue strLoop; 362 } 363 } 364 foundIdx = strIdxStart + i; 365 break; 366 } 367 if (foundIdx == -1) 368 { 369 return false; 370 } 371 patIdxStart = patIdxTmp; 372 strIdxStart = foundIdx + patLength; 373 } 374 375 for (int i = patIdxStart; i <= patIdxEnd; i++) 378 { 379 if (patArr[i] != '*') 380 { 381 return false; 382 } 383 } 384 return true; 385 } 386 } 387 | Popular Tags |