org.springframework.http.client.support.HttpRequestWrapper Java Examples

The following examples show how to use org.springframework.http.client.support.HttpRequestWrapper. 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: SpringHttpClientTagAdapterTest.java    From wingtips with Apache License 2.0 6 votes vote down vote up
@DataProvider(value = {
    "true   |   spring.asyncresttemplate",
    "false  |   spring.resttemplate"
}, splitBy = "\\|")
@Test
public void getSpanHandlerTagValue_works_as_expected_when_request_is_an_HttpRequestWrapper(
    boolean wrappedRequestIsAsyncClientHttpRequest, String expectedResult
) {
    // given
    HttpRequestWrapper request = mock(HttpRequestWrapper.class);
    HttpRequest wrappedRequest = (wrappedRequestIsAsyncClientHttpRequest)
                                 ? mock(AsyncClientHttpRequest.class)
                                 : mock(HttpRequest.class);

    doReturn(wrappedRequest).when(request).getRequest();

    // when
    String result = implSpy.getSpanHandlerTagValue(request, responseMock);

    // then
    assertThat(result).isEqualTo(expectedResult);
}
 
Example #2
Source File: SurgicalRoutingRequestTransformer.java    From spring-cloud-services-connector with Apache License 2.0 6 votes vote down vote up
@Override
public HttpRequest transformRequest(HttpRequest request, ServiceInstance instance) {
	Map<String, String> metadata = instance.getMetadata();
	if (metadata.containsKey(CF_APP_GUID) && metadata.containsKey(CF_INSTANCE_INDEX)) {
		final String headerValue = String.format("%s:%s", metadata.get(CF_APP_GUID), metadata.get(CF_INSTANCE_INDEX));
		// request.getHeaders might be immutable, so return a wrapper
		return new HttpRequestWrapper(request) {
			@Override
			public HttpHeaders getHeaders() {
				HttpHeaders headers = new HttpHeaders();
				headers.putAll(super.getHeaders());
				headers.add(SURGICAL_ROUTING_HEADER, headerValue);
				return headers;
			}
		};
	}
	return request;
}
 
Example #3
Source File: SurgicalRoutingRequestTransformer.java    From spring-cloud-services-starters with Apache License 2.0 6 votes vote down vote up
@Override
public HttpRequest transformRequest(HttpRequest request, ServiceInstance instance) {
	Map<String, String> metadata = instance.getMetadata();
	if (metadata.containsKey(CF_APP_GUID) && metadata.containsKey(CF_INSTANCE_INDEX)) {
		final String headerValue = String.format("%s:%s", metadata.get(CF_APP_GUID),
				metadata.get(CF_INSTANCE_INDEX));
		// request.getHeaders might be immutable, so return a wrapper
		return new HttpRequestWrapper(request) {
			@Override
			public HttpHeaders getHeaders() {
				HttpHeaders headers = new HttpHeaders();
				headers.putAll(super.getHeaders());
				headers.add(SURGICAL_ROUTING_HEADER, headerValue);
				return headers;
			}
		};
	}
	return request;
}
 
Example #4
Source File: DtsRemoteInterceptor.java    From dts with Apache License 2.0 5 votes vote down vote up
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
    throws IOException {
    HttpRequestWrapper requestWrapper = new HttpRequestWrapper(request);
    Map<String, String> contexts = SpringCloudDtsContext.getContext().getAttachments();
    for (Map.Entry<String, String> entry : contexts.entrySet()) {
        String key = entry.getKey();
        String value = entry.getValue();
        requestWrapper.getHeaders().set(CONTEXT_HEADER_PARENT + key, value);
    }
    return execution.execute(requestWrapper, body);
}
 
Example #5
Source File: SeataRestTemplateInterceptor.java    From seata-samples with Apache License 2.0 5 votes vote down vote up
public ClientHttpResponse intercept(HttpRequest httpRequest, byte[] bytes, ClientHttpRequestExecution clientHttpRequestExecution) throws IOException {
    HttpRequestWrapper requestWrapper = new HttpRequestWrapper(httpRequest);
    String xid = RootContext.getXID();
    if (StringUtils.isNotEmpty(xid)) {
        requestWrapper.getHeaders().add(RootContext.KEY_XID, xid);
    }

    return clientHttpRequestExecution.execute(requestWrapper, bytes);
}
 
