com.gargoylesoftware.htmlunit.StringWebResponse Java Examples

The following examples show how to use com.gargoylesoftware.htmlunit.StringWebResponse. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FrameWindow.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void setEnclosedPage(final Page page) {
    setPageDenied(PageDenied.NONE);
    super.setEnclosedPage(page);

    // we have updated a frame window by javascript write();
    // so we have to disable future updates during initialization
    // see com.gargoylesoftware.htmlunit.html.HtmlPage.loadFrames()
    final WebResponse webResponse = page.getWebResponse();
    if (webResponse instanceof StringWebResponse) {
        final StringWebResponse response = (StringWebResponse) webResponse;
        if (response.isFromJavascript()) {
            final BaseFrameElement frame = getFrameElement();
            frame.setContentLoaded();
        }
    }
}
 
Example #2
Source File: FrameWindow.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void setEnclosedPage(final Page page) {
    super.setEnclosedPage(page);

    // we have updated a frame window by javascript write();
    // so we have to disable future updates during initialization
    // see com.gargoylesoftware.htmlunit.html.HtmlPage.loadFrames()
    final WebResponse webResponse = page.getWebResponse();
    if (webResponse instanceof StringWebResponse) {
        final StringWebResponse response = (StringWebResponse) webResponse;
        if (response.isFromJavascript()) {
            final BaseFrameElement frame = getFrameElement();
            frame.setContentLoaded();
        }
    }
}
 
Example #3
Source File: HTMLParserTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Tests the new HTMLParser on a simple HTML string.
 * @throws Exception failure
 */
@Test
public void simpleHTMLString() throws Exception {
    final WebClient webClient = getWebClient();
    final WebResponse webResponse = new StringWebResponse(
        "<html><head><title>TITLE</title></head><body><div>TEST</div></body></html>", URL_FIRST);

    final HtmlPage page = webClient.getPageCreator().getHtmlParser()
                                        .parseHtml(webResponse, webClient.getCurrentWindow());

    final String stringVal = page.<HtmlDivision>getFirstByXPath("//div").getFirstChild().getNodeValue();
    assertEquals("TEST", stringVal);

    final HtmlElement node = (HtmlElement) page.getFirstByXPath("//*[./text() = 'TEST']");
    assertEquals(node.getTagName(), HtmlDivision.TAG_NAME);
}
 
Example #4
Source File: HTMLParserTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception failure
 */
@Test
public void tableWithoutColgroup() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_
        + "<html><head>\n"
        + "</head>\n"
        + "<body>\n"
        + "  <table><col width='7'/><col width='1'/><tbody><tr><td>seven</td><td>One</td></tr></tbody></table>\n"
        + "</body></html>";

    final WebClient webClient = getWebClient();
    final WebResponse webResponse = new StringWebResponse(html, URL_FIRST);

    final XHtmlPage page = webClient.getPageCreator().getHtmlParser()
                                .parseXHtml(webResponse, webClient.getCurrentWindow());

    final DomElement col = page.getElementsByTagName("col").get(0);
    assertEquals(col.getParentNode().getNodeName(), HtmlTableColumnGroup.TAG_NAME);
}
 
