org.springframework.http.RequestEntity.BodyBuilder Java Examples

The following examples show how to use org.springframework.http.RequestEntity.BodyBuilder. 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: ApiClient.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
/**
 * Add headers to the request that is being built
 * @param headers The headers to add
 * @param requestBuilder The current request
 */
protected void addHeadersToRequest(HttpHeaders headers, BodyBuilder requestBuilder) {
    for (Entry<String, List<String>> entry : headers.entrySet()) {
        List<String> values = entry.getValue();
        for(String value : values) {
            if (value != null) {
                requestBuilder.header(entry.getKey(), value);
            }
        }
    }
}
 
Example #2
Source File: ApiClient.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
/**
 * Add headers to the request that is being built
 * @param headers The headers to add
 * @param requestBuilder The current request
 */
protected void addHeadersToRequest(HttpHeaders headers, BodyBuilder requestBuilder) {
    for (Entry<String, List<String>> entry : headers.entrySet()) {
        List<String> values = entry.getValue();
        for(String value : values) {
            if (value != null) {
                requestBuilder.header(entry.getKey(), value);
            }
        }
    }
}
 
Example #3
Source File: ApiClient.java    From tutorials with MIT License 5 votes vote down vote up
/**
 * Add headers to the request that is being built
 * @param headers The headers to add
 * @param requestBuilder The current request
 */
protected void addHeadersToRequest(HttpHeaders headers, BodyBuilder requestBuilder) {
    for (Entry<String, List<String>> entry : headers.entrySet()) {
        List<String> values = entry.getValue();
        for(String value : values) {
            if (value != null) {
                requestBuilder.header(entry.getKey(), value);
            }
        }
    }
}
 
Example #4
Source File: HttpArtifactoryRepository.java    From artifactory-resource with Apache License 2.0 5 votes vote down vote up
private BodyBuilder deployRequest(DeployableArtifact artifact) throws IOException {
	URI uri = UriComponentsBuilder.fromUriString(this.uri).path(this.repositoryName).path(artifact.getPath())
			.path(buildMatrixParams(artifact.getProperties())).buildAndExpand(NO_VARIABLES).encode().toUri();
	Checksums checksums = artifact.getChecksums();
	return RequestEntity.put(uri).contentType(MediaType.APPLICATION_OCTET_STREAM).contentLength(artifact.getSize())
			.header("X-Checksum-Sha1", checksums.getSha1()).header("X-Checksum-Md5", checksums.getMd5());
}
 
Example #5
Source File: ApiClient.java    From tutorials with MIT License 5 votes vote down vote up
/**
 * Add headers to the request that is being built
 * @param headers The headers to add
 * @param requestBuilder The current request
 */
protected void addHeadersToRequest(HttpHeaders headers, BodyBuilder requestBuilder) {
    for (Entry<String, List<String>> entry : headers.entrySet()) {
        List<String> values = entry.getValue();
        for(String value : values) {
            if (value != null) {
                requestBuilder.header(entry.getKey(), value);
            }
        }
    }
}
 
Example #6
Source File: ApiClient.java    From tutorials with MIT License 5 votes vote down vote up
/**
 * Invoke API by sending HTTP request with the given options.
 *
 * @param <T> the return type to use
 * @param path The sub-path of the HTTP URL
 * @param method The request method
 * @param queryParams The query parameters
 * @param body The request body object
 * @param headerParams The header parameters
 * @param formParams The form parameters
 * @param accept The request's Accept header
 * @param contentType The request's Content-Type header
 * @param authNames The authentications to apply
 * @param returnType The return type into which to deserialize the response
 * @return The response body in chosen type
 */
public <T> T invokeAPI(String path, HttpMethod method, MultiValueMap<String, String> queryParams, Object body, HttpHeaders headerParams, MultiValueMap<String, Object> formParams, List<MediaType> accept, MediaType contentType, String[] authNames, ParameterizedTypeReference<T> returnType) throws RestClientException {
    updateParamsForAuth(authNames, queryParams, headerParams);
    
    final UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(basePath).path(path);
    if (queryParams != null) {
        builder.queryParams(queryParams);
    }
    
    final BodyBuilder requestBuilder = RequestEntity.method(method, builder.build().toUri());
    if(accept != null) {
        requestBuilder.accept(accept.toArray(new MediaType[accept.size()]));
    }
    if(contentType != null) {
        requestBuilder.contentType(contentType);
    }
    
    addHeadersToRequest(headerParams, requestBuilder);
    addHeadersToRequest(defaultHeaders, requestBuilder);
    
    RequestEntity<Object> requestEntity = requestBuilder.body(selectBody(body, formParams, contentType));

    ResponseEntity<T> responseEntity = restTemplate.exchange(requestEntity, returnType);
    
    statusCode = responseEntity.getStatusCode();
    responseHeaders = responseEntity.getHeaders();

    if (responseEntity.getStatusCode() == HttpStatus.NO_CONTENT) {
        return null;
    } else if (responseEntity.getStatusCode().is2xxSuccessful()) {
        if (returnType == null) {
            return null;
        }
        return responseEntity.getBody();
    } else {
        // The error handler built into the RestTemplate should handle 400 and 500 series errors.
        throw new RestClientException("API returned " + statusCode + " and it wasn't handled by the RestTemplate error handler");
    }
}
 
