Java Code Examples for com.gargoylesoftware.htmlunit.WebResponse#getResponseHeaderValue()

The following examples show how to use com.gargoylesoftware.htmlunit.WebResponse#getResponseHeaderValue() . 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: XMLHttpRequest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
private boolean isPreflightAuthorized(final WebResponse preflightResponse) {
    final String originHeader = preflightResponse.getResponseHeaderValue(HttpHeader.ACCESS_CONTROL_ALLOW_ORIGIN);
    if (!ALLOW_ORIGIN_ALL.equals(originHeader)
            && !webRequest_.getAdditionalHeaders().get(HttpHeader.ORIGIN).equals(originHeader)) {
        return false;
    }
    String headersHeader = preflightResponse.getResponseHeaderValue(HttpHeader.ACCESS_CONTROL_ALLOW_HEADERS);
    if (headersHeader == null) {
        headersHeader = "";
    }
    else {
        headersHeader = headersHeader.toLowerCase(Locale.ROOT);
    }
    for (final Entry<String, String> header : webRequest_.getAdditionalHeaders().entrySet()) {
        final String key = header.getKey().toLowerCase(Locale.ROOT);
        if (isPreflightHeader(key, header.getValue())
                && !headersHeader.contains(key)) {
            return false;
        }
    }
    return true;
}
 
Example 2
Source File: Attachment.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the attachment's filename, as suggested by the <tt>Content-Disposition</tt>
 * header, or {@code null} if no filename was suggested.
 * @return the attachment's suggested filename, or {@code null} if none was suggested
 */
public String getSuggestedFilename() {
    final WebResponse response = page_.getWebResponse();
    final String disp = response.getResponseHeaderValue(HttpHeader.CONTENT_DISPOSITION);
    int start = disp.indexOf("filename=");
    if (start == -1) {
        return null;
    }
    start += "filename=".length();
    if (start >= disp.length()) {
        return null;
    }

    int end = disp.indexOf(';', start);
    if (end == -1) {
        end = disp.length();
    }
    if (disp.charAt(start) == '"' && disp.charAt(end - 1) == '"') {
        start++;
        end--;
    }
    return disp.substring(start, end);
}
 
Example 3
Source File: XMLHTTPRequest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
private boolean isPreflightAuthorized(final WebResponse preflightResponse) {
    final String originHeader = preflightResponse.getResponseHeaderValue(HttpHeader.ACCESS_CONTROL_ALLOW_ORIGIN);
    if (!ALLOW_ORIGIN_ALL.equals(originHeader)
            && !webRequest_.getAdditionalHeaders().get(HttpHeader.ORIGIN).equals(originHeader)) {
        return false;
    }
    String headersHeader = preflightResponse.getResponseHeaderValue(HttpHeader.ACCESS_CONTROL_ALLOW_HEADERS);
    if (headersHeader == null) {
        headersHeader = "";
    }
    else {
        headersHeader = headersHeader.toLowerCase(Locale.ROOT);
    }
    for (final Entry<String, String> header : webRequest_.getAdditionalHeaders().entrySet()) {
        final String key = header.getKey().toLowerCase(Locale.ROOT);
        if (isPreflightHeader(key, header.getValue())
                && !headersHeader.contains(key)) {
            return false;
        }
    }
    return true;
}
 
Example 4
Source File: XMLHttpRequest.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
private boolean isPreflightAuthorized(final WebResponse preflightResponse) {
    final String originHeader = preflightResponse.getResponseHeaderValue(HttpHeader.ACCESS_CONTROL_ALLOW_ORIGIN);
    if (!ALLOW_ORIGIN_ALL.equals(originHeader)
            && !webRequest_.getAdditionalHeaders().get(HttpHeader.ORIGIN).equals(originHeader)) {
        return false;
    }
    String headersHeader = preflightResponse.getResponseHeaderValue(HttpHeader.ACCESS_CONTROL_ALLOW_HEADERS);
    if (headersHeader == null) {
        headersHeader = "";
    }
    else {
        headersHeader = headersHeader.toLowerCase(Locale.ROOT);
    }
    for (final Entry<String, String> header : webRequest_.getAdditionalHeaders().entrySet()) {
        final String key = header.getKey().toLowerCase(Locale.ROOT);
        if (isPreflightHeader(key, header.getValue())
                && !headersHeader.contains(key)) {
            return false;
        }
    }
    return true;
}
 
Example 5
Source File: Attachment.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the attachment's filename, as suggested by the <tt>Content-Disposition</tt>
 * header, or {@code null} if no filename was suggested.
 * @return the attachment's suggested filename, or {@code null} if none was suggested
 */
