Skip to content

Commit

Permalink
JAMES-1715 Aggregate SetMailboxes processors response
Browse files Browse the repository at this point in the history
  • Loading branch information
aduprat committed Apr 5, 2016
1 parent 68ab617 commit 169e7bf
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 12 deletions.
Expand Up @@ -98,7 +98,7 @@ private void createMailbox(MailboxCreationId mailboxCreationId, MailboxRequest m
mailboxManager.createMailbox(mailboxPath, mailboxSession);
Optional<Mailbox> mailbox = mailboxUtils.mailboxFromMailboxPath(mailboxPath, mailboxSession);
if (mailbox.isPresent()) {
builder.creation(mailboxCreationId, mailbox.get());
builder.created(mailboxCreationId, mailbox.get());
creationIdsToCreatedMailboxId.put(mailboxCreationId, mailbox.get().getId());
} else {
builder.notCreated(mailboxCreationId, SetError.builder()
Expand Down
Expand Up @@ -62,16 +62,20 @@ public Stream<JmapResponse> process(JmapRequest request, ClientId clientId, Mail
Preconditions.checkNotNull(mailboxSession);
Preconditions.checkArgument(request instanceof SetMailboxesRequest);
SetMailboxesRequest setMailboxesRequest = (SetMailboxesRequest) request;
return processors.stream()
.map(processor -> processor.process(setMailboxesRequest, mailboxSession))
.map(response -> toJmapResponse(clientId, response));
return Stream.of(
JmapResponse.builder().clientId(clientId)
.response(setMailboxesResponse(setMailboxesRequest, mailboxSession))
.responseName(RESPONSE_NAME)
.build());
}

private JmapResponse toJmapResponse(ClientId clientId, SetMailboxesResponse response) {
return JmapResponse.builder()
.clientId(clientId)
.responseName(RESPONSE_NAME)
.response(response)
private SetMailboxesResponse setMailboxesResponse(SetMailboxesRequest request, MailboxSession mailboxSession) {
return processors.stream()
.map(processor -> processor.process(request, mailboxSession))
.reduce(SetMailboxesResponse.builder(),
(builder, resp) -> resp.mergeInto(builder) ,
(builder1, builder2) -> builder2.build().mergeInto(builder1)
)
.build();
}
}
Expand Up @@ -23,6 +23,7 @@
import org.apache.james.jmap.methods.Method;
import org.apache.james.jmap.model.mailbox.Mailbox;

import com.google.common.base.Objects;
import com.google.common.collect.ImmutableMap;

public class SetMailboxesResponse implements Method.Response {
Expand All @@ -41,11 +42,16 @@ private Builder() {
notCreated = ImmutableMap.builder();
}

public Builder creation(MailboxCreationId creationId, Mailbox mailbox) {
public Builder created(MailboxCreationId creationId, Mailbox mailbox) {
created.put(creationId, mailbox);
return this;
}

public Builder created(ImmutableMap<MailboxCreationId, Mailbox> created) {
this.created.putAll(created);
return this;
}

public Builder notCreated(Map<MailboxCreationId, SetError> notCreated) {
this.notCreated.putAll(notCreated);
return this;
Expand All @@ -59,7 +65,6 @@ public Builder notCreated(MailboxCreationId mailboxCreationId, SetError setError
public SetMailboxesResponse build() {
return new SetMailboxesResponse(created.build(), notCreated.build());
}

}

private final ImmutableMap<MailboxCreationId, Mailbox> created;
Expand All @@ -77,4 +82,23 @@ public ImmutableMap<MailboxCreationId, Mailbox> getCreated() {
public Map<MailboxCreationId, SetError> getNotCreated() {
return notCreated;
}

public SetMailboxesResponse.Builder mergeInto(SetMailboxesResponse.Builder responseBuilder) {
return responseBuilder
.created(getCreated());
}

@Override
public int hashCode() {
return Objects.hashCode(created);
}

@Override
public boolean equals(Object obj) {
if (obj instanceof SetMailboxesResponse) {
SetMailboxesResponse other = (SetMailboxesResponse) obj;
return Objects.equal(this.created, other.created);
}
return false;
}
}
Expand Up @@ -93,7 +93,7 @@ public void processShouldCallCreatorProcessorWhenCreationRequest() {
SetMailboxesRequest creationRequest = SetMailboxesRequest.builder().create(creationId, fooFolder).build();

Mailbox createdfooFolder = Mailbox.builder().name("fooFolder").id("fooId").build();
SetMailboxesResponse creationResponse = SetMailboxesResponse.builder().creation(creationId, createdfooFolder).build();
SetMailboxesResponse creationResponse = SetMailboxesResponse.builder().created(creationId, createdfooFolder).build();
JmapResponse jmapResponse = JmapResponse.builder()
.response(creationResponse)
.clientId(ClientId.of("clientId"))
Expand Down

0 comments on commit 169e7bf

Please sign in to comment.