1 package org.jahia.clipbuilder.html.web.http.impl; 2 3 import java.io.*; 4 import java.net.*; 5 import java.util.*; 6 7 import org.jahia.clipbuilder.html.bean.*; 8 import org.jahia.clipbuilder.html.util.*; 9 import org.jahia.clipbuilder.html.web.http.*; 10 import org.org.apache.commons.httpclient.*; 11 import org.org.apache.commons.httpclient.methods.*; 12 import org.org.apache.commons.httpclient.params.*; 13 14 19 public class HttpProcessor implements HTMLClient { 20 private UrlBean urlBean; 22 23 private boolean javascriptEnable; 24 25 private String proxyHost; 27 private int proxyPort = 0; 28 29 private HttpClient defaultClient; 31 32 private HttpMethod method = null; 34 private NameValuePair[] paramAndValue; 35 36 private GetMethod redirect = null; 38 39 private HttpState stateBeforeExecution; 41 private HttpState stateAfterExecution; 42 private String htmlAsString; 43 private String charEncoding; 44 45 private boolean errorHasOccured = false; 46 private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(HttpProcessor.class); 47 48 49 56 57 public HttpProcessor(String httpMethod, UrlBean uBean) throws WebClippingException { 58 this(httpMethod, uBean, false); 59 } 60 61 62 63 71 public HttpProcessor(String httpMethod, UrlBean uBean, boolean process) throws WebClippingException { 72 setMethod(httpMethod); 74 setUrlBean(uBean); 75 initMethod(); 76 if (process) { 77 execute(); 78 } 79 80 } 81 82 83 84 89 public void setParamAndValue(NameValuePair[] paramAndValue) { 90 this.paramAndValue = paramAndValue; 91 } 92 93 94 99 public void setMethod(String httpMethod) { 100 logger.debug("[ Method is " + httpMethod + "]"); 102 if (httpMethod.equalsIgnoreCase("GET")) { 103 GetMethod method = new GetMethod(); 104 method.setFollowRedirects(false); 105 method.addRequestHeader("User-Agent", "Mozilla/5.0 (Macintosh; U; PPC MacOS X; en)"); 106 setObjectMethod(method); 107 108 } 109 else if (httpMethod.equalsIgnoreCase("POST")) { 110 PostMethod method = new PostMethod(); 111 method.setFollowRedirects(false); 112 method.addRequestHeader("User-Agent", "Mozilla/5.0 (Macintosh; U; PPC MacOS X; en)"); 113 setObjectMethod(method); 114 ((PostMethod) getObjectMethod()).addParameters(getParamAndValue()); 115 } 116 117 getObjectMethod().getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); 119 120 getObjectMethod().addRequestHeader(" Content-Type", " text/html; charset=UTF-8"); 122 123 } 124 125 126 131 public void setMethodParameters(String httpMethod) { 132 logger.debug("[ Method is " + httpMethod + "]"); 134 if (httpMethod == null) { 135 logger.error("Http Method is null !!"); 136 logger.debug("Http Method is null !!"); 137 } 138 if (httpMethod.equalsIgnoreCase("GET")) { 139 method = new GetMethod(); 140 141 } 142 else if (httpMethod.equalsIgnoreCase("POST")) { 143 ((PostMethod) method).addParameters(getParamAndValue()); 144 } 145 146 } 147 148 149 150 155 public void setUrlBean(UrlBean urlBean) { 156 this.urlBean = urlBean; 157 } 158 159 160 165 public void setDefaultClient(HttpClient client) { 166 this.defaultClient = client; 167 } 168 169 170 171 176 public void setStateBeforeExecution(Object state) { 177 if (state != null) { 178 logger.debug("[ State updated]"); 179 this.stateBeforeExecution = (HttpState) state; 180 } 181 else { 182 logger.debug("[ State is null ]"); 183 } 184 } 185 186 187 192 public void setStateAfterExecution(HttpState state) { 193 this.stateAfterExecution = state; 194 } 195 196 197 202 public void setHtmlAsString(String htmlAsString) { 203 this.htmlAsString = htmlAsString; 204 } 205 206 207 212 public void setProxyPort(int proxyPort) { 213 this.proxyPort = proxyPort; 214 } 215 216 217 222 public void setProxyHost(String proxyHost) { 223 this.proxyHost = proxyHost; 224 } 225 226 227 232 public void setCharEncoding(String charEncoding) { 233 this.charEncoding = charEncoding; 234 } 235 236 237 238 243 public boolean isJavascriptEnable() { 244 return this.javascriptEnable; 245 } 246 247 248 253 public NameValuePair[] getParamAndValue() { 254 return paramAndValue; 255 } 256 257 258 259 264 public UrlBean getUrlBean() { 265 return urlBean; 266 } 267 268 269 274 public HttpClient getHttpClient() { 275 if (defaultClient == null) { 276 defaultClient = getNewHttpClient(); 277 } 278 return defaultClient; 279 } 280 281 282 287 public String getMethod() { 288 return getObjectMethod().getName(); 289 } 290 291 292 293 298 public HttpState getStateBeforeExecution() { 299 return stateBeforeExecution; 300 } 301 302 303 308 public HttpState getStateAfterExecution() { 309 return stateAfterExecution; 310 } 311 312 313 318 public String getHtmlAsString() { 319 return htmlAsString; 320 } 321 322 323 328 public int getProxyPort() { 329 return proxyPort; 330 } 331 332 333 338 public String getProxyHost() { 339 return proxyHost; 340 } 341 342 343 348 public String getCharEncoding() { 349 return charEncoding; 350 } 351 352 353 354 359 public Header[] getRequestHeaders() { 360 return method.getRequestHeaders(); 361 } 362 363 364 367 public void removeLastUrl() { 368 } 370 371 372 377 public void enabledJavascript(boolean enable) { 378 this.javascriptEnable = enable; 379 } 380 381 382 387 public boolean errorHasOccured() { 388 return errorHasOccured; 389 } 390 391 392 397 public void addHeader(Hashtable params) { 398 Iterator it = params.entrySet().iterator(); 399 while (it.hasNext()) { 400 String name = (String ) it.next(); 402 String value = (String ) params.get(name); 403 method.addRequestHeader(name, value); 405 } 406 407 } 408 409 410 415 public void execute() throws WebClippingException { 416 execute(getHttpClient()); 417 } 418 419 420 425 private void setObjectMethod(HttpMethod method) { 426 this.method = method; 427 } 428 429 430 433 private void setParamAndValueFromUrlBean() { 434 setParamAndValue(getNameValuePairFromURLBean()); 435 HttpMethod method = getObjectMethod(); 436 if (method instanceof GetMethod) { 437 NameValuePair[] nvp = getParamAndValue(); 439 String query = method.getQueryString(); 440 if (query == null || query.equalsIgnoreCase("")) { 441 method.setQueryString(nvp); 442 return; 443 } 444 method.setQueryString(nvp); 445 String queryParam = method.getQueryString(); 446 if (queryParam == null || queryParam.equalsIgnoreCase("")) { 447 method.setQueryString(query); 448 } 449 else { 450 method.setQueryString(query + "&" + queryParam); 451 } 452 } 453 else { 454 if (method instanceof PostMethod) { 455 ((PostMethod) method).addParameters(getParamAndValue()); 456 String qqq = method.getQueryString(); 457 logger.debug("query is: " + qqq); 458 } 459 } 460 461 } 462 463 464 467 private void setRequestedFileFromUrlBean() { 468 HttpMethod method = getObjectMethod(); 469 URL processedUrl = getUrlBean().getAbsoluteURL(); 470 String path = processedUrl.getPath(); 471 logger.debug("query string from redirectUrl: " + processedUrl.getQuery()); 472 method.setQueryString(processedUrl.getQuery()); 473 method.setPath(path); 474 } 475 476 477 481 private void setHostConfigurationFromUrlBean() { 482 HostConfiguration hostConfig = getHttpClient().getHostConfiguration(); 483 String host = getUrlBean().getBaseURL().getHost(); 484 hostConfig.setHost(host); 485 } 486 487 488 491 private void setClientSate() { 492 if (getStateBeforeExecution() != null) { 493 getHttpClient().setState(getStateBeforeExecution()); 494 } 495 } 496 497 498 503 private HttpMethod getObjectMethod() { 504 return method; 505 } 506 507 508 513 private HttpClient getNewHttpClient() { 514 HttpClient client = new HttpClient(); 515 client.getHostConfiguration().setHost(getUrlBean().getBaseURL().toExternalForm()); 517 return client; 518 } 519 520 521 526 private NameValuePair[] getNameValuePairFromURLBean() { 527 logger.debug("[ Convert Map To NameValuePair[] ]"); 528 if (this.getUrlBean() == null) { 530 logger.error("[ URL is not set]"); 531 return null; 532 } 533 List queryParamBeanList = getUrlBean().getQueryParamBeanList(); 534 logger.debug("[ Url: " + getUrlBean().getAbsoluteUrlValue() + " ]"); 535 logger.debug("[ queryParamBeanList.size(): " + queryParamBeanList.size() + " ]"); 536 NameValuePair[] paramAndValue = new NameValuePair[queryParamBeanList.size()]; 538 for (int i = 0; i < queryParamBeanList.size(); i++) { 539 QueryParamBean qBean = (QueryParamBean) queryParamBeanList.get(i); 540 String name = qBean.getName(); 541 String value = qBean.getDefaultValue(); 542 logger.debug("[ Found Param: " + name + " with value " + value + " ]"); 543 paramAndValue[i] = new NameValuePair(name, value); 544 } 545 546 return (paramAndValue); 547 } 548 549 550 556 private void execute(HttpClient client) throws WebClippingException { 557 initClient(); 559 560 HttpState s = getHttpClient().getState(); 562 563 Cookie[] cookiess = s.getCookies(); 564 565 logger.debug("[ HttpClient: Begin Enumerate cookies before execution ]"); 566 for (int i = 0; i < cookiess.length; i++) { 567 Cookie c = cookiess[i]; 568 logger.debug("[ Found cookie before execution: " + c.getName() + " ]"); 569 } 570 logger.debug("[ HttpClient: Finish Enumerate cookies before execution ]"); 571 572 String html = null; 574 try { 575 576 logger.debug("[ Url bean att: " + this.getUrlBean().getAbsoluteUrlValue() + " ]"); 578 logger.debug("[ Url bean att: " + this.getUrlBean().getRedirectUrl().toExternalForm() + " ]"); 579 logger.debug("[ Url bean att: " + this.getUrlBean().getAbsoluteURL().getQuery() + " ]"); 580 logger.debug("[ Url bean att: " + this.getUrlBean().getRedirectUrl().getQuery() + " ]"); 581 582 logger.debug("[ Url bean att: " + this.getUrlBean().getAbsoluteUrlValue() + " ]"); 583 logger.debug("[ Client host: " + client.getHostConfiguration().getHost() + " ]"); 584 logger.debug("[ Requested file path: " + method.getPath() + " ]"); 585 logger.debug("[ Query is: " + method.getQueryString() + " ]"); 586 int statusCode = -1; 587 588 logger.debug("[ Execute with simple client]"); 589 statusCode = client.executeMethod(method); 590 logger.debug("[ Success whith simple client ]"); 591 592 logger.debug("[status code is " + statusCode + " ]"); 594 595 if ((statusCode == HttpStatus.SC_MOVED_TEMPORARILY) || 597 (statusCode == HttpStatus.SC_MOVED_PERMANENTLY) || 598 (statusCode == HttpStatus.SC_SEE_OTHER) || 599 (statusCode == HttpStatus.SC_TEMPORARY_REDIRECT)) { 600 601 Header locationHeader = method.getResponseHeader("location"); 603 logger.debug("[ Redirection: " + locationHeader + " ]"); 604 if (locationHeader != null) { 605 String newuri = locationHeader.getValue(); 606 if ((newuri == null) || (newuri.equals(""))) { 607 newuri = "/"; 608 } 609 logger.debug("[ Redirect target: " + newuri + ""); 610 String absoluteRedirect = URLUtilities.getUrlAbsoluteValue(getUrlBean().getAbsoluteUrlValue(), newuri); 611 612 redirect = new GetMethod(absoluteRedirect); 613 client.executeMethod(redirect); 614 615 html = redirect.getResponseBodyAsString(); 617 logger.debug("[ Redirect: " + redirect.getStatusLine().toString() + " ]"); 618 619 getUrlBean().setRedirectUrl(URLUtilities.getURL(absoluteRedirect)); 621 logger.debug("Redirect url is: " + URLUtilities.getURL(newuri)); 622 623 } 624 else { 625 getUrlBean().setRedirectUrl(URLUtilities.getURL(getUrlBean().getAbsoluteURL().getPath())); 627 logger.error("[ Invalid redirect ]"); 628 } 629 630 } 631 else { 632 633 html = method.getResponseBodyAsString(); 634 String charset = HTMLUtilities.getEncoding(html, "iso-8859-1"); 635 Header[] tab = method.getResponseHeaders(); 636 for (int i = 0; i < tab.length; i++) { 637 Header h = tab[i]; 638 logger.debug("[header found: " + h.getName() + "," + h.getValue() + "]"); 639 } 640 html = new String (method.getResponseBody(), charset); 641 642 } 643 644 setHtmlAsString(html); 646 647 } 648 catch (HttpException e) { 649 logger.error("Fatal protocol violation: " + e.getMessage()); 650 errorHasOccured = true; 651 throw new WebClippingException("Fatal protocol violation", e); 652 } 653 catch (IOException e) { 654 logger.error("Fatal transport error: " + e.getMessage()); 655 errorHasOccured = true; 656 throw new WebClippingException("Fatal transport error", e); 657 } 658 catch (Exception e) { 659 logger.error("Fatal Exception error: " + e.getMessage()); 660 errorHasOccured = true; 661 e.printStackTrace(); 662 throw new WebClippingException("Fatal Exception error", e); 663 } 664 665 if (method != null) { 667 method.releaseConnection(); 668 } 669 if (redirect != null) { 670 redirect.releaseConnection(); 671 } 672 673 HttpState state = getHttpClient().getState(); 675 Cookie[] cookies = state.getCookies(); 676 logger.debug("[ Begin Enumerate cookies after execution ]"); 677 for (int i = 0; i < cookies.length; i++) { 678 Cookie c = cookies[i]; 679 logger.debug("[ Found cookie after execution: " + c.getName() + " ]"); 680 } 681 setStateAfterExecution(state); 683 684 } 685 686 687 690 private void initClient() { 691 setHostConfigurationFromUrlBean(); 693 694 setClientSate(); 696 697 } 698 699 700 703 private void initMethod() { 704 setRequestedFileFromUrlBean(); 706 707 setParamAndValueFromUrlBean(); 709 } 710 711 } 712 | Popular Tags |