Java Code Examples for com.gargoylesoftware.htmlunit.WebClient#loadWebResponse()

The following examples show how to use com.gargoylesoftware.htmlunit.WebClient#loadWebResponse() . 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: HtmlEmbed.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Saves this content as the specified file.
 * @param file the file to save to
 * @throws IOException if an IO error occurs
 */
public void saveAs(final File file) throws IOException {
    final HtmlPage page = (HtmlPage) getPage();
    final WebClient webclient = page.getWebClient();

    final URL url = page.getFullyQualifiedUrl(getAttributeDirect(SRC_ATTRIBUTE));
    final WebRequest request = new WebRequest(url);
    request.setCharset(page.getCharset());
    request.setAdditionalHeader(HttpHeader.REFERER, page.getUrl().toExternalForm());
    final WebResponse webResponse = webclient.loadWebResponse(request);

    try (OutputStream fos = Files.newOutputStream(file.toPath());
            InputStream content =  webResponse.getContentAsStream()) {
        IOUtils.copy(content, fos);
    }
}
 
Example 2
Source File: HtmlImageInput.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * <p>Downloads the image contained by this image element.</p>
 * <p><span style="color:red">POTENTIAL PERFORMANCE KILLER - DOWNLOADS THE IMAGE - USE AT YOUR OWN RISK</span></p>
 * <p>If the image has not already been downloaded, this method triggers a download and caches the image.</p>
 *
 * @throws IOException if an error occurs while downloading the image
 */
private void downloadImageIfNeeded() throws IOException {
    if (!downloaded_) {
        // HTMLIMAGE_BLANK_SRC_AS_EMPTY
        final String src = getSrcAttribute();
        if (!"".equals(src)
                && !(hasFeature(HTMLIMAGE_BLANK_SRC_AS_EMPTY) && StringUtils.isBlank(src))) {
            final HtmlPage page = (HtmlPage) getPage();
            final WebClient webClient = page.getWebClient();

            final URL url = page.getFullyQualifiedUrl(src);
            final BrowserVersion browser = webClient.getBrowserVersion();
            final WebRequest request = new WebRequest(url, browser.getImgAcceptHeader(),
                                                            browser.getAcceptEncodingHeader());
            request.setCharset(page.getCharset());
            request.setAdditionalHeader(HttpHeader.REFERER, page.getUrl().toExternalForm());
            imageWebResponse_ = webClient.loadWebResponse(request);
        }

        downloaded_ = hasFeature(JS_IMAGE_COMPLETE_RETURNS_TRUE_FOR_NO_REQUEST)
                || (imageWebResponse_ != null && imageWebResponse_.getContentType().contains("image"));
    }
}
 
Example 3
Source File: HtmlLink.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
 *
 * If the linked content is not already downloaded it triggers a download. Then it stores the response
 * for later use.<br>
 *
 * @param downloadIfNeeded indicates if a request should be performed this hasn't been done previously
 * @param request the request; if null getWebRequest() is called to create one
 * @return {@code null} if no download should be performed and when this wasn't already done; the response
 * received when performing a request for the content referenced by this tag otherwise
 * @throws IOException if an error occurs while downloading the content
 */
public WebResponse getWebResponse(final boolean downloadIfNeeded, WebRequest request) throws IOException {
    if (downloadIfNeeded && cachedWebResponse_ == null) {
        final WebClient webclient = getPage().getWebClient();
        if (null == request) {
            request = getWebRequest();
        }
        try {
            cachedWebResponse_ = webclient.loadWebResponse(request);
            final int statusCode = cachedWebResponse_.getStatusCode();
            final boolean successful = statusCode >= HttpStatus.SC_OK
                                            && statusCode < HttpStatus.SC_MULTIPLE_CHOICES;
            if (successful) {
                executeEvent(Event.TYPE_LOAD);
            }
            else {
                executeEvent(Event.TYPE_ERROR);
            }
        }
        catch (final IOException e) {
            executeEvent(Event.TYPE_ERROR);
            throw e;
        }
    }
    return cachedWebResponse_;
}
 
Example 4
Source File: HtmlImageInput.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * <p>Downloads the image contained by this image element.</p>
 * <p><span style="color:red">POTENTIAL PERFORMANCE KILLER - DOWNLOADS THE IMAGE - USE AT YOUR OWN RISK</span></p>
 * <p>If the image has not already been downloaded, this method triggers a download and caches the image.</p>
 *
 * @throws IOException if an error occurs while downloading the image
 */
