MAILBOX-270: getmetadata command, new reponse encoder
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/b855debe Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/b855debe Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/b855debe Branch: refs/heads/master Commit: b855debed87c925d100409b71736d33aa16a789b Parents: 0794e4c Author: Quynh Nguyen <[email protected]> Authored: Tue Jul 5 13:57:00 2016 +0700 Committer: Quynh Nguyen <[email protected]> Committed: Tue Aug 30 09:21:30 2016 +0700 ---------------------------------------------------------------------- .../imap/encode/AnnotationResponseEncoder.java | 70 ++++++++++++ .../message/response/AnnotationResponse.java | 70 ++++++++++++ .../encode/AnnotationResponseEncoderTest.java | 109 +++++++++++++++++++ 3 files changed, 249 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/b855debe/protocols/imap/src/main/java/org/apache/james/imap/encode/AnnotationResponseEncoder.java ---------------------------------------------------------------------- diff --git a/protocols/imap/src/main/java/org/apache/james/imap/encode/AnnotationResponseEncoder.java b/protocols/imap/src/main/java/org/apache/james/imap/encode/AnnotationResponseEncoder.java new file mode 100644 index 0000000..d78cfb6 --- /dev/null +++ b/protocols/imap/src/main/java/org/apache/james/imap/encode/AnnotationResponseEncoder.java @@ -0,0 +1,70 @@ +/**************************************************************** + * 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.imap.encode; + +import java.io.IOException; +import java.util.List; + +import com.google.common.base.Optional; +import org.apache.james.imap.api.ImapConstants; +import org.apache.james.imap.api.ImapMessage; +import org.apache.james.imap.api.process.ImapSession; +import org.apache.james.imap.encode.base.AbstractChainedImapEncoder; +import org.apache.james.imap.message.response.AnnotationResponse; +import org.apache.james.mailbox.model.MailboxAnnotation; + +public class AnnotationResponseEncoder extends AbstractChainedImapEncoder { + + public AnnotationResponseEncoder(ImapEncoder next) { + super(next); + } + + protected void doEncode(ImapMessage acceptableMessage, final ImapResponseComposer composer, ImapSession session) throws IOException { + + AnnotationResponse response = (AnnotationResponse) acceptableMessage; + + composer.untagged(); + composer.commandName(ImapConstants.ANNOTATION_RESPONSE_NAME); + + composer.quote(Optional.fromNullable(response.getMailboxName()).or("")); + composeAnnotations(composer, response.getMailboxAnnotations(), session); + + composer.end(); + } + + private void composeAnnotations(ImapResponseComposer composer, List<MailboxAnnotation> annotations, ImapSession session) throws IOException { + if (!annotations.isEmpty()) { + composer.openParen(); + for (MailboxAnnotation annotation : annotations) { + if (annotation.isNil()) { + session.getLog().warn("There is nil data of key {} on store: ", annotation.getKey().getKey()); + } else { + composer.message(annotation.getKey().getKey()); + composer.quote(annotation.getValue().or("")); + } + } + composer.closeParen(); + } + } + + public boolean isAcceptable(ImapMessage message) { + return message instanceof AnnotationResponse; + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/b855debe/protocols/imap/src/main/java/org/apache/james/imap/message/response/AnnotationResponse.java ---------------------------------------------------------------------- diff --git a/protocols/imap/src/main/java/org/apache/james/imap/message/response/AnnotationResponse.java b/protocols/imap/src/main/java/org/apache/james/imap/message/response/AnnotationResponse.java new file mode 100644 index 0000000..6bd7872 --- /dev/null +++ b/protocols/imap/src/main/java/org/apache/james/imap/message/response/AnnotationResponse.java @@ -0,0 +1,70 @@ +/**************************************************************** + * 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.imap.message.response; + +import java.util.List; + +import com.google.common.base.MoreObjects; +import com.google.common.collect.ImmutableList; +import org.apache.james.imap.api.message.response.ImapResponseMessage; +import org.apache.james.mailbox.model.MailboxAnnotation; + +import com.google.common.base.Objects; + +public class AnnotationResponse implements ImapResponseMessage { + private final String mailboxName; + private final List<MailboxAnnotation> mailboxAnnotations; + + public AnnotationResponse(String mailboxName, List<MailboxAnnotation> mailboxAnnotations) { + this.mailboxName = mailboxName; + this.mailboxAnnotations = ImmutableList.copyOf(mailboxAnnotations); + } + + public String getMailboxName() { + return mailboxName; + } + + public List<MailboxAnnotation> getMailboxAnnotations() { + return mailboxAnnotations; + } + + @Override + public int hashCode() { + return Objects.hashCode(mailboxName, mailboxAnnotations); + } + + @Override + public boolean equals(Object obj) { + if (obj instanceof AnnotationResponse) { + AnnotationResponse o = (AnnotationResponse) obj; + return Objects.equal(mailboxName, o.getMailboxName()) && Objects.equal(mailboxAnnotations, o.getMailboxAnnotations()); + } else { + return false; + } + } + + public String toString() { + return MoreObjects.toStringHelper(AnnotationResponse.class) + .add("mailboxName", mailboxName) + .add("mailboxAnnotation", mailboxAnnotations) + .toString(); + } + +} http://git-wip-us.apache.org/repos/asf/james-project/blob/b855debe/protocols/imap/src/test/java/org/apache/james/imap/encode/AnnotationResponseEncoderTest.java ---------------------------------------------------------------------- diff --git a/protocols/imap/src/test/java/org/apache/james/imap/encode/AnnotationResponseEncoderTest.java b/protocols/imap/src/test/java/org/apache/james/imap/encode/AnnotationResponseEncoderTest.java new file mode 100644 index 0000000..86c4ad9 --- /dev/null +++ b/protocols/imap/src/test/java/org/apache/james/imap/encode/AnnotationResponseEncoderTest.java @@ -0,0 +1,109 @@ +/**************************************************************** + * 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.imap.encode; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.google.common.collect.ImmutableList; +import org.apache.james.imap.api.process.ImapSession; +import org.apache.james.imap.encode.base.ByteImapResponseWriter; +import org.apache.james.imap.encode.base.EndImapEncoder; +import org.apache.james.imap.encode.base.ImapResponseComposerImpl; +import org.apache.james.imap.message.response.AnnotationResponse; +import org.apache.james.mailbox.model.MailboxAnnotation; +import org.apache.james.mailbox.model.MailboxAnnotationKey; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; + +public class AnnotationResponseEncoderTest { + private static final MailboxAnnotationKey PRIVATE_KEY = new MailboxAnnotationKey("/private/comment"); + private static final MailboxAnnotationKey SHARED_KEY = new MailboxAnnotationKey("/shared/comment"); + + private static final MailboxAnnotation PRIVATE_ANNOTATION = MailboxAnnotation.newInstance(PRIVATE_KEY, "My own comment"); + private static final MailboxAnnotation SHARED_ANNOTATION = MailboxAnnotation.newInstance(SHARED_KEY, "Shared comment"); + + private ByteImapResponseWriter byteImapResponseWriter; + private ImapResponseComposer composer; + private AnnotationResponseEncoder encoder; + private ImapSession imapSession; + private Logger log; + + @Before + public void setUp() throws Exception { + byteImapResponseWriter = new ByteImapResponseWriter(); + + imapSession = mock(ImapSession.class); + log = mock(Logger.class); + + when(imapSession.getLog()).thenReturn(log); + + composer = new ImapResponseComposerImpl(byteImapResponseWriter, 1024); + encoder = new AnnotationResponseEncoder(new EndImapEncoder()); + } + + @Test + public void encodingShouldWellFormEmptyRequest() throws Exception { + AnnotationResponse response = new AnnotationResponse(null, ImmutableList.<MailboxAnnotation>of()); + + encoder.encode(response, composer, imapSession); + + assertThat(byteImapResponseWriter.getString()).isEqualTo("* METADATA \"\"\r\n"); + } + + @Test + public void encodingShouldWellFormWhenEmptyReturnedAnnotation() throws Exception { + AnnotationResponse response = new AnnotationResponse("INBOX", ImmutableList.<MailboxAnnotation>of()); + + encoder.encode(response, composer, imapSession); + + assertThat(byteImapResponseWriter.getString()).isEqualTo("* METADATA \"INBOX\"\r\n"); + } + + @Test + public void encodingShouldWellFormWhenOnlyOneReturnedAnnotation() throws Exception { + AnnotationResponse response = new AnnotationResponse("INBOX", ImmutableList.of(PRIVATE_ANNOTATION)); + + encoder.encode(response, composer, imapSession); + + assertThat(byteImapResponseWriter.getString()).isEqualTo("* METADATA \"INBOX\" (/private/comment \"My own comment\")\r\n"); + } + + @Test + public void encodingShouldWellFormWhenManyReturnedAnnotations() throws Exception { + AnnotationResponse response = new AnnotationResponse("INBOX", ImmutableList.of(PRIVATE_ANNOTATION, SHARED_ANNOTATION)); + encoder.encode(response, composer, imapSession); + + assertThat(byteImapResponseWriter.getString()).isEqualTo("* METADATA \"INBOX\" (/private/comment \"My own comment\" /shared/comment \"Shared comment\")\r\n"); + } + + @Test + public void encodingShouldWellFormWhenNilReturnedAnnotation() throws Exception { + AnnotationResponse response = new AnnotationResponse("INBOX", ImmutableList.of(MailboxAnnotation.nil(PRIVATE_KEY))); + + encoder.encode(response, composer, imapSession); + + verify(log).warn("There is nil data of key {} on store: ", PRIVATE_KEY.getKey()); + assertThat(byteImapResponseWriter.getString()).isEqualTo("* METADATA \"INBOX\" ()\r\n"); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