Example #6
Source File: CoreHttpRequestInterceptor.java    From paascloud-master with Apache License 2.0 5 votes vote down vote up
/**
 * Intercept client http response.
 *
 * @param request   the request
 * @param body      the body
 * @param execution the execution
 *
 * @return the client http response
 *
 * @throws IOException the io exception
 */
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body,
                                    ClientHttpRequestExecution execution) throws IOException {
	HttpRequestWrapper requestWrapper = new HttpRequestWrapper(request);

	String header = StringUtils.collectionToDelimitedString(
			CoreHeaderInterceptor.LABEL.get(),
			CoreHeaderInterceptor.HEADER_LABEL_SPLIT);
	log.info("header={} ", header);
	requestWrapper.getHeaders().add(CoreHeaderInterceptor.HEADER_LABEL, header);

	return execution.execute(requestWrapper, body);
}
 
Example #7
Source File: SeataRestTemplateInterceptor.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
@Override
public ClientHttpResponse intercept(HttpRequest httpRequest, byte[] bytes,
		ClientHttpRequestExecution clientHttpRequestExecution) throws IOException {
	HttpRequestWrapper requestWrapper = new HttpRequestWrapper(httpRequest);

	String xid = RootContext.getXID();

	if (!StringUtils.isEmpty(xid)) {
		requestWrapper.getHeaders().add(RootContext.KEY_XID, xid);
	}
	return clientHttpRequestExecution.execute(requestWrapper, bytes);
}
 
Example #8
Source File: CredHubRestTemplateFactory.java    From spring-credhub with Apache License 2.0 5 votes vote down vote up
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
		throws IOException {
	HttpRequestWrapper requestWrapper = new HttpRequestWrapper(request);

	HttpHeaders headers = requestWrapper.getHeaders();
	headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
	headers.setContentType(MediaType.APPLICATION_JSON);

	return execution.execute(requestWrapper, body);
}
 
Example #9
Source File: CredHubOAuth2RequestInterceptor.java    From spring-credhub with Apache License 2.0 5 votes vote down vote up
/**
 * Add an OAuth2 bearer token header to each request.
 *
 * {@inheritDoc}
 */
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
		throws IOException {
	HttpRequestWrapper requestWrapper = new HttpRequestWrapper(request);

	HttpHeaders headers = requestWrapper.getHeaders();
	headers.setBearerAuth(authorizeClient().getAccessToken().getTokenValue());

	return execution.execute(requestWrapper, body);
}
 
Example #10
Source File: SpringHttpClientTagAdapter.java    From wingtips with Apache License 2.0 5 votes vote down vote up
@Override
public @Nullable String getSpanHandlerTagValue(@Nullable HttpRequest request, @Nullable ClientHttpResponse response) {
    if (request instanceof HttpRequestWrapper) {
        request = ((HttpRequestWrapper) request).getRequest();
    }

    if (request instanceof AsyncClientHttpRequest) {
        return "spring.asyncresttemplate";
    }

    return "spring.resttemplate";
}
 
Example #11
Source File: JsonClientHttpRequestInterceptor.java    From bowman with Apache License 2.0 5 votes vote down vote up
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
		throws IOException {
	HttpRequestWrapper wrapped = new HttpRequestWrapper(request);
	wrapped.getHeaders().put("Content-Type", asList(MediaTypes.HAL_JSON_VALUE));
	wrapped.getHeaders().put("Accept", asList(MediaTypes.HAL_JSON_VALUE));
	return execution.execute(wrapped, body);
}
 
Example #12
Source File: HelloBrotliHttpControllerTest.java    From jbrotli with Apache License 2.0 5 votes vote down vote up
private static List<ClientHttpRequestInterceptor> createAcceptBrotliEncodingInterceptor() {
  return singletonList((ClientHttpRequestInterceptor) new ClientHttpRequestInterceptor() {
    @Override
    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
      HttpRequest wrapper = new HttpRequestWrapper(request);
      wrapper.getHeaders().set("Accept-Encoding", BROTLI_HTTP_CONTENT_CODING);
      return execution.execute(wrapper, body);
    }
  });
}