1 24 package org.ofbiz.base.util; 25 26 import java.io.BufferedReader ; 27 import java.io.DataOutputStream ; 28 import java.io.IOException ; 29 import java.io.InputStream ; 30 import java.io.InputStreamReader ; 31 import java.net.HttpURLConnection ; 32 import java.net.URL ; 33 import java.net.URLConnection ; 34 import java.util.HashMap ; 35 import java.util.Iterator ; 36 import java.util.Map ; 37 import java.util.Set ; 38 39 46 public class HttpClient { 47 48 public static final String module = HttpClient.class.getName(); 49 50 private int timeout = 30000; 51 private boolean debug = false; 52 private boolean lineFeed = true; 53 private boolean followRedirects = true; 54 55 private String url = null; 56 private String rawStream = null; 57 private String clientCertAlias = null; 58 private Map parameters = null; 59 private Map headers = null; 60 61 private URL requestUrl = null; 62 private URLConnection con = null; 63 64 65 public HttpClient() {} 66 67 68 public HttpClient(URL url) { 69 this.url = url.toExternalForm(); 70 } 71 72 73 public HttpClient(String url) { 74 this.url = url; 75 } 76 77 78 public HttpClient(String url, Map parameters) { 79 this.url = url; 80 this.parameters = parameters; 81 } 82 83 84 public HttpClient(URL url, Map parameters) { 85 this.url = url.toExternalForm(); 86 this.parameters = parameters; 87 } 88 89 90 public HttpClient(String url, Map parameters, Map headers) { 91 this.url = url; 92 this.parameters = parameters; 93 this.headers = headers; 94 } 95 96 97 public HttpClient(URL url, Map parameters, Map headers) { 98 this.url = url.toExternalForm(); 99 this.parameters = parameters; 100 this.headers = headers; 101 } 102 103 104 public void setDebug(boolean debug) { 105 this.debug = debug; 106 } 107 108 109 public void setTimeout(int timeout) { 110 this.timeout = timeout; 111 } 112 113 114 public void followRedirects(boolean followRedirects) { 115 this.followRedirects = followRedirects; 116 } 117 118 119 public void setLineFeed(boolean lineFeed) { 120 this.lineFeed = lineFeed; 121 } 122 123 124 public void setRawStream(String stream) { 125 this.rawStream = stream; 126 } 127 128 129 public void setUrl(URL url) { 130 this.url = url.toExternalForm(); 131 } 132 133 134 public void setUrl(String url) { 135 this.url = url; 136 } 137 138 139 public void setParameters(Map parameters) { 140 this.parameters = parameters; 141 } 142 143 144 public void setParameter(String name, String value) { 145 if (parameters == null) 146 parameters = new HashMap (); 147 parameters.put(name, value); 148 } 149 150 151 public void setHeaders(Map headers) { 152 this.headers = headers; 153 } 154 155 156 public void setHeader(String name, String value) { 157 if (headers == null) 158 headers = new HashMap (); 159 headers.put(name, value); 160 } 161 162 163 public Map getHeaders() { 164 return headers; 165 } 166 167 168 public Map getParameters() { 169 return parameters; 170 } 171 172 173 public String getUrl() { 174 return url; 175 } 176 177 178 public void setClientCertificateAlias(String alias) { 179 this.clientCertAlias = alias; 180 } 181 182 183 public String getClientCertificateAlias() { 184 return this.clientCertAlias; 185 } 186 187 188 public String get() throws HttpClientException { 189 return sendHttpRequest("get"); 190 } 191 192 193 public InputStream getStream() throws HttpClientException { 194 return sendHttpRequestStream("get"); 195 } 196 197 198 public String post() throws HttpClientException { 199 return sendHttpRequest("post"); 200 } 201 202 203 public String post(String stream) throws HttpClientException { 204 this.rawStream = stream; 205 return sendHttpRequest("post"); 206 } 207 208 209 public InputStream postStream() throws HttpClientException { 210 return sendHttpRequestStream("post"); 211 } 212 213 214 public String getResponseHeader(String header) throws HttpClientException { 215 if (con == null) { 216 throw new HttpClientException("Connection not yet established"); 217 } 218 return con.getHeaderField(header); 219 } 220 221 222 public String getResponseHeaderFieldKey(int n) throws HttpClientException { 223 if (con == null) { 224 throw new HttpClientException("Connection not yet established"); 225 } 226 return con.getHeaderFieldKey(n); 227 } 228 229 230 public String getResponseHeaderField(int n) throws HttpClientException { 231 if (con == null) { 232 throw new HttpClientException("Connection not yet established"); 233 } 234 return con.getHeaderField(n); 235 } 236 237 238 public Object getResponseContent() throws java.io.IOException , HttpClientException { 239 if (con == null) { 240 throw new HttpClientException("Connection not yet established"); 241 } 242 return con.getContent(); 243 } 244 245 246 public String getResponseContentType() throws HttpClientException { 247 if (con == null) { 248 throw new HttpClientException("Connection not yet established"); 249 } 250 return con.getContentType(); 251 } 252 253 254 public int getResponseContentLength() throws HttpClientException { 255 if (con == null) { 256 throw new HttpClientException("Connection not yet established"); 257 } 258 return con.getContentLength(); 259 } 260 261 262 public String getResponseContentEncoding() throws HttpClientException { 263 if (con == null) { 264 throw new HttpClientException("Connection not yet established"); 265 } 266 return con.getContentEncoding(); 267 } 268 269 private String sendHttpRequest(String method) throws HttpClientException { 270 InputStream in = sendHttpRequestStream(method); 271 if (in == null) return null; 272 273 StringBuffer buf = new StringBuffer (); 274 try { 275 if (Debug.verboseOn() || debug) { 276 try { 277 Debug.log("ContentEncoding: " + con.getContentEncoding() + "; ContentType: " + 278 con.getContentType() + " or: " + URLConnection.guessContentTypeFromStream(in), module); 279 } catch (IOException ioe) { 280 Debug.logWarning(ioe, "Caught exception printing content debugging information", module); 281 } 282 } 283 284 String charset = null; 285 String contentType = con.getContentType(); 286 if (contentType == null) { 287 try { 288 contentType = URLConnection.guessContentTypeFromStream(in); 289 } catch (IOException ioe) { 290 Debug.logWarning(ioe, "Problems guessing content type from steam", module); 291 } 292 } 293 294 if (Debug.verboseOn() || debug) Debug.log("Content-Type: " + contentType, module); 295 296 if (contentType != null) { 297 contentType = contentType.toUpperCase(); 298 int charsetEqualsLoc = contentType.indexOf("=", contentType.indexOf("CHARSET")); 299 int afterSemiColon = contentType.indexOf(";", charsetEqualsLoc); 300 if (charsetEqualsLoc >= 0 && afterSemiColon >= 0) { 301 charset = contentType.substring(charsetEqualsLoc + 1, afterSemiColon); 302 } else if (charsetEqualsLoc >= 0) { 303 charset = contentType.substring(charsetEqualsLoc + 1); 304 } 305 306 if (charset != null) charset = charset.trim(); 307 if (Debug.verboseOn() || debug) Debug.log("Getting text from HttpClient with charset: " + charset, module); 308 } 309 310 BufferedReader post = new BufferedReader (charset == null ? new InputStreamReader (in) : new InputStreamReader (in, charset)); 311 String line = new String (); 312 313 if (Debug.verboseOn() || debug) Debug.log("---- HttpClient Response Content ----", module); 314 while ((line = post.readLine()) != null) { 315 if (Debug.verboseOn() || debug) Debug.log("[HttpClient] : " + line, module); 316 buf.append(line); 317 if (lineFeed) { 318 buf.append("\n"); 319 } 320 } 321 } catch (Exception e) { 322 throw new HttpClientException("Error processing input stream", e); 323 } 324 return buf.toString(); 325 } 326 327 private InputStream sendHttpRequestStream(String method) throws HttpClientException { 328 SSLUtil.loadJsseProperties(); 330 331 String arguments = null; 332 InputStream in = null; 333 334 if (url == null) { 335 throw new HttpClientException("Cannot process a null URL."); 336 } 337 338 if (rawStream != null) { 339 arguments = rawStream; 340 } else if (parameters != null && parameters.size() > 0) { 341 arguments = UtilHttp.urlEncodeArgs(parameters, false); 342 } 343 344 if (method.equalsIgnoreCase("get") && arguments != null) { 346 url = url + "?" + arguments; 347 } 348 349 try { 351 requestUrl = new URL (url); 352 con = URLConnector.openConnection(requestUrl, timeout, clientCertAlias); 353 if (Debug.verboseOn() || debug) Debug.log("Connection opened to : " + requestUrl.toExternalForm(), module); 354 355 if ((con instanceof HttpURLConnection )) { 356 ((HttpURLConnection ) con).setInstanceFollowRedirects(followRedirects); 357 if (Debug.verboseOn() || debug) Debug.log("Connection is of type HttpURLConnection", module); 358 } 359 360 con.setDoOutput(true); 361 con.setUseCaches(false); 362 if (Debug.verboseOn() || debug) Debug.log("Do Input = true / Use Caches = false", module); 363 364 if (method.equalsIgnoreCase("post")) { 365 con.setRequestProperty("Content-type", "application/x-www-form-urlencoded"); 366 con.setDoInput(true); 367 if (Debug.verboseOn() || debug) Debug.log("Set content type to : application/x-www-form-urlencoded", module); 368 } 369 370 if (headers != null && headers.size() > 0) { 371 Set headerSet = headers.keySet(); 372 Iterator i = headerSet.iterator(); 373 374 while (i.hasNext()) { 375 String headerName = (String ) i.next(); 376 String headerValue = (String ) headers.get(headerName); 377 con.setRequestProperty(headerName, headerValue); 378 if (Debug.verboseOn() || debug) Debug.log("Header : " + headerName + " -> " + headerValue, module); 379 } 380 } else { 381 if (Debug.verboseOn() || debug) Debug.log("No headers to set", module); 382 } 383 384 if (method.equalsIgnoreCase("post")) { 385 DataOutputStream out = new DataOutputStream (con.getOutputStream()); 386 if (Debug.verboseOn() || debug) Debug.log("Opened output stream", module); 387 388 out.writeBytes(arguments); 389 if (Debug.verboseOn() || debug) Debug.log("Wrote arguements (parameters) : " + arguments, module); 390 391 out.flush(); 392 out.close(); 393 if (Debug.verboseOn() || debug) Debug.log("Flushed and closed buffer", module); 394 } 395 396 if (Debug.verboseOn() || debug) { 397 Map headerFields = con.getHeaderFields(); 398 Debug.log("Header Fields : " + headerFields, module); 399 } 400 401 in = con.getInputStream(); 402 } catch (IOException ioe) { 403 throw new HttpClientException("IO Error processing request", ioe); 404 } catch (Exception e) { 405 throw new HttpClientException("Error processing request", e); 406 } 407 408 return in; 409 } 410 } 411 | Popular Tags |