public String getSuggestedFilename() {
    final WebResponse response = page_.getWebResponse();
    final String disp = response.getResponseHeaderValue("Content-Disposition");
    int start = disp.indexOf("filename=");
    if (start == -1) {
        return null;
    }
    start += "filename=".length();
    if (start >= disp.length()) {
        return null;
    }

    int end = disp.indexOf(';', start);
    if (end == -1) {
        end = disp.length();
    }
    if (disp.charAt(start) == '"' && disp.charAt(end - 1) == '"') {
        start++;
        end--;
    }
    return disp.substring(start, end);
}
 
Example 6
Source File: XMLHTTPRequest.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
private boolean isPreflightAuthorized(final WebResponse preflightResponse) {
    final String originHeader = preflightResponse.getResponseHeaderValue(HttpHeader.ACCESS_CONTROL_ALLOW_ORIGIN);
    if (!ALLOW_ORIGIN_ALL.equals(originHeader)
            && !webRequest_.getAdditionalHeaders().get(HttpHeader.ORIGIN).equals(originHeader)) {
        return false;
    }
    String headersHeader = preflightResponse.getResponseHeaderValue(HttpHeader.ACCESS_CONTROL_ALLOW_HEADERS);
    if (headersHeader == null) {
        headersHeader = "";
    }
    else {
        headersHeader = headersHeader.toLowerCase(Locale.ROOT);
    }
    for (final Entry<String, String> header : webRequest_.getAdditionalHeaders().entrySet()) {
        final String key = header.getKey().toLowerCase(Locale.ROOT);
        if (isPreflightHeader(key, header.getValue())
                && !headersHeader.contains(key)) {
            return false;
        }
    }
    return true;
}
 
Example 7
Source File: AttachmentHandler.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Returns {@code true} if the specified response represents an attachment.
 * @param response the response to check
 * @return {@code true} if the specified response represents an attachment, {@code false} otherwise
 * @see <a href="http://www.ietf.org/rfc/rfc2183.txt">RFC 2183</a>
 */
default boolean isAttachment(final WebResponse response) {
    final String disp = response.getResponseHeaderValue(HttpHeader.CONTENT_DISPOSITION);
    if (disp == null) {
        return false;
    }
    return disp.toLowerCase(Locale.ROOT).startsWith("attachment");
}
 
Example 8
Source File: HeaderUtils.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * @param response {@code WebResponse}
 * @return if 'Cache-Control' header is present and contains 'max-age' value
 */
public static boolean containsMaxAgeOrSMaxage(final WebResponse response) {
    final String cacheControl = response.getResponseHeaderValue(HttpHeader.CACHE_CONTROL);
    if (StringUtils.contains(cacheControl, CACHE_CONTROL_MAX_AGE)) {
        return true;
    }
    return StringUtils.contains(cacheControl, CACHE_CONTROL_S_MAXAGE);
}
 
Example 9
Source File: HeaderUtils.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
private static long directiveValue(final WebResponse response, final Pattern pattern) {
    final String value = response.getResponseHeaderValue(HttpHeader.CACHE_CONTROL);
    if (value != null) {
        final Matcher matcher = pattern.matcher(value);
        if (matcher.matches()) {
            return Long.parseLong(matcher.group(1));
        }
    }

    return 0;
}
 
Example 10
Source File: Attachment.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Returns {@code true} if the specified response represents an attachment.
 * @param response the response to check
 * @return {@code true} if the specified response represents an attachment, {@code false} otherwise
 * @see <a href="http://www.ietf.org/rfc/rfc2183.txt">RFC 2183</a>
 */
public static boolean isAttachment(final WebResponse response) {
    final String disp = response.getResponseHeaderValue("Content-Disposition");
    if (disp == null) {
        return false;
    }
    return disp.startsWith("attachment");
}
 
Example 11
Source File: HeaderUtils.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
private static long directiveValue(final WebResponse response, final Pattern pattern) {
    final String value = response.getResponseHeaderValue(HttpHeader.CACHE_CONTROL);
    if (value != null) {
        final Matcher matcher = pattern.matcher(value);
        if (matcher.matches()) {
            return Long.valueOf(matcher.group(1));
        }
    }

    return 0;
}
 
