package dslab.protocol.dmap; import dslab.authentication.AuthenticationException; import dslab.authentication.AuthenticationService; import dslab.mailbox.MailboxManager; import dslab.mailbox.MessageNotFoundException; import dslab.protocol.Message; import java.util.List; import java.util.logging.Logger; import static java.lang.Thread.currentThread; /** * Contains the mailbox server's processing logic for DMAP commands. */ public class DmapImpl implements Dmap { private static final Logger LOG = Logger.getLogger(DmapImpl.class.getSimpleName()); private final MailboxManager mailboxManager; private final AuthenticationService authenticationService; public DmapImpl(MailboxManager mailboxManager, AuthenticationService authenticationService) { this.mailboxManager = mailboxManager; this.authenticationService = authenticationService; } @Override public void login(String username, String password) throws AuthenticationException { authenticationService.login(username, password); } @Override public void logout() throws AuthenticationException { assertLoggedIn(); authenticationService.logout(); } @Override public void quit() { currentThread().interrupt(); //this is checked in for-loop } @Override public void delete(Integer id) throws AuthenticationException, MessageNotFoundException { assertLoggedIn(); mailboxManager.deleteMessage(id); } @Override public Message show(Integer id) throws AuthenticationException, MessageNotFoundException { assertLoggedIn(); return mailboxManager.getMessage(id); } @Override public List list() throws AuthenticationException { assertLoggedIn(); var mailbox = mailboxManager.getMailbox(authenticationService.getLoggedInUser()); LOG.info("Listing " + mailbox); return mailbox.getMessageMetadata(); } private void assertLoggedIn() throws AuthenticationException { if (!authenticationService.isLoggedIn()) throw new AuthenticationException("Not logged in"); } }