Example #5
Source File: UIServletTest.java    From celos with Apache License 2.0 5 votes vote down vote up
@Test
public void testRender() throws Exception {
    ScheduledTime end = new ScheduledTime("2015-09-03T13:17Z");
    ScheduledTime start = new ScheduledTime("2015-09-03T13:11Z");
    NavigableSet<ScheduledTime> tileTimes = new TreeSet<>(ImmutableSet.of(new ScheduledTime("2015-09-03T13:10Z"), new ScheduledTime("2015-09-03T13:15Z")));
    WorkflowID id = new WorkflowID("foo");
    List<WorkflowGroup> groups = ImmutableList.of(new WorkflowGroup("All workflows", ImmutableList.of(id)));
    WorkflowInfo workflowInfo = new WorkflowInfo(new URL("http://example.com"), ImmutableList.of());
    SlotState state1 = new SlotState(new SlotID(id, new ScheduledTime("2015-09-03T13:16Z")), SlotState.Status.FAILURE);
    SlotState state2 = new SlotState(new SlotID(id, new ScheduledTime("2015-09-03T13:12Z")), SlotState.Status.WAITING);
    List<SlotState> slotStates = ImmutableList.of(state1, state2);
    Map<WorkflowID, WorkflowStatus> statuses = ImmutableMap.of(id, new WorkflowStatus(workflowInfo, slotStates, false));
    UIConfiguration conf = new UIConfiguration(start, end, tileTimes, groups, statuses, new URL("http://example.com"));
    
    StringWebResponse response = new StringWebResponse(UIServlet.render(conf), new URL("http://example.com"));
    WebClient webClient = new WebClient();
    webClient.setThrowExceptionOnFailingStatusCode(false);
    HtmlPage page = HTMLParser.parse(response, new TopLevelWindow("top", webClient));
    
    // Some basic sanity checking
    
    List<HtmlTableDataCell> slotCells = (List<HtmlTableDataCell>) page.getByXPath("//td[contains(@class, 'slot')]");
    Assert.assertEquals("fail", slotCells.get(0).getTextContent());
    Assert.assertEquals("wait", slotCells.get(1).getTextContent());
    
    List<HtmlTableDataCell> hourCells = (List<HtmlTableDataCell>) page.getByXPath("//td[contains(@class, 'hour')]");
    Assert.assertEquals("1315", hourCells.get(0).getTextContent());
    Assert.assertEquals("1310", hourCells.get(1).getTextContent());
    
    List<HtmlTableDataCell> workflowCells = (List<HtmlTableDataCell>) page.getByXPath("//td[@class='workflow']");
    Assert.assertEquals("foo", workflowCells.get(0).getTextContent());
    
    System.out.println(response.getContentAsString());
}
 
Example #6
Source File: XMLDocument.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
private static XmlPage createParserErrorXmlPage(final String message, final WebWindow webWindow)
        throws IOException {
    final String xml = "<parsererror xmlns=\"http://www.mozilla.org/newlayout/xml/parsererror.xml\">\n"
        + message + "\n"
        + "<sourcetext></sourcetext>\n"
        + "</parsererror>";

    final WebResponse webResponse = new StringWebResponse(xml, webWindow.getEnclosedPage().getUrl());

    return new XmlPage(webResponse, webWindow, false);
}
 
Example #7
Source File: RegexHttpWebConnection.java    From nutch-htmlunit with Apache License 2.0 5 votes vote down vote up
@Override
public WebResponse getResponse(final WebRequest request) throws IOException {
    final URL url = request.getUrl();
    if (StringUtils.isBlank(filter(url.toString())) || url.toString().indexOf("robots.txt") > -1) {
        LOG.info("Thread: {}, - Http Excluding URL: {}", Thread.currentThread().getId(), url);
        return new StringWebResponse("", url);
    }
    if (LOG.isInfoEnabled()) {
        LOG.info("Thread: {}, + Http Fetching URL: {}", Thread.currentThread().getId(), url);
    }
    return super.getResponse(request);
}
 
Example #8
Source File: XMLDOMDocument.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Loads an XML document using the supplied string.
 * @param strXML the XML string to load into this XML document object;
 *     this string can contain an entire XML document or a well-formed fragment
 * @return {@code true} if the load succeeded; {@code false} if the load failed
 */
@JsxFunction
public boolean loadXML(final String strXML) {
    try {
        final WebWindow webWindow = getWindow().getWebWindow();

        final WebResponse webResponse = new StringWebResponse(strXML, webWindow.getEnclosedPage().getUrl());
        final XmlPage page = new XmlPage(webResponse, webWindow, false, false);
        setDomNode(page);

        preserveWhiteSpaceDuringLoad_ = preserveWhiteSpace_;
        url_ = "";
        return true;
    }
    catch (final IOException e) {
        final XMLDOMParseError parseError = getParseError();
        parseError.setErrorCode(-1);
        parseError.setFilepos(1);
        parseError.setLine(1);
        parseError.setLinepos(1);
        parseError.setReason(e.getMessage());
        parseError.setSrcText("xml");
        parseError.setUrl("");
        if (LOG.isDebugEnabled()) {
            LOG.debug("Error parsing XML\n" + strXML, e);
        }
        return false;
    }
}
 
Example #9
Source File: FrameWindow.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
protected boolean isJavaScriptInitializationNeeded() {
    return getScriptableObject() == null
        || !(getEnclosedPage().getWebResponse() instanceof StringWebResponse);
    // TODO: find a better way to distinguish content written by document.open(),...
}
 
