Skip to content

Commit 3f808fa

Browse files
committed
Merge pull request #47 from SparkPost/feature/issue_44_transport_NPE
Feature/issue 44 transport npe
2 parents e1aea90 + 10f0429 commit 3f808fa

10 files changed

+412
-42
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
package com.sparkpost.error.samples;
3+
4+
import java.io.IOException;
5+
6+
import org.apache.log4j.Level;
7+
import org.apache.log4j.Logger;
8+
9+
import com.sparkpost.Client;
10+
import com.sparkpost.exception.SparkPostAuthorizationFailedException;
11+
import com.sparkpost.exception.SparkPostException;
12+
import com.sparkpost.resources.ResourceSendingDomains;
13+
import com.sparkpost.sdk.samples.helpers.SparkPostBaseApp;
14+
import com.sparkpost.transport.RestConnection;
15+
16+
/**
17+
* Lists all Sending Domains
18+
*/
19+
public class BadApiKeyErrorSample extends SparkPostBaseApp {
20+
21+
static final Logger logger = Logger.getLogger(BadApiKeyErrorSample.class);
22+
23+
private Client client;
24+
25+
public static void main(String[] args) throws SparkPostException, IOException {
26+
Logger.getRootLogger().setLevel(Level.DEBUG);
27+
28+
BadApiKeyErrorSample sample = new BadApiKeyErrorSample();
29+
sample.runApp();
30+
}
31+
32+
private void runApp() throws SparkPostException, IOException {
33+
this.client = this.newConfiguredClient();
34+
this.client.setAuthKey("This_is_a_bad_api_key");
35+
RestConnection connection = new RestConnection(this.client, getEndPoint());
36+
try {
37+
ResourceSendingDomains.list(connection);
38+
39+
throw new IllegalStateException("Error: Expected SparkPostAuthorizationFailedException");
40+
} catch (SparkPostAuthorizationFailedException e) {
41+
System.out.println("GOOD: Sucecssfuly got a SparkPostAuthorizationFailedException");
42+
}
43+
44+
}
45+
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
package com.sparkpost.error.samples;
3+
4+
import java.io.IOException;
5+
6+
import org.apache.log4j.Level;
7+
import org.apache.log4j.Logger;
8+
9+
import com.sparkpost.Client;
10+
import com.sparkpost.exception.SparkPostErrorServerResponseException;
11+
import com.sparkpost.exception.SparkPostException;
12+
import com.sparkpost.model.Webhook;
13+
import com.sparkpost.resources.ResourceWebhooks;
14+
import com.sparkpost.sdk.samples.helpers.SparkPostBaseApp;
15+
import com.sparkpost.transport.RestConnection;
16+
17+
public class ForceTransportError extends SparkPostBaseApp {
18+
19+
static final Logger logger = Logger.getLogger(ForceTransportError.class);
20+
21+
private Client client;
22+
23+
public static void main(String[] args) throws SparkPostException, IOException {
24+
Logger.getRootLogger().setLevel(Level.DEBUG);
25+
26+
ForceTransportError app = new ForceTransportError();
27+
app.runApp();
28+
}
29+
30+
private void runApp() throws SparkPostException, IOException {
31+
try {
32+
foceFourHundredError();
33+
} catch (SparkPostException e) {
34+
e.printStackTrace();
35+
} catch (Exception e1) {
36+
e1.printStackTrace();
37+
}
38+
}
39+
40+
// Test https://github.com/SparkPost/java-sparkpost/issues/44#issuecomment-215549742
41+
private void foceFourHundredError() throws SparkPostException, IOException {
42+
this.client = this.newConfiguredClient();
43+
44+
RestConnection restConnection = new RestConnection(this.client);
45+
Webhook webhook = new Webhook();
46+
webhook.setName("name with spaces");
47+
try {
48+
ResourceWebhooks.update(restConnection, webhook.getName(), webhook);
49+
throw new IllegalStateException("Error: Expected SparkPostErrorServerResponseException");
50+
} catch (SparkPostErrorServerResponseException e) {
51+
System.out.println("GOOD: Sucecssfuly got a SparkPostErrorServerResponseException");
52+
53+
}
54+
}
55+
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
2+
package com.sparkpost.error.samples;
3+
4+
import java.io.IOException;
5+
import java.util.ArrayList;
6+
import java.util.HashMap;
7+
import java.util.List;
8+
import java.util.Map;
9+
10+
import org.apache.log4j.Level;
11+
import org.apache.log4j.Logger;
12+
13+
import com.sparkpost.Client;
14+
import com.sparkpost.exception.SparkPostErrorServerResponseException;
15+
import com.sparkpost.exception.SparkPostException;
16+
import com.sparkpost.model.AddressAttributes;
17+
import com.sparkpost.model.RecipientAttributes;
18+
import com.sparkpost.model.TemplateContentAttributes;
19+
import com.sparkpost.model.TransmissionWithRecipientArray;
20+
import com.sparkpost.resources.ResourceTransmissions;
21+
import com.sparkpost.sdk.samples.helpers.SparkPostBaseApp;
22+
import com.sparkpost.transport.RestConnection;
23+
24+
public class SendEmailErrorSample extends SparkPostBaseApp {
25+
26+
static final Logger logger = Logger.getLogger(SendEmailErrorSample.class);
27+
28+
private Client client;
29+
30+
public static void main(String[] args) throws SparkPostException, IOException {
31+
Logger.getRootLogger().setLevel(Level.DEBUG);
32+
33+
SendEmailErrorSample sample = new SendEmailErrorSample();
34+
sample.runApp();
35+
}
36+
37+
private void runApp() throws SparkPostException, IOException {
38+
this.client = this.newConfiguredClient();
39+
40+
// Loads an email to send from the file system
41+
String fromAddress = getFromAddress();
42+
String[] recipients = getTestRecipients();
43+
44+
sendEmail(fromAddress, recipients);
45+
46+
}
47+
48+
private void sendEmail(String from, String[] recipients) throws SparkPostException {
49+
TransmissionWithRecipientArray transmission = new TransmissionWithRecipientArray();
50+
51+
// Populate Recipients
52+
List<RecipientAttributes> recipientArray = new ArrayList<RecipientAttributes>();
53+
for (String recipient : recipients) {
54+
RecipientAttributes recipientAttribs = new RecipientAttributes();
55+
recipientAttribs.setAddress(new AddressAttributes(recipient));
56+
recipientArray.add(recipientAttribs);
57+
}
58+
transmission.setRecipientArray(recipientArray);
59+
60+
// Populate Substitution Data
61+
Map<String, Object> substitutionData = new HashMap<String, Object>();
62+
substitutionData.put("yourContent", "You can add substitution data too.");
63+
transmission.setSubstitutionData(substitutionData);
64+
65+
// Populate Email Body
66+
TemplateContentAttributes contentAttributes = new TemplateContentAttributes();
67+
contentAttributes.setFrom(new AddressAttributes(from));
68+
contentAttributes.setSubject("Your subject content here. {{yourContent}}");
69+
contentAttributes.setText("Your Text content here. {{yourContent}}");
70+
contentAttributes.setHtml("<p>Your <b>HTML</b> content here. {{yourContent}}</p>");
71+
72+
// It is illegal to set a templateID with content
73+
contentAttributes.setTemplateId("This_Is_Bad_When_There_Is_Inline_Content");
74+
75+
transmission.setContentAttributes(contentAttributes);
76+
77+
transmission.setContentAttributes(contentAttributes);
78+
79+
// Send the Email
80+
RestConnection connection = new RestConnection(this.client, getEndPoint());
81+
82+
try {
83+
84+
ResourceTransmissions.create(connection, 0, transmission);
85+
86+
throw new IllegalStateException("Error: Expected SparkPostErrorServerResponseException");
87+
} catch (SparkPostErrorServerResponseException e) {
88+
System.out.println("GOOD: we got the expected SparkPostErrorServerResponseException");
89+
System.out.println(e.getMessage());
90+
91+
}
92+
93+
}
94+
95+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
package com.sparkpost.samples;
3+
4+
import java.io.IOException;
5+
6+
import org.apache.log4j.Level;
7+
import org.apache.log4j.Logger;
8+
9+
import com.sparkpost.Client;
10+
import com.sparkpost.exception.SparkPostException;
11+
import com.sparkpost.model.Webhook;
12+
import com.sparkpost.model.responses.WebhookListAllResponse;
13+
import com.sparkpost.resources.ResourceWebhooks;
14+
import com.sparkpost.sdk.samples.helpers.SparkPostBaseApp;
15+
import com.sparkpost.transport.RestConnection;
16+
17+
/**
18+
* Delete all webhooks with "deleteme" in their name
19+
*/
20+
public class DeleteWebhookSample extends SparkPostBaseApp {
21+
22+
static final Logger logger = Logger.getLogger(CreateTemplateSimple.class);
23+
24+
private Client client;
25+
26+
public static void main(String[] args) throws SparkPostException, IOException {
27+
Logger.getRootLogger().setLevel(Level.DEBUG);
28+
29+
DeleteWebhookSample sample = new DeleteWebhookSample();
30+
sample.runApp();
31+
}
32+
33+
private void runApp() throws SparkPostException, IOException {
34+
this.client = this.newConfiguredClient();
35+
RestConnection connection = new RestConnection(this.client, getEndPoint());
36+
WebhookListAllResponse response = ResourceWebhooks.listAll(connection, "America/Chicago");
37+
38+
int deletedCount = 0;
39+
for (Webhook webhook : response.getResults()) {
40+
if (webhook.getName().toLowerCase().contains("deleteme")) {
41+
System.out.println("Will delete: " + webhook.getName() + " (" + webhook.getId() + ")");
42+
ResourceWebhooks.delete(connection, webhook.getId());
43+
deletedCount++;
44+
}
45+
}
46+
47+
System.out.println("Deleted " + deletedCount + " webhooks.");
48+
}
49+
}

libs/sparkpost-lib/src/main/java/com/sparkpost/exception/SparkPostAccessForbiddenException.java

+12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
package com.sparkpost.exception;
23

34
public class SparkPostAccessForbiddenException extends SparkPostException {
@@ -6,8 +7,19 @@ public class SparkPostAccessForbiddenException extends SparkPostException {
67

78
private static final String MESSAGE = "API key has no required permission to perform this action.";
89

10+
private final String serverMessage;
11+
912
public SparkPostAccessForbiddenException() {
1013
super(MESSAGE);
14+
this.serverMessage = MESSAGE;
15+
}
16+
17+
public SparkPostAccessForbiddenException(String serverMessage) {
18+
super(MESSAGE);
19+
this.serverMessage = serverMessage;
1120
}
1221

22+
public String getServerMessage() {
23+
return this.serverMessage;
24+
}
1325
}

libs/sparkpost-lib/src/main/java/com/sparkpost/exception/SparkPostAuthorizationFailedException.java

+13
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
package com.sparkpost.exception;
23

34
public class SparkPostAuthorizationFailedException extends SparkPostException {
@@ -6,7 +7,19 @@ public class SparkPostAuthorizationFailedException extends SparkPostException {
67

78
private static final String MESSAGE = "Authorization failed. Check your API key.";
89

10+
private final String serverMessage;
11+
912
public SparkPostAuthorizationFailedException() {
1013
super(MESSAGE);
14+
this.serverMessage = MESSAGE;
15+
}
16+
17+
public SparkPostAuthorizationFailedException(String serverMessage) {
18+
super(MESSAGE);
19+
this.serverMessage = serverMessage;
20+
}
21+
22+
public String getServerMessage() {
23+
return this.serverMessage;
1124
}
1225
}

libs/sparkpost-lib/src/main/java/com/sparkpost/exception/SparkPostErrorServerResponseException.java

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
12
package com.sparkpost.exception;
23

34
import com.yepher.jsondoc.annotations.Description;
5+
46
import lombok.Getter;
57

68
public class SparkPostErrorServerResponseException extends SparkPostException {
@@ -15,4 +17,5 @@ public SparkPostErrorServerResponseException(String message, int responseCode) {
1517
super(message);
1618
this.responseCode = responseCode;
1719
}
20+
1821
}

libs/sparkpost-lib/src/main/java/com/sparkpost/exception/SparkPostException.java

-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11

22
package com.sparkpost.exception;
33

4-
/**
5-
* @author grava
6-
*/
74
public class SparkPostException extends Exception {
85

96
private static final long serialVersionUID = 4017633480414265142L;

libs/sparkpost-lib/src/main/java/com/sparkpost/exception/SparkPostIllegalServerResponseException.java

+10
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,18 @@ public class SparkPostIllegalServerResponseException extends SparkPostException
55

66
private static final long serialVersionUID = -1392320013310556792L;
77

8+
public SparkPostIllegalServerResponseException() {
9+
}
10+
811
public SparkPostIllegalServerResponseException(String message) {
912
super(message);
1013
}
1114

15+
public SparkPostIllegalServerResponseException(Throwable cause) {
16+
super(cause);
17+
}
18+
19+
public SparkPostIllegalServerResponseException(String message, Throwable cause) {
20+
super(message, cause);
21+
}
1222
}

0 commit comments

Comments
 (0)