org.springframework.http.RequestEntity#HeadersBuilder ( )源码实例Demo

下面列出了org.springframework.http.RequestEntity#HeadersBuilder ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

private void addHeaders(RequestEntity.HeadersBuilder<?> builder, HttpHeaders headers) {
	for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
		for (String value : entry.getValue()) {
			builder.header(entry.getKey(), value);
		}
	}
}
 
private void addHeaders(RequestEntity.HeadersBuilder<?> builder, HttpHeaders headers) {
	for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
		for (String value : entry.getValue()) {
			builder.header(entry.getKey(), value);
		}
	}
}
 
private Resource getResource(String profile, String label, String path, ResourceType resourceType) {
	Assert.isTrue(configClientProperties.getName() != null && !configClientProperties.getName().isEmpty(),
			"Spring application name is undefined.");

	Assert.notEmpty(configClientProperties.getUri(), "Config server URI is undefined");
	Assert.hasText(configClientProperties.getUri()[0], "Config server URI is undefined.");

	if (profile == null) {
		profile = configClientProperties.getProfile();
		if (profile == null || profile.isEmpty()) {
			profile = "default";
		}
	}

	if (label == null) {
		label = configClientProperties.getLabel();
	}

	UriComponentsBuilder urlBuilder = UriComponentsBuilder.fromHttpUrl(configClientProperties.getUri()[0])
			.pathSegment(configClientProperties.getName()).pathSegment(profile).pathSegment(label)
			.pathSegment(path);
	if (label == null) {
		urlBuilder.queryParam("useDefaultLabel");
	}
	RequestEntity.HeadersBuilder<?> requestBuilder = RequestEntity.get(urlBuilder.build().toUri());
	if (StringUtils.hasText(configClientProperties.getToken())) {
		requestBuilder.header(TOKEN_HEADER, configClientProperties.getToken());
	}
	if (resourceType == ResourceType.BINARY) {
		requestBuilder.accept(MediaType.APPLICATION_OCTET_STREAM);
	}

	ResponseEntity<Resource> forEntity = restTemplate.exchange(requestBuilder.build(), Resource.class);
	return forEntity.getBody();
}
 
private RequestEntity<Void> prepareGet(String url, HttpHeaders headers) throws Exception {
	URI uri = new URI("http://localhost:" + this.port + url);
	RequestEntity.HeadersBuilder<?> builder = get(uri);
	addHeaders(builder, headers);
	return builder.build();
}
 
private RequestEntity<Void> prepareOptions(String url, HttpHeaders headers) throws Exception {
	URI uri = new URI("http://localhost:" + this.port + url);
	RequestEntity.HeadersBuilder<?> builder = options(uri);
	addHeaders(builder, headers);
	return builder.build();
}
 
private RequestEntity<Void> prepareGet(String url, HttpHeaders headers) throws Exception {
	URI uri = new URI("http://localhost:" + this.port + url);
	RequestEntity.HeadersBuilder<?> builder = get(uri);
	addHeaders(builder, headers);
	return builder.build();
}
 
private RequestEntity<Void> prepareOptions(String url, HttpHeaders headers) throws Exception {
	URI uri = new URI("http://localhost:" + this.port + url);
	RequestEntity.HeadersBuilder<?> builder = options(uri);
	addHeaders(builder, headers);
	return builder.build();
}
 
private Resource getResource(String profile, String label,
		String path, ResourceType resourceType) {
	Assert.isTrue(
			configClientProperties.getName() != null
					&& !configClientProperties.getName().isEmpty(),
			"Spring application name is undefined.");

	Assert.notEmpty(configClientProperties.getUri(), "Config server URI is undefined");
	Assert.hasText(configClientProperties.getUri()[0], "Config server URI is undefined.");

	if (profile == null) {
		profile = configClientProperties.getProfile();
		if (profile == null || profile.isEmpty()) {
			profile = "default";
		}
	}

	if (label == null) {
		label = configClientProperties.getLabel();
	}

	UriComponentsBuilder urlBuilder = UriComponentsBuilder
			.fromHttpUrl(configClientProperties.getUri()[0])
			.pathSegment(configClientProperties.getName())
			.pathSegment(profile)
			.pathSegment(label)
			.pathSegment(path);
	if (label == null) {
		urlBuilder.queryParam("useDefaultLabel");
	}
	RequestEntity.HeadersBuilder<?> requestBuilder = RequestEntity.get(urlBuilder.build().toUri());
	if (StringUtils.hasText(configClientProperties.getToken())) {
		requestBuilder.header(TOKEN_HEADER, configClientProperties.getToken());
	}
	if (resourceType == ResourceType.BINARY) {
		requestBuilder.accept(MediaType.APPLICATION_OCTET_STREAM);
	}

	ResponseEntity<Resource> forEntity = restTemplate.exchange(requestBuilder.build(), Resource.class);
	return forEntity.getBody();
}