private void downloadImageIfNeeded() throws IOException {
    if (!downloaded_) {
        // HTMLIMAGE_BLANK_SRC_AS_EMPTY
        final String src = getSrcAttribute();
        if (!"".equals(src)
                && !(hasFeature(HTMLIMAGE_BLANK_SRC_AS_EMPTY) && StringUtils.isBlank(src))) {
            final HtmlPage page = (HtmlPage) getPage();
            final WebClient webclient = page.getWebClient();

            final URL url = page.getFullyQualifiedUrl(src);
            final String accept = webclient.getBrowserVersion().getImgAcceptHeader();
            final WebRequest request = new WebRequest(url, accept);
            request.setAdditionalHeader(HttpHeader.REFERER, page.getUrl().toExternalForm());
            imageWebResponse_ = webclient.loadWebResponse(request);
        }

        if (imageData_ != null) {
            imageData_.close();
            imageData_ = null;
        }
        downloaded_ = hasFeature(JS_IMAGE_COMPLETE_RETURNS_TRUE_FOR_NO_REQUEST)
                || (imageWebResponse_ != null && imageWebResponse_.getContentType().contains("image"));
    }
}
 
Example 5
Source File: HtmlLink.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
 *
 * If the linked content is not already downloaded it triggers a download. Then it stores the response
 * for later use.<br>
 *
 * @param downloadIfNeeded indicates if a request should be performed this hasn't been done previously
 * @param request the request; if null getWebRequest() is called to create one
 * @return {@code null} if no download should be performed and when this wasn't already done; the response
 * received when performing a request for the content referenced by this tag otherwise
 * @throws IOException if an error occurs while downloading the content
 */
public WebResponse getWebResponse(final boolean downloadIfNeeded, WebRequest request) throws IOException {
    if (downloadIfNeeded && cachedWebResponse_ == null) {
        final WebClient webclient = getPage().getWebClient();
        if (null == request) {
            request = getWebRequest();
        }
        try {
            cachedWebResponse_ = webclient.loadWebResponse(request);
            final int statusCode = cachedWebResponse_.getStatusCode();
            final boolean successful = statusCode >= HttpStatus.SC_OK
                                            && statusCode < HttpStatus.SC_MULTIPLE_CHOICES;
            if (successful) {
                executeEvent(Event.TYPE_LOAD);
            }
            else {
                executeEvent(Event.TYPE_ERROR);
            }
        }
        catch (final IOException e) {
            executeEvent(Event.TYPE_ERROR);
            throw e;
        }
    }
    return cachedWebResponse_;
}
 
Example 6
Source File: DedicatedWorkerGlobalScope.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
void loadAndExecute(final WebClient webClient, final String url,
        final Context context, final boolean checkMimeType) throws IOException {
    final HtmlPage page = (HtmlPage) owningWindow_.getDocument().getPage();
    final URL fullUrl = page.getFullyQualifiedUrl(url);

    final WebRequest webRequest = new WebRequest(fullUrl);
    final WebResponse response = webClient.loadWebResponse(webRequest);
    if (checkMimeType && !MimeType.isJavascriptMimeType(response.getContentType())) {
        throw Context.reportRuntimeError(
                "NetworkError: importScripts response is not a javascript response");
    }

    final String scriptCode = response.getContentAsString();
    final JavaScriptEngine javaScriptEngine = (JavaScriptEngine) webClient.getJavaScriptEngine();

    final DedicatedWorkerGlobalScope thisScope = this;
    final ContextAction<Object> action = new ContextAction<Object>() {
        @Override
        public Object run(final Context cx) {
            final Script script = javaScriptEngine.compile(page, thisScope, scriptCode,
                    fullUrl.toExternalForm(), 1);
            return javaScriptEngine.execute(page, thisScope, script);
        }
    };

    final ContextFactory cf = javaScriptEngine.getContextFactory();

    if (context != null) {
        action.run(context);
    }
    else {
        final JavaScriptJob job = new WorkerJob(cf, action, "loadAndExecute " + url);

        owningWindow_.getWebWindow().getJobManager().addJob(job, page);
    }
}
 
Example 7
Source File: HtmlImage.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * <p>Downloads the image contained by this image element.</p>
 * <p><span style="color:red">POTENTIAL PERFORMANCE KILLER - DOWNLOADS THE IMAGE - USE AT YOUR OWN RISK</span></p>
 * <p>If the image has not already been downloaded, this method triggers a download and caches the image.</p>
 *
 * @throws IOException if an error occurs while downloading the image
 */
