Java Code Examples for org.springframework.http.HttpRequest#getHeaders()
The following examples show how to use
org.springframework.http.HttpRequest#getHeaders() .
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: HttpRequestProducesMatcher.java From spring-cloud-alibaba with Apache License 2.0 | 6 votes |
@Override public boolean match(HttpRequest request) { if (expressions.isEmpty()) { return true; } HttpHeaders httpHeaders = request.getHeaders(); List<MediaType> acceptedMediaTypes = httpHeaders.getAccept(); for (ProduceMediaTypeExpression expression : expressions) { if (!expression.match(acceptedMediaTypes)) { return false; } } return true; }
Example 2
Source File: RequestContextHeaderInterceptor.java From logging-log4j-audit with Apache License 2.0 | 6 votes |
@Override public ClientHttpResponse intercept(HttpRequest httpRequest, byte[] body, ClientHttpRequestExecution clientHttpRequestExecution) throws IOException { Map<String, String> map = ThreadContext.getImmutableContext(); HttpHeaders headers = httpRequest.getHeaders(); for (Map.Entry<String, String> entry : map.entrySet()) { RequestContextMapping mapping = mappings.getMapping(entry.getKey()); if (mapping != null && !mapping.isLocal()) { String key = mappings.getHeaderPrefix() + mapping.getFieldName(); if (!headers.containsKey(key)) { headers.add(key, entry.getValue()); } } } return clientHttpRequestExecution.execute(httpRequest, body); }
Example 3
Source File: TestUtils.java From wingtips with Apache License 2.0 | 6 votes |
public static void verifyExpectedTracingHeaders(HttpRequest executedRequest, Span expectedSpanForHeaders) { HttpHeaders headers = executedRequest.getHeaders(); List<String> actualTraceIdHeaderVal = headers.get(TRACE_ID); List<String> actualSpanIdHeaderVal = headers.get(SPAN_ID); List<String> actualSampledHeaderVal = headers.get(TRACE_SAMPLED); List<String> actualParentSpanIdHeaderVal = headers.get(PARENT_SPAN_ID); if (expectedSpanForHeaders == null) { verifyExpectedTracingHeaderValue(actualTraceIdHeaderVal, null); verifyExpectedTracingHeaderValue(actualSpanIdHeaderVal, null); verifyExpectedTracingHeaderValue(actualSampledHeaderVal, null); verifyExpectedTracingHeaderValue(actualParentSpanIdHeaderVal, null); } else { verifyExpectedTracingHeaderValue(actualTraceIdHeaderVal, expectedSpanForHeaders.getTraceId()); verifyExpectedTracingHeaderValue(actualSpanIdHeaderVal, expectedSpanForHeaders.getSpanId()); verifyExpectedTracingHeaderValue( actualSampledHeaderVal, convertSampleableBooleanToExpectedB3Value(expectedSpanForHeaders.isSampleable()) ); verifyExpectedTracingHeaderValue(actualParentSpanIdHeaderVal, expectedSpanForHeaders.getParentSpanId()); } }
Example 4
Source File: RestTemplateHeaderInterceptor.java From blade-tool with GNU Lesser General Public License v3.0 | 6 votes |
@Override public ClientHttpResponse intercept( HttpRequest request, byte[] bytes, ClientHttpRequestExecution execution) throws IOException { HttpHeaders headers = BladeHttpHeadersContextHolder.get(); // 考虑2中情况 1. RestTemplate 不是用 hystrix 2. 使用 hystrix if (headers == null) { headers = BladeHttpHeadersContextHolder.toHeaders(accountGetter, properties); } if (headers != null && !headers.isEmpty()) { HttpHeaders httpHeaders = request.getHeaders(); headers.forEach((key, values) -> { values.forEach(value -> httpHeaders.add(key, value)); }); } return execution.execute(request, bytes); }
Example 5
Source File: TransactionRequestInterceptor.java From ByteJTA with GNU Lesser General Public License v3.0 | 6 votes |
private void invokeBeforeSendRequest(HttpRequest httpRequest, String identifier) throws IOException { SpringCloudBeanRegistry beanRegistry = SpringCloudBeanRegistry.getInstance(); TransactionBeanFactory beanFactory = beanRegistry.getBeanFactory(); TransactionManager transactionManager = beanFactory.getTransactionManager(); TransactionInterceptor transactionInterceptor = beanFactory.getTransactionInterceptor(); TransactionImpl transaction = // (TransactionImpl) transactionManager.getTransactionQuietly(); TransactionContext transactionContext = transaction.getTransactionContext(); byte[] reqByteArray = SerializeUtils.serializeObject(transactionContext); String reqTransactionStr = Base64.getEncoder().encodeToString(reqByteArray); HttpHeaders reqHeaders = httpRequest.getHeaders(); reqHeaders.add(HEADER_TRANCACTION_KEY, reqTransactionStr); reqHeaders.add(HEADER_PROPAGATION_KEY, this.identifier); TransactionRequestImpl request = new TransactionRequestImpl(); request.setTransactionContext(transactionContext); RemoteCoordinator coordinator = beanRegistry.getConsumeCoordinator(identifier); request.setTargetTransactionCoordinator(coordinator); transactionInterceptor.beforeSendRequest(request); }
Example 6
Source File: CompressingClientHttpRequestInterceptor.java From tutorials with MIT License | 5 votes |
/** * Compress a request body using Gzip and add Http headers. * * @param req * @param body * @param exec * @return * @throws IOException */ @Override public ClientHttpResponse intercept(HttpRequest req, byte[] body, ClientHttpRequestExecution exec) throws IOException { LOG.info("Compressing request..."); HttpHeaders httpHeaders = req.getHeaders(); httpHeaders.add(HttpHeaders.CONTENT_ENCODING, GZIP_ENCODING); httpHeaders.add(HttpHeaders.ACCEPT_ENCODING, GZIP_ENCODING); return exec.execute(req, GzipUtils.compress(body)); }
Example 7
Source File: UserContextInterceptor.java From spring-microservices-in-action with Apache License 2.0 | 5 votes |
@Override public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException { HttpHeaders headers = request.getHeaders(); headers.add(UserContext.CORRELATION_ID, UserContextHolder.getContext().getCorrelationId()); headers.add(UserContext.AUTH_TOKEN, UserContextHolder.getContext().getAuthToken()); return execution.execute(request, body); }
Example 8
Source File: CatRestInterceptor.java From piggymetrics with MIT License | 5 votes |
@Override public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException { Transaction t = Cat.newTransaction(CatConstants.TYPE_REMOTE_CALL, request.getURI().toString()); try { HttpHeaders headers = request.getHeaders(); // 保存和传递CAT调用链上下文 Context ctx = new CatContext(); Cat.logRemoteCallClient(ctx); headers.add(CatHttpConstants.CAT_HTTP_HEADER_ROOT_MESSAGE_ID, ctx.getProperty(Cat.Context.ROOT)); headers.add(CatHttpConstants.CAT_HTTP_HEADER_PARENT_MESSAGE_ID, ctx.getProperty(Cat.Context.PARENT)); headers.add(CatHttpConstants.CAT_HTTP_HEADER_CHILD_MESSAGE_ID, ctx.getProperty(Cat.Context.CHILD)); // 保证请求继续被执行 ClientHttpResponse response = execution.execute(request, body); t.setStatus(Transaction.SUCCESS); return response; } catch (Exception e) { Cat.getProducer().logError(e); t.setStatus(e); throw e; } finally { t.complete(); } }
Example 9
Source File: UserContextInterceptor.java From spring-microservices-in-action with Apache License 2.0 | 5 votes |
@Override public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException { HttpHeaders headers = request.getHeaders(); headers.add(UserContext.CORRELATION_ID, UserContextHolder.getContext().getCorrelationId()); headers.add(UserContext.AUTH_TOKEN, UserContextHolder.getContext().getAuthToken()); return execution.execute(request, body); }
Example 10
Source File: TaggingRequestInterceptor.java From multiapps-controller with Apache License 2.0 | 5 votes |
@Override public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException { HttpHeaders headers = request.getHeaders(); setHeader(headers, TAG_HEADER_NAME, headerValue); if (orgHeaderValue != null && spaceHeaderValue != null) { setHeader(headers, TAG_HEADER_ORG_NAME, orgHeaderValue); setHeader(headers, TAG_HEADER_SPACE_NAME, spaceHeaderValue); } return execution.execute(request, body); }
Example 11
Source File: HttpMessageConverterResolver.java From spring-cloud-alibaba with Apache License 2.0 | 5 votes |
public HttpMessageConverterHolder resolve(HttpRequest request, Class<?> parameterType) { HttpMessageConverterHolder httpMessageConverterHolder = null; HttpHeaders httpHeaders = request.getHeaders(); MediaType contentType = httpHeaders.getContentType(); if (contentType == null) { contentType = MediaType.APPLICATION_OCTET_STREAM; } for (HttpMessageConverter<?> converter : this.messageConverters) { if (converter instanceof GenericHttpMessageConverter) { GenericHttpMessageConverter genericConverter = (GenericHttpMessageConverter) converter; if (genericConverter.canRead(parameterType, parameterType, contentType)) { httpMessageConverterHolder = new HttpMessageConverterHolder( contentType, converter); break; } } else { if (converter.canRead(parameterType, contentType)) { httpMessageConverterHolder = new HttpMessageConverterHolder( contentType, converter); break; } } } return httpMessageConverterHolder; }
Example 12
Source File: RestTemplateInterceptor.java From sofa-tracer with Apache License 2.0 | 5 votes |
/** * add request tag * @param request * @param sofaTracerSpan */ private void appendRestTemplateRequestSpanTags(HttpRequest request, SofaTracerSpan sofaTracerSpan) { if (sofaTracerSpan == null) { return; } //appName String appName = SofaTracerConfiguration.getProperty( SofaTracerConfiguration.TRACER_APPNAME_KEY, StringUtils.EMPTY_STRING); //methodName String methodName = request.getMethod().name(); //appName sofaTracerSpan.setTag(CommonSpanTags.LOCAL_APP, appName == null ? StringUtils.EMPTY_STRING : appName); sofaTracerSpan.setTag(CommonSpanTags.REQUEST_URL, request.getURI().toString()); //method sofaTracerSpan.setTag(CommonSpanTags.METHOD, methodName); HttpHeaders headers = request.getHeaders(); //reqSize if (headers != null && headers.containsKey("Content-Length")) { List<String> contentLengthList = headers.get("Content-Length"); if (contentLengthList != null && !contentLengthList.isEmpty()) { sofaTracerSpan.setTag(CommonSpanTags.REQ_SIZE, Long.valueOf(contentLengthList.get(0))); } } else { sofaTracerSpan.setTag(CommonSpanTags.REQ_SIZE, String.valueOf(-1)); } //carrier this.injectCarrier(request, sofaTracerSpan); }
Example 13
Source File: CatRestInterceptor.java From cat_lab with MIT License | 5 votes |
@Override public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException { Transaction t = Cat.newTransaction(CatConstants.TYPE_CALL, request.getURI().toString()); try { HttpHeaders headers = request.getHeaders(); // 保存和传递CAT调用链上下文 Context ctx = new CatContext(); Cat.logRemoteCallClient(ctx); headers.add(CatHttpConstants.CAT_HTTP_HEADER_ROOT_MESSAGE_ID, ctx.getProperty(Cat.Context.ROOT)); headers.add(CatHttpConstants.CAT_HTTP_HEADER_PARENT_MESSAGE_ID, ctx.getProperty(Cat.Context.PARENT)); headers.add(CatHttpConstants.CAT_HTTP_HEADER_CHILD_MESSAGE_ID, ctx.getProperty(Cat.Context.CHILD)); // 保证请求继续被执行 ClientHttpResponse response = execution.execute(request, body); t.setStatus(Transaction.SUCCESS); return response; } catch (Exception e) { Cat.getProducer().logError(e); t.setStatus(e); throw e; } finally { t.complete(); } }
Example 14
Source File: UserContextInterceptor.java From spring-microservices-in-action with Apache License 2.0 | 5 votes |
@Override public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException { HttpHeaders headers = request.getHeaders(); headers.add(UserContext.CORRELATION_ID, UserContextHolder.getContext().getCorrelationId()); headers.add(UserContext.AUTH_TOKEN, UserContextHolder.getContext().getAuthToken()); return execution.execute(request, body); }
Example 15
Source File: WebUtils.java From java-technology-stack with MIT License | 5 votes |
/** * Check if the request is a same-origin one, based on {@code Origin}, {@code Host}, * {@code Forwarded}, {@code X-Forwarded-Proto}, {@code X-Forwarded-Host} and * {@code X-Forwarded-Port} headers. * * <p><strong>Note:</strong> as of 5.1 this method ignores * {@code "Forwarded"} and {@code "X-Forwarded-*"} headers that specify the * client-originated address. Consider using the {@code ForwardedHeaderFilter} * to extract and use, or to discard such headers. * @return {@code true} if the request is a same-origin one, {@code false} in case * of cross-origin request * @since 4.2 */ public static boolean isSameOrigin(HttpRequest request) { HttpHeaders headers = request.getHeaders(); String origin = headers.getOrigin(); if (origin == null) { return true; } String scheme; String host; int port; if (request instanceof ServletServerHttpRequest) { // Build more efficiently if we can: we only need scheme, host, port for origin comparison HttpServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest(); scheme = servletRequest.getScheme(); host = servletRequest.getServerName(); port = servletRequest.getServerPort(); } else { URI uri = request.getURI(); scheme = uri.getScheme(); host = uri.getHost(); port = uri.getPort(); } UriComponents originUrl = UriComponentsBuilder.fromOriginHeader(origin).build(); return (ObjectUtils.nullSafeEquals(scheme, originUrl.getScheme()) && ObjectUtils.nullSafeEquals(host, originUrl.getHost()) && getPort(scheme, port) == getPort(originUrl.getScheme(), originUrl.getPort())); }
Example 16
Source File: HttpAuthInterceptor.java From haven-platform with Apache License 2.0 | 5 votes |
@Override public ClientHttpResponse intercept(final HttpRequest request, final byte[] body, final ClientHttpRequestExecution execution) throws IOException { try { final HttpHeaders headers = request.getHeaders(); interceptInner(headers, request); return execution.execute(request, body); } finally { registryName.remove(); } }
Example 17
Source File: WebUtils.java From spring-analysis-note with MIT License | 5 votes |
/** * Check if the request is a same-origin one, based on {@code Origin}, {@code Host}, * {@code Forwarded}, {@code X-Forwarded-Proto}, {@code X-Forwarded-Host} and * {@code X-Forwarded-Port} headers. * * <p><strong>Note:</strong> as of 5.1 this method ignores * {@code "Forwarded"} and {@code "X-Forwarded-*"} headers that specify the * client-originated address. Consider using the {@code ForwardedHeaderFilter} * to extract and use, or to discard such headers. * @return {@code true} if the request is a same-origin one, {@code false} in case * of cross-origin request * @since 4.2 */ public static boolean isSameOrigin(HttpRequest request) { HttpHeaders headers = request.getHeaders(); String origin = headers.getOrigin(); if (origin == null) { return true; } String scheme; String host; int port; if (request instanceof ServletServerHttpRequest) { // Build more efficiently if we can: we only need scheme, host, port for origin comparison HttpServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest(); scheme = servletRequest.getScheme(); host = servletRequest.getServerName(); port = servletRequest.getServerPort(); } else { URI uri = request.getURI(); scheme = uri.getScheme(); host = uri.getHost(); port = uri.getPort(); } UriComponents originUrl = UriComponentsBuilder.fromOriginHeader(origin).build(); return (ObjectUtils.nullSafeEquals(scheme, originUrl.getScheme()) && ObjectUtils.nullSafeEquals(host, originUrl.getHost()) && getPort(scheme, port) == getPort(originUrl.getScheme(), originUrl.getPort())); }
Example 18
Source File: ClientRestTemplate.java From authmore-framework with Apache License 2.0 | 5 votes |
@Override public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException { Assert.notEmpty(token, "token cannot be empty"); HttpHeaders headers = request.getHeaders(); headers.add(HttpHeaders.AUTHORIZATION, "Bearer " + token); return execution.execute(request, body); }
Example 19
Source File: DtmRestTemplateInterceptor.java From spring-cloud-huawei with Apache License 2.0 | 5 votes |
@Override public ClientHttpResponse intercept(HttpRequest httpRequest, byte[] bytes, ClientHttpRequestExecution clientHttpRequestExecution) throws IOException { DTMContext dtmContext = DTMContext.getDTMContext(); long gid = dtmContext.getGlobalTxId(); HttpHeaders headers = httpRequest.getHeaders(); if (gid != -1) { DtmContextDTO dtmContextDTO = DtmContextDTO.fromDtmContext(dtmContext); headers.add(DtmConstants.DTM_CONTEXT, Json.encode(dtmContextDTO)); } return clientHttpRequestExecution.execute(httpRequest, bytes); }
Example 20
Source File: RestTemplateTraceIdInterceptor.java From framework with Apache License 2.0 | 3 votes |
/** * 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); }