Search in sources :

Example 1 with AsyncResponse

use of com.artipie.http.async.AsyncResponse in project artipie by artipie.

the class DockerRoutingSlice 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 String path = req.uri().getPath();
    final Matcher matcher = PTN_PATH.matcher(path);
    final Response rsp;
    if (matcher.matches()) {
        final String group = matcher.group(1);
        if (group.isEmpty() || group.equals("/")) {
            rsp = new AsyncResponse(this.settings.auth().thenApply(auth -> new BasicAuthSlice(new BaseEntity(), auth, user -> !user.equals(Permissions.ANY_USER)).response(line, headers, body)));
        } else {
            rsp = this.origin.response(new RequestLine(req.method().toString(), new URIBuilder(req.uri()).setPath(group).toString(), req.version()).toString(), new Headers.From(headers, DockerRoutingSlice.HDR_REAL_PATH, path), body);
        }
    } else {
        rsp = this.origin.response(line, headers, body);
    }
    return rsp;
}
Also used : AsyncResponse(com.artipie.http.async.AsyncResponse) BasicAuthSlice(com.artipie.http.auth.BasicAuthSlice) RequestLine(com.artipie.http.rq.RequestLine) Matcher(java.util.regex.Matcher) BaseEntity(com.artipie.docker.http.BaseEntity) RequestLineFrom(com.artipie.http.rq.RequestLineFrom) RequestLineFrom(com.artipie.http.rq.RequestLineFrom) AsyncResponse(com.artipie.http.async.AsyncResponse) URIBuilder(org.apache.http.client.utils.URIBuilder)

Example 2 with AsyncResponse

use of com.artipie.http.async.AsyncResponse 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 AsyncResponse

use of com.artipie.http.async.AsyncResponse in project maven-adapter by artipie.

the class PutMetadataSlice method response.

@Override
public Response response(final String line, final Iterable<Map.Entry<String, String>> headers, final Publisher<ByteBuffer> body) {
    final Response res;
    final Matcher matcher = PutMetadataSlice.PTN_META.matcher(new RequestLineFrom(line).uri().getPath());
    if (matcher.matches()) {
        final Key pkg = new KeyFromPath(matcher.group("pkg"));
        res = new AsyncResponse(new PublisherAs(body).asciiString().thenCombine(this.asto.list(new Key.From(UploadSlice.TEMP, pkg)), (xml, list) -> {
            final Optional<String> snapshot = new DeployMetadata(xml).snapshots().stream().filter(item -> list.stream().anyMatch(key -> key.string().contains(item))).findFirst();
            final Key key;
            if (snapshot.isPresent()) {
                key = new Key.From(UploadSlice.TEMP, pkg.string(), snapshot.get(), PutMetadataSlice.SUB_META, PutMetadataSlice.MAVEN_METADATA);
            } else {
                key = new Key.From(UploadSlice.TEMP, pkg.string(), new DeployMetadata(xml).release(), PutMetadataSlice.SUB_META, PutMetadataSlice.MAVEN_METADATA);
            }
            return this.asto.save(key, new Content.From(xml.getBytes(StandardCharsets.US_ASCII)));
        }).thenApply(nothing -> new RsWithStatus(RsStatus.CREATED)));
    } else {
        res = new RsWithStatus(RsStatus.BAD_REQUEST);
    }
    return res;
}
Also used : PublisherAs(com.artipie.asto.ext.PublisherAs) RequestLineFrom(com.artipie.http.rq.RequestLineFrom) RsWithStatus(com.artipie.http.rs.RsWithStatus) KeyFromPath(com.artipie.http.slice.KeyFromPath) DeployMetadata(com.artipie.maven.metadata.DeployMetadata) Slice(com.artipie.http.Slice) Publisher(org.reactivestreams.Publisher) Response(com.artipie.http.Response) RsStatus(com.artipie.http.rs.RsStatus) Content(com.artipie.asto.Content) Key(com.artipie.asto.Key) ByteBuffer(java.nio.ByteBuffer) StandardCharsets(java.nio.charset.StandardCharsets) PublisherAs(com.artipie.asto.ext.PublisherAs) Matcher(java.util.regex.Matcher) Storage(com.artipie.asto.Storage) Map(java.util.Map) AsyncResponse(com.artipie.http.async.AsyncResponse) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) Optional(java.util.Optional) Matcher(java.util.regex.Matcher) RequestLineFrom(com.artipie.http.rq.RequestLineFrom) Response(com.artipie.http.Response) AsyncResponse(com.artipie.http.async.AsyncResponse) DeployMetadata(com.artipie.maven.metadata.DeployMetadata) KeyFromPath(com.artipie.http.slice.KeyFromPath) RsWithStatus(com.artipie.http.rs.RsWithStatus) RequestLineFrom(com.artipie.http.rq.RequestLineFrom) AsyncResponse(com.artipie.http.async.AsyncResponse) Key(com.artipie.asto.Key)

Example 4 with AsyncResponse

use of com.artipie.http.async.AsyncResponse in project maven-adapter by artipie.

the class PutMetadataChecksumSlice method response.

