Skip to content

Commit

Permalink
JAMES-1796 Unwrap Cid
Browse files Browse the repository at this point in the history
  • Loading branch information
aduprat committed Jul 11, 2016
1 parent 0cad057 commit b87c939
Show file tree
Hide file tree
Showing 14 changed files with 198 additions and 29 deletions.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
import org.apache.james.mailbox.store.mail.model.Mailbox; import org.apache.james.mailbox.store.mail.model.Mailbox;
import org.apache.james.mailbox.store.mail.model.MailboxMessage; import org.apache.james.mailbox.store.mail.model.MailboxMessage;
import org.apache.james.mailbox.store.mail.model.MessageAttachment; import org.apache.james.mailbox.store.mail.model.MessageAttachment;
import org.apache.james.mailbox.store.mail.model.impl.Cid;
import org.apache.james.mailbox.store.mail.model.impl.PropertyBuilder; import org.apache.james.mailbox.store.mail.model.impl.PropertyBuilder;
import org.apache.james.mailbox.store.mail.model.impl.SimpleMailboxMessage; import org.apache.james.mailbox.store.mail.model.impl.SimpleMailboxMessage;
import org.apache.james.mailbox.store.mail.model.impl.SimpleProperty; import org.apache.james.mailbox.store.mail.model.impl.SimpleProperty;
Expand Down Expand Up @@ -354,7 +355,7 @@ private List<MessageAttachment> getAttachments(Row row, FetchType fetchType) {
MessageAttachment.builder() MessageAttachment.builder()
.attachment(attachmentsById.get(attachmentIdFrom(x))) .attachment(attachmentsById.get(attachmentIdFrom(x)))
.name(x.getString(Attachments.NAME)) .name(x.getString(Attachments.NAME))
.cid(x.getString(Attachments.CID)) .cid(com.google.common.base.Optional.fromNullable(x.getString(Attachments.CID)).transform(Cid::from))
.isInline(x.getBool(Attachments.IS_INLINE)) .isInline(x.getBool(Attachments.IS_INLINE))
.build())) .build()))
.collect(ImmutableCollectors.toImmutableList()); .collect(ImmutableCollectors.toImmutableList());
Expand Down Expand Up @@ -424,7 +425,7 @@ private UDTValue toUDT(MessageAttachment messageAttachment) {
.newValue() .newValue()
.setString(Attachments.ID, messageAttachment.getAttachmentId().getId()) .setString(Attachments.ID, messageAttachment.getAttachmentId().getId())
.setString(Attachments.NAME, messageAttachment.getName().orNull()) .setString(Attachments.NAME, messageAttachment.getName().orNull())
.setString(Attachments.CID, messageAttachment.getCid().orNull()) .setString(Attachments.CID, messageAttachment.getCid().transform(Cid::getValue).orNull())
.setBool(Attachments.IS_INLINE, messageAttachment.isInline()); .setBool(Attachments.IS_INLINE, messageAttachment.isInline());
} }


Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@


package org.apache.james.mailbox.store.mail.model; package org.apache.james.mailbox.store.mail.model;


import org.apache.james.mailbox.store.mail.model.impl.Cid;

