@Override public WebResponse loadWebResponse(final WebRequest webRequest) throws IOException { return new WebResponseProxy(super.loadWebResponse(webRequest)); } };
@Override public WebResponse loadWebResponse(final WebRequest webRequest) throws IOException { WebResponse webResponse = super.loadWebResponse(webRequest); if (!webResponseListeners.isEmpty()) { for (WebResponseListener listener : webResponseListeners) { listener.onLoadWebResponse(webRequest, webResponse); } } return webResponse; }
private OAuthToken getAccessToken(String refreshToken) throws IOException { final String tokenUrl = String.format(ACCESS_TOKEN_URL, refreshToken, oAuthParams.getRedirectUri(), oAuthParams.getClientId(), oAuthParams.getClientSecret()); final WebRequest webRequest = new WebRequest(new URL(tokenUrl), HttpMethod.POST); final WebResponse webResponse = webClient.loadWebResponse(webRequest); if (webResponse.getStatusCode() != HttpStatus.SC_OK) { throw new IOException(String.format("Error getting access token: [%s: %s]", webResponse.getStatusCode(), webResponse.getStatusMessage())); } final long currentTime = System.currentTimeMillis(); final ObjectMapper mapper = new ObjectMapper(); final Map map = mapper.readValue(webResponse.getContentAsStream(), Map.class); final String accessToken = map.get("access_token").toString(); final Integer expiresIn = Integer.valueOf(map.get("expires_in").toString()); return new OAuthToken(refreshToken, accessToken, currentTime + TimeUnit.MILLISECONDS.convert(expiresIn, TimeUnit.SECONDS)); }
/** * <span style="color:red">POTENIAL PERFORMANCE KILLER - DOWNLOADS THE RESOURCE - 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 * @return <code>null</code> 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) throws IOException { if (downloadIfNeeded && cachedWebResponse_ == null) { final WebClient webclient = getPage().getWebClient(); cachedWebResponse_ = webclient.loadWebResponse(getWebRequestSettings()); } return cachedWebResponse_; }
/** * <span style="color:red">POTENIAL PERFORMANCE KILLER - DOWNLOADS THE RESOURCE - 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 * @return <code>null</code> 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) throws IOException { if (downloadIfNeeded && cachedWebResponse_ == null) { final WebClient webclient = getPage().getWebClient(); cachedWebResponse_ = webclient.loadWebResponse(getWebRequestSettings()); } return cachedWebResponse_; }
/** * <span style="color:red">POTENIAL PERFORMANCE KILLER - DOWNLOADS THE RESOURCE - 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 * @return <code>null</code> 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) throws IOException { if (downloadIfNeeded && cachedWebResponse_ == null) { final WebClient webclient = getPage().getWebClient(); cachedWebResponse_ = webclient.loadWebResponse(getWebRequestSettings()); } return cachedWebResponse_; }
/** * 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); } }
/** * 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.setCharset(page.getCharset()); 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); } }
/** * Loads an XML document from the specified location. * * @param xmlSource a string containing a URL that specifies the location of the XML file * @return true if the load succeeded; false if the load failed */ @JsxFunction(FF) public boolean load(final String xmlSource) { if (async_) { if (LOG.isDebugEnabled()) { LOG.debug("XMLDocument.load(): 'async' is true, currently treated as false."); } } try { final WebWindow ww = getWindow().getWebWindow(); final HtmlPage htmlPage = (HtmlPage) ww.getEnclosedPage(); final WebRequest request = new WebRequest(htmlPage.getFullyQualifiedUrl(xmlSource)); final WebResponse webResponse = ww.getWebClient().loadWebResponse(request); final XmlPage page = new XmlPage(webResponse, ww, false); setDomNode(page); return true; } catch (final IOException e) { if (LOG.isDebugEnabled()) { LOG.debug("Error parsing XML from '" + xmlSource + "'", e); } return false; } }
/** * Performs the download and calls the callback method. */ @Override public void run() { final Scriptable scope = callback_.getParentScope(); final WebRequest request = new WebRequest(url_); try { final WebResponse webResponse = client_.loadWebResponse(request); final String content = webResponse.getContentAsString(); if (LOG.isDebugEnabled()) { LOG.debug("Downloaded content: " + StringUtils.abbreviate(content, 512)); } final Object[] args = new Object[] {content}; final ContextFactory cf = ((JavaScriptEngine) client_.getJavaScriptEngine()).getContextFactory(); cf.call(cx -> { callback_.call(cx, scope, scope, args); return null; }); } catch (final IOException e) { LOG.error("Behavior #default#download: Cannot download " + url_, e); } } }
/** * Performs the download and calls the callback method. */ public void run() { final WebClient client = getWindow().getWebWindow().getWebClient(); final Scriptable scope = callback_.getParentScope(); final WebRequestSettings settings = new WebRequestSettings(url_); try { final WebResponse webResponse = client.loadWebResponse(settings); final String content = webResponse.getContentAsString(); LOG.debug("Downloaded content: " + StringUtils.abbreviate(content, 512)); final Object[] args = new Object[] {content}; final ContextAction action = new ContextAction() { public Object run(final Context cx) { callback_.call(cx, scope, scope, args); return null; } }; final ContextFactory cf = client.getJavaScriptEngine().getContextFactory(); cf.call(action); } catch (final IOException e) { LOG.error("Behavior #default#download: Cannot download " + url_, e); } } }
/** * Performs the download and calls the callback method. */ public void run() { final WebClient client = getWindow().getWebWindow().getWebClient(); final Scriptable scope = callback_.getParentScope(); final WebRequestSettings settings = new WebRequestSettings(url_); try { final WebResponse webResponse = client.loadWebResponse(settings); final String content = webResponse.getContentAsString(); LOG.debug("Downloaded content: " + StringUtils.abbreviate(content, 512)); final Object[] args = new Object[] {content}; final ContextAction action = new ContextAction() { public Object run(final Context cx) { callback_.call(cx, scope, scope, args); return null; } }; final ContextFactory cf = client.getJavaScriptEngine().getContextFactory(); cf.call(action); } catch (final IOException e) { LOG.error("Behavior #default#download: Cannot download " + url_, e); } } }
/** * Performs the download and calls the callback method. */ public void run() { final WebClient client = getWindow().getWebWindow().getWebClient(); final Scriptable scope = callback_.getParentScope(); final WebRequestSettings settings = new WebRequestSettings(url_); try { final WebResponse webResponse = client.loadWebResponse(settings); final String content = webResponse.getContentAsString(); LOG.debug("Downloaded content: " + StringUtils.abbreviate(content, 512)); final Object[] args = new Object[] {content}; final ContextAction action = new ContextAction() { public Object run(final Context cx) { callback_.call(cx, scope, scope, args); return null; } }; final ContextFactory cf = client.getJavaScriptEngine().getContextFactory(); cf.call(action); } catch (final IOException e) { LOG.error("Behavior #default#download: Cannot download " + url_, e); } } }
/** * <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.setCharset(page.getCharset()); 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")); } }
request.setCharset(page.getCharset()); request.setAdditionalHeader(HttpHeader.REFERER, page.getUrl().toExternalForm()); imageWebResponse_ = webclient.loadWebResponse(request);
/** * <span style="color:red">POTENIAL PERFORMANCE KILLER - DOWNLOADS THE IMAGE - USE AT YOUR OWN RISK.</span><br/> * If the image is not already downloaded it triggers a download. Then it stores the image in the HtmlImage * object for later use.<br/> * * Downloads the image specified in the src attribute. * * @throws IOException if an error occurs while downloading the image or if the stream is of an * unsupported content-type */ private void downloadImageIfNeeded() throws IOException { if (!downloaded_) { final HtmlPage page = (HtmlPage) getPage(); final WebClient webclient = page.getWebClient(); final URL url = page.getFullyQualifiedUrl(getSrcAttribute()); final WebRequestSettings request = new WebRequestSettings(url); request.setAdditionalHeader("Referer", page.getWebResponse().getRequestSettings().getUrl().toExternalForm()); imageWebResponse_ = webclient.loadWebResponse(request); downloaded_ = true; } }
/** * <span style="color:red">POTENIAL PERFORMANCE KILLER - DOWNLOADS THE IMAGE - USE AT YOUR OWN RISK.</span><br/> * If the image is not already downloaded it triggers a download. Then it stores the image in the HtmlImage * object for later use.<br/> * * Downloads the image specified in the src attribute. * * @throws IOException if an error occurs while downloading the image or if the stream is of an * unsupported content-type */ private void downloadImageIfNeeded() throws IOException { if (!downloaded_) { final HtmlPage page = (HtmlPage) getPage(); final WebClient webclient = page.getWebClient(); final URL url = page.getFullyQualifiedUrl(getSrcAttribute()); final WebRequestSettings request = new WebRequestSettings(url); request.setAdditionalHeader("Referer", page.getWebResponse().getRequestSettings().getUrl().toExternalForm()); imageWebResponse_ = webclient.loadWebResponse(request); downloaded_ = true; } }
/** * <span style="color:red">POTENIAL PERFORMANCE KILLER - DOWNLOADS THE IMAGE - USE AT YOUR OWN RISK.</span><br/> * If the image is not already downloaded it triggers a download. Then it stores the image in the HtmlImage * object for later use.<br/> * * Downloads the image specified in the src attribute. * * @throws IOException if an error occurs while downloading the image or if the stream is of an * unsupported content-type */ private void downloadImageIfNeeded() throws IOException { if (!downloaded_) { final HtmlPage page = (HtmlPage) getPage(); final WebClient webclient = page.getWebClient(); final URL url = page.getFullyQualifiedUrl(getSrcAttribute()); final WebRequestSettings request = new WebRequestSettings(url); request.setAdditionalHeader("Referer", page.getWebResponse().getRequestSettings().getUrl().toExternalForm()); imageWebResponse_ = webclient.loadWebResponse(request); downloaded_ = true; } }
final WebResponse response = webClient.loadWebResponse(webRequest); final String scriptCode = response.getContentAsString(); final JavaScriptEngine javaScriptEngine = (JavaScriptEngine) webClient.getJavaScriptEngine();
final WebResponse response = webClient.loadWebResponse(webRequest); final String scriptCode = response.getContentAsString(); final JavaScriptEngine javaScriptEngine = (JavaScriptEngine) webClient.getJavaScriptEngine();