1 20 package org.enhydra.barracuda.plankton.http; 21 22 import java.io.*; 23 import java.net.*; 24 import java.text.*; 25 import java.util.*; 26 import javax.servlet.http.*; 27 28 import org.enhydra.barracuda.plankton.data.Base64; 29 30 44 public class HttpRequester { 45 46 public static final String POST = "POST"; 47 public static final String GET = "GET"; 48 49 protected URL url = null; 50 protected String method = GET; 51 protected Map props = null; 52 protected HttpOutputWriter outputWriter = null; 53 protected String user = null; 54 protected String password = null; 55 protected boolean authenticate = false; 56 protected boolean acceptCookies = true; protected List cookies = null; 59 protected OutputStream outStream = null; 60 protected InputStream inStream = null; 61 protected BufferedReader in = null; 62 protected URLConnection conn = null; 64 65 76 public void setRequest(String iurl, String imethod, Map iprops) throws MalformedURLException { 77 setRequest(iurl, imethod, iprops, null); 78 } 79 80 91 public void setRequest(URL iurl, String imethod, Map iprops) throws MalformedURLException { 92 setRequest(iurl, imethod, iprops, null); 93 } 94 95 107 public void setRequest(String iurl, String imethod, Map iprops, HttpOutputWriter ioutputWriter) throws MalformedURLException { 108 setRequest(iurl, imethod, iprops, null, null, null); 109 } 110 111 123 public void setRequest(URL iurl, String imethod, Map iprops, HttpOutputWriter ioutputWriter) throws MalformedURLException { 124 setRequest(iurl, imethod, iprops, null, null, null); 125 } 126 127 141 public void setRequest(String iurl, String imethod, Map iprops, String iuser, String ipwd, HttpOutputWriter ioutputWriter) throws MalformedURLException { 142 if (iurl!=null) setUrl(iurl); 143 if (imethod!=null) setMethod(imethod); 144 if (iprops!=null) setParams(iprops); 145 if (iuser!=null) setUser(iuser); 146 if (ipwd!=null) setPassword(ipwd); 147 if (ioutputWriter!=null) setOutputWriter(ioutputWriter); 148 } 149 150 164 public void setRequest(URL iurl, String imethod, Map iprops, String iuser, String ipwd, HttpOutputWriter ioutputWriter) throws MalformedURLException { 165 if (iurl!=null) setUrl(iurl); 166 if (imethod!=null) setMethod(imethod); 167 if (iprops!=null) setParams(iprops); 168 if (iuser!=null) setUser(iuser); 169 if (ipwd!=null) setPassword(ipwd); 170 if (ioutputWriter!=null) setOutputWriter(ioutputWriter); 171 } 172 173 179 public void setUrl(String iurl) throws MalformedURLException { 180 if (iurl==null) url = null; 183 else setUrl (new URL (iurl)); 184 } 185 186 191 public void setUrl(URL iurl) { 192 url = iurl; 193 } 194 195 200 public URL getUrl() { 201 return url; 202 } 203 204 210 public void setMethod(String imethod) { 211 if (imethod.toUpperCase().equals(POST)) method = POST; 212 else method = GET; 213 } 214 215 220 public String getMethod() { 221 return method; 222 } 223 224 234 public void setParams(Map iprops) { 235 props = iprops; 236 } 237 238 245 public Map getParams() { 246 if (props==null) { 249 if (url==null) return null; 251 252 props = HttpConverter.cvtURLStringToMap (url.toString(), "&"); 254 } 255 256 return props; 258 } 259 260 265 public void setUser(String iuser) { 266 user = iuser; 267 authenticate = (user!=null); 268 } 269 270 275 public String getUser() { 276 return user; 277 } 278 279 284 public void setPassword(String ipwd) { 285 password = ipwd; 286 authenticate = (password!=null); 287 } 288 289 294 protected String getPassword() { 295 return password; 296 } 297 298 309 public void setAcceptCookies(boolean accept) { 310 this.acceptCookies = accept; 311 } 312 313 316 public boolean getAcceptCookies() { 317 return acceptCookies; 318 } 319 320 328 public List getCookies() { 329 return cookies; 330 } 331 332 335 public void clearCookies() { 336 cookies = null; 337 } 338 340 345 public void setOutputWriter(HttpOutputWriter ioutputWriter) { 346 outputWriter = ioutputWriter; 347 } 348 349 354 public HttpOutputWriter getOutputWriter() { 355 if (outputWriter==null) { 358 return new DefaultOutputWriter(); 359 } else 360 return outputWriter; 361 } 362 363 369 public URLConnection getURLConnection() { 370 return conn; 371 } 372 373 379 public void connect() throws ConnectException, IOException { 380 if (url==null) throw new ConnectException ("Invalid URL. URL can not be NULL"); 382 if (method!=POST && method!=GET) throw new ConnectException ("Invalid Method. Method must be either POST or GET"); 383 384 386 if (method==POST) { 388 conn = url.openConnection(); conn.setDoOutput(true); 392 393 if (authenticate) { 398 String input = user + ":" + password; 399 String output = new String (Base64.encode(input.getBytes())); 400 conn.setRequestProperty("Authorization", "Basic " + output); 401 } 402 403 outStream = conn.getOutputStream(); 405 getOutputWriter().writeOutput(outStream); 406 407 } else { 414 if (props!=null) { 416 String newUrl = getUrl().toString(); 418 int pos = newUrl.indexOf("?"); 419 if (pos>0) newUrl = newUrl.substring(0,pos); 420 421 setUrl(newUrl+"?"+ HttpConverter.cvtMapToURLString(props, "&")); 423 newUrl = getUrl().toString(); 424 if (newUrl.endsWith("?")) setUrl(newUrl.substring(0,newUrl.length()-1)); 425 426 setParams(null); 429 } 430 431 conn = url.openConnection(); conn.setDoInput(true); 435 436 if (authenticate) { 441 String input = user + ":" + password; 442 String output = new String (Base64.encode(input.getBytes())); 443 conn.setRequestProperty("Authorization", "Basic " + output); 444 } 445 446 } 452 453 if(cookies != null) { 456 461 StringBuffer sb = new StringBuffer (); 462 int maxVersion = 0; 463 boolean haveCookies = false; 464 Iterator it = cookies.iterator(); 465 while (it.hasNext()) { 466 Cookie cookie = (Cookie)it.next(); 467 if (cookie.getDomain() == null || url.getHost().toLowerCase().endsWith(cookie.getDomain().toLowerCase())) { 468 if (cookie.getPath() == null || url.getPath().startsWith(cookie.getPath())) { 469 if (!cookie.getSecure() || url.getProtocol().equalsIgnoreCase("https")) { 470 haveCookies = true; 472 473 sb.append(';').append(cookie.getName()).append('=').append(cookie.getValue()); 474 if(cookie.getVersion() > 0) { 475 if(cookie.getVersion() > maxVersion) maxVersion = cookie.getVersion(); 476 if(cookie.getPath() != null) sb.append(";$Path=").append(cookie.getPath()); 477 if(cookie.getDomain() != null) sb.append(";$Domain=").append(cookie.getDomain()); 478 } 479 } else { 480 } 482 } else { 483 } 485 } else { 486 } 488 } 489 490 if (haveCookies) { 491 String cookieStr; 492 if(maxVersion == 0) cookieStr = sb.substring(1); else cookieStr = "$Version="+maxVersion+sb.toString(); 494 495 conn.setRequestProperty("Cookie", cookieStr); 497 } 498 } 499 500 inStream = conn.getInputStream(); 502 in = new BufferedReader(new InputStreamReader(inStream)); 503 504 506 List scookies = new ArrayList(); 509 for(int i = 0; conn.getHeaderFieldKey(i) != null; i++) { 510 if("Set-Cookie".equals(conn.getHeaderFieldKey(i))) { 511 scookies.add(conn.getHeaderField(i)); 512 } 513 } 514 if (acceptCookies && scookies != null) { 516 cookies = new ArrayList(scookies.size()); 517 Iterator it = scookies.iterator(); 519 while (it.hasNext()) { 520 String cookieStr = (String ) it.next(); 521 try { 523 Cookie cookie = HttpServices.parseCookie(cookieStr); 524 cookies.add(cookie); 527 } catch (ParseException e) { 528 e.printStackTrace(); 529 } 530 } 531 } 532 } 534 535 542 public String readLine() throws ConnectException, IOException { 543 if (in==null) throw new ConnectException ("Connection is not active"); 545 546 String inputLine = in.readLine(); 548 549 if (inputLine==null) disconnect(); 551 552 return inputLine; 554 } 555 556 562 public OutputStream getOutputStream() throws ConnectException { 563 if (outStream==null) throw new ConnectException ("Connection is not active"); 565 566 return outStream; 568 } 569 570 576 public InputStream getInputStream() throws ConnectException { 577 if (inStream==null) throw new ConnectException ("Connection is not active"); 579 580 return inStream; 582 } 583 584 590 public void disconnect() { 591 if (outStream!=null) try {outStream.close();} catch (IOException ioe) {} 592 outStream = null; 593 if (in!=null) try {in.close();} catch (IOException ioe) {} 594 in = null; 595 if (inStream!=null) try {inStream.close();} catch (IOException ioe) {} 596 inStream = null; 597 } 598 599 602 class DefaultOutputWriter implements HttpOutputWriter { 603 public void writeOutput(OutputStream outputStream) throws IOException { 604 System.out.println ("Using default HttpOutputWriter to POST data"); 605 PrintWriter out = new PrintWriter(outputStream); 606 try { 607 String paramStr = HttpConverter.cvtMapToURLString(props, "&"); 608 if (paramStr!=null && paramStr.trim().length()>0) out.print (paramStr); 609 System.out.println ("Data posted!"); 610 } finally { 611 out.close(); 612 outputStream.close(); 613 } 614 } 615 } 616 617 618 public static void main(String [] args) { 619 try { 621 HttpRequester hr = new HttpRequester(); 622 String urlStr = "http://localhost:8080/manager/reload?path=/examples"; String paramStr = null; 626 Map props = null; 627 if (paramStr!=null) HttpConverter.cvtURLStringToMap (paramStr, "&"); 628 hr.setRequest(urlStr, HttpRequester.GET, props, "admin", "123123", null); 629 hr.connect(); 630 String inputLine; 631 while ((inputLine = hr.readLine()) != null) { 632 System.out.println(inputLine); 633 } 634 hr.disconnect(); 635 } catch (Exception e) { 636 e.printStackTrace(); 637 } 638 656 657 try { 659 HttpRequester hr = new HttpRequester(); 660 hr.setRequest("http://www.psycinfo.com/cookie/set-cookie.cfm", HttpRequester.GET, null); 661 hr.connect(); 662 String inputLine; 663 while ((inputLine = hr.readLine()) != null) { 664 System.out.println(inputLine); 665 } 666 hr.disconnect(); 667 668 hr.setRequest("http://www.psycinfo.com/cookie/check-cookie.cfm", HttpRequester.GET, null); 669 hr.connect(); 670 while ((inputLine = hr.readLine()) != null) { 671 System.out.println(inputLine); 672 } 673 hr.disconnect(); 674 } catch (Exception e) { 675 e.printStackTrace(); 676 } 677 678 } 679 } 680 | Popular Tags |