Example #7
Source File: ProxyExchange.java    From spring-cloud-gateway with Apache License 2.0 5 votes vote down vote up
private BodyBuilder headers(BodyBuilder builder) {
	proxy();
	for (String name : filterHeaderKeys(headers)) {
		builder.header(name, headers.get(name).toArray(new String[0]));
	}
	return builder;
}
 
Example #8
Source File: ProxyExchange.java    From spring-cloud-gateway with Apache License 2.0 5 votes vote down vote up
private BodyBuilder headers(BodyBuilder builder) {
	Set<String> sensitive = this.sensitive;
	if (sensitive == null) {
		sensitive = DEFAULT_SENSITIVE;
	}
	proxy();
	for (String name : headers.keySet()) {
		if (sensitive.contains(name.toLowerCase())) {
			continue;
		}
		builder.header(name, headers.get(name).toArray(new String[0]));
	}
	return builder;
}
 
Example #9
Source File: ApiClient.java    From tutorials with MIT License 4 votes vote down vote up
/**
 * Add cookies to the request that is being built
 * @param cookies The cookies to add
 * @param requestBuilder The current request
 */
protected void addCookiesToRequest(MultiValueMap<String, String> cookies, BodyBuilder requestBuilder) {
    if (!cookies.isEmpty()) {
        requestBuilder.header("Cookie", buildCookieHeader(cookies));
    }
}
 
Example #10
Source File: ProxyExchange.java    From spring-cloud-gateway with Apache License 2.0 4 votes vote down vote up
public Mono<ResponseEntity<T>> delete() {
	RequestEntity<Void> requestEntity = headers(
			(BodyBuilder) RequestEntity.delete(uri)).build();
	return exchange(requestEntity);
}
 
Example #11
Source File: ProxyExchange.java    From spring-cloud-gateway with Apache License 2.0 4 votes vote down vote up
public Mono<ResponseEntity<T>> options() {
	RequestEntity<?> requestEntity = headers((BodyBuilder) RequestEntity.options(uri))
			.build();
	return exchange(requestEntity);
}
 
Example #12
Source File: ProxyExchange.java    From spring-cloud-gateway with Apache License 2.0 4 votes vote down vote up
public Mono<ResponseEntity<T>> head() {
	RequestEntity<?> requestEntity = headers((BodyBuilder) RequestEntity.head(uri))
			.build();
	return exchange(requestEntity);
}
 
Example #13
Source File: ProxyExchange.java    From spring-cloud-gateway with Apache License 2.0 4 votes vote down vote up
public Mono<ResponseEntity<T>> get() {
	RequestEntity<?> requestEntity = headers((BodyBuilder) RequestEntity.get(uri))
			.build();
	return exchange(requestEntity);
}
 
Example #14
Source File: ProxyExchange.java    From spring-cloud-gateway with Apache License 2.0 4 votes vote down vote up
public ResponseEntity<T> delete() {
	RequestEntity<Object> requestEntity = headers(
			(BodyBuilder) RequestEntity.delete(uri)).body(body());
	return exchange(requestEntity);
}
 
Example #15
Source File: ProxyExchange.java    From spring-cloud-gateway with Apache License 2.0 4 votes vote down vote up
public ResponseEntity<T> options() {
	RequestEntity<?> requestEntity = headers((BodyBuilder) RequestEntity.options(uri))
			.build();
	return exchange(requestEntity);
}
 
Example #16
Source File: ProxyExchange.java    From spring-cloud-gateway with Apache License 2.0 4 votes vote down vote up
public ResponseEntity<T> head() {
	RequestEntity<?> requestEntity = headers((BodyBuilder) RequestEntity.head(uri))
			.build();
	return exchange(requestEntity);
}
 
Example #17
Source File: ProxyExchange.java    From spring-cloud-gateway with Apache License 2.0 4 votes vote down vote up
public ResponseEntity<T> get() {
	RequestEntity<?> requestEntity = headers((BodyBuilder) RequestEntity.get(uri))
			.build();
	return exchange(requestEntity);
}
 
Example #18
Source File: ApiClient.java    From openapi-generator with Apache License 2.0 4 votes vote down vote up
/**
 * Add cookies to the request that is being built
 * @param cookies The cookies to add
 * @param requestBuilder The current request
 */
protected void addCookiesToRequest(MultiValueMap<String, String> cookies, BodyBuilder requestBuilder) {
    if (!cookies.isEmpty()) {
        requestBuilder.header("Cookie", buildCookieHeader(cookies));
    }
}
 
Example #19
Source File: ApiClient.java    From openapi-generator with Apache License 2.0 4 votes vote down vote up
/**
 * Add cookies to the request that is being built
 * @param cookies The cookies to add
 * @param requestBuilder The current request
 */
protected void addCookiesToRequest(MultiValueMap<String, String> cookies, BodyBuilder requestBuilder) {
    if (!cookies.isEmpty()) {
        requestBuilder.header("Cookie", buildCookieHeader(cookies));
    }
}