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.org.apache.commons.httpclient.*; 12 import org.jahia.clipbuilder.html.web.Constant.WebConstants; 13 14 19 public class HTMLUnitProcessor extends SimpleHTMLUnitProcessor { 20 private List historyPageList; 21 private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(HTMLUnitProcessor.class); 23 24 25 32 public HTMLUnitProcessor(Header[] headers, String httpMethod, UrlBean uBean) { 33 super(headers, httpMethod, uBean); 34 assertNotNullWebClient(); 35 } 36 37 38 43 public void execute() throws WebClippingException { 44 String html = ""; 45 HtmlPage page = null; 46 try { 47 if (lastPage_ == null) { 49 logger.debug("First page"); 50 setWebClient(getNewWebClient()); 51 historyPageList = new ArrayList(); 52 super.execute(); 53 } 54 else { 55 56 UrlBean uBean = getUrlBean(); 58 String from = uBean.getFrom(); 59 60 if (from.equalsIgnoreCase(WebConstants.FROM_LINK)) { 62 logger.debug("It's a link"); 63 64 HtmlAnchor anchor = getHtmlAnchor(); 66 67 page = (HtmlPage) anchor.click(); 69 } 70 else if (from.equalsIgnoreCase( 71 WebConstants.FROM_AREA)) { 72 logger.debug("It's an area"); 73 74 HtmlArea area = getHtmlArea(); 76 77 page = (HtmlPage) area.click(); 79 } 80 81 else if (from.equalsIgnoreCase( 82 WebConstants.FROM_FORM)) { 83 84 logger.debug("It's a submitted form"); 85 86 HtmlForm form = getSubmittedForm(); 88 89 fillForm(form); 91 92 HtmlInput submitButton = getSubmittedButton( 94 form); 95 96 if (submitButton == null) { 98 logger.warn("Submit button not found. Anyway submit the form"); 99 page = (HtmlPage) form.submit(); 100 } 101 else { 102 logger.debug(submitButton.getNameAttribute()); 103 logger.debug(submitButton.getValueAttribute()); 104 page = (HtmlPage) submitButton.click(); 105 } 106 } 107 else if (from.equalsIgnoreCase(WebConstants.FROM_MANUAL)) { 108 logger.debug("Manual: Simlpe HtmlUnit execution"); 109 super.execute(); 110 } 111 else { 112 logger.debug("Default: Simlpe HtmlUnit execution"); 113 super.execute(); 114 } 115 116 WebResponse response = page.getWebResponse(); 118 String charSet = response.getContentCharSet(); 119 logger.debug("CharSet: " + charSet); 120 121 logger.debug("[ URL before process is: " + getUrlBean().getAbsoluteURL().toExternalForm() + " ]"); 123 setFinalUrl(response.getUrl().toExternalForm()); 124 getUrlBean().setRedirectUrl(URLUtilities.getURL( 125 getFinalUrl())); 126 127 logger.debug("[ URL after process is: " + 128 getUrlBean().getAbsoluteURL(). 129 toExternalForm() + " ]"); 130 131 logger.debug(response.getContentCharSet()); 134 html = new String (response.getResponseBody(), HTMLUtilities.getEncoding(response.getContentAsString(), "iso-8859-1")); 135 136 logger.debug("[ Encoding of the response is: " + response.getContentCharSet() + " ]"); 137 138 setHtmlAsString(html); 140 141 refreshHandlerExceptionOccured_ = false; 143 } 144 145 validate(page); 147 148 } 149 catch (ElementNotFoundException ex) { 150 logger.warn("[ Exception occured: " + ex.getMessage() + " ]"); 151 } 152 153 catch (FailingHttpStatusCodeException ex) { 154 logger.error("[ FailingHttpStatusCodeException: " + ex.getMessage() + " ]"); 155 ex.printStackTrace(); 156 throw new WebClippingException("Failing Http Status Code", ex); 157 } 158 catch (MalformedURLException ex) { 159 logger.error("[ MalformedURLException: " + ex.getMessage() + " ]"); 160 ex.printStackTrace(); 161 throw new WebClippingException("Malformed URL", ex); 162 } 163 catch (IOException ex) { 164 logger.error("[ IOException occured: " + ex.getMessage() + " ]"); 165 ex.printStackTrace(); 166 throw new WebClippingException("IOException", ex); 167 } 168 catch (RuntimeException ex) { 169 if (!refreshHandlerExceptionOccured_) { 170 logger.warn("[ RuntimeException occured: " + ex.getMessage() + " ]"); 171 logger.debug("[ Try whith ThreadedRefreshHandler]"); 172 getWebClient().setRefreshHandler(new WaitingRefreshHandler()); 174 refreshHandlerExceptionOccured_ = true; 175 execute(); 176 } 177 else { 178 logger.error("[ RuntimeException occured: " + ex.getMessage() + " ]"); 179 ex.printStackTrace(); 180 } 182 } 183 catch (Exception ex) { 184 logger.error("[ Exception occured: " + ex.getMessage() + " ]"); 185 ex.printStackTrace(); 186 throw new WebClippingException("Exception", ex); 187 } 188 189 } 190 191 192 195 public void removeLastUrl() { 196 super.removeLastUrl(); 197 int lastIndex = historyPageList.size() - 1; 198 if (!historyPageList.isEmpty()) { 199 historyPageList.remove(lastIndex); 201 lastPage_ = getLastPage(); 203 } 204 else { 205 logger.debug("Error: page list is already Empty"); 206 } 207 } 208 209 210 216 private HtmlAnchor getHtmlAnchor() throws ElementNotFoundException { 217 String url = getRelativeUrlFromUrlBean(); 218 int position = getUrlBean().getPosition(); 219 HtmlPage page = (HtmlPage) historyPageList.get(position); 220 HtmlAnchor anchor = page.getAnchorByHref(url); 221 return anchor; 222 } 223 224 225 231 private HtmlArea getHtmlArea() throws ElementNotFoundException { 232 String url = getRelativeUrlFromUrlBean(); 233 Iterator allChildit = lastPage_.getAllHtmlChildElements(); 234 while (allChildit.hasNext()) { 235 Object o = allChildit.next(); 236 if (o instanceof HtmlArea) { 237 HtmlArea a = (HtmlArea) o; 238 String href = a.getHrefAttribute(); 239 if (href.equalsIgnoreCase(url)) { 240 return a; 241 } 242 } 243 } 244 return null; 245 } 246 247 248 249 254 private HtmlPage getLastPage() { 255 int lastIndex = historyPageList.size() - 1; 256 if (!historyPageList.isEmpty()) { 257 logger.debug("Get last page"); 258 HtmlPage page = (HtmlPage) historyPageList.remove(lastIndex); 259 return page; 260 } 261 else { 262 logger.warn("Page history list is Empty"); 263 return null; 264 } 265 } 266 267 268 269 277 private HtmlInput getSubmittedButton(HtmlForm form) throws ElementNotFoundException, IOException { 278 UrlBean uBean = getUrlBean(); 279 String fromHash = uBean.getHash(); 280 281 UrlBean uBeanJustBefore = getPreviousUrlBean(); 283 logger.debug("Url just before: " + uBeanJustBefore.getAbsoluteUrlValue()); 284 logger.debug("New url: " + uBean.getAbsoluteUrlValue()); 285 286 FormParamBean fBean = uBeanJustBefore.getSubmittedParamBean(fromHash); 287 288 HtmlInput submitButton = null; 290 if (fBean != null) { 292 String submitButtonId = fBean.getIdAtt(); 294 String submitButtonName = fBean.getName(); 295 String submitButtonValue = fBean.getUsedValue(); 296 if (submitButtonId != null && !submitButtonId.equalsIgnoreCase("")) { 297 submitButton = (HtmlInput) form.getHtmlElementById(submitButtonId); 298 } 299 else if (submitButtonName != null && !submitButtonName.equalsIgnoreCase("")) { 300 submitButton = form.getInputByName(submitButtonName); 301 } 302 else if (submitButtonValue != null && !submitButtonValue.equalsIgnoreCase("")) { 303 submitButton = form.getInputByValue(submitButtonValue); 304 } 305 } 306 307 return submitButton; 308 } 309 310 311 317 private HtmlForm getSubmittedForm() throws ElementNotFoundException { 318 String fromHash = getUrlBean().getHash(); 319 logger.debug("From Hash: " + fromHash); 320 HtmlForm form = null; 321 String submittedFormName = HashUtilities.getFormNameFromHash(fromHash); 322 String submittedFormId = HashUtilities.getFormIdFromHash(fromHash); 323 if (submittedFormName == null || submittedFormName.equalsIgnoreCase("")) { 325 if (submittedFormId == null || submittedFormId.equalsIgnoreCase("")) { 326 List formList = lastPage_.getForms(); 327 int pos = Integer.parseInt(HashUtilities.getFormPositionFromHash(fromHash)); 328 form = (HtmlForm) formList.get(pos); 329 330 } 331 else { 332 form = (HtmlForm) lastPage_.getHtmlElementById(submittedFormId); 333 } 334 } 335 else { 336 form = (HtmlForm) lastPage_.getFormByName(submittedFormName); 337 } 338 339 return form; 340 } 341 342 343 344 349 private WebClient getNewWebClient() { 350 String proxyHost = getProxyHost(); 352 int proxyPort = getProxyPort(); 353 354 WebClient webClient = null; 356 if (proxyHost != null && (proxyPort != 0)) { 357 webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER_6_0, proxyHost, proxyPort); 358 } 359 else { 360 webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER_6_0); 361 } 362 363 webClient.setRefreshHandler(new ThreadedRefreshHandler()); 364 365 webClient.setThrowExceptionOnScriptError(false); 367 webClient.setThrowExceptionOnFailingStatusCode(false); 368 369 370 373 return webClient; 374 } 375 376 377 382 private void validate(HtmlPage page) { 383 lastPage_ = page; 384 addPageToHistory(lastPage_); 385 } 386 387 388 393 private void addPageToHistory(HtmlPage page) { 394 System.err.println("add page size"); 395 int position = getUrlBean().getPosition(); 396 int lastPos = historyPageList.size() - 1; 397 if (position > lastPos) { 398 historyPageList.add(page); 399 } 400 else { 401 historyPageList.set(position, page); 402 } 403 } 404 405 406 407 412 private void fillForm(HtmlForm form) { 413 UrlBean uBean = getUrlBean(); 414 415 List queryParamList = uBean.getQueryParamBeanList(); 417 for (int i = 0; i < queryParamList.size(); i++) { 418 QueryParamBean qBean = (QueryParamBean) queryParamList.get(i); 419 String name = qBean.getName(); 420 String value = qBean.getDefaultValue(); 421 String type = null; 422 FormParamBean fBean = qBean.getFormParamBean(); 423 if (fBean != null) { 424 type = fBean.getType(); 425 } 426 else { 427 type = ""; 429 } 430 int position = qBean.getPosition(); 431 432 if (!type.equalsIgnoreCase(WebConstants.TYPE_HIDDEN)) { 434 fillInputByName(form, name, value, position); 435 } 436 437 fillSelectByName(form, name, value, position); 439 } 440 441 } 442 443 444 452 private void fillInputByName(HtmlForm form, String name, String value, int position) { 453 List allInputByName = form.getAllInputsByName(name); 455 if (allInputByName == null || allInputByName.size() == 0) { 456 logger.debug("[ Input List whith name = " + name + " not found ]"); 457 return; 458 } 459 460 HtmlInput input = (HtmlInput) allInputByName.get(position); 461 if (input != null) { 462 logger.debug("[ Input param for queryBean: " + name + ", value: " + value + " ]"); 463 464 input.setValueAttribute(value); 466 467 if (input instanceof HtmlCheckBoxInput) { 469 ((HtmlCheckBoxInput) input).setChecked(true); 470 } 471 if (input instanceof HtmlRadioButtonInput) { 473 ((HtmlRadioButtonInput) input).setChecked(true); 474 } 475 476 } 477 else { 478 logger.error("[ Input param for queryBean: " + name + " ]"); 479 } 480 } 481 482 483 491 private void fillSelectByName(HtmlForm form, String name, String value, int position) { 492 List allSelectByName = form.getSelectsByName(name); 494 if (allSelectByName == null || allSelectByName.size() == 0) { 495 return; 496 } 497 498 HtmlSelect select = (HtmlSelect) allSelectByName.get(position); 499 if (select != null) { 500 logger.debug("[ Input param for queryBean: " + name + " ]"); 501 select.setSelectedAttribute(value, true); 502 } 503 else { 504 logger.error("[ Input param for queryBean: " + name + " ]"); 505 } 506 } 507 508 509 512 private void assertNotNullWebClient() { 513 if (webClient == null) { 514 setWebClient(getNewWebClient()); 515 } 516 517 } 518 519 } 520 | Popular Tags |