use of com.artipie.http.Response in project artipie by artipie.
the class ResponseMetricsSliceTest method shouldForwardResponseUnmodified.
@Test
void shouldForwardResponseUnmodified() {
final Header rsheader = new Header("header2", "value2");
final byte[] body = "piece of code".getBytes();
final RsStatus rsstatus = RsStatus.CREATED;
final Response response = new ResponseMetricsSlice((rsline, rsheaders, rsbody) -> new RsFull(rsstatus, new Headers.From(rsheader), Flowable.just(ByteBuffer.wrap(body))), this.metrics).response(new RequestLine(RqMethod.PUT, "/").toString(), Headers.EMPTY, Flowable.empty());
MatcherAssert.assertThat(response, new ResponseMatcher(rsstatus, body, rsheader));
}
use of com.artipie.http.Response in project artipie by artipie.
the class ContentLengthRestrictionTest method shouldNotPassRequestsAboveLimit.
@Test
public void shouldNotPassRequestsAboveLimit() {
final int limit = 10;
final Slice slice = new ContentLengthRestriction((line, headers, body) -> new RsWithStatus(RsStatus.OK), limit);
final Response response = slice.response("", this.headers("11"), Flowable.empty());
MatcherAssert.assertThat(response, new RsHasStatus(RsStatus.PAYLOAD_TOO_LARGE));
}
use of com.artipie.http.Response in project artipie by artipie.
the class MeasuredSlice method debugResponse.
/**
* Debug response.
* @param line Request line
* @param headers Headers
* @param body Body
* @return Response
*/
private Response debugResponse(final String line, final Iterable<Map.Entry<String, String>> headers, final Publisher<ByteBuffer> body) {
final long start = System.nanoTime();
final StringBuilder message = new StringBuilder();
message.append(line).append(": ");
final Response response = this.origin.response(line, headers, body);
message.append(String.format("response=%s ", millisMessage(start)));
return new MeasuredResponse(response, message, start);
}
use of com.artipie.http.Response 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;
})));
}
use of com.artipie.http.Response 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;
}
Aggregations