import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.MoreObjects; import com.google.common.base.MoreObjects;
import com.google.common.base.Objects; import com.google.common.base.Objects;
Expand All @@ -35,7 +37,7 @@ public static class Builder {


private Attachment attachment; private Attachment attachment;
private Optional<String> name; private Optional<String> name;
private Optional<String> cid; private Optional<Cid> cid;
private Boolean isInline; private Boolean isInline;


private Builder() { private Builder() {
Expand All @@ -54,7 +56,14 @@ public Builder name(String name) {
return this; return this;
} }


public Builder cid(String cid) { public Builder cid(Optional<Cid> cid) {
Preconditions.checkNotNull(cid);
this.cid = cid;
return this;
}


public Builder cid(Cid cid) {
this.cid = Optional.fromNullable(cid); this.cid = Optional.fromNullable(cid);
return this; return this;
} }
Expand All @@ -78,10 +87,10 @@ public MessageAttachment build() {


private final Attachment attachment; private final Attachment attachment;
private final Optional<String> name; private final Optional<String> name;
private final Optional<String> cid; private final Optional<Cid> cid;
private final boolean isInline; private final boolean isInline;


@VisibleForTesting MessageAttachment(Attachment attachment, Optional<String> name, Optional<String> cid, boolean isInline) { @VisibleForTesting MessageAttachment(Attachment attachment, Optional<String> name, Optional<Cid> cid, boolean isInline) {
this.attachment = attachment; this.attachment = attachment;
this.name = name; this.name = name;
this.cid = cid; this.cid = cid;
Expand All @@ -100,7 +109,7 @@ public Optional<String> getName() {
return name; return name;
} }


public Optional<String> getCid() { public Optional<Cid> getCid() {
return cid; return cid;
} }


Expand Down
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,72 @@
/****************************************************************
* 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.mailbox.store.mail.model.impl;


import com.google.common.base.Objects;
import com.google.common.base.Preconditions;

public class Cid {

public static Cid from(String cidAsString) {
Preconditions.checkNotNull(cidAsString);
Preconditions.checkArgument(!cidAsString.isEmpty(), "'cidAsString' is mandatory");
return new Cid(normalizedCid(cidAsString));
}

private static String normalizedCid(String input) {
if (isWrappedWithAngleBrackets(input)) {
return unwrap(input);
}
return input;
}

private static String unwrap(String cidAsString) {
return cidAsString.substring(1, cidAsString.length() - 1);
}

private static boolean isWrappedWithAngleBrackets(String cidAsString) {
return cidAsString.startsWith("<") && cidAsString.endsWith(">");
}

private final String value;

private Cid(String value) {
this.value = value;
}

public String getValue() {
return value;
}

@Override
public final boolean equals(Object obj) {
if (obj instanceof Cid) {
Cid other = (Cid) obj;
return Objects.equal(this.value, other.value);
}
return false;
}

@Override
public final int hashCode() {
return Objects.hashCode(this.value);
}
}
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ private MessageAttachment retrieveAttachment(MessageWriter messageWriter, Entity
Optional<ContentTypeField> contentTypeField = getContentTypeField(entity); Optional<ContentTypeField> contentTypeField = getContentTypeField(entity);
Optional<String> contentType = contentType(contentTypeField); Optional<String> contentType = contentType(contentTypeField);
Optional<String> name = name(contentTypeField); Optional<String> name = name(contentTypeField);
Optional<String> cid = cid(castField(entity.getHeader().getField(CONTENT_ID), ContentIdField.class)); Optional<Cid> cid = cid(castField(entity.getHeader().getField(CONTENT_ID), ContentIdField.class));
boolean isInline = isInline(castField(entity.getHeader().getField(CONTENT_DISPOSITION), ContentDispositionField.class)); boolean isInline = isInline(castField(entity.getHeader().getField(CONTENT_DISPOSITION), ContentDispositionField.class));


return MessageAttachment.builder() return MessageAttachment.builder()
Expand Down Expand Up @@ -132,13 +132,20 @@ public Optional<String> apply(ContentTypeField field) {
}).or(Optional.<String> absent()); }).or(Optional.<String> absent());
} }


private Optional<String> cid(Optional<ContentIdField> contentIdField) { private Optional<Cid> cid(Optional<ContentIdField> contentIdField) {
return contentIdField.transform(new Function<ContentIdField, Optional<String>>() { return contentIdField.transform(new Function<ContentIdField, Optional<Cid>>() {
@Override @Override
public Optional<String> apply(ContentIdField field) { public Optional<Cid> apply(ContentIdField field) {
return Optional.fromNullable(field.getId()); return Optional.fromNullable(field.getId())
.transform(new Function<String, Cid>() {

@Override
public Cid apply(String cid) {
return Cid.from(cid);
}
});
} }
}).or(Optional.<String> absent()); }).or(Optional.<Cid> absent());
} }


private boolean isMultipart(Entity entity) { private boolean isMultipart(Entity entity) {
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@


import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;


import org.apache.james.mailbox.store.mail.model.impl.Cid;
import org.junit.Test; import org.junit.Test;


import com.google.common.base.Optional; import com.google.common.base.Optional;
Expand All @@ -45,7 +46,7 @@ public void buildShouldWorkWhenMandatoryAttributesAreGiven() {
.bytes("content".getBytes()) .bytes("content".getBytes())
.type("type") .type("type")
.build(); .build();
MessageAttachment expectedMessageAttachment = new MessageAttachment(attachment, Optional.<String> absent(), Optional.<String> absent(), false); MessageAttachment expectedMessageAttachment = new MessageAttachment(attachment, Optional.<String> absent(), Optional.<Cid> absent(), false);


MessageAttachment messageAttachment = MessageAttachment.builder() MessageAttachment messageAttachment = MessageAttachment.builder()
.attachment(attachment) .attachment(attachment)
Expand Down Expand Up @@ -87,12 +88,12 @@ public void buildShouldSetAttributesWhenAllAreGiven() {
.bytes("content".getBytes()) .bytes("content".getBytes())
.type("type") .type("type")
.build(); .build();
MessageAttachment expectedMessageAttachment = new MessageAttachment(attachment, Optional.of("name"), Optional.of("cid"), true); MessageAttachment expectedMessageAttachment = new MessageAttachment(attachment, Optional.of("name"), Optional.of(Cid.from("cid")), true);


MessageAttachment messageAttachment = MessageAttachment.builder() MessageAttachment messageAttachment = MessageAttachment.builder()
.attachment(attachment) .attachment(attachment)
.name("name") .name("name")
.cid("cid") .cid(Cid.from("cid"))
.isInline(true) .isInline(true)
.build(); .build();


Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.apache.james.mailbox.store.mail.AttachmentMapper; import org.apache.james.mailbox.store.mail.AttachmentMapper;
import org.apache.james.mailbox.store.mail.MessageMapper; import org.apache.james.mailbox.store.mail.MessageMapper;
import org.apache.james.mailbox.store.mail.MessageMapper.FetchType; import org.apache.james.mailbox.store.mail.MessageMapper.FetchType;
import org.apache.james.mailbox.store.mail.model.impl.Cid;
import org.apache.james.mailbox.store.mail.model.impl.PropertyBuilder; import org.apache.james.mailbox.store.mail.model.impl.PropertyBuilder;
import org.apache.james.mailbox.store.mail.model.impl.SimpleMailbox; import org.apache.james.mailbox.store.mail.model.impl.SimpleMailbox;
import org.apache.james.mailbox.store.mail.model.impl.SimpleMailboxMessage; import org.apache.james.mailbox.store.mail.model.impl.SimpleMailboxMessage;
Expand Down Expand Up @@ -118,19 +119,19 @@ public final void setProducer(IProducer<T> producer) throws MailboxException {
message7With1Attachment = createMessage(attachmentsMailbox, "Subject: Test7 \n\nBody7\n.\n", BODY_START, new PropertyBuilder(), message7With1Attachment = createMessage(attachmentsMailbox, "Subject: Test7 \n\nBody7\n.\n", BODY_START, new PropertyBuilder(),
ImmutableList.of(MessageAttachment.builder() ImmutableList.of(MessageAttachment.builder()
.attachment(attachment) .attachment(attachment)
.cid("cid") .cid(Cid.from("cid"))
.isInline(true) .isInline(true)
.build())); .build()));
message8With2Attachments = createMessage(attachmentsMailbox, "Subject: Test8 \n\nBody8\n.\n", BODY_START, new PropertyBuilder(), message8With2Attachments = createMessage(attachmentsMailbox, "Subject: Test8 \n\nBody8\n.\n", BODY_START, new PropertyBuilder(),
ImmutableList.of( ImmutableList.of(
MessageAttachment.builder() MessageAttachment.builder()
.attachment(attachment) .attachment(attachment)
.cid("cid") .cid(Cid.from("cid"))
.isInline(true) .isInline(true)
.build(), .build(),
MessageAttachment.builder() MessageAttachment.builder()
.attachment(attachment2) .attachment(attachment2)
.cid("cid2") .cid(Cid.from("cid2"))
.isInline(false) .isInline(false)
.build())); .build()));


Expand Down
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,74 @@
/****************************************************************
* 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.mailbox.store.mail.model.impl;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import nl.jqno.equalsverifier.EqualsVerifier;

public class CidTest {

@Rule public ExpectedException expectedException = ExpectedException.none();

@Test
public void fromShouldThrowWhenNull() {
expectedException.expect(NullPointerException.class);
Cid.from(null);
}

@Test
public void fromShouldThrowWhenEmpty() {
expectedException.expect(IllegalArgumentException.class);
Cid.from("");
}

@Test
public void fromShouldRemoveTagsWhenExists() {
Cid cid = Cid.from("<123>");
assertThat(cid.getValue()).isEqualTo("123");
}

@Test
public void fromShouldNotRemoveTagsWhenNone() {
Cid cid = Cid.from("123");
assertThat(cid.getValue()).isEqualTo("123");
}

@Test
public void fromShouldNotRemoveTagsWhenNotEndTag() {
Cid cid = Cid.from("<123");
assertThat(cid.getValue()).isEqualTo("<123");
}

@Test
public void fromShouldNotRemoveTagsWhenNotStartTag() {
Cid cid = Cid.from("123>");
assertThat(cid.getValue()).isEqualTo("123>");
}

@Test
public void shouldRespectJavaBeanContract() {
EqualsVerifier.forClass(Cid.class).verify();
}
}
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public void getAttachmentsShouldRetrieveTheAttachmentCIDWhenOne() throws Excepti
List<MessageAttachment> attachments = testee.retrieveAttachments(ClassLoader.getSystemResourceAsStream("eml/oneInlinedAttachment.eml")); List<MessageAttachment> attachments = testee.retrieveAttachments(ClassLoader.getSystemResourceAsStream("eml/oneInlinedAttachment.eml"));


assertThat(attachments).hasSize(1); assertThat(attachments).hasSize(1);
assertThat(attachments.get(0).getCid()).isEqualTo(Optional.of("<part1.37A15C92.A7C3488D@linagora.com>")); assertThat(attachments.get(0).getCid()).isEqualTo(Optional.of(Cid.from("part1.37A15C92.A7C3488D@linagora.com")));
} }


@Test @Test
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ Feature: GetMessages method
|blobId |"58aa22c2ec5770fb9e574ba19008dbfc647eba43" | |blobId |"58aa22c2ec5770fb9e574ba19008dbfc647eba43" |
|type |"image/jpeg" | |type |"image/jpeg" |
|size |597 | |size |597 |
|cid |"<part1.37A15C92.A7C3488D@linagora.com>" | |cid |"part1.37A15C92.A7C3488D@linagora.com" |
|isInline |true | |isInline |true |


Scenario: Retrieving message should return attachments and html body when some attachments and html message Scenario: Retrieving message should return attachments and html body when some attachments and html message
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ private BodyPart attachmentBodyPart(MessageAttachment att) throws IOException {


private void contentId(BodyPartBuilder builder, MessageAttachment att) { private void contentId(BodyPartBuilder builder, MessageAttachment att) {
if (att.getCid().isPresent()) { if (att.getCid().isPresent()) {
builder.setField(new RawField("Content-ID", att.getCid().get())); builder.setField(new RawField("Content-ID", att.getCid().get().getValue()));
} }
} }


Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
import org.apache.james.mailbox.store.mail.model.MailboxId; import org.apache.james.mailbox.store.mail.model.MailboxId;
import org.apache.james.mailbox.store.mail.model.MailboxMessage; import org.apache.james.mailbox.store.mail.model.MailboxMessage;
import org.apache.james.mailbox.store.mail.model.MessageAttachment; import org.apache.james.mailbox.store.mail.model.MessageAttachment;
import org.apache.james.mailbox.store.mail.model.impl.Cid;
import org.apache.james.mailbox.store.mail.model.impl.PropertyBuilder; import org.apache.james.mailbox.store.mail.model.impl.PropertyBuilder;
import org.apache.james.mailbox.store.mail.model.impl.SimpleMailboxMessage; import org.apache.james.mailbox.store.mail.model.impl.SimpleMailboxMessage;
import org.apache.james.util.streams.ImmutableCollectors; import org.apache.james.util.streams.ImmutableCollectors;
Expand Down Expand Up @@ -327,7 +328,7 @@ private MessageAttachment messageAttachment(AttachmentMapper attachmentMapper, A
return MessageAttachment.builder() return MessageAttachment.builder()
.attachment(attachmentMapper.getAttachment(AttachmentId.from(attachment.getBlobId().getRawValue()))) .attachment(attachmentMapper.getAttachment(AttachmentId.from(attachment.getBlobId().getRawValue())))
.name(attachment.getName().orElse(null)) .name(attachment.getName().orElse(null))
.cid(attachment.getCid().orElse(null)) .cid(attachment.getCid().map(Cid::from).orElse(null))
.isInline(attachment.isIsInline()) .isInline(attachment.isIsInline())
.build(); .build();
} catch (AttachmentNotFoundException e) { } catch (AttachmentNotFoundException e) {
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.james.mailbox.store.extractor.DefaultTextExtractor; import org.apache.james.mailbox.store.extractor.DefaultTextExtractor;
import org.apache.james.mailbox.store.mail.model.MailboxMessage; import org.apache.james.mailbox.store.mail.model.MailboxMessage;
import org.apache.james.mailbox.store.mail.model.MessageAttachment; import org.apache.james.mailbox.store.mail.model.MessageAttachment;
import org.apache.james.mailbox.store.mail.model.impl.Cid;
import org.apache.james.util.streams.ImmutableCollectors; import org.apache.james.util.streams.ImmutableCollectors;


import com.google.common.base.Strings; import com.google.common.base.Strings;
Expand Down Expand Up @@ -163,7 +164,7 @@ private Attachment fromMailboxAttachment(MessageAttachment attachment) {
.type(attachment.getAttachment().getType()) .type(attachment.getAttachment().getType())
.size(attachment.getAttachment().getSize()) .size(attachment.getAttachment().getSize())
.name(attachment.getName().orNull()) .name(attachment.getName().orNull())
.cid(attachment.getCid().orNull()) .cid(attachment.getCid().transform(Cid::getValue).orNull())
.isInline(attachment.isInline()) .isInline(attachment.isInline())
.build(); .build();
} }
Expand Down
Loading

0 comments on commit b87c939

Please sign in to comment.