use of io.helidon.webclient.WebClientResponse in project helidon by oracle.
the class BasicExampleTest method testProtectedDenied.
private void testProtectedDenied(String uri, String username, String password) {
WebClientResponse response = callProtected(uri, username, password);
assertThat(response.status(), is(Http.Status.FORBIDDEN_403));
}
use of io.helidon.webclient.WebClientResponse in project helidon by oracle.
the class MainTest method testTimeout.
@Test
void testTimeout() {
String response = client.get().path("/timeout/50").request(String.class).await(1, TimeUnit.SECONDS);
assertThat(response, is("Slept for 50 ms"));
WebClientResponse clientResponse = client.get().path("/timeout/105").request().await(1, TimeUnit.SECONDS);
response = clientResponse.content().as(String.class).await(1, TimeUnit.SECONDS);
// error handler specified in Main
assertThat(clientResponse.status(), is(Http.Status.REQUEST_TIMEOUT_408));
assertThat(response, is("timeout"));
}
use of io.helidon.webclient.WebClientResponse in project helidon by oracle.
the class MainTest method testCircuitBreaker.
@Test
void testCircuitBreaker() {
String response = client.get().path("/circuitBreaker/true").request(String.class).await(1, TimeUnit.SECONDS);
assertThat(response, is("blocked for 100 millis"));
// error ratio is 20% within 10 request
client.get().path("/circuitBreaker/false").request().await(1, TimeUnit.SECONDS);
// should work after first
response = client.get().path("/circuitBreaker/true").request(String.class).await(1, TimeUnit.SECONDS);
assertThat(response, is("blocked for 100 millis"));
// should open after second
client.get().path("/circuitBreaker/false").request().await(1, TimeUnit.SECONDS);
WebClientResponse clientResponse = client.get().path("/circuitBreaker/true").request().await(1, TimeUnit.SECONDS);
response = clientResponse.content().as(String.class).await(1, TimeUnit.SECONDS);
// registered an error handler in Main
assertThat(clientResponse.status(), is(Http.Status.SERVICE_UNAVAILABLE_503));
assertThat(response, is("circuit breaker"));
}
use of io.helidon.webclient.WebClientResponse in project helidon by oracle.
the class HelidonConnector method applyInternal.
private CompletionStage<ClientResponse> applyInternal(ClientRequest request) {
final WebClientRequestBuilder webClientRequestBuilder = webClient.method(request.getMethod());
webClientRequestBuilder.uri(request.getUri());
webClientRequestBuilder.headers(HelidonStructures.createHeaders(request.getRequestHeaders()));
for (String propertyName : request.getConfiguration().getPropertyNames()) {
Object property = request.getConfiguration().getProperty(propertyName);
if (!propertyName.startsWith("jersey") && String.class.isInstance(property)) {
webClientRequestBuilder.property(propertyName, (String) property);
}
}
for (String propertyName : request.getPropertyNames()) {
Object property = request.resolveProperty(propertyName, Object.class);
if (!propertyName.startsWith("jersey") && String.class.isInstance(property)) {
webClientRequestBuilder.property(propertyName, (String) property);
}
}
// TODO
// HelidonStructures.createProxy(request).ifPresent(webClientRequestBuilder::proxy);
webClientRequestBuilder.followRedirects(request.resolveProperty(ClientProperties.FOLLOW_REDIRECTS, true));
webClientRequestBuilder.readTimeout(request.resolveProperty(ClientProperties.READ_TIMEOUT, 10000), TimeUnit.MILLISECONDS);
CompletionStage<WebClientResponse> responseStage = null;
if (request.hasEntity()) {
responseStage = HelidonEntity.submit(entityType, request, webClientRequestBuilder, executorServiceKeeper.getExecutorService(request));
} else {
responseStage = webClientRequestBuilder.submit();
}
return responseStage.thenCompose((a) -> convertResponse(request, a));
}
use of io.helidon.webclient.WebClientResponse in project helidon by oracle.
the class HelidonEntity method submit.
/**
* Convert Jersey {@code OutputStream} to an entity based on the client request use case and submits to the provided
* {@code WebClientRequestBuilder}.
* @param type the type of the Helidon entity.
* @param requestContext Jersey {@link ClientRequest} providing the entity {@code OutputStream}.
* @param requestBuilder Helidon {@code WebClientRequestBuilder} which is used to submit the entity
* @param executorService {@link ExecutorService} that fills the entity instance for Helidon with data from Jersey
* {@code OutputStream}.
* @return Helidon Client response completion stage.
*/
static CompletionStage<WebClientResponse> submit(HelidonEntityType type, ClientRequest requestContext, WebClientRequestBuilder requestBuilder, ExecutorService executorService) {
CompletionStage<WebClientResponse> stage = null;
if (type != null) {
final int bufferSize = requestContext.resolveProperty(ClientProperties.OUTBOUND_CONTENT_LENGTH_BUFFER, 8192);
switch(type) {
case BYTE_ARRAY_OUTPUT_STREAM:
final ByteArrayOutputStream baos = new ByteArrayOutputStream(bufferSize);
requestContext.setStreamProvider(contentLength -> baos);
((ProcessingRunnable) requestContext::writeEntity).run();
stage = requestBuilder.submit(baos);
break;
case READABLE_BYTE_CHANNEL:
final OutputStreamChannel channel = new OutputStreamChannel(bufferSize);
requestContext.setStreamProvider(contentLength -> channel);
executorService.execute((ProcessingRunnable) requestContext::writeEntity);
stage = requestBuilder.submit(channel);
break;
case OUTPUT_STREAM_MULTI:
final OutputStreamMulti publisher = IoMulti.outputStreamMulti();
requestContext.setStreamProvider(contentLength -> publisher);
executorService.execute((ProcessingRunnable) () -> {
requestContext.writeEntity();
publisher.close();
});
stage = requestBuilder.submit(Multi.create(publisher).map(DataChunk::create));
break;
default:
}
}
return stage;
}
Aggregations