|
1 | 1 | package com.sendgrid; |
2 | 2 |
|
| 3 | +import com.fasterxml.jackson.annotation.JsonIgnoreType; |
3 | 4 | import com.fasterxml.jackson.annotation.JsonInclude; |
4 | 5 | import com.fasterxml.jackson.annotation.JsonInclude.Include; |
5 | 6 | import com.fasterxml.jackson.annotation.JsonProperty; |
| 7 | +import org.apache.commons.codec.binary.Base64; |
| 8 | + |
| 9 | +import java.io.*; |
6 | 10 |
|
7 | 11 | @JsonInclude(Include.NON_DEFAULT) |
8 | 12 | public class Attachments { |
@@ -56,4 +60,81 @@ public String getContentId() { |
56 | 60 | public void setContentId(String contentId) { |
57 | 61 | this.contentId = contentId; |
58 | 62 | } |
| 63 | + |
| 64 | + @JsonIgnoreType |
| 65 | + public static class Builder { |
| 66 | + |
| 67 | + private static final int BYTE_BUFFER_SIZE = 4096; |
| 68 | + |
| 69 | + private String fileName; |
| 70 | + private String content; |
| 71 | + private String type; |
| 72 | + private String disposition; |
| 73 | + private String contentId; |
| 74 | + |
| 75 | + public Builder(String fileName, InputStream content) { |
| 76 | + if (fileName == null) { |
| 77 | + throw new IllegalArgumentException("File name mustn't be null"); |
| 78 | + } |
| 79 | + |
| 80 | + if (content == null) { |
| 81 | + throw new IllegalArgumentException("Content mustn't be null"); |
| 82 | + } |
| 83 | + |
| 84 | + this.fileName = fileName; |
| 85 | + this.content = encodeToBase64(content); |
| 86 | + } |
| 87 | + |
| 88 | + public Builder(String fileName, String content) { |
| 89 | + if (fileName == null) { |
| 90 | + throw new IllegalArgumentException("File name mustn't be null"); |
| 91 | + } |
| 92 | + |
| 93 | + if (content == null) { |
| 94 | + throw new IllegalArgumentException("Content mustn't be null"); |
| 95 | + } |
| 96 | + |
| 97 | + this.fileName = fileName; |
| 98 | + this.content = content; |
| 99 | + } |
| 100 | + |
| 101 | + private String encodeToBase64(InputStream content) { |
| 102 | + int read = 0; |
| 103 | + byte[] bytes = new byte[BYTE_BUFFER_SIZE]; |
| 104 | + try(ByteArrayOutputStream baos = new ByteArrayOutputStream()) { |
| 105 | + while ((read = content.read(bytes)) != -1) { |
| 106 | + baos.write(bytes, 0, read); |
| 107 | + } |
| 108 | + |
| 109 | + return Base64.encodeBase64String(baos.toByteArray()); |
| 110 | + } catch (IOException e) { |
| 111 | + throw new RuntimeException("Unable to convert content stream to base 64 encoded string", e); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + public Builder withType(String type) { |
| 116 | + this.type = type; |
| 117 | + return this; |
| 118 | + } |
| 119 | + |
| 120 | + public Builder withDisposition(String disposition) { |
| 121 | + this.disposition = disposition; |
| 122 | + return this; |
| 123 | + } |
| 124 | + |
| 125 | + public Builder withContentId(String contentId) { |
| 126 | + this.contentId = contentId; |
| 127 | + return this; |
| 128 | + } |
| 129 | + |
| 130 | + public Attachments build() { |
| 131 | + Attachments attachments = new Attachments(); |
| 132 | + attachments.setContent(content); |
| 133 | + attachments.setFilename(fileName); |
| 134 | + attachments.setDisposition(disposition); |
| 135 | + attachments.setContentId(contentId); |
| 136 | + attachments.setType(type); |
| 137 | + return attachments; |
| 138 | + } |
| 139 | + } |
59 | 140 | } |
0 commit comments