Skip to content

Commit b0a4f03

Browse files
Bug Fix #61 - To, ToName and Substitution values getting jumbled.
Also found another bug in handling SendGrid Response. Provided fix for that as well.
1 parent cad7642 commit b0a4f03

File tree

1 file changed

+60
-50
lines changed

1 file changed

+60
-50
lines changed

src/main/java/com/sendgrid/SendGrid.java

Lines changed: 60 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,31 @@
11
package com.sendgrid;
22

3-
import org.json.JSONObject;
43
import com.sendgrid.smtpapi.SMTPAPI;
5-
6-
import java.util.ArrayList;
7-
import java.io.ByteArrayInputStream;
8-
import java.io.FileNotFoundException;
9-
import java.util.Arrays;
10-
import java.util.HashMap;
11-
import java.util.Iterator;
12-
import java.util.Map;
13-
import java.io.FileInputStream;
14-
15-
import java.io.File;
16-
import java.io.InputStream;
17-
import java.io.IOException;
18-
19-
import org.apache.http.HttpResponse;
204
import org.apache.http.HttpEntity;
21-
import org.apache.http.entity.mime.MultipartEntityBuilder;
5+
import org.apache.http.HttpResponse;
226
import org.apache.http.client.methods.HttpPost;
23-
import org.apache.http.impl.client.HttpClientBuilder;
7+
import org.apache.http.entity.ContentType;
8+
import org.apache.http.entity.mime.MultipartEntityBuilder;
249
import org.apache.http.impl.client.CloseableHttpClient;
10+
import org.apache.http.impl.client.HttpClientBuilder;
2511
import org.apache.http.util.EntityUtils;
26-
import org.apache.http.entity.ContentType;
12+
import org.json.JSONException;
13+
import org.json.JSONObject;
14+
15+
import java.io.*;
16+
import java.util.*;
2717

