Skip to content

Commit 6c96eea

Browse files
Merge pull request #160 from dmitraver/read-attachments-from-inputstrem_134
Adds an attachment builder that supports InputStream content.
2 parents 207a872 + c04e25f commit 6c96eea

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed

src/main/java/com/sendgrid/helpers/mail/objects/Attachments.java

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package com.sendgrid;
22

3+
import com.fasterxml.jackson.annotation.JsonIgnoreType;
34
import com.fasterxml.jackson.annotation.JsonInclude;
45
import com.fasterxml.jackson.annotation.JsonInclude.Include;
56
import com.fasterxml.jackson.annotation.JsonProperty;
7+
import org.apache.commons.codec.binary.Base64;
8+
9+
import java.io.*;
610

711
@JsonInclude(Include.NON_DEFAULT)
812
public class Attachments {
@@ -56,4 +60,81 @@ public String getContentId() {
5660
public void setContentId(String contentId) {
5761
this.contentId = contentId;
5862
}
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+
}
59140
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.sendgrid.helpers;
2+
3+
import com.sendgrid.Attachments;
4+
import org.apache.commons.codec.binary.Base64;
5+
import org.junit.Assert;
6+
import org.junit.Test;
7+
8+
import java.io.ByteArrayInputStream;
9+
import java.io.InputStream;
10+
import java.nio.charset.Charset;
11+
12+
public class AttachmentBuilderTest {
13+
14+
@Test
15+
public void testCreateAttachments() {
16+
String fileName = "book.txt";
17+
String type = "text/plain";
18+
String content = "This test checks if the builder works fine";
19+
InputStream contentStream = new ByteArrayInputStream(content.getBytes(Charset.forName("UTF-8")));
20+
String contentId = "someId";
21+
String dispositon = "someDisposition";
22+
23+
Attachments attachments = new Attachments.Builder(fileName, contentStream)
24+
.withType(type)
25+
.withContentId(contentId)
26+
.withDisposition(dispositon)
27+
.build();
28+
29+
Assert.assertEquals(attachments.getType(), type);
30+
Assert.assertEquals(attachments.getFilename(), fileName);
31+
Assert.assertEquals(attachments.getContentId(), contentId);
32+
Assert.assertEquals(attachments.getDisposition(), dispositon);
33+
Assert.assertEquals(attachments.getContent(), Base64.encodeBase64String(content.getBytes(Charset.forName("UTF-8"))));
34+
}
35+
36+
@Test(expected = IllegalArgumentException.class)
37+
public void testCreateAttachmentsMissingRequiredFileNameParam() {
38+
String content = "This test checks if the builder works fine";
39+
InputStream contentStream = new ByteArrayInputStream(content.getBytes(Charset.forName("UTF-8")));
40+
Attachments attachments = new Attachments.Builder(null, contentStream).build();
41+
}
42+
43+
@Test(expected = IllegalArgumentException.class)
44+
public void testCreateAttachmentsMissingRequiredContentParam() {
45+
String type = "text";
46+
String content = null;
47+
Attachments attachments = new Attachments.Builder(type, content).build();
48+
}
49+
}

0 commit comments

Comments
 (0)