All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.james.jmap.model.SubMessage Maven / Gradle / Ivy

/****************************************************************
 * 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.jmap.model;

import java.time.ZonedDateTime;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;

@JsonDeserialize(builder = SubMessage.Builder.class)
public class SubMessage {

    public static Builder builder() {
        return new Builder();
    }

    @JsonPOJOBuilder(withPrefix = "")
    public static class Builder {
        private ImmutableMap headers;
        private Emailer from;
        private final ImmutableList.Builder to;
        private final ImmutableList.Builder cc;
        private final ImmutableList.Builder bcc;
        private final ImmutableList.Builder replyTo;
        private String subject;
        private ZonedDateTime date;
        private Optional textBody = Optional.empty();
        private Optional htmlBody = Optional.empty();
        private final ImmutableList.Builder attachments;
        private final ImmutableMap.Builder attachedMessages;
        
        private Builder() {
            to = ImmutableList.builder();
            cc = ImmutableList.builder();
            bcc = ImmutableList.builder();
            replyTo = ImmutableList.builder();
            attachments = ImmutableList.builder();
            attachedMessages = ImmutableMap.builder();
        }

        public Builder headers(ImmutableMap headers) {
            this.headers = headers;
            return this;
        }

        public Builder from(Emailer from) {
            this.from = from;
            return this;
        }

        public Builder to(List to) {
            this.to.addAll(to);
            return this;
        }

        public Builder cc(List cc) {
            this.cc.addAll(cc);
            return this;
        }

        public Builder bcc(List bcc) {
            this.bcc.addAll(bcc);
            return this;
        }

        public Builder replyTo(List replyTo) {
            this.replyTo.addAll(replyTo);
            return this;
        }

        public Builder subject(String subject) {
            this.subject = subject;
            return this;
        }

        public Builder date(ZonedDateTime date) {
            this.date = date;
            return this;
        }

        public Builder textBody(Optional textBody) {
            this.textBody = textBody;
            return this;
        }

        public Builder htmlBody(Optional htmlBody) {
            this.htmlBody = htmlBody;
            return this;
        }

        public Builder attachments(List attachments) {
            this.attachments.addAll(attachments);
            return this;
        }

        public Builder attachedMessages(Map attachedMessages) {
            this.attachedMessages.putAll(attachedMessages);
            return this;
        }

        public SubMessage build() {
            Preconditions.checkState(headers != null, "'headers' is mandatory");
            Preconditions.checkState(!Strings.isNullOrEmpty(subject), "'subject' is mandatory");
            Preconditions.checkState(date != null, "'date' is mandatory");
            ImmutableList attachments = this.attachments.build();
            ImmutableMap attachedMessages = this.attachedMessages.build();
            Preconditions.checkState(Message.areAttachedMessagesKeysInAttachments(attachments, attachedMessages), "'attachedMessages' keys must be in 'attachements'");
            return new SubMessage(headers, Optional.ofNullable(from), to.build(), cc.build(), bcc.build(),
                    replyTo.build(), subject, date, textBody, htmlBody,
                    attachments, attachedMessages
                    );
        }
    }

    private final ImmutableMap headers;
    private final Optional from;
    private final ImmutableList to;
    private final ImmutableList cc;
    private final ImmutableList bcc;
    private final ImmutableList replyTo;
    private final String subject;
    private final ZonedDateTime date;
    private final Optional textBody;
    private final Optional htmlBody;
    private final ImmutableList attachments;
    private final ImmutableMap attachedMessages;

    @VisibleForTesting SubMessage(ImmutableMap headers, Optional from, ImmutableList to, ImmutableList cc, ImmutableList bcc, ImmutableList replyTo, String subject, ZonedDateTime date, Optional textBody,
            Optional htmlBody, ImmutableList attachments, ImmutableMap attachedMessages) {
        super();
        this.headers = headers;
        this.from = from;
        this.to = to;
        this.cc = cc;
        this.bcc = bcc;
        this.replyTo = replyTo;
        this.subject = subject;
        this.date = date;
        this.textBody = textBody;
        this.htmlBody = htmlBody;
        this.attachments = attachments;
        this.attachedMessages = attachedMessages;
    }

    public ImmutableMap getHeaders() {
        return headers;
    }

    public Optional getFrom() {
        return from;
    }

    public ImmutableList getTo() {
        return to;
    }

    public ImmutableList getCc() {
        return cc;
    }

    public ImmutableList getBcc() {
        return bcc;
    }

    public ImmutableList getReplyTo() {
        return replyTo;
    }

    public String getSubject() {
        return subject;
    }

    public ZonedDateTime getDate() {
        return date;
    }

    public Optional getTextBody() {
        return textBody;
    }

    public Optional getHtmlBody() {
        return htmlBody;
    }

    public ImmutableList getAttachments() {
        return attachments;
    }

    public ImmutableMap getAttachedMessages() {
        return attachedMessages;
    }
    
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy