Search in sources :

Example 1 with RsWithBody

use of com.artipie.http.rs.RsWithBody in project artipie by artipie.

the class TrafficMetricSliceTest method updatesMetrics.

@Test
void updatesMetrics() throws Exception {
    final Metrics metrics = new InMemoryMetrics();
    final int req = 100;
    final int res = 50;
    new TrafficMetricSlice((line, head, body) -> new RsWithBody(Flowable.fromPublisher(body).ignoreElements().andThen(Flowable.just(ByteBuffer.wrap(new byte[res])))), metrics).response(new RequestLine(RqMethod.GET, "/foo").toString(), Collections.emptyList(), Flowable.just(ByteBuffer.wrap(new byte[req]))).send((status, headers, body) -> Flowable.fromPublisher(body).ignoreElements().to(CompletableInterop.await())).toCompletableFuture().get();
    final TestMetricsOutput out = new TestMetricsOutput();
    metrics.publish(out);
    MatcherAssert.assertThat("request body size wasn't updated", out.counters().get("request.body.size"), Matchers.equalTo((long) req));
    MatcherAssert.assertThat("response body size wasn't updated", out.counters().get("response.body.size"), Matchers.equalTo((long) res));
}
Also used : RequestLine(com.artipie.http.rq.RequestLine) InMemoryMetrics(com.artipie.metrics.memory.InMemoryMetrics) Metrics(com.artipie.metrics.Metrics) InMemoryMetrics(com.artipie.metrics.memory.InMemoryMetrics) TestMetricsOutput(com.artipie.metrics.memory.TestMetricsOutput) RsWithBody(com.artipie.http.rs.RsWithBody) Test(org.junit.jupiter.api.Test)

Example 2 with RsWithBody

use of com.artipie.http.rs.RsWithBody in project maven-adapter by artipie.

the class CachedProxySlice method response.

@Override
public Response response(final String line, final Iterable<Map.Entry<String, String>> headers, final Publisher<ByteBuffer> body) {
    final RequestLineFrom req = new RequestLineFrom(line);
    final Key key = new KeyFromPath(req.uri().getPath());
    return new AsyncResponse(new RepoHead(this.client).head(req.uri().getPath()).thenCompose(head -> this.cache.load(key, new Remote.WithErrorHandling(() -> {
        final CompletableFuture<Optional<? extends Content>> promise = new CompletableFuture<>();
        this.client.response(line, Headers.EMPTY, Content.EMPTY).send((rsstatus, rsheaders, rsbody) -> {
            final CompletableFuture<Void> term = new CompletableFuture<>();
            if (rsstatus.success()) {
                final Flowable<ByteBuffer> res = Flowable.fromPublisher(rsbody).doOnError(term::completeExceptionally).doOnTerminate(() -> term.complete(null));
                promise.complete(Optional.of(new Content.From(res)));
            } else {
                promise.complete(Optional.empty());
            }
            return term;
        });
        return promise;
    }), new CacheControl.All(StreamSupport.stream(head.orElse(Headers.EMPTY).spliterator(), false).map(Header::new).map(CachedProxySlice::checksumControl).collect(Collectors.toUnmodifiableList()))).handle((content, throwable) -> {
        final Response result;
        if (throwable == null && content.isPresent()) {
            result = new RsWithBody(StandardRs.OK, new Content.From(content.get()));
        } else {
            result = StandardRs.NOT_FOUND;
        }
        return result;
    })));
}
Also used : Header(com.artipie.http.headers.Header) Slice(com.artipie.http.Slice) CompletableFuture(java.util.concurrent.CompletableFuture) Hex(org.apache.commons.codec.binary.Hex) DigestVerification(com.artipie.asto.cache.DigestVerification) RsWithBody(com.artipie.http.rs.RsWithBody) ByteBuffer(java.nio.ByteBuffer) Digests(com.artipie.asto.ext.Digests) Matcher(java.util.regex.Matcher) Flowable(io.reactivex.Flowable) Locale(java.util.Locale) Map(java.util.Map) StreamSupport(java.util.stream.StreamSupport) RequestLineFrom(com.artipie.http.rq.RequestLineFrom) Remote(com.artipie.asto.cache.Remote) KeyFromPath(com.artipie.http.slice.KeyFromPath) Headers(com.artipie.http.Headers) DecoderException(org.apache.commons.codec.DecoderException) Publisher(org.reactivestreams.Publisher) Response(com.artipie.http.Response) Content(com.artipie.asto.Content) Key(com.artipie.asto.Key) Collectors(java.util.stream.Collectors) AsyncResponse(com.artipie.http.async.AsyncResponse) Optional(java.util.Optional) Cache(com.artipie.asto.cache.Cache) Pattern(java.util.regex.Pattern) StandardRs(com.artipie.http.rs.StandardRs) CacheControl(com.artipie.asto.cache.CacheControl) Optional(java.util.Optional) RequestLineFrom(com.artipie.http.rq.RequestLineFrom) ByteBuffer(java.nio.ByteBuffer) RsWithBody(com.artipie.http.rs.RsWithBody) Response(com.artipie.http.Response) AsyncResponse(com.artipie.http.async.AsyncResponse) CompletableFuture(java.util.concurrent.CompletableFuture) KeyFromPath(com.artipie.http.slice.KeyFromPath) Content(com.artipie.asto.Content) RequestLineFrom(com.artipie.http.rq.RequestLineFrom) AsyncResponse(com.artipie.http.async.AsyncResponse) Key(com.artipie.asto.Key)

