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 com.gargoylesoftware.htmlunit.*; 8 import com.gargoylesoftware.htmlunit.html.*; 9 import org.jahia.clipbuilder.html.bean.*; 10 import org.jahia.clipbuilder.html.util.*; 11 import org.jahia.clipbuilder.html.web.http.*; 12 import org.org.apache.commons.httpclient.*; 13 import org.org.apache.commons.httpclient.params.*; 14 15 20 public class SimpleHTMLUnitProcessor implements HTMLClient { 21 25 protected UrlBean urlBean; 26 29 protected boolean javascriptEnable; 30 31 35 protected String proxyHost; 36 39 protected int proxyPort = 0; 40 41 45 protected WebClient webClient = null; 46 49 protected RefreshHandler refreshHandler_; 50 53 protected Header[] headers; 54 55 59 protected HtmlPage lastPage_; 60 61 65 protected List allNameFrame = new ArrayList(); 66 67 71 protected SubmitMethod method = null; 72 73 77 protected String finalUrl; 78 81 protected String htmlAsString; 82 85 protected String responseCharSet; 86 87 90 protected boolean errorHasOccured = false; 91 92 95 protected boolean refreshHandlerExceptionOccured_ = false; 96 private UrlBean previousUrlBean; 97 100 protected static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(SimpleHTMLUnitProcessor.class); 101 102 103 110 public SimpleHTMLUnitProcessor(Header[] headers, String httpMethod, UrlBean uBean) { 111 this.setHeaders(headers); 112 setMethod(httpMethod); 113 setUrlBean(uBean); 114 } 116 117 118 123 public void setUrlBean(UrlBean urlBean) { 124 this.urlBean = urlBean; 125 } 126 127 128 133 public void setMethod(String method) { 134 if (method.equalsIgnoreCase("GET")) { 135 this.method = SubmitMethod.GET; 136 } 137 else if (method.equalsIgnoreCase("POST")) { 138 this.method = SubmitMethod.POST; 139 } 140 else { 141 logger.error("[ Method " + method + " unknown]"); 142 } 143 144 } 145 146 147 152 public void setWebClient(Object webClient) { 153 this.webClient = (WebClient) webClient; 154 } 155 156 157 158 163 public void setProxyPort(int proxyPort) { 164 this.proxyPort = proxyPort; 165 } 166 167 168 173 public void setProxyHost(String proxy) { 174 this.proxyHost = proxy; 175 } 176 177 178 183 public void setHtmlAsString(String htmlAsString) { 184 this.htmlAsString = htmlAsString; 185 186 } 187 188 189 194 public void setFinalUrl(String finalUrl) { 195 this.finalUrl = finalUrl; 196 } 197 198 199 204 public void setHeaders(Header[] headers) { 205 this.headers = headers; 206 207 } 208 209 210 215 public void setResponseCharSet(String responseCharSet) { 216 this.responseCharSet = responseCharSet; 217 } 218 219 220 225 public void setPreviousUrlBean(UrlBean previousUrlBean) { 226 this.previousUrlBean = previousUrlBean; 227 } 228 229 230 235 public UrlBean getUrlBean() { 236 return urlBean; 237 } 238 239 240 245 public SubmitMethod getSubmitMethod() { 246 return method; 247 } 248 249 250 255 public String getMethod() { 256 return method.getName(); 257 } 258 259 260 265 public WebClient getWebClient() { 266 return webClient; 267 } 268 269 270 271 276 public int getProxyPort() { 277 return proxyPort; 278 } 279 280 281 286 public String getProxyHost() { 287 return proxyHost; 288 } 289 290 291 296 public List getAllNameFrame() { 297 return allNameFrame; 298 } 299 300 301 306 public String getHtmlAsString() { 307 return htmlAsString; 308 } 309 310 311 316 public String getFinalUrl() { 317 return finalUrl; 318 } 319 320 321 327 public String getRelativeUrlFromUrlBean() { 328 return getUrlBean().getRelativeUrlValue(); 329 } 330 331 332 338 public String getAbsoluteUrlFromUrlBean() { 339 return getUrlBean().getAbsoluteUrlValue(); 340 } 341 342 343 344 349 public boolean isJavascriptEnable() { 350 return this.javascriptEnable; 351 } 352 353 354 359 public String getResponseCharSet() { 360 return responseCharSet; 361 } 362 363 364 369 public UrlBean getPreviousUrlBean() { 370 return previousUrlBean; 371 } 372 373 374 379 public Header[] getHeader() { 380 return headers; 381 } 382 383 384 389 public void addNameFrame(String name) { 390 getAllNameFrame().add(name); 391 } 392 393 394 399 public void execute() throws WebClippingException { 400 String html = ""; 401 try { 402 403 assertNotNullWebClient(); 405 406 URL url = new URL(getAbsoluteUrlFromUrlBean()); 408 409 webClient.getWebConnection().getStateForUrl(url); 411 412 415 SubmitMethod httpMethod = getSubmitMethod(); 417 ArrayList paramsList = getNameValuePairListFromURLBean(); 418 419 lastPage_ = (HtmlPage) webClient.getPage(url, httpMethod, paramsList); 421 422 WebResponse response = lastPage_.getWebResponse(); 424 String charSet = response.getContentCharSet(); 425 logger.debug("CharSet: " + charSet); 426 427 logger.debug("[ URL before process is: " + getUrlBean().getAbsoluteURL().toExternalForm() + " ]"); 429 setFinalUrl(response.getUrl().toExternalForm()); 430 getUrlBean().setRedirectUrl(URLUtilities.getURL(getFinalUrl())); 431 432 logger.debug("[ URL after process is: " + getUrlBean().getAbsoluteURL().toExternalForm() + " ]"); 433 434 html = new String (response.getResponseBody(), HTMLUtilities.getEncoding(response.getContentAsString(), "iso-8859-1")); 436 437 logger.debug("[ Encoding of the response is: " + response.getContentCharSet() + " ]"); 438 439 setHtmlAsString(html); 441 442 } 443 catch (FailingHttpStatusCodeException ex) { 444 logger.error("[ FailingHttpStatusCodeException: " + ex.getMessage() + " ]"); 445 ex.printStackTrace(); 446 throw new WebClippingException("Failing Http Status Code", ex); 447 } 448 catch (RuntimeException ex) { 449 if (!refreshHandlerExceptionOccured_) { 450 logger.warn("[ RuntimeException occured: " + ex.getMessage() + " ]"); 451 logger.debug("[ Try whith WaitingRefreshHandler]"); 452 getWebClient().setRefreshHandler(new WaitingRefreshHandler()); 453 refreshHandlerExceptionOccured_ = true; 454 this.execute(); 455 } 456 else { 457 logger.error("[ RuntimeException occured: " + ex.getMessage() + " ]"); 458 ex.printStackTrace(); 459 throw new WebClippingException("RefreshHandler", ex); 460 } 461 } 462 463 catch (MalformedURLException ex) { 464 logger.error("[ MalformedURLException: " + ex.getMessage() + " ]"); 465 ex.printStackTrace(); 466 throw new WebClippingException("Malformed URL", ex); 467 } 468 catch (IOException ex) { 469 logger.error("[ IOException occured: " + ex.getMessage() + " ]"); 470 ex.printStackTrace(); 471 throw new WebClippingException("IOException", ex); 472 } 473 catch (Exception ex) { 474 logger.debug("[ Exception occured: " + ex.getMessage() + " ]"); 475 throw new WebClippingException("Exception", ex); 476 } 477 478 } 479 480 481 482 487 public boolean foundFrame() { 488 489 HtmlPage htmlPage = (HtmlPage) lastPage_; 490 List frameList = htmlPage.getFrames(); 491 return !frameList.isEmpty(); 492 } 493 494 495 500 public void enabledJavascript(boolean enable) { 501 javascriptEnable = enable; 502 WebClient webClient = this.getWebClient(); 504 webClient.setJavaScriptEnabled(isJavascriptEnable()); 505 } 506 507 508 511 public void removeLastUrl() { 512 } 514 515 516 522 private ArrayList getNameValuePairListFromURLBean() { 523 logger.debug("[ Convert Map To List NameValuePair ]"); 524 if (getUrlBean() == null) { 526 logger.error("[ URL is not set]"); 527 return null; 528 } 529 List queryParamBeanList = getUrlBean().getQueryParamBeanList(); 530 531 ArrayList listNamePairAndValue = new ArrayList(); 533 for (int i = 0; i < queryParamBeanList.size(); i++) { 534 QueryParamBean qBean = (QueryParamBean) queryParamBeanList.get(i); 535 String name = qBean.getName(); 536 String value = qBean.getDefaultValue(); 537 logger.debug("[ Found Param: " + name + " with value " + value + " ]"); 538 NameValuePair paramAndValue = new NameValuePair(name, value); 539 listNamePairAndValue.add(paramAndValue); 540 } 541 542 return listNamePairAndValue; 543 } 544 545 546 551 private WebClient getNewWebClient() { 552 String proxyHost = getProxyHost(); 554 int proxyPort = getProxyPort(); 555 556 WebClient webClient = null; 558 if (proxyHost != null && (proxyPort != 0)) { 559 webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER_6_0, proxyHost, proxyPort); 560 } 561 else { 562 webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER_6_0); 563 } 564 565 webClient.setThrowExceptionOnScriptError(false); 567 webClient.setThrowExceptionOnFailingStatusCode(false); 568 569 webClient.setRedirectEnabled(true); 571 webClient.setJavaScriptEnabled(isJavascriptEnable()); 572 573 return webClient; 574 } 575 576 577 580 private void addHeaders() { 581 logger.debug("[ Add headers ]"); 582 Header[] header = this.getHeader(); 583 if (header != null) { 584 for (int i = 0; i < header.length; i++) { 585 Header h = header[i]; 586 if (h != null) { 587 webClient.addRequestHeader(h.getName(), h.getValue()); 588 } 589 } 590 } 591 } 592 593 594 597 private void assertNotNullWebClient() { 598 if (webClient == null) { 599 setWebClient(getNewWebClient()); 600 } 601 602 } 603 604 } 605 | Popular Tags |