Here are the examples of the java api com.artipie.asto.Key taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
131 Examples
19
Source : StorageHasRepoMd.java
with MIT License
from artipie
with MIT License
from artipie
/**
* Matcher for checking rempomd.xml file presence and information in the storage.
*
* @since 1.1
*/
public final clreplaced StorageHasRepoMd extends AllOf<Storage> {
/**
* Repodata key.
*/
private static final Key BASE = new Key.From("repodata");
/**
* Repomd rey.
*/
private static final Key.From REPOMD = new Key.From(StorageHasRepoMd.BASE, "repomd.xml");
/**
* Ctor.
* @param config Rmp repo config
*/
public StorageHasRepoMd(final RepoConfig config) {
super(matchers(config));
}
/**
* List of matchers.
* @param config Rmp repo config
* @return Matchers list
*/
private static List<Matcher<? super Storage>> matchers(final RepoConfig config) {
final List<Matcher<? super Storage>> res = new ArrayList<>(4);
res.add(new MatcherOf<Storage>(storage -> storage.exists(StorageHasRepoMd.REPOMD).join(), new FormattedText("Repomd is present")));
new XmlPackage.Stream(config.filelists()).get().forEach(pckg -> res.add(new MatcherOf<Storage>(storage -> hasRecord(storage, pckg, config.digest()), new FormattedText("Repomd has record for %s xml", pckg.name()))));
return res;
}
/**
* Has repomd record for xml metadata package?
* @param storage Storage
* @param pckg Metadata package
* @param digest Digest algorithm
* @return True if record is present
*/
private static boolean hasRecord(final Storage storage, final XmlPackage pckg, final Digest digest) {
final Optional<Content> repomd = storage.list(StorageHasRepoMd.BASE).join().stream().filter(item -> item.string().contains(pckg.filename())).findFirst().map(item -> storage.value(new Key.From(item)).join());
boolean res = false;
if (repomd.isPresent()) {
final String checksum = new ContentDigest(repomd.get(), digest::messageDigest).hex().toCompletableFuture().join();
res = !new XMLDoreplacedent(new PublisherAs(storage.value(StorageHasRepoMd.REPOMD).join()).asciiString().toCompletableFuture().join()).nodes(String.format(// @checkstyle LineLengthCheck (1 line)
"/*[name()='repomd']/*[name()='data' and @type='%s']/*[name()='checksum' and @type='%s' and text()='%s']", pckg.name().toLowerCase(Locale.US), digest.type(), checksum)).isEmpty();
}
return res;
}
}
19
Source : Rpm.java
with MIT License
from artipie
with MIT License
from artipie
/**
* Update the meta info for single artifact.
*
* @param key The name of the file just updated
* @return Completion or error signal.
* @deprecated This method calls {@link #batchUpdate(Key)} with parent of the key
*/
@Deprecated
public Completable update(final Key key) {
final String[] parts = key.string().split("/");
final Key folder;
if (parts.length == 1) {
folder = Key.ROOT;
} else {
folder = new Key.From(Arrays.stream(parts).limit(parts.length - 1).toArray(String[]::new));
}
return this.batchUpdate(folder);
}
19
Source : Rpm.java
with MIT License
from artipie
with MIT License
from artipie
/**
* Removes old metadata.
* @param preserve Metadata to keep
* @param prefix Repo prefix
* @return Completable
*/
private Completable removeOldMetadata(final Set<String> preserve, final Key prefix) {
return new RxStorageWrapper(this.storage).list(new Key.From(prefix, "repodata")).flatMapObservable(Observable::fromIterable).filter(item -> !preserve.contains(Paths.get(item.string()).getFileName().toString())).flatMapCompletable(item -> new RxStorageWrapper(this.storage).delete(item));
}
19
Source : Rpm.java
with MIT License
from artipie
with MIT License
from artipie
/**
* Moves repodata to storage.
* @param local Local storage
* @param path Metadata to move
* @param prefix Repo prefix
* @return Metadata path
*/
private Single<Path> moveRepodataToStorage(final Storage local, final Path path, final Key prefix) {
return new RxStorageWrapper(local).value(new Key.From(path.getFileName().toString())).flatMapCompletable(content -> new RxStorageWrapper(new SubStorage(prefix, this.storage)).save(new Key.From("repodata", path.getFileName().toString()), content)).toSingleDefault(path);
}
19
Source : SearchSlice.java
with MIT License
from artipie
with MIT License
from artipie
/**
* Copy artifact to the temp storage.
* @param artifact Artifact key to copy
* @param temp Temp dir
* @return Path of the temp file
*/
private CompletableFuture<Path> tempArtifact(final Key artifact, final Path temp) {
return new Copy(this.storage, new ListOf<>(artifact)).copy(new FileStorage(temp)).thenApply(nothing -> temp.resolve(artifact.string()));
}
19
Source : FromStorageCacheTest.java
with MIT License
from artipie
with MIT License
from artipie
@Test
void loadsFromCache() throws Exception {
final Key key = new Key.From("key1");
final byte[] data = "hello1".getBytes();
new BlockingStorage(this.storage).save(key, data);
Matcherreplacedert.replacedertThat(new FromStorageCache(this.storage).load(key, new Remote.Failed(new IllegalStateException("Failing remote 1")), CacheControl.Standard.ALWAYS).toCompletableFuture().get().get(), new ContentIs(data));
}
19
Source : S3Storage.java
with MIT License
from artipie
with MIT License
from artipie
/**
* Uploads content using put request.
*
* @param key Object key.
* @param content Object content to be uploaded.
* @return Completion stage which is completed when response received from S3.
*/
private CompletableFuture<Void> put(final Key key, final Content content) {
return this.client.putObject(PutObjectRequest.builder().bucket(this.bucket).key(key.string()).build(), new ContentBody(content)).thenApply(ignored -> null);
}
19
Source : S3Storage.java
with MIT License
from artipie
with MIT License
from artipie
@Override
public <T> CompletionStage<T> exclusively(final Key key, final Function<Storage, CompletionStage<T>> operation) {
return new UnderLockOperation<>(new StorageLock(this, key), operation).perform(this);
}
19
Source : S3Storage.java
with MIT License
from artipie
with MIT License
from artipie
@Override
public CompletableFuture<Void> delete(final Key key) {
return this.exists(key).thenCompose(exists -> {
final CompletionStage<Void> deleted;
if (exists) {
deleted = this.client.deleteObject(DeleteObjectRequest.builder().bucket(this.bucket).key(key.string()).build()).thenCompose(response -> CompletableFuture.allOf());
} else {
deleted = new FailedCompletionStage<>(new IllegalArgumentException(String.format("Key does not exist: %s", key)));
}
return deleted;
});
}
19
Source : S3Storage.java
with MIT License
from artipie
with MIT License
from artipie
@Override
public CompletableFuture<Void> move(final Key source, final Key destination) {
return this.client.copyObject(CopyObjectRequest.builder().copySource(String.format("%s/%s", this.bucket, source.string())).bucket(this.bucket).key(destination.string()).build()).thenCompose(copied -> this.client.deleteObject(DeleteObjectRequest.builder().bucket(this.bucket).key(source.string()).build()).thenCompose(deleted -> CompletableFuture.allOf()));
}
19
Source : S3Storage.java
with MIT License
from artipie
with MIT License
from artipie
@Override
public CompletableFuture<Void> save(final Key key, final Content content) {
final CompletionStage<Content> result;
final Content onetime = new Content.OneTime(content);
if (this.multipart) {
result = new EstimatedContentCompliment(onetime, S3Storage.MIN_MULTIPART).estimate();
} else {
result = new EstimatedContentCompliment(onetime).estimate();
}
return result.thenCompose(estimated -> {
final CompletionStage<Void> res;
if (this.multipart && estimated.size().filter(x -> x > S3Storage.MIN_MULTIPART).isPresent()) {
res = this.putMultipart(key, estimated);
} else {
res = this.put(key, estimated);
}
return res;
}).toCompletableFuture();
}
19
Source : MultipartUpload.java
with MIT License
from artipie
with MIT License
from artipie
/**
* Multipart upload of S3 object.
*
* @since 0.15
*/
final clreplaced MultipartUpload {
/**
* Minimum part size. See https://docs.aws.amazon.com/AmazonS3/latest/dev/qfacts.html
*/
private static final long MIN_PART_SIZE = 5 * 1024 * 1024;
/**
* Bucket.
*/
private final Bucket bucket;
/**
* S3 object key.
*/
private final Key key;
/**
* ID of this upload.
*/
private final String id;
/**
* Ctor.
*
* @param bucket Bucket.
* @param key S3 object key.
* @param id ID of this upload.
*/
MultipartUpload(final Bucket bucket, final Key key, final String id) {
this.bucket = bucket;
this.key = key;
this.id = id;
}
/**
* Uploads all content by parts.
*
* @param content Object content to be uploaded in parts.
* @return Completion stage which is completed when responses received from S3 for all parts.
*/
public CompletionStage<Void> upload(final Content content) {
final AtomicInteger part = new AtomicInteger();
return Flowable.fromPublisher(content).compose(FlowableTransformers.bufferWhile(new Predicate<ByteBuffer>() {
private long sum;
@Override
public boolean test(final ByteBuffer buffer) {
final int length = buffer.remaining();
final boolean keep;
if (this.sum + length > MultipartUpload.MIN_PART_SIZE) {
this.sum = length;
keep = false;
} else {
this.sum += length;
keep = true;
}
return keep;
}
})).map(chunk -> this.uploadPart(part.incrementAndGet(), Flowable.fromIterable(chunk)).<Void>thenApply(ignored -> null)).reduce(CompletableFuture.allOf(), (acc, stage) -> acc.thenCompose(o -> stage)).to(SingleInterop.get()).toCompletableFuture().thenCompose(Function.idenreplacedy());
}
/**
* Completes the upload.
*
* @return Completion stage which is completed when success response received from S3.
*/
public CompletionStage<Void> complete() {
return this.bucket.completeMultipartUpload(CompleteMultipartUploadRequest.builder().key(this.key.string()).uploadId(this.id).build()).thenApply(ignored -> null);
}
/**
* Aborts the upload.
*
* @return Completion stage which is completed when success response received from S3.
*/
public CompletionStage<Void> abort() {
return this.bucket.abortMultipartUpload(AbortMultipartUploadRequest.builder().key(this.key.string()).uploadId(this.id).build()).thenApply(ignored -> null);
}
/**
* Uploads part.
*
* @param part Part number.
* @param content Part content to be uploaded.
* @return Completion stage which is completed when success response received from S3.
*/
private CompletionStage<UploadPartResponse> uploadPart(final int part, final Publisher<ByteBuffer> content) {
return Observable.fromPublisher(content).reduce(0L, (total, buf) -> total + buf.remaining()).to(SingleInterop.get()).toCompletableFuture().thenCompose(length -> this.bucket.uploadPart(UploadPartRequest.builder().key(this.key.string()).uploadId(this.id).partNumber(part).contentLength(length).build(), AsyncRequestBody.fromPublisher(content)));
}
}
19
Source : KeyLastPart.java
with MIT License
from artipie
with MIT License
from artipie
/**
* Last part of the storage {@link com.artipie.asto.Key}.
* @since 0.24
*/
public final clreplaced KeyLastPart {
/**
* Origin key.
*/
private final Key origin;
/**
* Ctor.
* @param origin Key
*/
public KeyLastPart(final Key origin) {
this.origin = origin;
}
/**
* Get last part of the key.
* @return Key last part as string
*/
public String get() {
final String[] parts = this.origin.string().replaceAll("/$", "").split("/");
return parts[parts.length - 1];
}
}
19
Source : BlockingStorage.java
with MIT License
from artipie
with MIT License
from artipie
/**
* Obtain value for the specified key.
*
* @param key The key
* @return Value replacedociated with the key
*/
public byte[] value(final Key key) {
return new Remaining(this.storage.value(key).thenApplyAsync(pub -> new Concatenation(pub).single().blockingGet()).join(), true).bytes();
}
19
Source : BlockingStorage.java
with MIT License
from artipie
with MIT License
from artipie
/**
* Get value size.
*
* @param key The key of value.
* @return Size of value in bytes.
*/
public long size(final Key key) {
return this.storage.size(key).join();
}
19
Source : BlockingStorage.java
with MIT License
from artipie
with MIT License
from artipie
/**
* Save the content.
*
* @param key The key
* @param content The content
*/
public void save(final Key key, final byte[] content) {
this.storage.save(key, new Content.From(content)).join();
}
19
Source : BlockingStorage.java
with MIT License
from artipie
with MIT License
from artipie
/**
* This file exists?
*
* @param key The key (file name)
* @return TRUE if exists, FALSE otherwise
*/
public boolean exists(final Key key) {
return this.storage.exists(key).join();
}
19
Source : BlockingStorage.java
with MIT License
from artipie
with MIT License
from artipie
/**
* Moves value from one location to another.
*
* @param source Source key.
* @param destination Destination key.
*/
public void move(final Key source, final Key destination) {
this.storage.move(source, destination).join();
}
19
Source : BlockingStorage.java
with MIT License
from artipie
with MIT License
from artipie
/**
* Removes value from storage. Fails if value does not exist.
*
* @param key Key for value to be deleted.
*/
public void delete(final Key key) {
this.storage.delete(key).join();
}
19
Source : BlockingStorage.java
with MIT License
from artipie
with MIT License
from artipie
/**
* Return the list of keys that start with this prefix, for
* example "foo/bar/".
*
* @param prefix The prefix.
* @return Collection of relative keys.
*/
public Collection<Key> list(final Key prefix) {
return this.storage.list(prefix).join();
}
19
Source : StorageYamlConfig.java
with MIT License
from artipie
with MIT License
from artipie
/**
* SubStorage with specified prefix.
* @param prefix Storage prefix
* @return SubStorage with specified prefix.
*/
public Storage subStorage(final Key prefix) {
return new SubStorage(prefix, new LoggingStorage(Level.INFO, this.storage()));
}
18
Source : Rpm.java
with MIT License
from artipie
with MIT License
from artipie
/**
* Copies rpms to local storage and constacts {@link FilePackage} instance.
* @param prefix Repo prefix
* @param tmpdir Tempdir
* @param local Local storage
* @return Flowable of FilePackage
*/
private Flowable<FilePackage> filePackageFromRpm(final Key prefix, final Path tmpdir, final Storage local) {
return SingleInterop.fromFuture(this.storage.list(prefix)).flatMapPublisher(Flowable::fromIterable).filter(key -> key.string().endsWith(".rpm")).flatMapSingle(key -> {
final String filename;
if (key.equals(prefix)) {
filename = key.string();
} else {
filename = key.string().replaceFirst(prefix.string(), "").replaceFirst("^/", "");
}
return new RxStorageWrapper(this.storage).value(key).flatMapCompletable(content -> new RxStorageWrapper(local).save(new Key.From(filename), content)).andThen(Single.fromCallable(() -> new FilePackage(tmpdir.resolve(filename), filename)));
});
}
18
Source : SliceIndex.java
with MIT License
from artipie
with MIT License
from artipie
@Override
public Response response(final String line, final Iterable<Map.Entry<String, String>> headers, final Publisher<ByteBuffer> publisher) {
final Key rqkey = new KeyFromPath(new RequestLineFrom(line).uri().toString());
final String prefix = new RequestLinePrefix(rqkey.string(), headers).get();
return new AsyncResponse(SingleInterop.fromFuture(this.storage.list(rqkey)).flatMapPublisher(Flowable::fromIterable).flatMapSingle(key -> Single.fromFuture(this.storage.value(key).thenCompose(value -> new ContentDigest(value, Digests.SHA256).hex()).thenApply(hex -> String.format("<a href=\"%s#sha256=%s\">%s</a><br/>", String.format("%s/%s", prefix, key.string()), hex, new KeyLastPart(key).get())))).collect(StringBuilder::new, StringBuilder::append).<Response>map(resp -> new RsWithBody(new RsWithHeaders(new RsWithStatus(RsStatus.OK), new ContentType("text/html")), String.format("<!DOCTYPE html>\n<html>\n </body>\n%s\n</body>\n</html>", resp.toString()), StandardCharsets.UTF_8)));
}
18
Source : ProxySlice.java
with MIT License
from artipie
with MIT License
from artipie
/**
* Obtains key from request line with names normalization.
* @param line Request line
* @return Instance of {@link Key}.
*/
private static Key keyFromPath(final String line) {
final URI uri = new RequestLineFrom(line).uri();
Key res = new KeyFromPath(uri.getPath());
if (!uri.toString().matches(ProxySlice.FORMATS)) {
final String last = new KeyLastPart(res).get();
res = new Key.From(res.string().replaceAll(String.format("%s$", last), new NormalizedProjectName.Simple(last).value()));
}
return res;
}
18
Source : Versions.java
with MIT License
from artipie
with MIT License
from artipie
/**
* Saves binary content to storage.
*
* @param storage Storage to use for saving.
* @param key Key to store data at.
* @return Completion of save operation.
*/
public CompletableFuture<Void> save(final Storage storage, final Key key) {
try {
return storage.save(key, new Content.From(this.content.read()));
} catch (final IOException ex) {
throw new UncheckedIOException(ex);
}
}
18
Source : SliceDeleteTest.java
with MIT License
from artipie
with MIT License
from artipie
@Test
void deleteCorrectEntry() throws Exception {
final Key key = new Key.From("foo");
final Key another = new Key.From("bar");
new BlockingStorage(this.storage).save(key, "anything".getBytes());
new BlockingStorage(this.storage).save(another, "another".getBytes());
Matcherreplacedert.replacedertThat("Didn't respond with NO_CONTENT status", new SliceDelete(this.storage), new SliceHasResponse(new RsHreplacedtatus(RsStatus.NO_CONTENT), new RequestLine(RqMethod.DELETE, "/foo")));
Matcherreplacedert.replacedertThat("Didn't delete from storage", new BlockingStorage(this.storage).exists(key), new IsEqual<>(false));
Matcherreplacedert.replacedertThat("Deleted another key", new BlockingStorage(this.storage).exists(another), new IsEqual<>(true));
}
18
Source : StorageLockTest.java
with MIT License
from artipie
with MIT License
from artipie
@Test
void shouldRemoveProposalOnRelease() {
final String uuid = UUID.randomUUID().toString();
final Key proposal = new Key.From(new Proposals.RootKey(this.target), uuid);
this.storage.save(proposal, Content.EMPTY).toCompletableFuture().join();
new StorageLock(this.storage, this.target, uuid, Optional.empty()).release().toCompletableFuture().join();
Matcherreplacedert.replacedertThat(this.storage.exists(proposal).toCompletableFuture().join(), new IsEqual<>(false));
}
18
Source : FromStorageCacheTest.java
with MIT License
from artipie
with MIT License
from artipie
@Test
void savesToCacheFromRemote() throws Exception {
final Key key = new Key.From("key2");
final byte[] data = "hello2".getBytes();
final FromStorageCache cache = new FromStorageCache(this.storage);
final Content load = cache.load(key, () -> CompletableFuture.supplyAsync(() -> Optional.of(new Content.From(data))), CacheControl.Standard.ALWAYS).toCompletableFuture().get().get();
Matcherreplacedert.replacedertThat("Cache returned broken remote content", load, new ContentIs(data));
Matcherreplacedert.replacedertThat("Cache didn't save remote content locally", cache.load(key, new Remote.Failed(new IllegalStateException("Failing remote 1")), CacheControl.Standard.ALWAYS).toCompletableFuture().get().get(), new ContentIs(data));
}
18
Source : FromStorageCacheTest.java
with MIT License
from artipie
with MIT License
from artipie
@Test
void dontCacheFailedRemote() throws Exception {
final Key key = new Key.From("key3");
final AtomicInteger cnt = new AtomicInteger();
new FromStorageCache(this.storage).load(key, () -> CompletableFuture.supplyAsync(() -> Optional.of(new Content.From(Flowable.generate(emitter -> {
if (cnt.incrementAndGet() < 3) {
emitter.onNext(ByteBuffer.allocate(4));
} else {
emitter.onError(new Exception("Error!"));
}
})))), CacheControl.Standard.ALWAYS).exceptionally(err -> {
Logger.info(this, "Handled error: %s", err.getMessage());
return null;
}).toCompletableFuture().get();
Matcherreplacedert.replacedertThat(new BlockingStorage(this.storage).exists(key), Matchers.is(false));
}
18
Source : FromRemoteCacheTest.java
with MIT License
from artipie
with MIT License
from artipie
@Test
void obtainsItemFromRemoteAndCaches() {
final byte[] content = "123".getBytes();
final Key key = new Key.From("item");
Matcherreplacedert.replacedertThat("Returns content from remote", new PublisherAs(this.cache.load(key, () -> CompletableFuture.completedFuture(Optional.of(new Content.From(content))), CacheControl.Standard.ALWAYS).toCompletableFuture().join().get()).bytes().toCompletableFuture().join(), new IsEqual<>(content));
Matcherreplacedert.replacedertThat("Saves to storage", new PublisherAs(this.storage.value(key).join()).bytes().toCompletableFuture().join(), new IsEqual<>(content));
}
18
Source : FromRemoteCacheTest.java
with MIT License
from artipie
with MIT License
from artipie
@Test
void obtainsItemFromCacheIfRemoteValueIsAbsent() {
final byte[] content = "765".getBytes();
final Key key = new Key.From("key");
this.storage.save(key, new Content.From(content)).join();
Matcherreplacedert.replacedertThat("Returns content from cache", new PublisherAs(this.cache.load(key, () -> CompletableFuture.completedFuture(Optional.empty()), CacheControl.Standard.ALWAYS).toCompletableFuture().join().get()).bytes().toCompletableFuture().join(), new IsEqual<>(content));
}
18
Source : FromRemoteCacheTest.java
with MIT License
from artipie
with MIT License
from artipie
@Test
void loadsFromCacheWhenObtainFromRemoteFailed() {
final byte[] content = "098".getBytes();
final Key key = new Key.From("some");
this.storage.save(key, new Content.From(content)).join();
Matcherreplacedert.replacedertThat("Returns content from storage", new PublisherAs(this.cache.load(key, new Remote.Failed(new IOException("IO error")), CacheControl.Standard.ALWAYS).toCompletableFuture().join().get()).bytes().toCompletableFuture().join(), new IsEqual<>(content));
}
18
Source : TestResource.java
with MIT License
from artipie
with MIT License
from artipie
/**
* Adds files from resources (specified folder) to storage, storage items keys are constructed
* from the `base` key and filename.
* @param storage Where to save
* @param base Base key
*/
public void addFilesTo(final Storage storage, final Key base) {
final Storage resources = new FileStorage(this.asPath());
resources.list(Key.ROOT).thenCompose(keys -> CompletableFuture.allOf(keys.stream().map(Key::string).map(item -> resources.value(new Key.From(item)).thenCompose(content -> storage.save(new Key.From(base, item), content))).toArray(CompletableFuture[]::new))).join();
}
18
Source : TestResource.java
with MIT License
from artipie
with MIT License
from artipie
/**
* Reads recourse and saves it to storage by given key.
* @param storage Where to save
* @param key Key to save by
*/
public void saveTo(final Storage storage, final Key key) {
storage.save(key, new Content.From(this.asBytes())).join();
}
18
Source : Proposals.java
with MIT License
from artipie
with MIT License
from artipie
/**
* Proposals for acquiring storage lock.
*
* @since 0.24
*/
final clreplaced Proposals {
/**
* Storage.
*/
private final Storage storage;
/**
* Target key.
*/
private final Key target;
/**
* Ctor.
*
* @param storage Storage.
* @param target Target key.
*/
Proposals(final Storage storage, final Key target) {
this.storage = storage;
this.target = target;
}
/**
* Create proposal with specified UUID.
*
* @param uuid UUID.
* @param expiration Expiration time.
* @return Completion of proposal create operation.
*/
public CompletionStage<Void> create(final String uuid, final Optional<Instant> expiration) {
return this.storage.save(this.proposalKey(uuid), expiration.<Content>map(instant -> new Content.From(instant.toString().getBytes(StandardCharsets.US_ASCII))).orElse(Content.EMPTY));
}
/**
* Check that there is single proposal with specified UUID.
*
* @param uuid UUID.
* @return Completion of proposals check operation.
*/
public CompletionStage<Void> checkSingle(final String uuid) {
final Instant now = Instant.now();
final Key own = this.proposalKey(uuid);
return this.storage.list(new RootKey(this.target)).thenCompose(proposals -> CompletableFuture.allOf(proposals.stream().filter(key -> !key.equals(own)).map(proposal -> this.valueIfPresent(proposal).thenCompose(value -> value.map(content -> new PublisherAs(content).asciiString().thenCompose(expiration -> {
if (isNotExpired(expiration, now)) {
throw new IllegalStateException(String.join("\n", "Failed to acquire lock.", String.format("Own: `%s`", own), String.format("Others: %s", proposals.stream().map(Key::toString).map(str -> String.format("`%s`", str)).collect(Collectors.joining(", "))), String.format("Not expired: `%s` `%s`", proposal, expiration)));
}
return CompletableFuture.allOf();
})).orElse(CompletableFuture.allOf()))).toArray(CompletableFuture[]::new)));
}
/**
* Delete proposal with specified UUID.
*
* @param uuid UUID.
* @return Completion of proposal delete operation.
*/
public CompletionStage<Void> delete(final String uuid) {
return this.storage.delete(this.proposalKey(uuid));
}
/**
* Construct proposal key with specified UUID.
*
* @param uuid UUID.
* @return Proposal key.
*/
private Key proposalKey(final String uuid) {
return new Key.From(new RootKey(this.target), uuid);
}
/**
* Checks that instant in string format is not expired, e.g. is after current time.
* Empty string considered to never expire.
*
* @param instant Instant in string format.
* @param now Current time.
* @return True if instant is not expired, false - otherwise.
*/
private static boolean isNotExpired(final String instant, final Instant now) {
return instant.isEmpty() || Instant.parse(instant).isAfter(now);
}
/**
* Loads value content is it is present.
*
* @param key Key for the value.
* @return Content if value presents, empty otherwise.
*/
private CompletableFuture<Optional<Content>> valueIfPresent(final Key key) {
final CompletableFuture<Optional<Content>> value = this.storage.value(key).thenApply(Optional::of);
return value.handle((content, throwable) -> {
final CompletableFuture<Optional<Content>> result;
if (throwable != null && throwable.getCause() instanceof ValueNotFoundException) {
result = CompletableFuture.completedFuture(Optional.empty());
} else {
result = value;
}
return result;
}).thenCompose(Function.idenreplacedy());
}
/**
* Root key for lock proposals.
*
* @since 0.24
*/
static clreplaced RootKey extends Key.Wrap {
/**
* Ctor.
*
* @param target Target key.
*/
protected RootKey(final Key target) {
super(new From(new From(".artipie-locks"), new From(target)));
}
}
}
18
Source : VertxFileStorage.java
with MIT License
from artipie
with MIT License
from artipie
@Override
public CompletableFuture<Content> value(final Key key) {
final CompletableFuture<Content> res;
if (Key.ROOT.equals(key)) {
res = new CompletableFutureSupport.Failed<Content>(new IOException("Unable to load from root")).get();
} else {
res = this.size(key).thenApply(size -> new Content.OneTime(new Content.From(size, new VertxRxFile(this.path(key), this.vertx).flow())));
}
return res;
}
18
Source : VertxFileStorage.java
with MIT License
from artipie
with MIT License
from artipie
/**
* Resolves key to file system path.
*
* @param key Key to be resolved to path.
* @return Path created from key.
*/
private Path path(final Key key) {
return Paths.get(this.dir.toString(), key.string());
}
18
Source : VertxFileStorage.java
with MIT License
from artipie
with MIT License
from artipie
@Override
public CompletableFuture<Boolean> exists(final Key key) {
return Single.fromCallable(() -> {
final Path path = this.path(key);
return Files.exists(path) && !Files.isDirectory(path);
}).subscribeOn(RxHelper.blockingScheduler(this.vertx.getDelegate())).to(SingleInterop.get()).toCompletableFuture();
}
18
Source : VertxFileStorage.java
with MIT License
from artipie
with MIT License
from artipie
@Override
public CompletableFuture<Void> delete(final Key key) {
return new VertxRxFile(this.path(key), this.vertx).delete().to(CompletableInterop.await()).toCompletableFuture().thenCompose(ignored -> CompletableFuture.allOf());
}
18
Source : FileStorage.java
with MIT License
from artipie
with MIT License
from artipie
@Override
public CompletableFuture<Void> move(final Key source, final Key destination) {
return FileStorage.move(this.path(source), this.path(destination));
}
18
Source : DigestVerification.java
with MIT License
from artipie
with MIT License
from artipie
@Override
public CompletionStage<Boolean> validate(final Key item, final Remote content) {
return content.get().thenCompose(val -> val.map(pub -> new ContentDigest(pub, this.digest).bytes()).orElse(CompletableFuture.completedFuture(new byte[] {}))).thenApply(actual -> Arrays.equals(this.expected, actual));
}
18
Source : StorageYamlConfigTest.java
with MIT License
from artipie
with MIT License
from artipie
@Test
void returnsSubStorageWithCorrectPrefix() throws IOException {
final Key prefix = new Key.From("prefix");
final Key path = new Key.From("some/path");
final Key full = new Key.From(prefix, path);
final byte[] data = "content".getBytes();
this.config().subStorage(prefix).save(path, new Content.From(data)).join();
Matcherreplacedert.replacedertThat(new PublisherAs(new FileStorage(this.tmp).value(full).join()).bytes().toCompletableFuture().join(), new IsEqual<>(data));
}
18
Source : UsersFromStorageYaml.java
with MIT License
from artipie
with MIT License
from artipie
/**
* Credentials from main artipie config.
* @since 0.12.2
*/
public final clreplaced UsersFromStorageYaml implements Users {
/**
* Credentials yaml mapping key.
*/
private static final String CREDENTIALS = "credentials";
/**
* Credentials yaml mapping key.
*/
private static final String EMAIL = "email";
/**
* Groups yaml mapping key.
*/
private static final String GROUPS = "groups";
/**
* Storage.
*/
private final Storage storage;
/**
* Credentials key.
*/
private final Key key;
/**
* Ctor.
* @param storage Storage
* @param key Credentials key
*/
public UsersFromStorageYaml(final Storage storage, final Key key) {
this.storage = storage;
this.key = key;
}
@Override
public CompletionStage<List<User>> list() {
return this.yaml().thenApply(yaml -> yaml.yamlMapping(UsersFromStorageYaml.CREDENTIALS).keys().stream().map(node -> {
final String name = node.replacedcalar().value();
return new User(name, Optional.ofNullable(yaml.yamlMapping(UsersFromStorageYaml.CREDENTIALS).yamlMapping(name).string(UsersFromStorageYaml.EMAIL)), Optional.ofNullable(yaml.yamlMapping(UsersFromStorageYaml.CREDENTIALS).yamlMapping(name).yamlSequence(UsersFromStorageYaml.GROUPS)).map(groups -> groups.values().stream().map(item -> item.replacedcalar().value()).collect(Collectors.toSet())).orElse(Collections.emptySet()));
}).collect(Collectors.toList()));
}
@Override
public CompletionStage<Void> add(final User user, final String pswd, final PreplacedwordFormat format) {
return this.yaml().thenCompose(yaml -> {
YamlMappingBuilder result = UsersFromStorageYaml.removeUserRecord(user.name(), yaml);
YamlMappingBuilder info = Yaml.createYamlMappingBuilder().add("type", format.name().toLowerCase(Locale.US)).add("preplaced", pswd);
if (user.email().isPresent()) {
info = info.add(UsersFromStorageYaml.EMAIL, user.email().get());
}
if (!user.groups().isEmpty()) {
YamlSequenceBuilder seq = Yaml.createYamlSequenceBuilder();
for (final String group : user.groups()) {
seq = seq.add(group);
}
info = info.add(UsersFromStorageYaml.GROUPS, seq.build());
}
result = result.add(user.name(), info.build());
return this.buildAndSaveCredentials(result);
});
}
@Override
public CompletionStage<Void> remove(final String username) {
return this.yaml().thenCompose(yaml -> this.buildAndSaveCredentials(UsersFromStorageYaml.removeUserRecord(username, yaml)));
}
@Override
public CompletionStage<Authentication> auth() {
return this.yaml().thenApply(AuthFromYaml::new);
}
/**
* Credentials as yaml.
* @return Completion action with yaml
* @todo #730:30min When `ContentAs` is used here instead of `Concatenation` and
* `Remaining` ITs get stuck on github actions (this does not happen locally on mac os),
* figure out why, make necessary corrections and use `ContentAs` here.
*/
public CompletionStage<YamlMapping> yaml() {
return SingleInterop.fromFuture(new ConfigFile(this.key).valueFrom(this.storage)).flatMap(content -> new Concatenation(content).single()).map(Remaining::new).map(Remaining::bytes).map(bytes -> Yaml.createYamlInput(new String(bytes, StandardCharsets.US_ASCII))).map(YamlInput::readYamlMapping).to(SingleInterop.get());
}
/**
* Build credentials yaml from users yaml mapping and saves it to storage.
* @param users Users mapping
* @return Credentials yaml string representation
*/
private CompletionStage<Void> buildAndSaveCredentials(final YamlMappingBuilder users) {
return this.storage.save(this.key, new Content.From(Yaml.createYamlMappingBuilder().add(UsersFromStorageYaml.CREDENTIALS, users.build()).build().toString().getBytes(StandardCharsets.UTF_8)));
}
/**
* Removes user record from credentials.yaml.
* @param username User name to remove
* @param yaml Credentials mapping
* @return YamlMappingBuilder without removed user
*/
private static YamlMappingBuilder removeUserRecord(final String username, final YamlMapping yaml) {
YamlMappingBuilder result = Yaml.createYamlMappingBuilder();
final YamlMapping credentials = yaml.yamlMapping(UsersFromStorageYaml.CREDENTIALS);
final List<YamlNode> keep = credentials.keys().stream().filter(node -> !node.replacedcalar().value().equals(username)).collect(Collectors.toList());
for (final YamlNode node : keep) {
result = result.add(node, credentials.value(node));
}
return result;
}
}
18
Source : RepoPermissionsFromStorage.java
with MIT License
from artipie
with MIT License
from artipie
@Override
public CompletionStage<Void> update(final String repo, final Collection<PermissionItem> permissions, final Collection<PathPattern> patterns) {
final Key key = RepoPermissionsFromStorage.repoSettingsKey(repo);
return this.repo(key).thenApply(mapping -> {
YamlMappingBuilder res = RepoPermissionsFromStorage.copyRepoSection(mapping);
YamlMappingBuilder perms = Yaml.createYamlMappingBuilder();
if (!permissions.isEmpty()) {
for (final PermissionItem item : permissions) {
perms = perms.add(item.username(), item.yaml().build());
}
res = res.add(RepoPermissionsFromStorage.PERMS, perms.build());
}
if (!patterns.isEmpty()) {
YamlSequenceBuilder builder = Yaml.createYamlSequenceBuilder();
for (final PathPattern pattern : patterns) {
builder = builder.add(pattern.string());
}
res = res.add(RepoPermissionsFromStorage.INCLUDE_PATTERNS, builder.build());
}
return Yaml.createYamlMappingBuilder().add(RepoPermissionsFromStorage.REPO_SECTION, res.build()).build();
}).thenCompose(result -> this.saveSettings(key, result));
}
18
Source : RepoPermissionsFromStorage.java
with MIT License
from artipie
with MIT License
from artipie
/**
* Saves changed settings to storage.
* @param key Key to save storage item by
* @param result Yaml result
* @return Completable operation
*/
private CompletableFuture<Void> saveSettings(final Key key, final YamlMapping result) {
return this.storage.save(key, new Content.From(result.toString().getBytes(StandardCharsets.UTF_8)));
}
18
Source : RepoPermissionsFromStorage.java
with MIT License
from artipie
with MIT License
from artipie
@Override
public CompletionStage<Void> remove(final String repo) {
final Key key = RepoPermissionsFromStorage.repoSettingsKey(repo);
return this.repo(key).thenApply(mapping -> Yaml.createYamlMappingBuilder().add(RepoPermissionsFromStorage.REPO_SECTION, RepoPermissionsFromStorage.copyRepoSection(mapping).build()).build()).thenCompose(result -> this.saveSettings(key, result));
}
18
Source : YamlProxyConfig.java
with MIT License
from artipie
with MIT License
from artipie
/**
* Proxy repository config from YAML.
*
* @since 0.12
*/
public final clreplaced YamlProxyConfig implements ProxyConfig {
/**
* Storages.
*/
private final StorageAliases storages;
/**
* Cache storage prefix.
*/
private final Key prefix;
/**
* Source YAML.
*/
private final YamlMapping yaml;
/**
* Ctor.
*
* @param storages Storages.
* @param prefix Cache storage prefix.
* @param yaml Source YAML.
*/
public YamlProxyConfig(final StorageAliases storages, final Key prefix, final YamlMapping yaml) {
this.storages = storages;
this.prefix = prefix;
this.yaml = yaml;
}
@Override
public Collection<YamlRemote> remotes() {
return StreamSupport.stream(Optional.ofNullable(this.yaml.yamlSequence("remotes")).orElseGet(() -> Yaml.createYamlSequenceBuilder().build()).spliterator(), false).map(remote -> {
if (!(remote instanceof YamlMapping)) {
throw new IllegalStateException("`remotes` element is not mapping in proxy config");
}
return new YamlRemote((YamlMapping) remote);
}).collect(Collectors.toList());
}
/**
* Proxy repository remote from YAML.
*
* @since 0.12
*/
public final clreplaced YamlRemote implements Remote {
/**
* Source YAML.
*/
private final YamlMapping source;
/**
* Ctor.
*
* @param source Source YAML.
*/
YamlRemote(final YamlMapping source) {
this.source = source;
}
@Override
public String url() {
return Optional.ofNullable(this.source.string("url")).orElseThrow(() -> new IllegalStateException("`url` is not specified for proxy remote"));
}
@Override
public Authenticator auth() {
final Authenticator result;
final String username = this.source.string("username");
final String preplacedword = this.source.string("preplacedword");
if (username == null && preplacedword == null) {
result = new GenericAuthenticator(SliceFromConfig.HTTP);
} else {
if (username == null) {
throw new IllegalStateException("`username` is not specified for proxy remote");
}
if (preplacedword == null) {
throw new IllegalStateException("`preplacedword` is not specified for proxy remote");
}
result = new GenericAuthenticator(SliceFromConfig.HTTP, username, preplacedword);
}
return result;
}
@Override
public Optional<Storage> cache() {
return Optional.ofNullable(this.source.yamlMapping("cache")).flatMap(root -> Optional.ofNullable(root.value("storage")).map(node -> new MeasuredStorage(new StorageYamlConfig(node, YamlProxyConfig.this.storages).subStorage(YamlProxyConfig.this.prefix))));
}
}
}
18
Source : ConfigFileApi.java
with MIT License
from artipie
with MIT License
from artipie
@Override
public CompletionStage<Content> value(final Key filename) {
return new ConfigFile(filename).valueFrom(this.storage);
}
18
Source : ConfigFileApi.java
with MIT License
from artipie
with MIT License
from artipie
@Override
public Optional<String> extension(final Key filename) {
return new ConfigFile(filename).extension();
}
18
Source : ConfigFileApi.java
with MIT License
from artipie
with MIT License
from artipie
@Override
public CompletionStage<Boolean> exists(final Key filename) {
return new ConfigFile(filename).existsIn(this.storage);
}
See More Examples