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

com.revinate.sendgrid.model.Email Maven / Gradle / Ivy

The newest version!
package com.revinate.sendgrid.model;

import com.revinate.sendgrid.smtpapi.SmtpApi;
import com.revinate.sendgrid.smtpapi.SmtpApiException;
import com.revinate.sendgrid.smtpapi.SmtpApiImpl;
import com.revinate.sendgrid.util.JsonUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Email extends SendGridModel implements SmtpApi {

    private List tos = new ArrayList();
    private List toNames = new ArrayList();
    private List ccs = new ArrayList();
    private List ccNames = new ArrayList();
    private List bccs = new ArrayList();
    private List bccNames = new ArrayList();
    private String from;
    private String fromName;
    private String replyTo;
    private String subject;
    private String text;
    private String html;
    private Map attachments = new HashMap();
    private Map contentIds = new HashMap();
    private Map headers = new HashMap();

    private SmtpApi smtpApi = new SmtpApiImpl();

    public List getTos() {
        return new ArrayList(tos);
    }

    public Email setTos(List addresses) {
        tos = new ArrayList(addresses);
        return this;
    }

    public Email addTo(String address) {
        tos.add(address);
        return this;
    }

    public Email addTo(String address, String name) {
        addTo(address);
        addToName(name);
        return this;
    }

    public List getToNames() {
        return new ArrayList(toNames);
    }

    public Email setToNames(List names) {
        toNames = new ArrayList(names);
        return this;
    }

    public Email addToName(String name) {
        toNames.add(name);
        return this;
    }

    public List getCcs() {
        return new ArrayList(ccs);
    }

    public Email setCcs(List addresses) {
        ccs = new ArrayList(addresses);
        return this;
    }

    public Email addCc(String address) {
        ccs.add(address);
        return this;
    }

    public Email addCc(String address, String name) {
        addCc(address);
        addCcName(name);
        return this;
    }

    public List getCcNames() {
        return new ArrayList(ccNames);
    }

    public Email setCcNames(List names) {
        ccNames = new ArrayList(names);
        return this;
    }

    public Email addCcName(String name) {
        ccNames.add(name);
        return this;
    }

    public List getBccs() {
        return new ArrayList(bccs);
    }

    public Email setBccs(List addresses) {
        bccs = new ArrayList(addresses);
        return this;
    }

    public Email addBcc(String address) {
        bccs.add(address);
        return this;
    }

    public Email addBcc(String address, String name) {
        addBcc(address);
        addBccName(name);
        return this;
    }

    public List getBccNames() {
        return new ArrayList(bccNames);
    }

    public Email setBccNames(List names) {
        bccNames = new ArrayList(names);
        return this;
    }

    public Email addBccName(String name) {
        bccNames.add(name);
        return this;
    }

    public String getFrom() {
        return from;
    }

    public Email setFrom(String address) {
        from = address;
        return this;
    }

    public Email setFrom(String address, String name) {
        setFrom(address);
        setFromName(name);
        return this;
    }

    public String getFromName() {
        return fromName;
    }

    public Email setFromName(String name) {
        fromName = name;
        return this;
    }

    public String getReplyTo() {
        return replyTo;
    }

    public Email setReplyTo(String address) {
        replyTo = address;
        return this;
    }

    public String getSubject() {
        return subject;
    }

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

    public String getText() {
        return text;
    }

    public Email setText(String text) {
        this.text = text;
        return this;
    }

    public String getHtml() {
        return html;
    }

    public Email setHtml(String html) {
        this.html = html;
        return this;
    }

    public Map getAttachments() {
        return new HashMap(attachments);
    }

    public Email setAttachments(Map attachments) {
        this.attachments = new HashMap(attachments);
        return this;
    }

    public InputStream getAttachment(String name) {
        return attachments.get(name);
    }

    public Email setAttachment(String name, InputStream file) {
        attachments.put(name, file);
        return this;
    }

    public Email setAttachment(String name, InputStream file, String contentId) {
        setAttachment(name, file);
        setContentId(name, contentId);
        return this;
    }

    public Email setAttachment(String name, File file) throws IOException {
        return setAttachment(name, new FileInputStream(file));
    }

    public Email setAttachment(String name, String file) {
        return setAttachment(name, new ByteArrayInputStream(file.getBytes()));
    }

    public Map getContentIds() {
        return new HashMap(contentIds);
    }

    public Email setContentIds(Map contentIds) {
        this.contentIds = new HashMap(contentIds);
        return this;
    }

    public String getContentId(String key) {
        return contentIds.get(key);
    }

    public Email setContentId(String key, String val) {
        contentIds.put(key, val);
        return this;
    }

    public Map getHeaders() {
        return new HashMap(headers);
    }

    public Email setHeaders(Map headers) {
        this.headers = new HashMap(headers);
        return this;
    }

    public String getHeader(String key) {
        return headers.get(key);
    }

    public Email setHeader(String key, String val) {
        headers.put(key, val);
        return this;
    }

    public String toHeaders() {
        try {
            return JsonUtils.toJson(headers);
        } catch (IOException e) {
            return "{}";
        }
    }

    /**
     * Convenience method to set the template
     *
     * @param templateId The ID string of your template
     * @return this
     */
    public Email setTemplateId(String templateId) {
        Map filter = new HashMap();
        filter.put("enable", 1);
        filter.put("template_id", templateId);
        setFilter("templates", filter);
        return this;
    }

    // Implementations of SMTP API methods: delegate to the SmtpApiImpl

    @Override
    public String getVersion() {
        return smtpApi.getVersion();
    }

    @Override
    public String toSmtpApiHeader() {
        return smtpApi.toSmtpApiHeader();
    }

    @Override
    public String toRawSmtpApiHeader() {
        return smtpApi.toRawSmtpApiHeader();
    }

    @Override
    public Integer getAsmGroupId() {
        return smtpApi.getAsmGroupId();
    }

    @Override
    public Email setAsmGroupId(Integer val) {
        smtpApi.setAsmGroupId(val);
        return this;
    }

    @Override
    public Integer getSendAt() {
        return smtpApi.getSendAt();
    }

    @Override
    public Email setSendAt(Integer val) {
        smtpApi.setSendAt(val);
        return this;
    }

    @Override
    public String getIpPool() {
        return smtpApi.getIpPool();
    }

    @Override
    public Email setIpPool(String ipPool) {
        smtpApi.setIpPool(ipPool);
        return this;
    }

    @Override
    public List getSmtpApiTos() {
        return smtpApi.getSmtpApiTos();
    }

    @Override
    public Email setSmtpApiTos(List tos) {
        smtpApi.setSmtpApiTos(tos);
        return this;
    }

    @Override
    public Email addSmtpApiTo(String to) {
        smtpApi.addSmtpApiTo(to);
        return this;
    }

    @Override
    public Email addSmtpApiTo(String address, String name) {
        smtpApi.addSmtpApiTo(address, name);
        return this;
    }

    @Override
    public List getCategories() {
        return smtpApi.getCategories();
    }

    @Override
    public Email setCategories(List categories) {
        smtpApi.setCategories(categories);
        return this;
    }

    @Override
    public Email addCategory(String category) {
        smtpApi.addCategory(category);
        return this;
    }

    @Override
    public Map getUniqueArgs() {
        return smtpApi.getUniqueArgs();
    }

    @Override
    public Email setUniqueArgs(Map args) {
        smtpApi.setUniqueArgs(args);
        return this;
    }

    @Override
    public String getUniqueArg(String key) {
        return smtpApi.getUniqueArg(key);
    }

    @Override
    public Email setUniqueArg(String key, String val) {
        smtpApi.setUniqueArg(key, val);
        return this;
    }

    @Override
    public Map getSections() {
        return smtpApi.getSections();
    }

    @Override
    public Email setSections(Map sections) {
        smtpApi.setSections(sections);
        return this;
    }

    @Override
    public String getSection(String key) {
        return smtpApi.getSection(key);
    }

    @Override
    public Email setSection(String key, String val) {
        smtpApi.setSection(key, val);
        return this;
    }

    @Override
    public Map> getSubstitutions() {
        return smtpApi.getSubstitutions();
    }

    @Override
    public Email setSubstitutions(Map> substitutions) {
        smtpApi.setSubstitutions(substitutions);
        return this;
    }

    @Override
    public List getSubstitution(String key) {
        return smtpApi.getSubstitution(key);
    }

    @Override
    public Email setSubstitution(String key, List vals) {
        smtpApi.setSubstitution(key, vals);
        return this;
    }

    @Override
    public Email addValueToSubstitution(String key, String val) {
        smtpApi.addValueToSubstitution(key, val);
        return this;
    }

    @Override
    public Map> getFilters() {
        return smtpApi.getFilters();
    }

    @Override
    public Email setFilters(Map> filters) throws SmtpApiException {
        smtpApi.setFilters(filters);
        return this;
    }

    @Override
    public Map getFilter(String filterName) {
        return smtpApi.getFilter(filterName);
    }

    @Override
    public Email setFilter(String filterName, Map filter) throws SmtpApiException {
        smtpApi.setFilter(filterName, filter);
        return this;
    }

    @Override
    public Email setSettingInFilter(String filterName, String settingName, Object settingVal) throws SmtpApiException {
        smtpApi.setSettingInFilter(filterName, settingName, settingVal);
        return this;
    }

    // Override accept from SendGridModel - Email needs custom HttpEntity construction

    @Override
    public void accept(SendGridModelVisitor visitor) {
        visitor.visit(this);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy