Java Code Examples for org.springframework.http.HttpHeaders#put()

The following examples show how to use org.springframework.http.HttpHeaders#put() . 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: StandardMultipartHttpServletRequest.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public HttpHeaders getMultipartHeaders(String paramOrFileName) {
	try {
		Part part = getPart(paramOrFileName);
		if (part != null) {
			HttpHeaders headers = new HttpHeaders();
			for (String headerName : part.getHeaderNames()) {
				headers.put(headerName, new ArrayList<String>(part.getHeaders(headerName)));
			}
			return headers;
		}
		else {
			return null;
		}
	}
	catch (Throwable ex) {
		throw new MultipartException("Could not access multipart servlet request", ex);
	}
}
 
Example 2
Source File: SockJsClient.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Nullable
private HttpHeaders getHttpRequestHeaders(@Nullable HttpHeaders webSocketHttpHeaders) {
	if (getHttpHeaderNames() == null || webSocketHttpHeaders == null) {
		return webSocketHttpHeaders;
	}
	else {
		HttpHeaders httpHeaders = new HttpHeaders();
		for (String name : getHttpHeaderNames()) {
			List<String> values = webSocketHttpHeaders.get(name);
			if (values != null) {
				httpHeaders.put(name, values);
			}
		}
		return httpHeaders;
	}
}
 
Example 3
Source File: HttpHeadersUtils.java    From raptor with Apache License 2.0 5 votes vote down vote up
public static HttpHeaders getHttpHeaders(Map<String, Collection<String>> headers) {
    HttpHeaders httpHeaders = new HttpHeaders();
    for (Map.Entry<String, Collection<String>> entry : headers.entrySet()) {
        httpHeaders.put(entry.getKey(), new ArrayList<>(entry.getValue()));
    }
    return httpHeaders;
}
 
Example 4
Source File: MockMultipartHttpServletRequest.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public HttpHeaders getRequestHeaders() {
	HttpHeaders headers = new HttpHeaders();
	Enumeration<String> headerNames = getHeaderNames();
	while (headerNames.hasMoreElements()) {
		String headerName = headerNames.nextElement();
		headers.put(headerName, Collections.list(getHeaders(headerName)));
	}
	return headers;
}
 
Example 5
Source File: HttpRequestWrapperWithModifiableHeaders.java    From wingtips with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new {@code HttpRequest} wrapping the given request object.
 *
 * @param request The request object to be wrapped
 */
public HttpRequestWrapperWithModifiableHeaders(HttpRequest request) {
    super(request);

    modifiableHeaders = new HttpHeaders();
    for (Map.Entry<String, List<String>> origHeaderEntry : request.getHeaders().entrySet()) {
        modifiableHeaders.put(origHeaderEntry.getKey(), new ArrayList<>(origHeaderEntry.getValue()));
    }
}
 
Example 6
Source File: PrintingResultHandler.java    From java-technology-stack with MIT License 5 votes vote down vote up
protected final HttpHeaders getResponseHeaders(MockHttpServletResponse response) {
	HttpHeaders headers = new HttpHeaders();
	for (String name : response.getHeaderNames()) {
		headers.put(name, response.getHeaders(name));
	}
	return headers;
}
 
Example 7
Source File: HttpHeadersClientHeaderAdaptor.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
@Override
public void setHeader(final HttpHeaders header, final String name, final String value) {
    if (header != null) {
        header.put(name, Arrays.asList(value));
        if (isDebug) {
            logger.debug("Set header {}={}", name, value);
        }
    }
}
 
Example 8
Source File: MockMultipartHttpServletRequest.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public HttpHeaders getRequestHeaders() {
	HttpHeaders headers = new HttpHeaders();
	Enumeration<String> headerNames = getHeaderNames();
	while (headerNames.hasMoreElements()) {
		String headerName = headerNames.nextElement();
		headers.put(headerName, Collections.list(getHeaders(headerName)));
	}
	return headers;
}
 
Example 9
Source File: AbstractWebSocketClient.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public final ListenableFuture<WebSocketSession> doHandshake(WebSocketHandler webSocketHandler,
		WebSocketHttpHeaders headers, URI uri) {

	Assert.notNull(webSocketHandler, "webSocketHandler must not be null");
	assertUri(uri);

	if (logger.isDebugEnabled()) {
		logger.debug("Connecting to " + uri);
	}

	HttpHeaders headersToUse = new HttpHeaders();
	if (headers != null) {
		for (String header : headers.keySet()) {
			if (!specialHeaders.contains(header.toLowerCase())) {
				headersToUse.put(header, headers.get(header));
			}
		}
	}

	List<String> subProtocols = ((headers != null) && (headers.getSecWebSocketProtocol() != null)) ?
			headers.getSecWebSocketProtocol() : Collections.<String>emptyList();

	List<WebSocketExtension> extensions = ((headers != null) && (headers.getSecWebSocketExtensions() != null)) ?
			headers.getSecWebSocketExtensions() : Collections.<WebSocketExtension>emptyList();

	return doHandshakeInternal(webSocketHandler, headersToUse, uri, subProtocols, extensions,
			Collections.<String, Object>emptyMap());
}
 
Example 10
Source File: AbstractMultipartHttpServletRequest.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public HttpHeaders getRequestHeaders() {
	HttpHeaders headers = new HttpHeaders();
	Enumeration<String> headerNames = getHeaderNames();
	while (headerNames.hasMoreElements()) {
		String headerName = headerNames.nextElement();
		headers.put(headerName, Collections.list(getHeaders(headerName)));
	}
	return headers;
}
 
Example 11
Source File: Fix302GlobalFilter.java    From WeEvent with Apache License 2.0 5 votes vote down vote up
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
    URI requestUri = exchange.getRequest().getURI();
    ServerHttpResponseDecorator decoratedResponse = new ServerHttpResponseDecorator(exchange.getResponse()) {
        @Override
        public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
            switch (getStatusCode()) {
                case MOVED_PERMANENTLY: //301
                case FOUND: //302
                    HttpHeaders headers = getHeaders();
                    String location = headers.getFirst(HttpHeaders.LOCATION);
                    if (!StringUtils.isEmpty(location)) {
                        UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(location);
                        // replace scheme//host:port with original request's
                        uriComponentsBuilder.scheme(requestUri.getScheme())
                                .host(requestUri.getHost())
                                .port(requestUri.getPort());
                        String newLocation = uriComponentsBuilder.build().toUri().toString();
                        headers.put(HttpHeaders.LOCATION, Collections.singletonList(newLocation));

                        log.info("301/302 redirect in R: {}, FIX location {} -> {}", requestUri, location, newLocation);
                    }
                    break;

                default:
                    break;
            }

            return super.writeWith(body);
        }
    };

    // replace response with decorator
    return chain.filter(exchange.mutate().response(decoratedResponse).build());
}
 
Example 12
Source File: SockJsClient.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private HttpHeaders getHttpRequestHeaders(HttpHeaders webSocketHttpHeaders) {
	if (getHttpHeaderNames() == null) {
		return webSocketHttpHeaders;
	}
	else {
		HttpHeaders httpHeaders = new HttpHeaders();
		for (String name : getHttpHeaderNames()) {
			if (webSocketHttpHeaders.containsKey(name)) {
				httpHeaders.put(name, webSocketHttpHeaders.get(name));
			}
		}
		return httpHeaders;
	}
}
 
Example 13
Source File: ResultErrorDecoder.java    From onetwo with Apache License 2.0 5 votes vote down vote up
static HttpHeaders getHttpHeaders(Map<String, Collection<String>> headers) {
	HttpHeaders httpHeaders = new HttpHeaders();
	for (Map.Entry<String, Collection<String>> entry : headers.entrySet()) {
		httpHeaders.put(entry.getKey(), new ArrayList<>(entry.getValue()));
	}
	return httpHeaders;
}
 
Example 14
Source File: MockMultipartHttpServletRequest.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public HttpHeaders getRequestHeaders() {
	HttpHeaders headers = new HttpHeaders();
	Enumeration<String> headerNames = getHeaderNames();
	while (headerNames.hasMoreElements()) {
		String headerName = headerNames.nextElement();
		headers.put(headerName, Collections.list(getHeaders(headerName)));
	}
	return headers;
}
 
Example 15
Source File: StandardMultipartHttpServletRequest.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public HttpHeaders getMultipartHeaders(String paramOrFileName) {
	try {
		Part part = getPart(paramOrFileName);
		if (part != null) {
			HttpHeaders headers = new HttpHeaders();
			for (String headerName : part.getHeaderNames()) {
				headers.put(headerName, new ArrayList<String>(part.getHeaders(headerName)));
			}
			return headers;
		}
		else {
			return null;
		}
	}
	catch (Exception ex) {
		throw new MultipartException("Could not access multipart servlet request", ex);
	}
}
 
Example 16
Source File: PrintingResultHandler.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
protected final HttpHeaders getResponseHeaders(MockHttpServletResponse response) {
	HttpHeaders headers = new HttpHeaders();
	for (String name : response.getHeaderNames()) {
		headers.put(name, response.getHeaders(name));
	}
	return headers;
}
 
Example 17
Source File: CommonRequestProxyHeadersRewriter.java    From charon-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
void rewriteHeaders(HttpHeaders headers, URI uri, Consumer<HttpHeaders> headersSetter) {
    HttpHeaders rewrittenHeaders = copyHeaders(headers);
    List<String> forwardedFor = new ArrayList<>(emptyIfNull(rewrittenHeaders.get(X_FORWARDED_FOR)));
    forwardedFor.add(uri.getAuthority());
    rewrittenHeaders.put(X_FORWARDED_FOR, forwardedFor);
    rewrittenHeaders.set(X_FORWARDED_PROTO, uri.getScheme());
    rewrittenHeaders.set(X_FORWARDED_HOST, uri.getHost());
    rewrittenHeaders.set(X_FORWARDED_PORT, resolvePort(uri));
    headersSetter.accept(rewrittenHeaders);
    log.debug("Request headers rewritten from {} to {}", headers, rewrittenHeaders);
}
 
Example 18
Source File: ReverseProxyFilter.java    From staffjoy with MIT License 5 votes vote down vote up
protected void addForwardHeaders(HttpServletRequest request, HttpHeaders headers) {
    List<String> forwordedFor = headers.get(X_FORWARDED_FOR_HEADER);
    if (isEmpty(forwordedFor)) {
        forwordedFor = new ArrayList<>(1);
    }
    forwordedFor.add(request.getRemoteAddr());
    headers.put(X_FORWARDED_FOR_HEADER, forwordedFor);
    headers.set(X_FORWARDED_PROTO_HEADER, request.getScheme());
    headers.set(X_FORWARDED_HOST_HEADER, request.getServerName());
    headers.set(X_FORWARDED_PORT_HEADER, valueOf(request.getServerPort()));
}
 
Example 19
Source File: HttpEntityMethodProcessor.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void handleReturnValue(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()) {
		for (Map.Entry<String, List<String>> entry : entityHeaders.entrySet()) {
			if (HttpHeaders.VARY.equals(entry.getKey()) && outputHeaders.containsKey(HttpHeaders.VARY)) {
				List<String> values = getVaryRequestHeadersToAdd(outputHeaders, entityHeaders);
				if (!values.isEmpty()) {
					outputHeaders.setVary(values);
				}
			}
			else {
				outputHeaders.put(entry.getKey(), entry.getValue());
			}
		}
	}

	if (responseEntity instanceof ResponseEntity) {
		int returnStatus = ((ResponseEntity<?>) responseEntity).getStatusCodeValue();
		outputMessage.getServletResponse().setStatus(returnStatus);
		if (returnStatus == 200) {
			if (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;
			}
		}
	}

	// 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: RestTemplateTraceIdInterceptor.java    From framework with Apache License 2.0 3 votes vote down vote up
/**
 * Description: <br>
 * 
 * @author 王伟<br>
 * @taskId <br>
 * @param httpRequest
 * @param bytes
 * @param clientHttpRequestExecution
 * @return
 * @throws IOException <br>
 */
@Override
public ClientHttpResponse intercept(final HttpRequest httpRequest, final byte[] bytes,
    final ClientHttpRequestExecution clientHttpRequestExecution) throws IOException {
    HttpHeaders headers = httpRequest.getHeaders();

    headers.put(TraceIdFilter.TRACE_ID, Arrays.asList(TxManager.getTraceId()));

    return clientHttpRequestExecution.execute(httpRequest, bytes);
}