Example #10
Source File: HTMLDocument.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
@JsxFunction(FF)
public void close() throws IOException {
    if (writeInCurrentDocument_) {
        LOG.warn("close() called when document is not open.");
    }
    else {
        final HtmlPage page = getPage();
        final URL url = page.getUrl();
        final StringWebResponse webResponse = new StringWebResponse(writeBuilder_.toString(), url);
        webResponse.setFromJavascript(true);
        writeInCurrentDocument_ = true;
        writeBuilder_.setLength(0);

        final WebClient webClient = page.getWebClient();
        final WebWindow window = page.getEnclosingWindow();
        // reset isAttachedToPageDuringOnload_ to trigger the onload event for chrome also
        if (window instanceof FrameWindow) {
            final BaseFrameElement frame = ((FrameWindow) window).getFrameElement();
            final ScriptableObject scriptable = frame.getScriptableObject();
            if (scriptable instanceof HTMLIFrameElement) {
                ((HTMLIFrameElement) scriptable).onRefresh();
            }
        }
        webClient.loadWebResponseInto(webResponse, window);
    }
}
 
Example #11
Source File: DOMImplementation.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Creates an {@link HTMLDocument}.
 * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/DOMImplementation/createHTMLDocument">
 *   createHTMLDocument (MDN)</a>
 *
 * @param titleObj the document title
 * @return the newly created {@link HTMLDocument}
 */
@JsxFunction
public HTMLDocument createHTMLDocument(final Object titleObj) {
    if (titleObj == Undefined.instance
            && getBrowserVersion().hasFeature(JS_DOMIMPLEMENTATION_CREATE_HTMLDOCOMENT_REQUIRES_TITLE)) {
        throw Context.reportRuntimeError("Title is required");
    }

    final HTMLDocument document = new HTMLDocument();
    document.setParentScope(getParentScope());
    document.setPrototype(getPrototype(document.getClass()));

    // According to spec and behavior of function in browsers new document
    // has no location object and is not connected with any window
    final WebResponse resp = new StringWebResponse("", WebClient.URL_ABOUT_BLANK);
    final HtmlPage page = new HtmlPage(resp, getWindow().getWebWindow());
    page.setEnclosingWindow(null);
    document.setDomNode(page);

    final HTMLHtmlElement html = (HTMLHtmlElement) document.createElement("html");
    page.appendChild(html.getDomNodeOrDie());

    final HTMLHeadElement head = (HTMLHeadElement) document.createElement("head");
    html.appendChild(head);

    if (titleObj != Undefined.instance) {
        final HTMLTitleElement title = (HTMLTitleElement) document.createElement("title");
        head.appendChild(title);
        title.setTextContent(Context.toString(titleObj));
    }

    final HTMLBodyElement body = (HTMLBodyElement) document.createElement("body");
    html.appendChild(body);

    return document;
}
 
Example #12
Source File: XMLDocument.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
private static XmlPage createParserErrorXmlPage(final String message, final WebWindow webWindow)
        throws IOException {
    final String xml = "<parsererror xmlns=\"http://www.mozilla.org/newlayout/xml/parsererror.xml\">\n"
        + message + "\n"
        + "<sourcetext></sourcetext>\n"
        + "</parsererror>";

    final WebResponse webResponse = new StringWebResponse(xml, webWindow.getEnclosedPage().getUrl());

    return new XmlPage(webResponse, webWindow, false);
}
 
Example #13
Source File: XmlPageTest.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
private void asText(final String xml, final String expected) throws Exception {
    final StringWebResponse response = new StringWebResponse(xml, new URL("http://www.test.com"));
    final XmlPage xmlPage = new XmlPage(response, getWebClient().getCurrentWindow());

    final DomElement msg = (DomElement) xmlPage.getFirstByXPath("//msg");
    assertNotNull("No element found by XPath '//msg'", msg);
    assertEquals(expected, msg.asText());
}
 
Example #14
Source File: XmlPageTest.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
public void asTextOnlyText() throws Exception {
    final StringWebResponse response = new StringWebResponse("<msg>abc</msg>", new URL("http://www.test.com"));
    final XmlPage xmlPage = new XmlPage(response, getWebClient().getCurrentWindow());

    assertEquals("abc", ((DomText) xmlPage.getFirstByXPath("/msg/text()")).asText());
}
 
