chibenwa commented on a change in pull request #551: URL: https://github.com/apache/james-project/pull/551#discussion_r676287945
########## File path: server/data/data-jmap/src/main/java/org/apache/james/jmap/memory/upload/InMemoryUploadRepository.java ########## @@ -0,0 +1,149 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.jmap.memory.upload; + +import java.io.IOException; +import java.io.InputStream; +import java.io.UncheckedIOException; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +import javax.inject.Inject; + +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.tuple.ImmutablePair; +import org.apache.james.blob.api.BlobId; +import org.apache.james.blob.api.BlobStore; +import org.apache.james.blob.api.BucketName; +import org.apache.james.core.Username; +import org.apache.james.jmap.api.exception.UploadNotFoundException; +import org.apache.james.jmap.api.model.Upload; +import org.apache.james.jmap.api.model.UploadId; +import org.apache.james.jmap.api.model.UploadMetaData; +import org.apache.james.jmap.api.upload.UploadRepository; +import org.apache.james.mailbox.model.ContentType; +import org.reactivestreams.Publisher; + +import com.google.common.base.Preconditions; + +import reactor.core.publisher.Mono; + +public class InMemoryUploadRepository implements UploadRepository { + + private static final Map<UploadId, ImmutablePair<Username, BlobId>> uploadStore = new HashMap<>(); + private static final Map<UploadId, UploadMetaData> uploadMetaDataStore = new HashMap<>(); + + static class UploadContext { + private final UploadId uploadId; + private final boolean exists; + private final byte[] data; + + private UploadContext(UploadId uploadId, + boolean exists, + byte[] data) { + this.uploadId = uploadId; + this.exists = exists; + this.data = data; + } + } + + + private final BlobStore blobStore; + private final BucketName bucketName; + + @Inject + public InMemoryUploadRepository(BlobStore blobStore) { + this.blobStore = blobStore; + this.bucketName = blobStore.getDefaultBucketName(); + } + + @Override + public Publisher<UploadId> upload(InputStream data, ContentType contentType, Username user) { + return Mono.just(checkUploadExits(data, user)) + .flatMap(uploadContext -> { + if (!uploadContext.exists) { + return store(uploadContext.data, uploadContext.uploadId, contentType, user); + } else { + return Mono.just(uploadContext.uploadId); + } + }); + } + + @Override + public Publisher<Upload> retrieve(UploadId id, Username user) { + Preconditions.checkNotNull(id); + Preconditions.checkNotNull(user); + + return retrieveBlobId(id, user) + .flatMap(blobId -> retrieveUpload(id, blobId)); + } + + private Mono<BlobId> retrieveBlobId(UploadId id, Username user) { + return Mono.justOrEmpty(uploadStore.get(id)) + .filter(pair -> userHasAccessToUpload(user, pair.left)) + .map(userAndBlobId -> userAndBlobId.right) + .switchIfEmpty(Mono.error(() -> new UploadNotFoundException(id))); + } + + private Mono<Upload> retrieveUpload(UploadId id, BlobId blobId) { + return Mono.zip(Mono.from(blobStore.readBytes(bucketName, blobId)), + Mono.just(uploadMetaDataStore.get(id))) + .map(bytesAndMetadata -> Upload.from(bytesAndMetadata.getT2(), bytesAndMetadata.getT1())); + } + + private boolean userHasAccessToUpload(Username userRequest, Username owner) { Review comment: I prefer having user.equals(owner) directly, I would find it clearer. We can extract to a method later, when it makes sense IMO... ########## File path: server/data/data-jmap/src/test/java/org/apache/james/jmap/api/upload/UploadRepositoryContract.scala ########## @@ -0,0 +1,108 @@ + /*************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.jmap.api.upload + +import org.apache.commons.io.IOUtils +import org.apache.james.core.Username +import org.apache.james.jmap.api.exception.UploadNotFoundException +import org.apache.james.jmap.api.model.{Upload, UploadId} +import org.apache.james.jmap.api.upload.UploadRepositoryContract.{CONTENT_TYPE, DATA, DATA_STRING, USER} +import org.apache.james.mailbox.model.ContentType +import org.assertj.core.api.Assertions.{assertThat, assertThatThrownBy} +import org.junit.jupiter.api.Test +import reactor.core.scala.publisher.SMono + +import java.io.InputStream +import java.nio.charset.StandardCharsets + + +object UploadRepositoryContract { + private lazy val CONTENT_TYPE: ContentType = ContentType + .of("text/html") + private lazy val DATA_STRING: String = "123321" + private lazy val DATA: InputStream = IOUtils.toInputStream(DATA_STRING, StandardCharsets.UTF_8) + private lazy val USER: Username = Username.of("Bob") +} + + +trait UploadRepositoryContract { + + def testee: UploadRepository + + @Test + def uploadShouldSuccess(): Unit = { + val uploadId: UploadId = SMono.fromPublisher(testee.upload(DATA, CONTENT_TYPE, USER)).block() + + assertThat(SMono.fromPublisher(testee.retrieve(uploadId, USER)).block()) + .isNotNull + } + + @Test + def uploadShouldReturnSameIdWhenReUploadSameContent(): Unit = { Review comment: Idem -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