Example 12
Source File: DemoApplicationTests.java    From keycloak-springsecurity5-sample with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void requestAuthorizeGitHubClientWhenLinkClickedThenStatusRedirectForAuthorization() throws Exception {
	HtmlPage page = this.webClient.getPage("/");

	ClientRegistration clientRegistration = this.clientRegistrationRepository.findByRegistrationId("github");

	HtmlAnchor clientAnchorElement = this.getClientAnchorElement(page, clientRegistration);
	assertThat(clientAnchorElement).isNotNull();

	WebResponse response = this.followLinkDisableRedirects(clientAnchorElement);

	assertThat(response.getStatusCode()).isEqualTo(HttpStatus.MOVED_PERMANENTLY.value());

	String authorizeRedirectUri = response.getResponseHeaderValue("Location");
	assertThat(authorizeRedirectUri).isNotNull();

	UriComponents uriComponents = UriComponentsBuilder.fromUri(URI.create(authorizeRedirectUri)).build();

	String requestUri = uriComponents.getScheme() + "://" + uriComponents.getHost() + uriComponents.getPath();
	assertThat(requestUri).isEqualTo(clientRegistration.getProviderDetails().getAuthorizationUri());

	Map<String, String> params = uriComponents.getQueryParams().toSingleValueMap();

	assertThat(params.get(OAuth2ParameterNames.RESPONSE_TYPE)).isEqualTo(OAuth2AuthorizationResponseType.CODE.getValue());
	assertThat(params.get(OAuth2ParameterNames.CLIENT_ID)).isEqualTo(clientRegistration.getClientId());
	String redirectUri = AUTHORIZE_BASE_URL + "/" + clientRegistration.getRegistrationId();
	assertThat(URLDecoder.decode(params.get(OAuth2ParameterNames.REDIRECT_URI), "UTF-8")).isEqualTo(redirectUri);
	assertThat(URLDecoder.decode(params.get(OAuth2ParameterNames.SCOPE), "UTF-8"))
		.isEqualTo(clientRegistration.getScopes().stream().collect(Collectors.joining(" ")));
	assertThat(params.get(OAuth2ParameterNames.STATE)).isNotNull();
}
 
Example 13
Source File: ExtHtmlunitCache.java    From nutch-htmlunit with Apache License 2.0 5 votes vote down vote up
protected boolean isDynamicContent(final WebResponse response) {
    final String cacheControl = response.getResponseHeaderValue("Cache-Control");
    if (StringUtils.isNotBlank(cacheControl) && cacheControl.toLowerCase().indexOf("max-age") > -1) {
        return false;
    }
    
    return super.isDynamicContent(response);
}
 
Example 14
Source File: HeaderUtils.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
private static boolean containsCacheControlValue(final WebResponse response, final String value) {
    final String cacheControl = response.getResponseHeaderValue(HttpHeader.CACHE_CONTROL);
    return StringUtils.contains(cacheControl, value);
}
 
Example 15
Source File: HeaderUtils.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
private static boolean containsValue(final WebResponse response, final String value) {
    final String cacheControl = response.getResponseHeaderValue(HttpHeader.CACHE_CONTROL);
    return StringUtils.contains(cacheControl, value);
}
 
Example 16
Source File: HtmlUnitDownloder.java    From gecco-htmlunit with MIT License 4 votes vote down vote up
public HttpResponse download(HttpRequest request, int timeout) throws DownloadException {
	try {
		URL url = new URL(request.getUrl());
		WebRequest webRequest = new WebRequest(url);
		webRequest.setHttpMethod(HttpMethod.GET);
		if(request instanceof HttpPostRequest) {//post
			HttpPostRequest post = (HttpPostRequest)request;
			webRequest.setHttpMethod(HttpMethod.POST);
			List<NameValuePair> requestParameters = new ArrayList<NameValuePair>();
			for(Map.Entry<String, Object> entry : post.getFields().entrySet()) {
				NameValuePair nvp = new NameValuePair(entry.getKey(), entry.getValue().toString());
				requestParameters.add(nvp);
			}
			webRequest.setRequestParameters(requestParameters);	
		}
		//header
		boolean isMobile = SpiderThreadLocal.get().getEngine().isMobile();
		webRequest.setAdditionalHeader("User-Agent", UserAgent.getUserAgent(isMobile));
		webRequest.setAdditionalHeaders(request.getHeaders());
		//proxy
		HttpHost proxy = Proxys.getProxy();
		if(proxy != null) {
			webRequest.setProxyHost(proxy.getHostName());
			webRequest.setProxyPort(proxy.getPort());
		}
		//timeout
		this.webClient.getOptions().setTimeout(timeout);
		//request,response
		webClient.getPage(webRequest);
		HtmlPage page = webClient.getPage(request.getUrl());
		HttpResponse resp = new HttpResponse();
		WebResponse webResponse = page.getWebResponse();
		int status = webResponse.getStatusCode();
		resp.setStatus(status);
		if(status == 302 || status == 301) {
			String redirectUrl = webResponse.getResponseHeaderValue("Location");
			resp.setContent(UrlUtils.relative2Absolute(request.getUrl(), redirectUrl));
		} else if(status == 200) {
			String content = page.asXml();
			resp.setContent(content);
			resp.setRaw(webResponse.getContentAsStream());
			String contentType = webResponse.getContentType();
			resp.setContentType(contentType);
			String charset = getCharset(request.getCharset(), contentType);
			resp.setCharset(charset);
		} else {
			throw new DownloadException("ERROR : " + status);
		}
		return resp;
	} catch(Exception ex) {
		throw new DownloadException(ex);
	}
}