use of io.helidon.webclient.WebClientRequestBuilder in project helidon by oracle.
the class RestApiBase method invokeBytesRequest.
@Override
public <T extends ApiResponse> Single<T> invokeBytesRequest(Http.RequestMethod method, String path, ApiRequest<?> request, Flow.Publisher<DataChunk> byteRequest, ApiResponse.Builder<?, T> responseBuilder) {
String requestId = requestId(request);
LOGGER.finest(() -> requestId + ": Invoking " + method + " on path " + path + " with bytes request");
WebClientRequestBuilder requestBuilder = webClient.method(method).path(path);
addHeaders(requestBuilder, path, request, method, requestId);
addQueryParams(requestBuilder, path, request, method, requestId);
Supplier<Single<WebClientResponse>> responseSupplier = requestBytesPayload(path, request, method, requestId, requestBuilder, byteRequest);
return ftHandler.invoke(responseSupplier).flatMapSingle(response -> handleResponse(path, request, method, requestId, response, responseBuilder));
}
use of io.helidon.webclient.WebClientRequestBuilder in project helidon by oracle.
the class RestApiBase method requestBytesPayload.
/**
* Create a supplier for a response with publisher request.
* Defaults to "{@code () -> clientRequest.submit(publisher)}".
* Also configures content type and accept headers.
*
* @param path path requested
* @param request API request
* @param method HTTP method
* @param requestId ID of this request
* @param requestBuilder {@link io.helidon.webclient.WebClient} request builder
* @param publisher publisher to be used as request entity
*
* @return supplier of a web client response
*/
protected Supplier<Single<WebClientResponse>> requestBytesPayload(String path, ApiRequest<?> request, Http.RequestMethod method, String requestId, WebClientRequestBuilder requestBuilder, Flow.Publisher<DataChunk> publisher) {
requestBuilder.accept(request.responseMediaType().orElse(MediaType.APPLICATION_JSON));
requestBuilder.contentType(request.requestMediaType().orElse(MediaType.APPLICATION_OCTET_STREAM));
AtomicBoolean updated = new AtomicBoolean();
return () -> {
if (updated.compareAndSet(false, true)) {
return updateRequestBuilderBytesPayload(requestBuilder, path, request, method, requestId).flatMapSingle(it -> it.submit(publisher));
} else {
return requestBuilder.submit(publisher);
}
};
}
use of io.helidon.webclient.WebClientRequestBuilder in project helidon by oracle.
the class OciRestApi method requestJsonPayload.
@Override
protected Supplier<Single<WebClientResponse>> requestJsonPayload(String path, ApiRequest<?> request, Http.RequestMethod method, String requestId, WebClientRequestBuilder requestBuilder, JsonObject jsonObject) {
requestBuilder.accept(request.responseMediaType().orElse(MediaType.APPLICATION_JSON));
requestBuilder.contentType(request.requestMediaType().orElse(MediaType.APPLICATION_JSON));
// common stuff
updateRequestBuilder(requestBuilder, request, requestId);
// signature requirement
// this requires a content length and hash
ByteArrayOutputStream baos = new ByteArrayOutputStream();
jsonWriterFactory().createWriter(baos).write(jsonObject);
byte[] requestBytes = baos.toByteArray();
String sha256 = HASH_DIGEST.digest(Base64Value.create(requestBytes)).toBase64();
requestBuilder.headers(headers -> {
headers.contentLength(requestBytes.length);
headers.add("x-content-sha256", sha256);
return headers;
}).contentType(request.requestMediaType().orElse(MediaType.APPLICATION_JSON));
return () -> requestBuilder.submit(requestBytes);
}
use of io.helidon.webclient.WebClientRequestBuilder in project helidon by oracle.
the class MainTest method testNamedGreetWithCors.
// Run after CORS test changes greeting to Cheers.
@Order(12)
@Test
void testNamedGreetWithCors() {
WebClientRequestBuilder builder = webClient.get();
Headers headers = builder.headers();
headers.add("Origin", "http://foo.com");
headers.add("Host", "here.com");
WebClientResponse r = getResponse("/greet/Maria", builder);
assertEquals(200, r.status().code(), "HTTP response");
assertTrue(fromPayload(r).getMessage().contains("Cheers Maria"));
headers = r.headers();
Optional<String> allowOrigin = headers.value(CrossOriginConfig.ACCESS_CONTROL_ALLOW_ORIGIN);
assertTrue(allowOrigin.isPresent(), "Expected CORS header " + CrossOriginConfig.ACCESS_CONTROL_ALLOW_ORIGIN + " is absent");
assertEquals(allowOrigin.get(), "*");
}
use of io.helidon.webclient.WebClientRequestBuilder in project helidon by oracle.
the class MainTest method testGreetingChangeWithCorsAndOtherOrigin.
// After all other tests so we can rely on deterministic greetings.
@Order(100)
@Test
void testGreetingChangeWithCorsAndOtherOrigin() {
WebClientRequestBuilder builder = webClient.put();
Headers headers = builder.headers();
headers.add("Origin", "http://other.com");
headers.add("Host", "here.com");
WebClientResponse r = putResponse("/greet/greeting", new GreetingMessage("Ahoy"), builder);
// Result depends on whether we are using overrides or not.
boolean isOverriding = Config.create().get("cors").exists();
assertEquals(isOverriding ? 204 : 403, r.status().code(), "HTTP response3");
}
Aggregations