Skip to content

Commit

Permalink
JAMES-1818 Use AttachmentManager in upload servlet
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphael Ouazana committed Aug 29, 2016
1 parent 6e81f25 commit 49b3ba3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
Expand Up @@ -28,38 +28,35 @@

import org.apache.james.jmap.json.ObjectMapperFactory;
import org.apache.james.jmap.model.UploadResponse;
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.Attachment;
import org.apache.james.mailbox.store.MailboxSessionMapperFactory;
import org.apache.james.mailbox.store.mail.AttachmentMapper;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.io.ByteStreams;

public class UploadHandler {
private final MailboxSessionMapperFactory mailboxSessionMapperFactory;
private final AttachmentManager attachmentManager;
private final ObjectMapper objectMapper;

@Inject
private UploadHandler(MailboxSessionMapperFactory mailboxSessionMapperFactory, ObjectMapperFactory objectMapperFactory) {
this.mailboxSessionMapperFactory = mailboxSessionMapperFactory;
private UploadHandler(AttachmentManager attachmentManager, ObjectMapperFactory objectMapperFactory) {
this.attachmentManager = attachmentManager;
this.objectMapper = objectMapperFactory.forWriting();
}

public void handle(String contentType, InputStream content, HttpServletResponse response) throws IOException, MailboxException {
UploadResponse storedContent = uploadContent(contentType, content);
public void handle(String contentType, InputStream content, MailboxSession mailboxSession, HttpServletResponse response) throws IOException, MailboxException {
UploadResponse storedContent = uploadContent(contentType, content, mailboxSession);
buildResponse(response, storedContent);
}

private UploadResponse uploadContent(String contentType, InputStream inputStream) throws IOException, MailboxException {
MailboxSession session = null;
AttachmentMapper attachmentMapper = mailboxSessionMapperFactory.createAttachmentMapper(session);
private UploadResponse uploadContent(String contentType, InputStream inputStream, MailboxSession session) throws IOException, MailboxException {
Attachment attachment = Attachment.builder()
.bytes(ByteStreams.toByteArray(inputStream))
.type(contentType)
.build();
attachmentMapper.storeAttachment(attachment);
attachmentManager.storeAttachment(attachment, session);
return UploadResponse.builder()
.blobId(attachment.getAttachmentId().getId())
.type(attachment.getType())
Expand Down
Expand Up @@ -29,6 +29,7 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.james.mailbox.MailboxSession;
import org.apache.james.mailbox.exception.MailboxException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -52,11 +53,16 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws S
resp.setStatus(SC_BAD_REQUEST);
} else {
try {
uploadHandler.handle(contentType, req.getInputStream(), resp);
uploadHandler.handle(contentType, req.getInputStream(), getMailboxSession(req), resp);
} catch (IOException | MailboxException e) {
LOGGER.error("Error while uploading content", e);
resp.setStatus(SC_INTERNAL_SERVER_ERROR);
}
}
}

private MailboxSession getMailboxSession(HttpServletRequest req) {
return (MailboxSession) req.getAttribute(AuthenticationFilter.MAILBOX_SESSION);
}

}

0 comments on commit 49b3ba3

Please sign in to comment.