private void downloadImageIfNeeded() throws IOException {
    if (!downloaded_) {
        // HTMLIMAGE_BLANK_SRC_AS_EMPTY
        final String src = getSrcAttribute();

        if (!"".equals(src)) {
            final HtmlPage page = (HtmlPage) getPage();
            final WebClient webClient = page.getWebClient();
            final BrowserVersion browser = webClient.getBrowserVersion();

            if (!(browser.hasFeature(HTMLIMAGE_BLANK_SRC_AS_EMPTY)
                    && StringUtils.isBlank(src))) {
                final URL url = page.getFullyQualifiedUrl(src);
                final WebRequest request = new WebRequest(url, browser.getImgAcceptHeader(),
                                                                browser.getAcceptEncodingHeader());
                request.setCharset(page.getCharset());
                request.setAdditionalHeader(HttpHeader.REFERER, page.getUrl().toExternalForm());
                imageWebResponse_ = webClient.loadWebResponse(request);
            }
        }

        if (imageData_ != null) {
            imageData_.close();
            imageData_ = null;
        }
        downloaded_ = true;
        isComplete_ = hasFeature(JS_IMAGE_COMPLETE_RETURNS_TRUE_FOR_NO_REQUEST)
                || (imageWebResponse_ != null && imageWebResponse_.getContentType().contains("image"));

        width_ = -1;
        height_ = -1;
    }
}
 
Example 8
Source File: DedicatedWorkerGlobalScope.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
void loadAndExecute(final String url, final Context context) throws IOException {
    final HtmlPage page = (HtmlPage) owningWindow_.getDocument().getPage();
    final URL fullUrl = page.getFullyQualifiedUrl(url);

    final WebClient webClient = owningWindow_.getWebWindow().getWebClient();

    final WebRequest webRequest = new WebRequest(fullUrl);
    final WebResponse response = webClient.loadWebResponse(webRequest);
    final String scriptCode = response.getContentAsString();
    final JavaScriptEngine javaScriptEngine = (JavaScriptEngine) webClient.getJavaScriptEngine();

    final DedicatedWorkerGlobalScope thisScope = this;
    final ContextAction action = new ContextAction() {
        @Override
        public Object run(final Context cx) {
            final Script script = javaScriptEngine.compile(page, thisScope, scriptCode,
                    fullUrl.toExternalForm(), 1);
            return javaScriptEngine.execute(page, thisScope, script);
        }
    };

    final ContextFactory cf = javaScriptEngine.getContextFactory();

    if (context != null) {
        action.run(context);
    }
    else {
        final JavaScriptJob job = new WorkerJob(cf, action, "loadAndExecute " + url);

        owningWindow_.getWebWindow().getJobManager().addJob(job, page);
    }
}
 
Example 9
Source File: HtmlImage.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * <p>Downloads the image contained by this image element.</p>
 * <p><span style="color:red">POTENTIAL PERFORMANCE KILLER - DOWNLOADS THE IMAGE - USE AT YOUR OWN RISK</span></p>
 * <p>If the image has not already been downloaded, this method triggers a download and caches the image.</p>
 *
 * @throws IOException if an error occurs while downloading the image
 */
private void downloadImageIfNeeded() throws IOException {
    if (!downloaded_) {
        // HTMLIMAGE_BLANK_SRC_AS_EMPTY
        final String src = getSrcAttribute();
        if (!"".equals(src)
                && !(hasFeature(HTMLIMAGE_BLANK_SRC_AS_EMPTY) && StringUtils.isBlank(src))) {
            final HtmlPage page = (HtmlPage) getPage();
            final WebClient webclient = page.getWebClient();

            final URL url = page.getFullyQualifiedUrl(src);
            final String accept = webclient.getBrowserVersion().getImgAcceptHeader();
            final WebRequest request = new WebRequest(url, accept);
            request.setAdditionalHeader(HttpHeader.REFERER, page.getUrl().toExternalForm());
            imageWebResponse_ = webclient.loadWebResponse(request);
        }

        if (imageData_ != null) {
            imageData_.close();
            imageData_ = null;
        }
        downloaded_ = true;
        isComplete_ = hasFeature(JS_IMAGE_COMPLETE_RETURNS_TRUE_FOR_NO_REQUEST)
                || (imageWebResponse_ != null && imageWebResponse_.getContentType().contains("image"));

        width_ = -1;
        height_ = -1;
    }
}
 
Example 10
Source File: HtmlEmbed.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Saves this content as the specified file.
 * @param file the file to save to
 * @throws IOException if an IO error occurs
 */
public void saveAs(final File file) throws IOException {
    final HtmlPage page = (HtmlPage) getPage();
    final WebClient webclient = page.getWebClient();

    final URL url = page.getFullyQualifiedUrl(getAttributeDirect("src"));
    final WebRequest request = new WebRequest(url);
    request.setAdditionalHeader(HttpHeader.REFERER, page.getUrl().toExternalForm());
    final WebResponse webResponse = webclient.loadWebResponse(request);

    try (FileOutputStream fos = new FileOutputStream(file);
            InputStream content =  webResponse.getContentAsStream()) {
        IOUtils.copy(content, fos);
    }
}