Example #15
Source File: XMLDOMDocument.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Loads an XML document using the supplied string.
 * @param strXML the XML string to load into this XML document object;
 *     this string can contain an entire XML document or a well-formed fragment
 * @return {@code true} if the load succeeded; {@code false} if the load failed
 */
@JsxFunction
public boolean loadXML(final String strXML) {
    try {
        final WebWindow webWindow = getWindow().getWebWindow();

        final WebResponse webResponse = new StringWebResponse(strXML, webWindow.getEnclosedPage().getUrl());
        final XmlPage page = new XmlPage(webResponse, webWindow, false, false);
        setDomNode(page);

        preserveWhiteSpaceDuringLoad_ = preserveWhiteSpace_;
        url_ = "";
        return true;
    }
    catch (final IOException e) {
        final XMLDOMParseError parseError = getParseError();
        parseError.setErrorCode(-1);
        parseError.setFilepos(1);
        parseError.setLine(1);
        parseError.setLinepos(1);
        parseError.setReason(e.getMessage());
        parseError.setSrcText("xml");
        parseError.setUrl("");
        if (LOG.isDebugEnabled()) {
            LOG.debug("Error parsing XML\n" + strXML, e);
        }
        return false;
    }
}
 
Example #16
Source File: FrameWindow.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
protected boolean isJavaScriptInitializationNeeded() {
    return getScriptableObject() == null
        || !(getEnclosedPage().getWebResponse() instanceof StringWebResponse);
    // TODO: find a better way to distinguish content written by document.open(),...
}
 
Example #17
Source File: HTMLDocument.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
@Override
@JsxFunction({FF, FF68, FF60})
public void close() throws IOException {
    if (writeInCurrentDocument_) {
        LOG.warn("close() called when document is not open.");
    }
    else {
        final HtmlPage page = getPage();
        final URL url = page.getUrl();
        final StringWebResponse webResponse = new StringWebResponse(writeBuilder_.toString(), url);
        webResponse.setFromJavascript(true);
        writeInCurrentDocument_ = true;
        writeBuilder_.setLength(0);

        final WebClient webClient = page.getWebClient();
        final WebWindow window = page.getEnclosingWindow();
        // reset isAttachedToPageDuringOnload_ to trigger the onload event for chrome also
        if (window instanceof FrameWindow) {
            final BaseFrameElement frame = ((FrameWindow) window).getFrameElement();
            final ScriptableObject scriptable = frame.getScriptableObject();
            if (scriptable instanceof HTMLIFrameElement) {
                ((HTMLIFrameElement) scriptable).onRefresh();
            }
        }
        webClient.loadWebResponseInto(webResponse, window);
    }
}
 
Example #18
Source File: DOMImplementation.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Creates an {@link HTMLDocument}.
 * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/DOMImplementation/createHTMLDocument">
 *   createHTMLDocument (MDN)</a>
 *
 * @param titleObj the document title
 * @return the newly created {@link HTMLDocument}
 */
@JsxFunction
public HTMLDocument createHTMLDocument(final Object titleObj) {
    if (Undefined.isUndefined(titleObj)
            && getBrowserVersion().hasFeature(JS_DOMIMPLEMENTATION_CREATE_HTMLDOCOMENT_REQUIRES_TITLE)) {
        throw Context.reportRuntimeError("Title is required");
    }

    final HTMLDocument document = new HTMLDocument();
    document.setParentScope(getParentScope());
    document.setPrototype(getPrototype(document.getClass()));

    // According to spec and behavior of function in browsers new document
    // has no location object and is not connected with any window
    final WebResponse resp = new StringWebResponse("", WebClient.URL_ABOUT_BLANK);
    final HtmlPage page = new HtmlPage(resp, getWindow().getWebWindow());
    page.setEnclosingWindow(null);
    document.setDomNode(page);

    final HTMLHtmlElement html = (HTMLHtmlElement) document.createElement("html");
    page.appendChild(html.getDomNodeOrDie());

    final HTMLHeadElement head = (HTMLHeadElement) document.createElement("head");
    html.appendChild(head);

    if (!Undefined.isUndefined(titleObj)) {
        final HTMLTitleElement title = (HTMLTitleElement) document.createElement("title");
        head.appendChild(title);
        title.setTextContent(Context.toString(titleObj));
    }

    final HTMLBodyElement body = (HTMLBodyElement) document.createElement("body");
    html.appendChild(body);

    return document;
}