Example 3 with RsWithBody

use of com.artipie.http.rs.RsWithBody in project maven-adapter by artipie.

the class CachedProxySliceTest method loadsOriginIfCacheNotFound.

@Test
void loadsOriginIfCacheNotFound() {
    final byte[] data = "remote".getBytes();
    MatcherAssert.assertThat(new CachedProxySlice((line, headers, body) -> new RsWithBody(ByteBuffer.wrap(data)), (key, supplier, control) -> supplier.get()), new SliceHasResponse(Matchers.allOf(new RsHasStatus(RsStatus.OK), new RsHasBody(data)), new RequestLine(RqMethod.GET, "/bar")));
}
Also used : RqMethod(com.artipie.http.rq.RqMethod) RsWithStatus(com.artipie.http.rs.RsWithStatus) SliceHasResponse(com.artipie.http.hm.SliceHasResponse) RsHasBody(com.artipie.http.hm.RsHasBody) RsStatus(com.artipie.http.rs.RsStatus) Matchers(org.hamcrest.Matchers) CompletableFuture(java.util.concurrent.CompletableFuture) Content(com.artipie.asto.Content) RsHasStatus(com.artipie.http.hm.RsHasStatus) RsWithBody(com.artipie.http.rs.RsWithBody) FailedCompletionStage(com.artipie.asto.FailedCompletionStage) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test) RequestLine(com.artipie.http.rq.RequestLine) MatcherAssert(org.hamcrest.MatcherAssert) SliceSimple(com.artipie.http.slice.SliceSimple) Optional(java.util.Optional) RequestLine(com.artipie.http.rq.RequestLine) SliceHasResponse(com.artipie.http.hm.SliceHasResponse) RsHasStatus(com.artipie.http.hm.RsHasStatus) RsHasBody(com.artipie.http.hm.RsHasBody) RsWithBody(com.artipie.http.rs.RsWithBody) Test(org.junit.jupiter.api.Test)

Example 4 with RsWithBody

use of com.artipie.http.rs.RsWithBody in project maven-adapter by artipie.

the class CachedProxySliceTest method loadsCachedContent.

@Test
void loadsCachedContent() {
    final byte[] data = "cache".getBytes();
    MatcherAssert.assertThat(new CachedProxySlice((line, headers, body) -> new RsWithBody(ByteBuffer.wrap("123".getBytes())), (key, supplier, control) -> CompletableFuture.supplyAsync(() -> Optional.of(new Content.From(data)))), new SliceHasResponse(Matchers.allOf(new RsHasStatus(RsStatus.OK), new RsHasBody(data)), new RequestLine(RqMethod.GET, "/foo")));
}
Also used : RqMethod(com.artipie.http.rq.RqMethod) RsWithStatus(com.artipie.http.rs.RsWithStatus) SliceHasResponse(com.artipie.http.hm.SliceHasResponse) RsHasBody(com.artipie.http.hm.RsHasBody) RsStatus(com.artipie.http.rs.RsStatus) Matchers(org.hamcrest.Matchers) CompletableFuture(java.util.concurrent.CompletableFuture) Content(com.artipie.asto.Content) RsHasStatus(com.artipie.http.hm.RsHasStatus) RsWithBody(com.artipie.http.rs.RsWithBody) FailedCompletionStage(com.artipie.asto.FailedCompletionStage) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test) RequestLine(com.artipie.http.rq.RequestLine) MatcherAssert(org.hamcrest.MatcherAssert) SliceSimple(com.artipie.http.slice.SliceSimple) Optional(java.util.Optional) RequestLine(com.artipie.http.rq.RequestLine) SliceHasResponse(com.artipie.http.hm.SliceHasResponse) RsHasStatus(com.artipie.http.hm.RsHasStatus) RsHasBody(com.artipie.http.hm.RsHasBody) RsWithBody(com.artipie.http.rs.RsWithBody) Test(org.junit.jupiter.api.Test)

Aggregations

RsWithBody (com.artipie.http.rs.RsWithBody)4 Content (com.artipie.asto.Content)3 RequestLine (com.artipie.http.rq.RequestLine)3 ByteBuffer (java.nio.ByteBuffer)3 Optional (java.util.Optional)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 Test (org.junit.jupiter.api.Test)3 FailedCompletionStage (com.artipie.asto.FailedCompletionStage)2 RsHasBody (com.artipie.http.hm.RsHasBody)2 RsHasStatus (com.artipie.http.hm.RsHasStatus)2 SliceHasResponse (com.artipie.http.hm.SliceHasResponse)2 RqMethod (com.artipie.http.rq.RqMethod)2 RsStatus (com.artipie.http.rs.RsStatus)2 RsWithStatus (com.artipie.http.rs.RsWithStatus)2 SliceSimple (com.artipie.http.slice.SliceSimple)2 Key (com.artipie.asto.Key)1 Cache (com.artipie.asto.cache.Cache)1 CacheControl (com.artipie.asto.cache.CacheControl)1 DigestVerification (com.artipie.asto.cache.DigestVerification)1 Remote (com.artipie.asto.cache.Remote)1