blob: 823505eca867314f0b07c1fffccb364e95424eff [file] [log] [blame]
/****************************************************************
O * 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.draft.utils;
import java.util.Optional;
import jakarta.inject.Inject;
import org.apache.james.mailbox.MailboxManager;
import org.apache.james.mailbox.MailboxSession;
import org.apache.james.mailbox.MessageManager;
import org.apache.james.mailbox.exception.MailboxException;
import org.apache.james.mailbox.exception.MailboxNotFoundException;
import org.apache.james.mailbox.model.MailboxId;
import com.github.fge.lambdas.Throwing;
import com.google.common.annotations.VisibleForTesting;
public class MailboxUtils {
private final MailboxManager mailboxManager;
@Inject
@VisibleForTesting
public MailboxUtils(MailboxManager mailboxManager) {
this.mailboxManager = mailboxManager;
}
public boolean hasChildren(MailboxId mailboxId, MailboxSession mailboxSession) throws MailboxException {
return getMailboxFromId(mailboxId, mailboxSession)
.map(Throwing.function(MessageManager::getMailboxPath).sneakyThrow())
.map(Throwing.function(path -> mailboxManager.hasChildren(path, mailboxSession)))
.orElse(false);
}
private Optional<MessageManager> getMailboxFromId(MailboxId mailboxId, MailboxSession mailboxSession) throws MailboxException {
try {
return Optional.of(mailboxManager.getMailbox(mailboxId, mailboxSession));
} catch (MailboxNotFoundException e) {
return Optional.empty();
}
}
}