All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.james.jmap.methods.AttachmentChecker Maven / Gradle / Ivy

/****************************************************************
 * 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.methods;

import java.util.List;

import javax.inject.Inject;

import org.apache.james.jmap.exceptions.AttachmentsNotFoundException;
import org.apache.james.jmap.model.Attachment;
import org.apache.james.jmap.model.BlobId;
import org.apache.james.mailbox.AttachmentManager;
import org.apache.james.mailbox.MailboxSession;
import org.apache.james.mailbox.exception.MailboxException;
import org.apache.james.mailbox.model.AttachmentId;

import com.github.fge.lambdas.Throwing;
import com.github.fge.lambdas.predicates.ThrowingPredicate;
import com.github.steveash.guavate.Guavate;

public class AttachmentChecker {
    private final AttachmentManager attachmentManager;

    @Inject
    public AttachmentChecker(AttachmentManager attachmentManager) {
        this.attachmentManager = attachmentManager;
    }

    public void assertAttachmentsExist(ValueWithId.CreationMessageEntry entry, MailboxSession session) throws AttachmentsNotFoundException, MailboxException {
        List attachments = entry.getValue().getAttachments();
        List notFounds = listAttachmentsNotFound(attachments, session);
        if (!notFounds.isEmpty()) {
            throw new AttachmentsNotFoundException(notFounds);
        }
    }

    private List listAttachmentsNotFound(List attachments, MailboxSession session) throws MailboxException {
        ThrowingPredicate notExists =
            attachment -> !attachmentManager.exists(getAttachmentId(attachment), session);
        return attachments.stream()
            .filter(Throwing.predicate(notExists).sneakyThrow())
            .map(Attachment::getBlobId)
            .collect(Guavate.toImmutableList());
    }

    private AttachmentId getAttachmentId(Attachment attachment) {
        return AttachmentId.from(attachment.getBlobId().getRawValue());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy