/** * 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; }
private void setNettyHeaders(HttpHeaders httpHeaders, io.netty.handler.codec.http.HttpHeaders nettyHeaders) { httpHeaders.forEach(nettyHeaders::set); }
@Override public void onHandshakeRequest(UpgradeRequest request) { this.headers.forEach(request::setHeader); }
private static void addHttpHeaders(Request request, HttpHeaders headers) { headers.forEach((key, values) -> { for (String value : values) { request.header(key, value); } }); }
@Override public void beforeRequest(Map<String, List<String>> headers) { this.requestHeaders.forEach(headers::put); if (this.delegate != null) { this.delegate.beforeRequest(headers); } }
/** * Add the given headers to the given HTTP connection. * @param connection the connection to add the headers to * @param headers the headers to add */ static void addHeaders(HttpURLConnection connection, HttpHeaders headers) { headers.forEach((headerName, headerValues) -> { if (HttpHeaders.COOKIE.equalsIgnoreCase(headerName)) { // RFC 6265 String headerValue = StringUtils.collectionToDelimitedString(headerValues, "; "); connection.setRequestProperty(headerName, headerValue); } else { for (String headerValue : headerValues) { String actualHeaderValue = headerValue != null ? headerValue : ""; connection.addRequestProperty(headerName, actualHeaderValue); } } }); }
@Override protected void applyHeaders() { getHeaders().forEach((key, value) -> this.request.requestHeaders().set(key, value)); }
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(); }
@Override protected void applyHeaders() { getHeaders().forEach((headerName, headerValues) -> { for (String headerValue : headerValues) { this.response.addHeader(headerName, headerValue); } }); MediaType contentType = getHeaders().getContentType(); if (this.response.getContentType() == null && contentType != null) { this.response.setContentType(contentType.toString()); } Charset charset = (contentType != null ? contentType.getCharset() : null); if (this.response.getCharacterEncoding() == null && charset != null) { this.response.setCharacterEncoding(charset.name()); } }
/** * Add the given headers to the given HTTP request. * @param httpRequest the request to add the headers to * @param headers the headers to add */ static void addHeaders(HttpUriRequest httpRequest, HttpHeaders headers) { headers.forEach((headerName, headerValues) -> { if (HttpHeaders.COOKIE.equalsIgnoreCase(headerName)) { // RFC 6265 String headerValue = StringUtils.collectionToDelimitedString(headerValues, "; "); httpRequest.addHeader(headerName, headerValue); } else if (!HTTP.CONTENT_LEN.equalsIgnoreCase(headerName) && !HTTP.TRANSFER_ENCODING.equalsIgnoreCase(headerName)) { for (String headerValue : headerValues) { httpRequest.addHeader(headerName, headerValue); } } }); }
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; }
private static void addHttpHeaders(ClientRequest request, HttpHeaders headers) { HeaderMap headerMap = request.getRequestHeaders(); headers.forEach((key, values) -> { for (String value : values) { headerMap.add(HttpString.tryFromString(key), value); } }); }
@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, "*/*"); } }
resourceHeaders.forEach((headerName, headerValues) -> { boolean first = true; for (String headerValue : headerValues) {
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(); }
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; }
private void writeHeaders() { if (!this.headersWritten) { getHeaders().forEach((headerName, headerValues) -> { for (String headerValue : headerValues) { this.servletResponse.addHeader(headerName, headerValue); } }); // HttpServletResponse exposes some headers as properties: we should include those if not already present if (this.servletResponse.getContentType() == null && this.headers.getContentType() != null) { this.servletResponse.setContentType(this.headers.getContentType().toString()); } if (this.servletResponse.getCharacterEncoding() == null && this.headers.getContentType() != null && this.headers.getContentType().getCharset() != null) { this.servletResponse.setCharacterEncoding(this.headers.getContentType().getCharset().name()); } this.headersWritten = true; } }
@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, "*/*"); } }
@Override public ClientHttpResponse execute(HttpRequest request, byte[] body) throws IOException { if (this.iterator.hasNext()) { ClientHttpRequestInterceptor nextInterceptor = this.iterator.next(); return nextInterceptor.intercept(request, body, this); } else { HttpMethod method = request.getMethod(); Assert.state(method != null, "No standard HTTP method"); ClientHttpRequest delegate = requestFactory.createRequest(request.getURI(), method); request.getHeaders().forEach((key, value) -> delegate.getHeaders().addAll(key, value)); if (body.length > 0) { if (delegate instanceof StreamingHttpOutputMessage) { StreamingHttpOutputMessage streamingOutputMessage = (StreamingHttpOutputMessage) delegate; streamingOutputMessage.setBody(outputStream -> StreamUtils.copy(body, outputStream)); } else { StreamUtils.copy(body, delegate.getBody()); } } return delegate.execute(); } } }
@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; } }