Skip to content

Commit

Permalink
Merge branch 'main' into fix/CVE-2023-33201
Browse files Browse the repository at this point in the history
  • Loading branch information
shrutiburman authored Nov 8, 2023
2 parents 8d67f9c + 1ed8732 commit 44598dd
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 7 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
# Change Log
All notable changes to this project will be documented in this file.

[2023-11-06] Version 4.10.0
---------------------------
**Library - Feature**
- [PR #743](https://github.com/sendgrid/sendgrid-java/pull/743): Add data residency for eu and global regions. Thanks to [@shrutiburman](https://github.com/shrutiburman)!

**Library - Test**
- [PR #735](https://github.com/sendgrid/sendgrid-java/pull/735): Adding misc as PR type. Thanks to [@rakatyal](https://github.com/rakatyal)!


[2022-06-29] Version 4.9.3
--------------------------
**Library - Chore**
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ touch Example.java
Add the example you want to test to Example.java, including the headers at the top of the file.

``` bash
javac -classpath ../repo/com/sendgrid/4.9.3/sendgrid-4.9.3-jar.jar:. Example.java && java -classpath ../repo/com/sendgrid/4.9.3/sendgrid-4.9.3-jar.jar:. Example
javac -classpath ../repo/com/sendgrid/4.10.0/sendgrid-4.10.0-jar.jar:. Example.java && java -classpath ../repo/com/sendgrid/4.10.0/sendgrid-4.10.0-jar.jar:. Example
```

<a name="understanding-the-codebase"></a>
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Add the following to your build.gradle file in the root of your project.
...
dependencies {
...
implementation 'com.sendgrid:sendgrid-java:4.9.3'
implementation 'com.sendgrid:sendgrid-java:4.10.0'
}
repositories {
Expand All @@ -81,7 +81,7 @@ mvn install

You can just drop the jar file in. It's a fat jar - it has all the dependencies built in.

[sendgrid-java.jar](https://github.com/sendgrid/sendgrid-java/releases/download/4.9.3/sendgrid-java.jar)
[sendgrid-java.jar](https://github.com/sendgrid/sendgrid-java/releases/download/4.10.0/sendgrid-java.jar)

## Dependencies

Expand Down
77 changes: 77 additions & 0 deletions examples/dataresidency/setregion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import com.sendgrid.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

//////////////////////////////////////////////////////////////////
// Set data residency to navigate to a region/edge.
// Currently supported: "global", "eu"


public class Example {
public static void main(String[] args) throws IOException {
try {

final Mail helloWorld = buildHelloEmail();

Request request = new Request();
request.setEndpoint("mail/send");
request.setBody(helloWorld.build());
request.setMethod(Method.POST);

// sending to global data residency
Sendgrid sg = buildSendgridObj("global");
Response response = sg.api(request);
System.out.println("Sending to hostname: " + sg.getHost());
System.out.println(response.getStatusCode());
System.out.println(response.getBody());
System.out.println(response.getHeaders());

// sending to EU data residency
Sendgrid sg = buildSendgridObj("eu");
Response response = sg.api(request);
System.out.println("Sending to hostname: " + sg.getHost());
System.out.println(response.getStatusCode());
System.out.println(response.getBody());
System.out.println(response.getHeaders());

// not configuring any region defaults to global
Sendgrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY"));
Response response = sg.api(request);
System.out.println("Sending to hostname: " + sg.getHost());
System.out.println(response.getStatusCode());
System.out.println(response.getBody());
System.out.println(response.getHeaders());

} catch (IOException ex) {
throw ex;
}
}

public static Mail buildHelloEmail() {
Email from = new Email("[email protected]");
String subject = "Hello World from the Twilio SendGrid Java Library";
Email to = new Email("[email protected]");
Content content = new Content("text/plain", "some text here");
// Note that when you use this constructor an initial personalization object
// is created for you. It can be accessed via
// mail.personalization.get(0) as it is a List object
Mail mail = new Mail(from, subject, to, content);
Email email = new Email("[email protected]");
mail.personalization.get(0).addTo(email);

return mail;
}

public static Sendgrid buildSendgridObj(String region){
SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY"));
sg.setDataResidency(region);
return sg;

}
}

4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<groupId>com.sendgrid</groupId>
<artifactId>sendgrid-java</artifactId>
<name>Twilio SendGrid Java helper library</name>
<version>4.9.3</version>
<version>4.10.0</version>
<description>This Java module allows you to quickly and easily send emails through Twilio SendGrid using Java.</description>
<url>https://github.com/sendgrid/sendgrid-java</url>
<licenses>
Expand All @@ -26,7 +26,7 @@
<url>https://github.com/sendgrid/sendgrid-java</url>
<connection>scm:git:[email protected]:sendgrid/sendgrid-java.git</connection>
<developerConnection>scm:git:[email protected]:sendgrid/sendgrid-java.git</developerConnection>
<tag>4.9.3</tag>
<tag>4.10.0</tag>
</scm>
<profiles>
<profile>
Expand Down
27 changes: 26 additions & 1 deletion src/main/java/com/sendgrid/BaseInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,19 @@
*/
public abstract class BaseInterface implements SendGridAPI {

private static final String VERSION = "4.9.3";
private static final String VERSION = "4.10.0";

private static final String USER_AGENT = "sendgrid/" + VERSION + ";java";
private static final int RATE_LIMIT_RESPONSE_CODE = 429;
private static final int THREAD_POOL_SIZE = 8;

private static final Map<String, String> allowedRegionsHostMap;
static {
//todo: change this to Map.of() when we've moved on from Java 8
allowedRegionsHostMap = new HashMap<>();
allowedRegionsHostMap.put("eu", "api.eu.sendgrid.com");
allowedRegionsHostMap.put("global", "api.sendgrid.com");
}
private ExecutorService pool;

/**
Expand Down Expand Up @@ -336,4 +343,22 @@ public void run() {
}
});
}

/*
* Client libraries contain setters for specifying region/edge.
* This allows support global and eu regions only. This set will likely expand in the future.
* Global should be the default
* Global region means the message should be sent through:
* HTTP: api.sendgrid.com
* EU region means the message should be sent through:
* HTTP: api.eu.sendgrid.com
*/
public void setDataResidency(String region){
if (allowedRegionsHostMap.containsKey(region)){
this.host = allowedRegionsHostMap.get(region);
}
else{
throw new IllegalArgumentException("region can only be \"eu\" or \"global\"");
}
}
}
48 changes: 47 additions & 1 deletion src/test/java/com/sendgrid/SendGridTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void testConstructWithClient() throws IOException {
@Test
public void testLibraryVersion() {
SendGrid sg = new SendGrid(SENDGRID_API_KEY);
Assert.assertEquals(sg.getLibraryVersion(), "4.9.3");
Assert.assertEquals(sg.getLibraryVersion(), "4.10.0");
}

@Test
Expand Down Expand Up @@ -3242,4 +3242,50 @@ public void test_request_headers_override_sendgrid_object_headers() throws IOExc
sg.api(request);
verify(client).api(argThat((Request req) -> req.getHeaders().get("set-on-both").equals("456")));
}

@Test
public void testSetResidency_happy_path_eu() {
SendGrid sg = new SendGrid(SENDGRID_API_KEY);
sg.setDataResidency("eu");
Assert.assertEquals(sg.getHost(), "api.eu.sendgrid.com");
}
@Test
public void testSetResidency_happy_path_global() {
SendGrid sg = new SendGrid(SENDGRID_API_KEY);
sg.setDataResidency("global");
Assert.assertEquals(sg.getHost(), "api.sendgrid.com");
}


@Test
public void testSetResidency_override_host() {
SendGrid sg = new SendGrid(SENDGRID_API_KEY);
sg.setHost("api.new.com");
sg.setDataResidency("eu");
Assert.assertEquals(sg.getHost(), "api.eu.sendgrid.com");
}

@Test
public void testsetResidency_override_data_residency() {
SendGrid sg = new SendGrid(SENDGRID_API_KEY);
sg.setDataResidency("eu");
sg.setHost("api.new.com");
Assert.assertEquals(sg.getHost(), "api.new.com");
}

@Test (expected = IllegalArgumentException.class)
public void testsetResidency_incorrect_region() {
SendGrid sg = new SendGrid(SENDGRID_API_KEY);
sg.setDataResidency("foo");
}
@Test (expected = IllegalArgumentException.class)
public void testsetResidency_null_region(){
SendGrid sg = new SendGrid(SENDGRID_API_KEY);
sg.setDataResidency("");
}
@Test
public void testsetResidency_default_region() {
SendGrid sg = new SendGrid(SENDGRID_API_KEY);
Assert.assertEquals(sg.getHost(), "api.sendgrid.com");
}
}

0 comments on commit 44598dd

Please sign in to comment.