2818
public class SendGrid {
2919
private static final String VERSION = "2.1.0";
3020
private static final String USER_AGENT = "sendgrid/" + VERSION + ";java";
3121

32-
private static final String PARAM_TO = "to[%d]";
33-
private static final String PARAM_TONAME = "toname[%d]";
34-
private static final String PARAM_CC = "cc[%d]";
22+
private static final String PARAM_TO = "to[]";
23+
private static final String PARAM_TONAME = "toname[]";
24+
private static final String PARAM_CC = "cc[]";
3525
private static final String PARAM_FROM = "from";
3626
private static final String PARAM_FROMNAME = "fromname";
3727
private static final String PARAM_REPLYTO = "replyto";
38-
private static final String PARAM_BCC = "bcc[%d]";
28+
private static final String PARAM_BCC = "bcc[]";
3929
private static final String PARAM_SUBJECT = "subject";
4030
private static final String PARAM_HTML = "html";
4131
private static final String PARAM_TEXT = "text";
@@ -91,16 +81,17 @@ public HttpEntity buildBody(Email email) {
9181

9282
// If SMTPAPI Header is used, To is still required. #workaround.
9383
if (tos.length == 0) {
94-
builder.addTextBody(String.format(PARAM_TO, 0), email.getFrom(), ContentType.create("text/plain", "UTF-8"));
84+
builder.addTextBody(PARAM_TO, email.getFrom(), ContentType.create("text/plain", "UTF-8"));
9585
}
86+
//Bug fix for #61 - To, ToName and Substitution values getting jumbled.
9687
for (int i = 0, len = tos.length; i < len; i++)
97-
builder.addTextBody(String.format(PARAM_TO, i), tos[i], ContentType.create("text/plain", "UTF-8"));
88+
builder.addTextBody(PARAM_TO, tos[i], ContentType.create("text/plain", "UTF-8"));
9889
for (int i = 0, len = tonames.length; i < len; i++)
99-
builder.addTextBody(String.format(PARAM_TONAME, i), tonames[i], ContentType.create("text/plain", "UTF-8"));
90+
builder.addTextBody(PARAM_TONAME, tonames[i], ContentType.create("text/plain", "UTF-8"));
10091
for (int i = 0, len = ccs.length; i < len; i++)
101-
builder.addTextBody(String.format(PARAM_CC, i), ccs[i], ContentType.create("text/plain", "UTF-8"));
92+
builder.addTextBody(PARAM_CC, ccs[i], ContentType.create("text/plain", "UTF-8"));
10293
for (int i = 0, len = bccs.length; i < len; i++)
103-
builder.addTextBody(String.format(PARAM_BCC, i), bccs[i], ContentType.create("text/plain", "UTF-8"));
94+
builder.addTextBody(PARAM_BCC, bccs[i], ContentType.create("text/plain", "UTF-8"));
10495
// Files
10596
if (email.getAttachments().size() > 0) {
10697
Iterator it = email.getAttachments().entrySet().iterator();
@@ -110,7 +101,7 @@ public HttpEntity buildBody(Email email) {
110101
}
111102
}
112103

113-
if (email.getContentIds().size() > 0) {
104+
if (email.getContentIds().size() > 0) {
114105
Iterator it = email.getContentIds().entrySet().iterator();
115106
while (it.hasNext()) {
116107
Map.Entry entry = (Map.Entry) it.next();
@@ -208,17 +199,17 @@ public String[] getTos() {
208199
return this.to.toArray(new String[this.to.size()]);
209200
}
210201

211-
public Email addSmtpApiTo(String to) {
202+
public Email addSmtpApiTo(String to) throws JSONException {
212203
this.smtpapi.addTo(to);
213204
return this;
214205
}
215206

216-
public Email addSmtpApiTo(String[] to) {
207+
public Email addSmtpApiTo(String[] to) throws JSONException {
217208
this.smtpapi.addTos(to);
218209
return this;
219210
}
220211

221-
public Email addToName(String toname) {
212+
public Email addToName(String toname) {
222213
this.toname.add(toname);
223214
return this;
224215
}
@@ -329,66 +320,66 @@ public String getHtml() {
329320
return this.html;
330321
}
331322

332-
public Email addSubstitution(String key, String[] val) {
323+
public Email addSubstitution(String key, String[] val) throws JSONException {
333324
this.smtpapi.addSubstitutions(key, val);
334325
return this;
335326
}
336327

337-
public JSONObject getSubstitutions() {
328+
public JSONObject getSubstitutions() throws JSONException {
338329
return this.smtpapi.getSubstitutions();
339330
}
340331

341-
public Email addUniqueArg(String key, String val) {
332+
public Email addUniqueArg(String key, String val) throws JSONException {
342333
this.smtpapi.addUniqueArg(key, val);
343334
return this;
344335
}
345336

346-
public JSONObject getUniqueArgs() {
337+
public JSONObject getUniqueArgs() throws JSONException {
347338
return this.smtpapi.getUniqueArgs();
348339
}
349340

350-
public Email addCategory(String category) {
341+
public Email addCategory(String category) throws JSONException {
351342
this.smtpapi.addCategory(category);
352343
return this;
353344
}
354345

355-
public String[] getCategories() {
346+
public String[] getCategories() throws JSONException {
356347
return this.smtpapi.getCategories();
357348
}
358349

359-
public Email addSection(String key, String val) {
350+
public Email addSection(String key, String val) throws JSONException {
360351
this.smtpapi.addSection(key, val);
361352
return this;
362353
}
363354

364-
public JSONObject getSections() {
355+
public JSONObject getSections() throws JSONException {
365356
return this.smtpapi.getSections();
366357
}
367358

368-
public Email addFilter(String filter_name, String parameter_name, String parameter_value) {
359+
public Email addFilter(String filter_name, String parameter_name, String parameter_value) throws JSONException {
369360
this.smtpapi.addFilter(filter_name, parameter_name, parameter_value);
370361
return this;
371362
}
372363

373-
public JSONObject getFilters() {
364+
public JSONObject getFilters() throws JSONException {
374365
return this.smtpapi.getFilters();
375366
}
376367

377-
public Email setASMGroupId(int val) {
368+
public Email setASMGroupId(int val) throws JSONException {
378369
this.smtpapi.setASMGroupId(val);
379370
return this;
380371
}
381372

382-
public Integer getASMGroupId() {
373+
public Integer getASMGroupId() throws JSONException {
383374
return this.smtpapi.getASMGroupId();
384375
}
385376

386-
public Email setSendAt(int sendAt) {
377+
public Email setSendAt(int sendAt) throws JSONException {
387378
this.smtpapi.setSendAt(sendAt);
388379
return this;
389380
}
390381

391-
public int getSendAt() {
382+
public int getSendAt() throws JSONException {
392383
return this.smtpapi.getSendAt();
393384
}
394385

@@ -410,12 +401,12 @@ public Map getAttachments() {
410401
}
411402

412403
public Email addContentId(String attachmentName, String cid) {
413-
this.contents.put(attachmentName, cid);
414-
return this;
404+
this.contents.put(attachmentName, cid);
405+
return this;
415406
}
416407

417408
public Map getContentIds() {
418-
return this.contents;
409+
return this.contents;
419410
}
420411

421412
public Email addHeader(String key, String val) {
@@ -438,7 +429,26 @@ public static class Response {
438429
private String message;
439430

440431
public Response(int code, String msg) {
441-
this.code = code;
432+
//Handling Sendgrid Response in case of failure.
433+
//Since the HTTP status returned is 200 but in the response JSON there is code parameter which contains acutal error code as 400
434+
JSONObject exception = null;
435+
boolean error = false;
436+
try {
437+
exception = new JSONObject(msg);
438+
Iterator it = exception.keys();
439+
while(it.hasNext()){
440+
String val = (String) it.next();
441+
if(val.equals("error") || val.equals("errors")) {
442+
error = true;
443+
break;
444+
}
445+
}
446+
447+
this.code = error ? 400 : 200;
448+
} catch (Exception e) {
449+
this.code = code;
450+
}
451+
442452
this.success = code == 200;
443453
this.message = msg;
444454
}

0 commit comments

Comments
 (0)