@Override public List<String> remove(Object key) { return this.headers.remove(key); }
/** * Set the given header value, or remove the header if {@code null}. * @param headerName the header name * @param headerValue the header value, or {@code null} for none */ private void setOrRemove(String headerName, @Nullable String headerValue) { if (headerValue != null) { set(headerName, headerValue); } else { remove(headerName); } }
/** * Set the (new) entity tag of the body, as specified by the {@code ETag} header. */ public void setETag(@Nullable String etag) { if (etag != null) { Assert.isTrue(etag.startsWith("\"") || etag.startsWith("W/"), "Invalid ETag: does not start with W/ or \""); Assert.isTrue(etag.endsWith("\""), "Invalid ETag: does not end with \""); set(ETAG, etag); } else { remove(ETAG); } }
/** * Set the (new) value of the {@code Host} header. * <p>If the given {@linkplain InetSocketAddress#getPort() port} is {@code 0}, * the host header will only contain the * {@linkplain InetSocketAddress#getHostString() host name}. * @since 5.0 */ public void setHost(@Nullable InetSocketAddress host) { if (host != null) { String value = host.getHostString(); int port = host.getPort(); if (port != 0) { value = value + ":" + port; } set(HOST, value); } else { remove(HOST, null); } }
/** * Set the given header value, or remove the header if {@code null}. * @param headerName the header name * @param headerValue the header value, or {@code null} for none */ private void setOrRemove(String headerName, @Nullable String headerValue) { if (headerValue != null) { set(headerName, headerValue); } else { remove(headerName); } }
/** * Set the (new) entity tag of the body, as specified by the {@code ETag} header. */ public void setETag(@Nullable String etag) { if (etag != null) { Assert.isTrue(etag.startsWith("\"") || etag.startsWith("W/"), "Invalid ETag: does not start with W/ or \""); Assert.isTrue(etag.endsWith("\""), "Invalid ETag: does not end with \""); set(ETAG, etag); } else { remove(ETAG); } }
/** * Set the {@linkplain MediaType media type} of the body, * as specified by the {@code Content-Type} header. */ public void setContentType(@Nullable MediaType mediaType) { if (mediaType != null) { Assert.isTrue(!mediaType.isWildcardType(), "Content-Type cannot contain wildcard type '*'"); Assert.isTrue(!mediaType.isWildcardSubtype(), "Content-Type cannot contain wildcard subtype '*'"); set(CONTENT_TYPE, mediaType.toString()); } else { remove(CONTENT_TYPE); } }
/** * Set the (new) value of the {@code Host} header. * <p>If the given {@linkplain InetSocketAddress#getPort() port} is {@code 0}, * the host header will only contain the * {@linkplain InetSocketAddress#getHostString() host name}. * @since 5.0 */ public void setHost(@Nullable InetSocketAddress host) { if (host != null) { String value = host.getHostString(); int port = host.getPort(); if (port != 0) { value = value + ":" + port; } set(HOST, value); } else { remove(HOST, null); } }
private void removeContentLength() { if (!this.isCommitted()) { this.getHeaders().remove(HttpHeaders.CONTENT_LENGTH); } }
private void expectHeaderNamesNotPresent(String... headerNames) { for (String headerName : headerNames) { this.expectedHeaders.remove(headerName); this.headerNamesNotPresent.add(headerName); } }
/** * Set the {@linkplain MediaType media type} of the body, * as specified by the {@code Content-Type} header. */ public void setContentType(@Nullable MediaType mediaType) { if (mediaType != null) { Assert.isTrue(!mediaType.isWildcardType(), "Content-Type cannot contain wildcard type '*'"); Assert.isTrue(!mediaType.isWildcardSubtype(), "Content-Type cannot contain wildcard subtype '*'"); set(CONTENT_TYPE, mediaType.toString()); } else { remove(CONTENT_TYPE); } }
private void removeContentLength() { if (!this.isCommitted()) { this.getHeaders().remove(HttpHeaders.CONTENT_LENGTH); } }
private boolean isResourceNotModified(ServletServerHttpRequest request, ServletServerHttpResponse response) { ServletWebRequest servletWebRequest = new ServletWebRequest(request.getServletRequest(), response.getServletResponse()); HttpHeaders responseHeaders = response.getHeaders(); String etag = responseHeaders.getETag(); long lastModifiedTimestamp = responseHeaders.getLastModified(); if (request.getMethod() == HttpMethod.GET || request.getMethod() == HttpMethod.HEAD) { responseHeaders.remove(HttpHeaders.ETAG); responseHeaders.remove(HttpHeaders.LAST_MODIFIED); } return servletWebRequest.checkNotModified(etag, lastModifiedTimestamp); }
public void reset(ServerWebExchange exchange) { //TODO: what else to do to reset SWE? Set<String> addedHeaders = exchange.getAttributeOrDefault(CLIENT_RESPONSE_HEADER_NAMES, Collections.emptySet()); addedHeaders.forEach(header -> exchange.getResponse().getHeaders().remove(header)); exchange.getAttributes().remove(GATEWAY_ALREADY_ROUTED_ATTR); }
private static Function<ClientResponse, Mono<ClientResponse>> convertClientResponse(Function<Flux<DataBuffer>, Flux<DataBuffer>> bodConverter, MediaType contentType) { return response -> { ClientResponse convertedResponse = ClientResponse.from(response).headers(headers -> { headers.replace(HttpHeaders.CONTENT_TYPE, singletonList(contentType.toString())); headers.remove(HttpHeaders.CONTENT_LENGTH); }).body(response.bodyToFlux(DataBuffer.class).transform(bodConverter)).build(); return Mono.just(convertedResponse); }; }
private boolean isResourceNotModified(ServletServerHttpRequest request, ServletServerHttpResponse response) { ServletWebRequest servletWebRequest = new ServletWebRequest(request.getServletRequest(), response.getServletResponse()); HttpHeaders responseHeaders = response.getHeaders(); String etag = responseHeaders.getETag(); long lastModifiedTimestamp = responseHeaders.getLastModified(); if (request.getMethod() == HttpMethod.GET || request.getMethod() == HttpMethod.HEAD) { responseHeaders.remove(HttpHeaders.ETAG); responseHeaders.remove(HttpHeaders.LAST_MODIFIED); } return servletWebRequest.checkNotModified(etag, lastModifiedTimestamp); }
@Override public GatewayFilter apply(NameConfig config) { return (exchange, chain) -> chain.filter(exchange).then(Mono.fromRunnable(() -> { exchange.getResponse().getHeaders().remove(config.getName()); })); } }
@Test public void headersWhenHstsCustomThenCustomHstsWritten() { this.expectedHeaders.remove(StrictTransportSecurityServerHttpHeadersWriter.STRICT_TRANSPORT_SECURITY); this.expectedHeaders.add(StrictTransportSecurityServerHttpHeadersWriter.STRICT_TRANSPORT_SECURITY, "max-age=60"); this.headers.hsts() .maxAge(Duration.ofSeconds(60)) .includeSubdomains(false); assertHeaders(); }
@Test public void headersWhenHstsCustomWithPreloadThenCustomHstsWritten() { this.expectedHeaders.remove(StrictTransportSecurityServerHttpHeadersWriter.STRICT_TRANSPORT_SECURITY); this.expectedHeaders.add(StrictTransportSecurityServerHttpHeadersWriter.STRICT_TRANSPORT_SECURITY, "max-age=60 ; includeSubDomains ; preload"); this.headers.hsts() .maxAge(Duration.ofSeconds(60)) .preload(true); assertHeaders(); }
@Override public GatewayFilter apply(NameConfig config) { return (exchange, chain) -> { ServerHttpRequest request = exchange.getRequest().mutate() .headers(httpHeaders -> httpHeaders.remove(config.getName())) .build(); return chain.filter(exchange.mutate().request(request).build()); }; } }