forked from sendgrid/sendgrid-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAttachment.java
More file actions
244 lines (211 loc) · 6.82 KB
/
Attachment.java
File metadata and controls
244 lines (211 loc) · 6.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package com.sendgrid;
import com.fasterxml.jackson.annotation.JsonIgnoreType;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.commons.codec.binary.Base64;
import java.io.*;
/**
* An attachment object.
*/
@JsonInclude(Include.NON_DEFAULT)
public class Attachment {
@JsonProperty("content")
private String content;
@JsonProperty("type")
private String type;
@JsonProperty("filename")
private String filename;
@JsonProperty("disposition")
private String disposition;
/**
* The attachment content ID. This is used when the
* disposition is set to “inline” and the attachment
* is an image, allowing the file to be displayed within
* the body of your email.
*/
@JsonProperty("content_id")
private String contentId;
/**
* Get the attachment's content.
* @return the content.
*/
@JsonProperty("content")
public String getContent() {
return content;
}
/**
* Set the attachment's content.
* @param content the content.
*/
public Attachment content(String content) {
this.content = content;
return this;
}
/**
* Get the mime type of the content you are attaching. For example,
* “text/plain” or “text/html”.
* @return the mime type.
*/
@JsonProperty("type")
public String getType() {
return type;
}
/**
* Set the mime type of the content.
* @param type the mime type.
*/
public Attachment type(String type) {
this.type = type;
return this;
}
/**
* Get the filename for this attachment.
* @return the file name.
*/
@JsonProperty("filename")
public String getFilename() {
return filename;
}
/**
* Set the filename for this attachment.
* @param filename the filename.
*/
public Attachment filename(String filename) {
this.filename = filename;
return this;
}
/**
* Get the content-disposition of the attachment specifying
* how you would like the attachment to be displayed.
* For example, “inline” results in the attached file
* being displayed automatically within the message
* while “attachment” results in the attached file
* requiring some action to be taken before it is
* displayed (e.g. opening or downloading the file).
* @return the disposition.
*/
@JsonProperty("disposition")
public String getDisposition() {
return disposition;
}
/**
* Set the content-disposition of the attachment.
* @param disposition the disposition.
*/
public Attachment disposition(String disposition) {
this.disposition = disposition;
return this;
}
/**
* Get the attachment content ID. This is used when the
* disposition is set to “inline” and the attachment
* is an image, allowing the file to be displayed within
* the body of your email.
* @return the content ID.
*/
@JsonProperty("content_id")
public String getContentId() {
return contentId;
}
/**
* Set the content ID.
* @param contentId the content ID.
*/
public Attachment contentId(String contentId) {
this.contentId = contentId;
return this;
}
/**
* A helper object to construct usable attachment.
*/
@JsonIgnoreType
public static class Builder {
private static final int BYTE_BUFFER_SIZE = 4096;
private String fileName;
private String content;
private String type;
private String disposition;
private String contentId;
/**
* Construct a new attachment builder.
* @param fileName the filename to include.
* @param content an input stream for the content.
* @throws IllegalArgumentException in case either the fileName or the content is null.
*/
public Builder(String fileName, InputStream content) {
if (fileName == null) {
throw new IllegalArgumentException("File name mustn't be null");
}
if (content == null) {
throw new IllegalArgumentException("Content mustn't be null");
}
this.fileName = fileName;
this.content = encodeToBase64(content);
}
/**
* Construct a new attachment builder.
* @param fileName the filename to include.
* @param content an input string for the content.
* @throws IllegalArgumentException in case either the fileName or the content is null.
*/
public Builder(String fileName, String content) {
if (fileName == null) {
throw new IllegalArgumentException("File name mustn't be null");
}
if (content == null) {
throw new IllegalArgumentException("Content mustn't be null");
}
this.fileName = fileName;
this.content = content;
}
private String encodeToBase64(InputStream content) {
int read = 0;
byte[] bytes = new byte[BYTE_BUFFER_SIZE];
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
while ((read = content.read(bytes)) != -1) {
baos.write(bytes, 0, read);
}
return Base64.encodeBase64String(baos.toByteArray());
} catch (IOException e) {
throw new RuntimeException("Unable to convert content stream to base 64 encoded string", e);
}
}
/**
* Set the type of this attachment builder.
* @param type the attachment type.
*/
public Builder withType(String type) {
this.type = type;
return this;
}
/**
* Set the disposition of this attachment builder.
* @param disposition the disposition.
*/
public Builder withDisposition(String disposition) {
this.disposition = disposition;
return this;
}
/**
* Set the content ID of this attachment builder.
* @param contentId the content ID.
*/
public Builder withContentId(String contentId) {
this.contentId = contentId;
return this;
}
/**
* Construct the attachment object.
*/
public Attachment build() {
Attachment attachment = new Attachment()
.content(content)
.filename(fileName)
.disposition(disposition)
.contentId(contentId)
.type(type);
return attachment;
}
}
}