1 57 58 package org.apache.soap.util.net; 59 60 import java.net.*; 61 import java.io.*; 62 import java.util.*; 63 64 import org.apache.soap.*; 65 import org.apache.soap.rpc.*; 66 import org.apache.soap.transport.*; 67 import org.apache.soap.util.mime.*; 68 import javax.mail.*; 69 import javax.mail.internet.*; 70 import javax.activation.*; 71 import java.lang.reflect.*; 72 73 83 public class HTTPUtils { 84 private static final String HTTP_VERSION = "1.0"; 85 private static final int HTTP_DEFAULT_PORT = 80; 86 private static final int HTTPS_DEFAULT_PORT = 443; 87 88 public static final int DEFAULT_OUTPUT_BUFFER_SIZE = 512; 89 90 97 private static Socket buildSocket(URL url, int targetPort, 98 String httpProxyHost, int httpProxyPort, 99 Boolean tcpNoDelay) 100 throws Exception { 101 Socket s = null; 102 String host = null; 103 int port = targetPort; 104 host = url.getHost(); 105 106 if (url.getProtocol().equalsIgnoreCase("HTTPS")) { 107 Class SSLUtilsClass = 109 Class.forName("org.apache.soap.util.net.SSLUtils"); 110 Class [] paramTypes = new Class [] {String .class, int.class, String .class, int.class}; 111 Method buildSSLSocket = SSLUtilsClass.getMethod( 112 "buildSSLSocket", paramTypes); 113 Object [] params = new Object [] {host, new Integer (port), 114 httpProxyHost, new Integer (httpProxyPort)}; 115 s = (Socket)buildSSLSocket.invoke(null, params); 116 } else { 117 if (httpProxyHost != null) { 118 host = httpProxyHost; 119 port = httpProxyPort; 120 } 121 s = new Socket(host, port); 122 } 123 124 if (tcpNoDelay != null) 125 { 126 if (s != null) 127 s.setTcpNoDelay(tcpNoDelay.booleanValue()); 128 } 129 130 return s; 131 } 132 133 139 private static int getPort(URL url) throws IOException { 140 int port = url.getPort(); 141 if (port < 0) if (url.getProtocol().equalsIgnoreCase("HTTPS")) 143 port = HTTPS_DEFAULT_PORT; 144 else 145 port = HTTP_DEFAULT_PORT; 146 return port; 147 } 148 149 161 public static TransportMessage post(URL url, TransportMessage request, 162 int timeout, 163 String httpProxyHost, int httpProxyPort) 164 throws IllegalArgumentException , IOException, SOAPException { 165 return post(url, 166 request, 167 timeout, 168 httpProxyHost, 169 httpProxyPort, 170 DEFAULT_OUTPUT_BUFFER_SIZE, 171 null); 172 } 173 174 187 public static TransportMessage post(URL url, TransportMessage request, 188 int timeout, 189 String httpProxyHost, int httpProxyPort, 190 int outputBufferSize) 191 throws IllegalArgumentException , IOException, SOAPException { 192 return post(url, 193 request, 194 timeout, 195 httpProxyHost, 196 httpProxyPort, 197 outputBufferSize, 198 null); 199 } 200 201 215 public static TransportMessage post(URL url, TransportMessage request, 216 int timeout, 217 String httpProxyHost, int httpProxyPort, 218 int outputBufferSize, 219 Boolean tcpNoDelay) 220 throws IllegalArgumentException , IOException, SOAPException { 221 222 OutputStream outStream = null; 223 InputStream inStream = null; 224 BufferedReader in = null; 225 int port; 226 Socket s; 227 try { 228 port = getPort(url); 229 230 s = buildSocket(url, port, httpProxyHost, httpProxyPort, tcpNoDelay); 231 if (url.getProtocol().equalsIgnoreCase("HTTPS")) { 232 httpProxyHost = null; 234 } 235 236 if (timeout > 0) s.setSoTimeout(timeout); 238 239 outStream = s.getOutputStream (); 240 inStream = s.getInputStream (); 241 } 242 catch (Exception e) { 243 Throwable t = e; 244 245 if (t instanceof InvocationTargetException) { 246 t = ((InvocationTargetException)t).getTargetException(); 247 } 248 249 throw new IllegalArgumentException ("Error opening socket: " + t); 250 } 251 252 253 String URI = (httpProxyHost == null ? url.getFile() : url.toString()); 254 if (URI.length() == 0) URI = "/"; 255 256 257 StringBuffer headerbuf = new StringBuffer (); 258 headerbuf.append(Constants.HEADER_POST).append(' ').append(URI) 259 .append(" HTTP/").append(HTTP_VERSION).append("\r\n") 260 .append(Constants.HEADER_HOST).append(": ").append(url.getHost()) 261 .append(':').append(port) 262 .append("\r\n") 263 .append(Constants.HEADER_CONTENT_TYPE).append(": ") 264 .append(request.getContentType()).append("\r\n") 265 .append(Constants.HEADER_CONTENT_LENGTH).append(": ") 266 .append(request.getContentLength()).append("\r\n"); 267 for (Enumeration e = request.getHeaderNames(); e.hasMoreElements(); ) { 268 Object key = e.nextElement(); 269 headerbuf.append(key).append(": ") 270 .append(request.getHeader((String )key)).append("\r\n"); 271 } 272 headerbuf.append("\r\n"); 273 274 275 BufferedOutputStream bOutStream = new BufferedOutputStream(outStream, outputBufferSize); 276 bOutStream.write( 277 headerbuf.toString().getBytes(Constants.HEADERVAL_DEFAULT_CHARSET)); 278 request.writeTo(bOutStream); 279 bOutStream.flush(); 282 outStream.flush(); 283 284 BufferedInputStream bInStream = new BufferedInputStream(inStream); 285 286 int statusCode = 0; 287 String statusString = null; 288 StringBuffer linebuf = new StringBuffer (); 289 int b = 0; 290 while (b != '\n' && b != -1) { 291 b = bInStream.read(); 292 if (b != '\n' && b != '\r' && b != -1) 293 linebuf.append((char)b); 294 } 295 String line = linebuf.toString(); 296 try { 297 StringTokenizer st = new StringTokenizer(line); 298 st.nextToken(); statusCode = Integer.parseInt (st.nextToken()); 300 StringBuffer sb = new StringBuffer (); 301 while (st.hasMoreTokens()) { 302 sb.append (st.nextToken()); 303 if (st.hasMoreTokens()) { 304 sb.append(" "); 305 } 306 } 307 statusString = sb.toString(); 308 } 309 catch (Exception e) { 310 throw new IllegalArgumentException ( 311 "Error parsing HTTP status line \"" + line + "\": " + e); 312 } 313 314 316 ByteArrayDataSource ds = new ByteArrayDataSource(bInStream, 317 Constants.HEADERVAL_DEFAULT_CHARSET); 318 319 320 byte[] bytes = ds.toByteArray(); 321 Hashtable respHeaders = new Hashtable(); 322 int respContentLength = -1; 323 String respContentType = null; 324 StringBuffer namebuf = new StringBuffer (); 325 StringBuffer valuebuf = new StringBuffer (); 326 boolean parsingName = true; 327 int offset; 328 for (offset = 0; offset < bytes.length; offset++) { 329 if (bytes[offset] == '\n') { 330 if (namebuf.length() == 0) 331 break; 332 String name = namebuf.toString(); 333 334 int valueLen = valuebuf.length(); 336 337 if (valueLen > 0 && valuebuf.charAt(valueLen - 1) == ';') { 338 valuebuf.deleteCharAt(valueLen - 1); 339 } 340 341 String value = valuebuf.toString(); 342 if (name.equalsIgnoreCase(Constants.HEADER_CONTENT_LENGTH)) 343 respContentLength = Integer.parseInt(value); 344 else if (name.equalsIgnoreCase(Constants.HEADER_CONTENT_TYPE)) 345 respContentType = value; 346 else 347 respHeaders.put(name, value); 348 namebuf = new StringBuffer (); 349 valuebuf = new StringBuffer (); 350 parsingName = true; 351 } 352 else if (bytes[offset] != '\r') { 353 if (parsingName) { 354 if (bytes[offset] == ':') { 355 parsingName = false; 356 if ((offset != bytes.length-1) && 357 bytes[offset+1] == ' ') 358 offset++; 359 } 360 else 361 namebuf.append((char)bytes[offset]); 362 } 363 else 364 valuebuf.append((char)bytes[offset]); 365 } 366 } 367 InputStream is = ds.getInputStream(); 368 is.skip(offset + 1); 369 if (respContentLength < 0) 370 respContentLength = ds.getSize() - offset - 1; 371 372 373 SOAPContext ctx; 374 TransportMessage response; 375 try { 376 ctx = new SOAPContext(); 378 response = new TransportMessage(is, respContentLength, 380 respContentType, ctx, respHeaders); 381 response.read(); 383 } catch (MessagingException me) { 384 throw new IllegalArgumentException ("Error parsing response: " + me); 385 } 386 387 388 bOutStream.close(); 389 outStream.close(); 390 bInStream.close(); 391 inStream.close(); 392 s.close(); 393 return response; 394 } 395 } 396 | Popular Tags |