Search in sources :

Example 1 with Publisher

use of java.util.concurrent.Flow.Publisher in project julian-http-client by ljtfreitas.

the class DefaultHTTPClientResponse method valueOf.

static DefaultHTTPClientResponse valueOf(HttpResponse<Publisher<List<ByteBuffer>>> response) {
    HTTPStatus status = HTTPStatusCode.select(response.statusCode()).map(HTTPStatus::new).orElseGet(() -> HTTPStatus.valueOf(response.statusCode()));
    HTTPHeaders headers = response.headers().map().entrySet().stream().map(e -> HTTPHeader.create(e.getKey(), e.getValue())).reduce(HTTPHeaders.empty(), HTTPHeaders::join, (a, b) -> b);
    HTTPResponseBody body = HTTPResponseBody.optional(status, headers, () -> HTTPResponseBody.lazy(response.body()));
    return new DefaultHTTPClientResponse(status, headers, body);
}
Also used : HTTPStatusCode(com.github.ljtfreitas.julian.http.HTTPStatusCode) Response(com.github.ljtfreitas.julian.Response) List(java.util.List) Publisher(java.util.concurrent.Flow.Publisher) HTTPHeader(com.github.ljtfreitas.julian.http.HTTPHeader) HTTPHeaders(com.github.ljtfreitas.julian.http.HTTPHeaders) HTTPStatus(com.github.ljtfreitas.julian.http.HTTPStatus) Optional(java.util.Optional) Function(java.util.function.Function) ByteBuffer(java.nio.ByteBuffer) HTTPResponseBody(com.github.ljtfreitas.julian.http.HTTPResponseBody) HttpResponse(java.net.http.HttpResponse) HTTPResponseBody(com.github.ljtfreitas.julian.http.HTTPResponseBody) HTTPHeaders(com.github.ljtfreitas.julian.http.HTTPHeaders) HTTPStatus(com.github.ljtfreitas.julian.http.HTTPStatus)

Example 2 with Publisher

use of java.util.concurrent.Flow.Publisher in project helidon by oracle.

the class ReadableByteChannelPublisherTest method negativeDelay.

@Test
void negativeDelay() throws Exception {
    PeriodicalChannel pc = createChannelWithNoAvailableData(10, 1);
    RetrySchema schema = (i, delay) -> i >= 3 ? -10 : 0;
    ReadableByteChannelPublisher publisher = new ReadableByteChannelPublisher(pc, schema);
    // assert
    try {
        ContentReaders.readBytes(publisher).get(5, TimeUnit.SECONDS);
        fail("Did not throw expected ExecutionException!");
    } catch (ExecutionException e) {
        assertThat(e.getCause(), instanceOf(TimeoutException.class));
    }
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.fail(org.junit.jupiter.api.Assertions.fail) Subscription(java.util.concurrent.Flow.Subscription) DataChunk(io.helidon.common.http.DataChunk) TimeoutException(java.util.concurrent.TimeoutException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Disabled(org.junit.jupiter.api.Disabled) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteBuffer(java.nio.ByteBuffer) CoreMatchers.notNullValue(org.hamcrest.CoreMatchers.notNullValue) HashSet(java.util.HashSet) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) Publisher(java.util.concurrent.Flow.Publisher) LazyValue(io.helidon.common.LazyValue) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Single(io.helidon.common.reactive.Single) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) CoreMatchers.nullValue(org.hamcrest.CoreMatchers.nullValue) IntFunction(java.util.function.IntFunction) ReadableByteChannel(java.nio.channels.ReadableByteChannel) ClosedChannelException(java.nio.channels.ClosedChannelException) Set(java.util.Set) IOException(java.io.IOException) CompletionException(java.util.concurrent.CompletionException) RetrySchema(io.helidon.common.reactive.RetrySchema) StandardCharsets(java.nio.charset.StandardCharsets) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) Subscriber(java.util.concurrent.Flow.Subscriber) RetrySchema(io.helidon.common.reactive.RetrySchema) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test)

Example 3 with Publisher

use of java.util.concurrent.Flow.Publisher in project helidon by oracle.

the class BodyPartBodyStreamReader method read.

