diff --git a/Paubox_Java/paubox-java/pom.xml b/Paubox_Java/paubox-java/pom.xml
index a4e95a7..7f9086e 100644
--- a/Paubox_Java/paubox-java/pom.xml
+++ b/Paubox_Java/paubox-java/pom.xml
@@ -68,6 +68,27 @@
json-simple
1.1.1
-
+
+ org.json
+ json
+ 20211205
+
+
+ org.apache.httpcomponents
+ httpmime
+ 4.5.13
+
+
+ commons-io
+ commons-io
+ 2.11.0
+
+
+ org.junit.jupiter
+ junit-jupiter-api
+ 5.8.1
+ test
+
+
\ No newline at end of file
diff --git a/Paubox_Java/paubox-java/src/com/paubox/data/DynamicTemplate.java b/Paubox_Java/paubox-java/src/com/paubox/data/DynamicTemplate.java
new file mode 100644
index 0000000..7eb8279
--- /dev/null
+++ b/Paubox_Java/paubox-java/src/com/paubox/data/DynamicTemplate.java
@@ -0,0 +1,49 @@
+
+package com.paubox.data;
+
+public class DynamicTemplate {
+ private int id;
+ private String name;
+ private String content;
+
+ public DynamicTemplate(int id, String name) {
+ this.id = id;
+ this.name = name;
+ }
+
+ public DynamicTemplate(String name, String content) {
+ this.name = name;
+ this.content = content;
+ }
+
+ public DynamicTemplate(int id, String name, String content) {
+ this.id = id;
+ this.name = name;
+ this.content = content;
+ }
+
+ public int getId() {
+ return id;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public String getContent() {
+ return content;
+ }
+
+ public void setContent(String content) {
+ this.content = content;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public void setId(int id) {
+ this.id = id;
+ }
+
+}
\ No newline at end of file
diff --git a/Paubox_Java/paubox-java/src/com/paubox/service/DynamicTemplateService.java b/Paubox_Java/paubox-java/src/com/paubox/service/DynamicTemplateService.java
new file mode 100644
index 0000000..1d78cc7
--- /dev/null
+++ b/Paubox_Java/paubox-java/src/com/paubox/service/DynamicTemplateService.java
@@ -0,0 +1,181 @@
+package com.paubox.service;
+
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpDelete;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPatch;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.ContentType;
+import org.apache.http.entity.mime.MultipartEntityBuilder;
+import org.apache.http.entity.mime.content.FileBody;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.util.EntityUtils;
+import org.json.JSONArray;
+import org.json.JSONObject;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import com.paubox.common.Constants;
+import com.paubox.data.DynamicTemplate;
+
+public class DynamicTemplateService {
+
+ private String baseApiUrl = "https://api.paubox.net/v1/" + Constants.API_USER + "/";
+
+ public List getAllDynamicTemplates() throws IOException {
+ // Set API endpoint URL
+ String url = baseApiUrl + "dynamic_templates";
+
+ // Set request headers
+ Map headers = new HashMap<>();
+ headers.put("Authorization", getAuthorizationHeader());
+
+ // Create HTTP client and request object
+ HttpClient httpClient = HttpClients.createDefault();
+ HttpGet httpGet = new HttpGet(url);
+ // headers.forEach(httpGet::setHeader);
+ for (Map.Entry entry : headers.entrySet()) {
+ httpGet.setHeader(entry.getKey(), entry.getValue());
+ }
+
+ // Make API request
+ HttpResponse response = httpClient.execute(httpGet);
+
+ // Handle API response
+ int statusCode = response.getStatusLine().getStatusCode();
+ if (statusCode == 200 || statusCode == 201) {
+ HttpEntity entity = response.getEntity();
+ String responseBody = EntityUtils.toString(entity);
+ JSONArray jsonArray = new JSONArray(responseBody);
+ List templates = new ArrayList<>();
+ for (int i = 0; i < jsonArray.length(); i++) {
+ JSONObject jsonObject = jsonArray.getJSONObject(i);
+ int templateId = jsonObject.getInt("id");
+ String templateName = jsonObject.getString("name");
+ DynamicTemplate template = new DynamicTemplate(templateId, templateName);
+ templates.add(template);
+ }
+ return templates;
+ } else {
+ throw new IOException("Paubox API Request Error: " + response.getStatusLine().getReasonPhrase());
+ }
+ }
+
+ public String createDynamicTemplate(String templateName, File templateFile) throws IOException {
+ String url = baseApiUrl + "dynamic_templates";
+ Map headers = new HashMap<>();
+ headers.put("Authorization", getAuthorizationHeader());
+
+ HttpClient httpClient = HttpClients.createDefault();
+ HttpPost httpPost = new HttpPost(url);
+ for (Map.Entry entry : headers.entrySet()) {
+ httpPost.setHeader(entry.getKey(), entry.getValue());
+ }
+ FileBody templateBody = new FileBody(templateFile, ContentType.TEXT_PLAIN);
+ HttpEntity requestEntity = MultipartEntityBuilder.create()
+ .addTextBody("data[name]", templateName)
+ .addPart("data[body]", templateBody)
+ .build();
+ httpPost.setEntity(requestEntity);
+ HttpResponse response = httpClient.execute(httpPost);
+ int statusCode = response.getStatusLine().getStatusCode();
+ if (statusCode == 200 || statusCode == 201) {
+ HttpEntity entity = response.getEntity();
+ String responseBody = EntityUtils.toString(entity);
+ JSONObject jsonObject = new JSONObject(responseBody);
+ String message = jsonObject.getString("message");
+ return "Message: " + message;
+ } else {
+ throw new IOException("Paubox API Request Error: " + response.getStatusLine().getReasonPhrase());
+ }
+ }
+
+ public String updateDynamicTemplate(String templateId, String templateName, File templateFile) throws IOException {
+ String url = baseApiUrl + "dynamic_templates/" + templateId;
+ Map headers = new HashMap<>();
+ headers.put("Authorization", getAuthorizationHeader());
+
+ HttpClient httpClient = HttpClients.createDefault();
+ HttpPatch httpPatch = new HttpPatch(url);
+ for (Map.Entry entry : headers.entrySet()) {
+ httpPatch.setHeader(entry.getKey(), entry.getValue());
+ }
+ FileBody templateBody = new FileBody(templateFile, ContentType.TEXT_PLAIN);
+ HttpEntity requestEntity = MultipartEntityBuilder.create()
+ .addTextBody("data[name]", templateName)
+ .addPart("data[body]", templateBody)
+ .build();
+ httpPatch.setEntity(requestEntity);
+ HttpResponse response = httpClient.execute(httpPatch);
+ int statusCode = response.getStatusLine().getStatusCode();
+ if (statusCode == 200 || statusCode == 201) {
+ HttpEntity entity = response.getEntity();
+ String responseBody = EntityUtils.toString(entity);
+ JSONObject jsonObject = new JSONObject(responseBody);
+ String message = jsonObject.getString("message");
+ return "Message: " + message;
+ } else {
+ throw new IOException("Paubox API Request Error: " + response.getStatusLine().getReasonPhrase());
+ }
+ }
+
+ public String deleteDynamicTemplate(String templateId) throws IOException {
+ String url = baseApiUrl + "dynamic_templates/" + templateId;
+ Map headers = new HashMap<>();
+ headers.put("Authorization", getAuthorizationHeader());
+
+ HttpClient httpClient = HttpClients.createDefault();
+ HttpDelete httpDelete = new HttpDelete(url);
+ for (Map.Entry entry : headers.entrySet()) {
+ httpDelete.setHeader(entry.getKey(), entry.getValue());
+ }
+ HttpResponse response = httpClient.execute(httpDelete);
+ int statusCode = response.getStatusLine().getStatusCode();
+ if (statusCode == 200 || statusCode == 201) {
+ HttpEntity entity = response.getEntity();
+ String responseBody = EntityUtils.toString(entity);
+ JSONObject jsonObject = new JSONObject(responseBody);
+ String message = jsonObject.getString("message");
+ return "Message: " + message;
+ } else {
+ throw new IOException("Paubox API Request Error: " + response.getStatusLine().getReasonPhrase());
+ }
+ }
+
+ public DynamicTemplate getDynamicTemplate(String templateId) throws IOException {
+ String url = baseApiUrl + "dynamic_templates/" + templateId;
+ Map headers = new HashMap<>();
+ headers.put("Authorization", getAuthorizationHeader());
+
+ HttpClient httpClient = HttpClients.createDefault();
+ HttpGet httpGet = new HttpGet(url);
+ for (Map.Entry entry : headers.entrySet()) {
+ httpGet.setHeader(entry.getKey(), entry.getValue());
+ }
+ HttpResponse response = httpClient.execute(httpGet);
+ int statusCode = response.getStatusLine().getStatusCode();
+ if (statusCode == 200 || statusCode == 201) {
+ HttpEntity entity = response.getEntity();
+ String responseBody = EntityUtils.toString(entity);
+ JSONObject jsonObject = new JSONObject(responseBody);
+ int templateIdFromAPI = jsonObject.getInt("id");
+ String templateName = jsonObject.getString("name");
+ DynamicTemplate template = new DynamicTemplate(templateIdFromAPI, templateName);
+ template.setContent(jsonObject.getString("body"));
+ return template;
+ } else {
+ throw new IOException("Paubox API Request Error: " + response.getStatusLine().getReasonPhrase());
+ }
+ }
+
+ private static String getAuthorizationHeader() {
+ return "Token token=" + Constants.API_KEY;
+ }
+}
diff --git a/Paubox_Java/paubox-java/src/test/TestDynamicTemplate.java b/Paubox_Java/paubox-java/src/test/TestDynamicTemplate.java
new file mode 100644
index 0000000..a0d0f00
--- /dev/null
+++ b/Paubox_Java/paubox-java/src/test/TestDynamicTemplate.java
@@ -0,0 +1,147 @@
+package test;
+
+import org.apache.commons.io.FileUtils;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.jupiter.api.*;
+
+import com.paubox.service.DynamicTemplateService;
+
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.util.List;
+import java.util.function.Predicate;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.jupiter.api.Assertions.*;
+
+/**
+ * Unit test for simple App.
+ */
+public class TestDynamicTemplate {
+ private static final String TEMPLATE_NAME = "test_template";
+ private static final String TEMPLATE_FILE_PATH = "template.html";
+
+ private static DynamicTemplateService service;
+
+ @Before
+ public void setup() {
+
+ String propertiesFile = System.getProperty("properties");
+ if (propertiesFile == null || propertiesFile.equals("")) {
+ propertiesFile = "src/test/config.properties";
+ }
+ ConfigurationManager.getProperties(propertiesFile);
+
+ service = new DynamicTemplateService();
+
+ String text = "Hello, world!
";
+ File file = new File(TEMPLATE_FILE_PATH);
+
+ try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
+ writer.write(text);
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+ }
+
+ @After
+ public void cleanup() throws IOException {
+ // Delete the test template after running the tests
+ List templates = service.getAllDynamicTemplates();
+ for (DynamicTemplate template : templates) {
+ if (template.getName().equals(TEMPLATE_NAME)) {
+ service.deleteDynamicTemplate(String.valueOf(template.getId()));
+ }
+ }
+ }
+
+ @Test
+ public void testGetAllDynamicTemplates() throws IOException {
+ List templates = service.getAllDynamicTemplates();
+ assertNotNull(templates);
+ assertTrue(templates.size() > 0);
+ }
+
+ @Test
+ public void testCreateDynamicTemplate() throws IOException {
+ // Create a test template
+ File templateFile = new File(TEMPLATE_FILE_PATH);
+ String templateContent = FileUtils.readFileToString(templateFile, StandardCharsets.UTF_8);
+ DynamicTemplate template = new DynamicTemplate(TEMPLATE_NAME, templateContent);
+ String response = service.createDynamicTemplate(template.getName(), templateFile);
+ assertEquals("Message: Template test_template created!", response);
+ }
+
+ @Test
+ public void testUpdateDynamicTemplate() throws IOException {
+ // Create a test template
+ File templateFile = new File(TEMPLATE_FILE_PATH);
+ String templateContent = FileUtils.readFileToString(templateFile, StandardCharsets.UTF_8);
+ DynamicTemplate template = new DynamicTemplate(TEMPLATE_NAME, templateContent);
+ String createResponse = service.createDynamicTemplate(template.getName(), templateFile);
+ assertEquals("Message: Template test_template created!", createResponse);
+ List all = service.getAllDynamicTemplates();
+ template.setId(all.get(all.size() - 1).getId());
+
+ // Update the test template
+ String updatedTemplateName = "updated_test_template";
+ String updatedTemplateContent = templateContent.replace("{{subject}}", "Updated Subject");
+ File updatedTemplateFile = new File(TEMPLATE_FILE_PATH);
+ FileUtils.writeStringToFile(updatedTemplateFile, updatedTemplateContent, StandardCharsets.UTF_8);
+ String updateResponse = service.updateDynamicTemplate(String.valueOf(template.getId()), updatedTemplateName, updatedTemplateFile);
+ assertEquals("Message: Template updated_test_template updated!", updateResponse);
+
+ // Verify that the updated template has the new name and content
+ DynamicTemplate updatedTemplate = service.getDynamicTemplate(String.valueOf(template.getId()));
+ assertEquals(updatedTemplateName, updatedTemplate.getName());
+ assertEquals(updatedTemplateContent, updatedTemplate.getContent());
+ }
+
+ @Test
+ public void testDeleteDynamicTemplate() throws IOException {
+ // Create a test template
+ List all = service.getAllDynamicTemplates();
+ DynamicTemplate last = all.get(all.size() - 1);
+ DynamicTemplate template = new DynamicTemplate(last.getId(), last.getName(), last.getContent());
+
+ // Delete the test template
+ String deleteResponse = service.deleteDynamicTemplate(String.valueOf(template.getId()));
+ assertEquals("Message: Template Tempate Name deleted!", deleteResponse);
+
+ // Verify that the template has been deleted
+ List templates = service.getAllDynamicTemplates();
+ assertFalse(templates.stream().anyMatch(new Predicate() {
+ @Override
+ public boolean test(DynamicTemplate t) {
+ return t.getName().equals(TEMPLATE_NAME);
+ }
+ }));
+ }
+
+ @Test
+ public void testGetDynamicTemplate() throws IOException {
+ // Create a test template
+ File templateFile = new File(TEMPLATE_FILE_PATH);
+ String templateContent = FileUtils.readFileToString(templateFile, StandardCharsets.UTF_8);
+ DynamicTemplate template = new DynamicTemplate(TEMPLATE_NAME, templateContent);
+ String createResponse = service.createDynamicTemplate(template.getName(), templateFile);
+ assertEquals("Message: Template test_template created!", createResponse);
+ List all = service.getAllDynamicTemplates();
+ template.setId(all.get(all.size() - 1).getId());
+ template.setContent(all.get(all.size() - 1).getContent());
+ template.setName(all.get(all.size() - 1).getName());
+ // Get the test template by ID
+ DynamicTemplate retrievedTemplate = service.getDynamicTemplate(String.valueOf(template.getId()));
+ assertNotNull(retrievedTemplate);
+ assertEquals(template.getId(), retrievedTemplate.getId());
+ assertEquals(template.getName(), retrievedTemplate.getName());
+ assertEquals(template.getContent(), retrievedTemplate.getContent());
+ }
+}
diff --git a/README.md b/README.md
index 7ebc77e..de54191 100644
--- a/README.md
+++ b/README.md
@@ -224,6 +224,54 @@ static void GetEmailDisposition()
}
```
+## Dynamic Template
+
+- Create a dynamic template
+- Update a dynamic template
+- Delete a dynamic template
+- Get a list of dynamic templates
+- Get a specific dynamic template
+
+
+### Usage
+1. Import the DynamicTemplateService class in your Java code:
+
+```Java
+import com.paubox.service.DynamicTemplateService;
+```
+
+2. Create an instance of the DynamicTemplateService class with your credentials:
+
+```Java
+DynamicTemplateService service = new DynamicTemplateService(USERNAME, API_KEY);
+```
+4. Call the API methods as needed:
+```Java
+// Create a dynamic template
+String createTemplateResponse = service.createDynamicTemplate(TEMPLATE_NAME, TEMPLATE_FILE);
+
+// Update a dynamic template
+String updateTemplateResponse = service.updateDynamicTemplate(TEMPLATE_ID, TEMPLATE_NAME, TEMPLATE_FILE);
+
+// Delete a dynamic template
+String deleteTemplateResponse = service.deleteDynamicTemplate(TEMPLATE_ID);
+
+// Get a list of dynamic templates
+List templates = service.getAllDynamicTemplates();
+
+// Get a specific dynamic template
+DynamicTemplate template = service.getDynamicTemplate(TEMPLATE_ID);
+```
+
+5. Check the responses returned by the API calls:
+```Java
+System.out.println(createTemplateResponse);
+System.out.println(updateTemplateResponse);
+System.out.println(deleteTemplateResponse);
+System.out.println(templates);
+System.out.println(template);
+```
+
## Contributing