Skip to content

Commit

Permalink
JAMES-1818 Use AttachmentManager in download servlet
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphael Ouazana committed Aug 29, 2016
1 parent f2d46ab commit 6e81f25
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
Expand Up @@ -35,6 +35,8 @@
import org.apache.james.jmap.send.MailFactory;
import org.apache.james.jmap.send.MailSpool;
import org.apache.james.jmap.utils.HeadersAuthenticationExtractor;
import org.apache.james.mailbox.AttachmentManager;
import org.apache.james.mailbox.store.StoreAttachmentManager;
import org.apache.james.util.date.DefaultZonedDateTimeProvider;
import org.apache.james.util.date.ZonedDateTimeProvider;
import org.apache.mailet.base.AutomaticallySentMailDetector;
Expand Down Expand Up @@ -62,6 +64,7 @@ protected void configure() {
bind(MessageFactory.class).in(Scopes.SINGLETON);
bind(MessagePreviewGenerator.class).in(Scopes.SINGLETON);
bind(HeadersAuthenticationExtractor.class).in(Scopes.SINGLETON);
bind(StoreAttachmentManager.class).in(Scopes.SINGLETON);

bind(SignatureHandler.class).to(JamesSignatureHandler.class);
bind(ZonedDateTimeProvider.class).to(DefaultZonedDateTimeProvider.class);
Expand All @@ -71,6 +74,7 @@ protected void configure() {

bindConstant().annotatedWith(Names.named(AccessTokenRepository.TOKEN_EXPIRATION_IN_MS)).to(DEFAULT_TOKEN_EXPIRATION_IN_MS);
bind(AccessTokenManager.class).to(AccessTokenManagerImpl.class);
bind(AttachmentManager.class).to(StoreAttachmentManager.class);
}

@Provides
Expand Down
Expand Up @@ -35,13 +35,12 @@
import org.apache.commons.io.IOUtils;
import org.apache.james.jmap.api.SimpleTokenFactory;
import org.apache.james.jmap.utils.DownloadPath;
import org.apache.james.mailbox.AttachmentManager;
import org.apache.james.mailbox.MailboxSession;
import org.apache.james.mailbox.exception.AttachmentNotFoundException;
import org.apache.james.mailbox.exception.MailboxException;
import org.apache.james.mailbox.model.Attachment;
import org.apache.james.mailbox.model.AttachmentId;
import org.apache.james.mailbox.store.MailboxSessionMapperFactory;
import org.apache.james.mailbox.store.mail.AttachmentMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -52,12 +51,12 @@ public class DownloadServlet extends HttpServlet {
private static final Logger LOGGER = LoggerFactory.getLogger(DownloadServlet.class);
private static final String TEXT_PLAIN_CONTENT_TYPE = "text/plain";

private final MailboxSessionMapperFactory mailboxSessionMapperFactory;
private final AttachmentManager attachmentManager;
private final SimpleTokenFactory simpleTokenFactory;

@Inject
@VisibleForTesting DownloadServlet(MailboxSessionMapperFactory mailboxSessionMapperFactory, SimpleTokenFactory simpleTokenFactory) {
this.mailboxSessionMapperFactory = mailboxSessionMapperFactory;
@VisibleForTesting DownloadServlet(AttachmentManager attachmentManager, SimpleTokenFactory simpleTokenFactory) {
this.attachmentManager = attachmentManager;
this.simpleTokenFactory = simpleTokenFactory;
}

Expand Down Expand Up @@ -89,9 +88,8 @@ private void respondAttachmentAccessToken(MailboxSession mailboxSession, Downloa
}

private boolean attachmentExists(MailboxSession mailboxSession, String blobId) throws MailboxException {
AttachmentMapper attachmentMapper = mailboxSessionMapperFactory.createAttachmentMapper(mailboxSession);
try {
attachmentMapper.getAttachment(AttachmentId.from(blobId));
attachmentManager.getAttachment(AttachmentId.from(blobId), mailboxSession);
return true;
} catch (AttachmentNotFoundException e) {
return false;
Expand All @@ -114,8 +112,7 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se
try {
addContentDispositionHeader(downloadPath.getName(), resp);

AttachmentMapper attachmentMapper = mailboxSessionMapperFactory.createAttachmentMapper(mailboxSession);
Attachment attachment = attachmentMapper.getAttachment(AttachmentId.from(blobId));
Attachment attachment = attachmentManager.getAttachment(AttachmentId.from(blobId), mailboxSession);
IOUtils.copy(attachment.getStream(), resp.getOutputStream());

resp.setStatus(SC_OK);
Expand Down
Expand Up @@ -19,6 +19,8 @@

package org.apache.james.jmap;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand All @@ -27,22 +29,22 @@

import org.apache.james.jmap.api.SimpleTokenFactory;
import org.apache.james.jmap.utils.DownloadPath;
import org.apache.james.mailbox.AttachmentManager;
import org.apache.james.mailbox.MailboxSession;
import org.apache.james.mailbox.exception.MailboxException;
import org.apache.james.mailbox.store.MailboxSessionMapperFactory;
import org.junit.Test;

public class DownloadServletTest {

@Test
public void downloadMayFailWhenUnableToCreateAttachmentMapper() throws Exception {
public void downloadMayFailWhenUnknownErrorOnAttachmentManager() throws Exception {
MailboxSession mailboxSession = mock(MailboxSession.class);
MailboxSessionMapperFactory mailboxSessionMapperFactory = mock(MailboxSessionMapperFactory.class);
when(mailboxSessionMapperFactory.createAttachmentMapper(mailboxSession))
AttachmentManager mockedAttachmentManager = mock(AttachmentManager.class);
when(mockedAttachmentManager.getAttachment(any(), eq(mailboxSession)))
.thenThrow(new MailboxException());
SimpleTokenFactory nullSimpleTokenFactory = null;

DownloadServlet testee = new DownloadServlet(mailboxSessionMapperFactory, nullSimpleTokenFactory);
DownloadServlet testee = new DownloadServlet(mockedAttachmentManager, nullSimpleTokenFactory);

HttpServletResponse resp = mock(HttpServletResponse.class);
testee.download(mailboxSession, DownloadPath.from("/blobId"), resp);
Expand Down

0 comments on commit 6e81f25

Please sign in to comment.