@Override
@SuppressWarnings("unchecked")
public <U extends ReadableBodyPart> Publisher<U> read(Publisher<DataChunk> publisher, GenericType<U> type, MessageBodyReaderContext context) {
    String boundary = null;
    MediaType contentType = context.contentType().orElse(null);
    if (contentType != null) {
        boundary = contentType.parameters().get("boundary");
    }
    if (boundary == null) {
        throw new IllegalStateException("boudary header is missing");
    }
    MultiPartDecoder decoder = MultiPartDecoder.create(boundary, context);
    publisher.subscribe(decoder);
    return (Publisher<U>) decoder;
}
Also used : MediaType(io.helidon.common.http.MediaType) Publisher(java.util.concurrent.Flow.Publisher)

Example 4 with Publisher

use of java.util.concurrent.Flow.Publisher in project helidon by oracle.

the class JsonpBodyStreamWriter method write.

@Override
public Multi<DataChunk> write(Publisher<? extends JsonStructure> publisher, GenericType<? extends JsonStructure> type, MessageBodyWriterContext context) {
    MediaType contentType = context.findAccepted(MediaType.JSON_PREDICATE, MediaType.APPLICATION_JSON);
    context.contentType(contentType);
    // we do not have join operator
    AtomicBoolean first = new AtomicBoolean(true);
    JsonStructureToChunks jsonToChunks = new JsonStructureToChunks(true, jsonWriterFactory, context.charset());
    return Multi.create(publisher).map(jsonToChunks).flatMap(it -> {
        if (first.getAndSet(false)) {
            // first record, do not prepend a comma
            return Multi.just(DataChunk.create(ARRAY_JSON_BEGIN_BYTES), it);
        } else {
            // any subsequent record starts with a comma
            return Multi.just(DataChunk.create(COMMA_BYTES), it);
        }
    }).onCompleteResume(DataChunk.create(ARRAY_JSON_END_BYTES));
}
Also used : JsonWriterFactory(jakarta.json.JsonWriterFactory) JsonStructure(jakarta.json.JsonStructure) Publisher(java.util.concurrent.Flow.Publisher) DataChunk(io.helidon.common.http.DataChunk) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MessageBodyStreamWriter(io.helidon.media.common.MessageBodyStreamWriter) GenericType(io.helidon.common.GenericType) JsonStructureToChunks(io.helidon.media.jsonp.JsonpBodyWriter.JsonStructureToChunks) StandardCharsets(java.nio.charset.StandardCharsets) Multi(io.helidon.common.reactive.Multi) MessageBodyWriterContext(io.helidon.media.common.MessageBodyWriterContext) MediaType(io.helidon.common.http.MediaType) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MediaType(io.helidon.common.http.MediaType) JsonStructureToChunks(io.helidon.media.jsonp.JsonpBodyWriter.JsonStructureToChunks)

Aggregations

Publisher (java.util.concurrent.Flow.Publisher)4 DataChunk (io.helidon.common.http.DataChunk)2 MediaType (io.helidon.common.http.MediaType)2 ByteBuffer (java.nio.ByteBuffer)2 StandardCharsets (java.nio.charset.StandardCharsets)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Response (com.github.ljtfreitas.julian.Response)1 HTTPHeader (com.github.ljtfreitas.julian.http.HTTPHeader)1 HTTPHeaders (com.github.ljtfreitas.julian.http.HTTPHeaders)1 HTTPResponseBody (com.github.ljtfreitas.julian.http.HTTPResponseBody)1 HTTPStatus (com.github.ljtfreitas.julian.http.HTTPStatus)1 HTTPStatusCode (com.github.ljtfreitas.julian.http.HTTPStatusCode)1 GenericType (io.helidon.common.GenericType)1 LazyValue (io.helidon.common.LazyValue)1 Multi (io.helidon.common.reactive.Multi)1 RetrySchema (io.helidon.common.reactive.RetrySchema)1 Single (io.helidon.common.reactive.Single)1 MessageBodyStreamWriter (io.helidon.media.common.MessageBodyStreamWriter)1 MessageBodyWriterContext (io.helidon.media.common.MessageBodyWriterContext)1 JsonStructureToChunks (io.helidon.media.jsonp.JsonpBodyWriter.JsonStructureToChunks)1