Java Code Examples for org.springframework.http.HttpHeaders#forEach()
The following examples show how to use
org.springframework.http.HttpHeaders#forEach() .
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: OkHttp3ClientHttpRequestFactory.java From java-technology-stack with MIT License | 6 votes |
static Request buildRequest(HttpHeaders headers, byte[] content, URI uri, HttpMethod method) throws MalformedURLException { okhttp3.MediaType contentType = getContentType(headers); RequestBody body = (content.length > 0 || okhttp3.internal.http.HttpMethod.requiresRequestBody(method.name()) ? RequestBody.create(contentType, content) : null); Request.Builder builder = new Request.Builder().url(uri.toURL()).method(method.name(), body); headers.forEach((headerName, headerValues) -> { for (String headerValue : headerValues) { builder.addHeader(headerName, headerValue); } }); return builder.build(); }
Example 2
Source File: Netty4ClientHttpRequest.java From java-technology-stack with MIT License | 6 votes |
private FullHttpRequest createFullHttpRequest(HttpHeaders headers) { io.netty.handler.codec.http.HttpMethod nettyMethod = io.netty.handler.codec.http.HttpMethod.valueOf(this.method.name()); String authority = this.uri.getRawAuthority(); String path = this.uri.toString().substring(this.uri.toString().indexOf(authority) + authority.length()); FullHttpRequest nettyRequest = new DefaultFullHttpRequest( HttpVersion.HTTP_1_1, nettyMethod, path, this.body.buffer()); nettyRequest.headers().set(HttpHeaders.HOST, this.uri.getHost() + ":" + getPort(this.uri)); nettyRequest.headers().set(HttpHeaders.CONNECTION, "close"); headers.forEach((headerName, headerValues) -> nettyRequest.headers().add(headerName, headerValues)); if (!nettyRequest.headers().contains(HttpHeaders.CONTENT_LENGTH) && this.body.buffer().readableBytes() > 0) { nettyRequest.headers().set(HttpHeaders.CONTENT_LENGTH, this.body.buffer().readableBytes()); } return nettyRequest; }
Example 3
Source File: Netty4ClientHttpRequest.java From spring-analysis-note with MIT License | 6 votes |
private FullHttpRequest createFullHttpRequest(HttpHeaders headers) { io.netty.handler.codec.http.HttpMethod nettyMethod = io.netty.handler.codec.http.HttpMethod.valueOf(this.method.name()); String authority = this.uri.getRawAuthority(); String path = this.uri.toString().substring(this.uri.toString().indexOf(authority) + authority.length()); FullHttpRequest nettyRequest = new DefaultFullHttpRequest( HttpVersion.HTTP_1_1, nettyMethod, path, this.body.buffer()); nettyRequest.headers().set(HttpHeaders.HOST, this.uri.getHost() + ":" + getPort(this.uri)); nettyRequest.headers().set(HttpHeaders.CONNECTION, "close"); headers.forEach((headerName, headerValues) -> nettyRequest.headers().add(headerName, headerValues)); if (!nettyRequest.headers().contains(HttpHeaders.CONTENT_LENGTH) && this.body.buffer().readableBytes() > 0) { nettyRequest.headers().set(HttpHeaders.CONTENT_LENGTH, this.body.buffer().readableBytes()); } return nettyRequest; }
Example 4
Source File: OkHttp3ClientHttpRequestFactory.java From spring-analysis-note with MIT License | 6 votes |
static Request buildRequest(HttpHeaders headers, byte[] content, URI uri, HttpMethod method) throws MalformedURLException { okhttp3.MediaType contentType = getContentType(headers); RequestBody body = (content.length > 0 || okhttp3.internal.http.HttpMethod.requiresRequestBody(method.name()) ? RequestBody.create(contentType, content) : null); Request.Builder builder = new Request.Builder().url(uri.toURL()).method(method.name(), body); headers.forEach((headerName, headerValues) -> { for (String headerValue : headerValues) { builder.addHeader(headerName, headerValue); } }); return builder.build(); }
Example 5
Source File: JettyClientHttpRequest.java From java-technology-stack with MIT License | 5 votes |
@Override protected void applyHeaders() { HttpHeaders headers = getHeaders(); headers.forEach((key, value) -> value.forEach(v -> this.jettyRequest.header(key, v))); if (!headers.containsKey(HttpHeaders.ACCEPT)) { this.jettyRequest.header(HttpHeaders.ACCEPT, "*/*"); } }
Example 6
Source File: BladeFeignRequestHeaderInterceptor.java From blade-tool with GNU Lesser General Public License v3.0 | 5 votes |
@Override public void apply(RequestTemplate requestTemplate) { HttpHeaders headers = BladeHttpHeadersContextHolder.get(); if (headers != null && !headers.isEmpty()) { headers.forEach((key, values) -> { values.forEach(value -> requestTemplate.header(key, value)); }); } }
Example 7
Source File: JettyXhrTransport.java From java-technology-stack with MIT License | 5 votes |
private static void addHttpHeaders(Request request, HttpHeaders headers) { headers.forEach((key, values) -> { for (String value : values) { request.header(key, value); } }); }
Example 8
Source File: AbstractTyrusRequestUpgradeStrategy.java From java-technology-stack with MIT License | 5 votes |
private RequestContext createRequestContext(HttpServletRequest request, String endpointPath, HttpHeaders headers) { RequestContext context = RequestContext.Builder.create() .requestURI(URI.create(endpointPath)) .userPrincipal(request.getUserPrincipal()) .secure(request.isSecure()) .remoteAddr(request.getRemoteAddr()) .build(); headers.forEach((header, value) -> context.getHeaders().put(header, value)); return context; }
Example 9
Source File: JettyWebSocketClient.java From java-technology-stack with MIT License | 5 votes |
@Override public ListenableFuture<WebSocketSession> doHandshakeInternal(WebSocketHandler wsHandler, HttpHeaders headers, final URI uri, List<String> protocols, List<WebSocketExtension> extensions, Map<String, Object> attributes) { final ClientUpgradeRequest request = new ClientUpgradeRequest(); request.setSubProtocols(protocols); for (WebSocketExtension e : extensions) { request.addExtensions(new WebSocketToJettyExtensionConfigAdapter(e)); } headers.forEach(request::setHeader); Principal user = getUser(); final JettyWebSocketSession wsSession = new JettyWebSocketSession(attributes, user); final JettyWebSocketHandlerAdapter listener = new JettyWebSocketHandlerAdapter(wsHandler, wsSession); Callable<WebSocketSession> connectTask = () -> { Future<Session> future = this.client.connect(listener, uri, request); future.get(); return wsSession; }; if (this.taskExecutor != null) { return this.taskExecutor.submitListenable(connectTask); } else { ListenableFutureTask<WebSocketSession> task = new ListenableFutureTask<>(connectTask); task.run(); return task; } }
Example 10
Source File: ResourceHttpRequestHandler.java From java-technology-stack with MIT License | 5 votes |
/** * Set headers on the given servlet response. * Called for GET requests as well as HEAD requests. * @param response current servlet response * @param resource the identified resource (never {@code null}) * @param mediaType the resource's media type (never {@code null}) * @throws IOException in case of errors while setting the headers */ protected void setHeaders(HttpServletResponse response, Resource resource, @Nullable MediaType mediaType) throws IOException { long length = resource.contentLength(); if (length > Integer.MAX_VALUE) { response.setContentLengthLong(length); } else { response.setContentLength((int) length); } if (mediaType != null) { response.setContentType(mediaType.toString()); } if (resource instanceof HttpResource) { HttpHeaders resourceHeaders = ((HttpResource) resource).getResponseHeaders(); resourceHeaders.forEach((headerName, headerValues) -> { boolean first = true; for (String headerValue : headerValues) { if (first) { response.setHeader(headerName, headerValue); } else { response.addHeader(headerName, headerValue); } first = false; } }); } response.setHeader(HttpHeaders.ACCEPT_RANGES, "bytes"); }
Example 11
Source File: VertxServerHttpResponse.java From vertx-spring-boot with Apache License 2.0 | 5 votes |
@Override protected void applyHeaders() { HttpHeaders headers = getHeaders(); if (!headers.containsKey(HttpHeaders.CONTENT_LENGTH)) { logger.debug("Setting chunked response"); delegate.setChunked(true); } headers.forEach(delegate::putHeader); }
Example 12
Source File: AbstractTyrusRequestUpgradeStrategy.java From spring-analysis-note with MIT License | 5 votes |
private RequestContext createRequestContext(HttpServletRequest request, String endpointPath, HttpHeaders headers) { RequestContext context = RequestContext.Builder.create() .requestURI(URI.create(endpointPath)) .userPrincipal(request.getUserPrincipal()) .secure(request.isSecure()) .remoteAddr(request.getRemoteAddr()) .build(); headers.forEach((header, value) -> context.getHeaders().put(header, value)); return context; }
Example 13
Source File: JettyWebSocketClient.java From spring-analysis-note with MIT License | 5 votes |
@Override public ListenableFuture<WebSocketSession> doHandshakeInternal(WebSocketHandler wsHandler, HttpHeaders headers, final URI uri, List<String> protocols, List<WebSocketExtension> extensions, Map<String, Object> attributes) { final ClientUpgradeRequest request = new ClientUpgradeRequest(); request.setSubProtocols(protocols); for (WebSocketExtension e : extensions) { request.addExtensions(new WebSocketToJettyExtensionConfigAdapter(e)); } headers.forEach(request::setHeader); Principal user = getUser(); final JettyWebSocketSession wsSession = new JettyWebSocketSession(attributes, user); final JettyWebSocketHandlerAdapter listener = new JettyWebSocketHandlerAdapter(wsHandler, wsSession); Callable<WebSocketSession> connectTask = () -> { Future<Session> future = this.client.connect(listener, uri, request); future.get(); return wsSession; }; if (this.taskExecutor != null) { return this.taskExecutor.submitListenable(connectTask); } else { ListenableFutureTask<WebSocketSession> task = new ListenableFutureTask<>(connectTask); task.run(); return task; } }
Example 14
Source File: DefaultServerRequestTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void header() { HttpHeaders httpHeaders = new HttpHeaders(); List<MediaType> accept = Collections.singletonList(MediaType.APPLICATION_JSON); httpHeaders.setAccept(accept); List<Charset> acceptCharset = Collections.singletonList(UTF_8); httpHeaders.setAcceptCharset(acceptCharset); long contentLength = 42L; httpHeaders.setContentLength(contentLength); MediaType contentType = MediaType.TEXT_PLAIN; httpHeaders.setContentType(contentType); InetSocketAddress host = InetSocketAddress.createUnresolved("localhost", 80); httpHeaders.setHost(host); List<HttpRange> range = Collections.singletonList(HttpRange.createByteRange(0, 42)); httpHeaders.setRange(range); MockHttpServletRequest servletRequest = new MockHttpServletRequest("GET", "/"); httpHeaders.forEach(servletRequest::addHeader); servletRequest.setContentType(MediaType.TEXT_PLAIN_VALUE); DefaultServerRequest request = new DefaultServerRequest(servletRequest, this.messageConverters); ServerRequest.Headers headers = request.headers(); assertEquals(accept, headers.accept()); assertEquals(acceptCharset, headers.acceptCharset()); assertEquals(OptionalLong.of(contentLength), headers.contentLength()); assertEquals(Optional.of(contentType), headers.contentType()); assertEquals(httpHeaders, headers.asHttpHeaders()); }
Example 15
Source File: Headers.java From riptide with MIT License | 5 votes |
static void writeHeaders(final HttpHeaders headers, final HttpUriRequest request) { headers.forEach((name, values) -> values.forEach(value -> request.addHeader(name, value))); request.removeHeaders(CONTENT_LENGTH); request.removeHeaders(TRANSFER_ENCODING); }
Example 16
Source File: ResourceHttpRequestHandler.java From spring-analysis-note with MIT License | 5 votes |
/** * Set headers on the given servlet response. * Called for GET requests as well as HEAD requests. * @param response current servlet response * @param resource the identified resource (never {@code null}) * @param mediaType the resource's media type (never {@code null}) * @throws IOException in case of errors while setting the headers */ protected void setHeaders(HttpServletResponse response, Resource resource, @Nullable MediaType mediaType) throws IOException { long length = resource.contentLength(); if (length > Integer.MAX_VALUE) { response.setContentLengthLong(length); } else { response.setContentLength((int) length); } if (mediaType != null) { response.setContentType(mediaType.toString()); } if (resource instanceof HttpResource) { HttpHeaders resourceHeaders = ((HttpResource) resource).getResponseHeaders(); resourceHeaders.forEach((headerName, headerValues) -> { boolean first = true; for (String headerValue : headerValues) { if (first) { response.setHeader(headerName, headerValue); } else { response.addHeader(headerName, headerValue); } first = false; } }); } response.setHeader(HttpHeaders.ACCEPT_RANGES, "bytes"); }
Example 17
Source File: MockHttpServletRequestBuilder.java From spring-analysis-note with MIT License | 4 votes |
/** * Add all headers to the request. Values are always added. * @param httpHeaders the headers and values to add */ public MockHttpServletRequestBuilder headers(HttpHeaders httpHeaders) { httpHeaders.forEach(this.headers::addAll); return this; }
Example 18
Source File: VertxWebSocketClient.java From vertx-spring-boot with Apache License 2.0 | 4 votes |
private VertxHttpHeaders convertHeaders(HttpHeaders headers) { VertxHttpHeaders vertxHeaders = new VertxHttpHeaders(); headers.forEach(vertxHeaders::add); return vertxHeaders; }
Example 19
Source File: HttpEntityMethodProcessor.java From java-technology-stack with MIT License | 4 votes |
@Override public void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception { mavContainer.setRequestHandled(true); if (returnValue == null) { return; } ServletServerHttpRequest inputMessage = createInputMessage(webRequest); ServletServerHttpResponse outputMessage = createOutputMessage(webRequest); Assert.isInstanceOf(HttpEntity.class, returnValue); HttpEntity<?> responseEntity = (HttpEntity<?>) returnValue; HttpHeaders outputHeaders = outputMessage.getHeaders(); HttpHeaders entityHeaders = responseEntity.getHeaders(); if (!entityHeaders.isEmpty()) { entityHeaders.forEach((key, value) -> { if (HttpHeaders.VARY.equals(key) && outputHeaders.containsKey(HttpHeaders.VARY)) { List<String> values = getVaryRequestHeadersToAdd(outputHeaders, entityHeaders); if (!values.isEmpty()) { outputHeaders.setVary(values); } } else { outputHeaders.put(key, value); } }); } if (responseEntity instanceof ResponseEntity) { int returnStatus = ((ResponseEntity<?>) responseEntity).getStatusCodeValue(); outputMessage.getServletResponse().setStatus(returnStatus); if (returnStatus == 200) { if (SAFE_METHODS.contains(inputMessage.getMethod()) && isResourceNotModified(inputMessage, outputMessage)) { // Ensure headers are flushed, no body should be written. outputMessage.flush(); // Skip call to converters, as they may update the body. return; } } else if (returnStatus / 100 == 3) { String location = outputHeaders.getFirst("location"); if (location != null) { saveFlashAttributes(mavContainer, webRequest, location); } } } // Try even with null body. ResponseBodyAdvice could get involved. writeWithMessageConverters(responseEntity.getBody(), returnType, inputMessage, outputMessage); // Ensure headers are flushed even if no body was written. outputMessage.flush(); }
Example 20
Source File: MockHttpServletRequestBuilder.java From java-technology-stack with MIT License | 4 votes |
/** * Add all headers to the request. Values are always added. * @param httpHeaders the headers and values to add */ public MockHttpServletRequestBuilder headers(HttpHeaders httpHeaders) { httpHeaders.forEach(this.headers::addAll); return this; }