@Override
public Response response(final String line, final Iterable<Map.Entry<String, String>> headers, final Publisher<ByteBuffer> body) {
    final Matcher matcher = PutMetadataChecksumSlice.PTN.matcher(new RequestLineFrom(line).uri().getPath());
    final Response res;
    if (matcher.matches()) {
        final String alg = matcher.group("alg");
        final String pkg = matcher.group("pkg");
        res = new AsyncResponse(this.findAndSave(body, alg, pkg).thenCompose(key -> {
            final CompletionStage<Response> resp;
            if (key.isPresent() && key.get().parent().isPresent() && key.get().parent().get().parent().isPresent()) {
                final Key location = key.get().parent().get().parent().get();
                // @checkstyle NestedIfDepthCheck (10 lines)
                resp = this.valid.ready(location).thenCompose(ready -> {
                    final CompletionStage<Response> action;
                    if (ready) {
                        action = this.validateAndUpdate(pkg, location);
                    } else {
                        action = CompletableFuture.completedFuture(new RsWithStatus(RsStatus.CREATED));
                    }
                    return action;
                });
            } else {
                resp = CompletableFuture.completedFuture(PutMetadataChecksumSlice.BAD_REQUEST);
            }
            return resp;
        }));
    } else {
        res = new RsWithStatus(RsStatus.BAD_REQUEST);
    }
    return res;
}
Also used : Response(com.artipie.http.Response) AsyncResponse(com.artipie.http.async.AsyncResponse) ContentDigest(com.artipie.asto.ext.ContentDigest) Slice(com.artipie.http.Slice) CompletableFuture(java.util.concurrent.CompletableFuture) Single(io.reactivex.Single) ByteBuffer(java.nio.ByteBuffer) Digests(com.artipie.asto.ext.Digests) Matcher(java.util.regex.Matcher) Storage(com.artipie.asto.Storage) Locale(java.util.Locale) Map(java.util.Map) Observable(io.reactivex.Observable) RequestLineFrom(com.artipie.http.rq.RequestLineFrom) Maven(com.artipie.maven.Maven) RsWithStatus(com.artipie.http.rs.RsWithStatus) Publisher(org.reactivestreams.Publisher) Response(com.artipie.http.Response) RsStatus(com.artipie.http.rs.RsStatus) Content(com.artipie.asto.Content) Key(com.artipie.asto.Key) StandardCharsets(java.nio.charset.StandardCharsets) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) SingleInterop(hu.akarnokd.rxjava2.interop.SingleInterop) ValidUpload(com.artipie.maven.ValidUpload) PublisherAs(com.artipie.asto.ext.PublisherAs) CompletionStage(java.util.concurrent.CompletionStage) RxStorageWrapper(com.artipie.asto.rx.RxStorageWrapper) AsyncResponse(com.artipie.http.async.AsyncResponse) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) RsWithStatus(com.artipie.http.rs.RsWithStatus) Matcher(java.util.regex.Matcher) RequestLineFrom(com.artipie.http.rq.RequestLineFrom) AsyncResponse(com.artipie.http.async.AsyncResponse) Key(com.artipie.asto.Key) CompletionStage(java.util.concurrent.CompletionStage)

Example 5 with AsyncResponse

use of com.artipie.http.async.AsyncResponse in project maven-adapter by artipie.

the class HeadProxySlice method response.

@Override
public Response response(final String line, final Iterable<Map.Entry<String, String>> headers, final Publisher<ByteBuffer> body) {
    final CompletableFuture<Response> promise = new CompletableFuture<>();
    this.client.response(line, Headers.EMPTY, Content.EMPTY).send((status, rsheaders, rsbody) -> {
        promise.complete(new RsWithHeaders(new RsWithStatus(status), rsheaders));
        return CompletableFuture.allOf();
    });
    return new AsyncResponse(promise);
}
Also used : Response(com.artipie.http.Response) AsyncResponse(com.artipie.http.async.AsyncResponse) CompletableFuture(java.util.concurrent.CompletableFuture) RsWithStatus(com.artipie.http.rs.RsWithStatus) RsWithHeaders(com.artipie.http.rs.RsWithHeaders) AsyncResponse(com.artipie.http.async.AsyncResponse)

Aggregations

AsyncResponse (com.artipie.http.async.AsyncResponse)5 Response (com.artipie.http.Response)4 RequestLineFrom (com.artipie.http.rq.RequestLineFrom)4 Matcher (java.util.regex.Matcher)4 Content (com.artipie.asto.Content)3 Key (com.artipie.asto.Key)3 Slice (com.artipie.http.Slice)3 RsWithStatus (com.artipie.http.rs.RsWithStatus)3 ByteBuffer (java.nio.ByteBuffer)3 Map (java.util.Map)3 Optional (java.util.Optional)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 Pattern (java.util.regex.Pattern)3 Publisher (org.reactivestreams.Publisher)3 Storage (com.artipie.asto.Storage)2 Digests (com.artipie.asto.ext.Digests)2 PublisherAs (com.artipie.asto.ext.PublisherAs)2 RsStatus (com.artipie.http.rs.RsStatus)2 KeyFromPath (com.artipie.http.slice.KeyFromPath)2 StandardCharsets (java.nio.charset.StandardCharsets)2