Skip to content

Commit

Permalink
Merge pull request #15 from gscriver/feature/PATCH
Browse files Browse the repository at this point in the history
Fixes #10 HttpsURLConnection does not support PATCH
  • Loading branch information
alexanderwe authored Feb 28, 2018
2 parents f51e721 + 6b069ab commit de4e4fc
Show file tree
Hide file tree
Showing 4 changed files with 213 additions and 149 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@
<artifactId>json</artifactId>
<version>20160212</version>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.5</version>
</dependency>
</dependencies>

<build>
Expand Down
348 changes: 202 additions & 146 deletions src/main/java/com/github/alexanderwe/bananaj/connection/Connection.java
Original file line number Diff line number Diff line change
@@ -1,159 +1,215 @@
package com.github.alexanderwe.bananaj.connection;

import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.net.URL;
import java.util.NoSuchElementException;
import java.util.Scanner;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import org.apache.http.HttpEntity;
import org.apache.http.client.entity.EntityBuilder;
import org.apache.http.client.methods.CloseableHttpResponse;
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.client.methods.HttpPut;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

/**
* Created by Alexander on 10.08.2016.
*/
public class Connection {


public String do_Get(URL url, String authorization) throws Exception{
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();

// optional default is GET
con.setRequestMethod("GET");

//add request header
con.setRequestProperty("Authorization",authorization);


int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode+"\n");

BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();

while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
return response.toString();
}

public String do_Post(URL url, String post_string, String authorization) throws Exception{

HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
con.setRequestProperty("Authorization", authorization);
con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
// Indicate that we want to write to the HTTP request body
con.setDoOutput(true);
con.setRequestMethod("POST");

// Writing the post data to the HTTP request body
BufferedWriter httpRequestBodyWriter = new BufferedWriter(new OutputStreamWriter(con.getOutputStream()));
httpRequestBodyWriter.write(post_string);
httpRequestBodyWriter.close();

int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url + System.lineSeparator() + "Send data: " + post_string);
System.out.println("Response Code : " + responseCode+"\n");

// Reading from the HTTP response body
Scanner httpResponseScanner = new Scanner(con.getInputStream());
String inputLine;
StringBuilder response = new StringBuilder();
try{
while ((inputLine = httpResponseScanner.nextLine()) != null) {
response.append(inputLine);
}
}catch (NoSuchElementException nsee){
System.out.println("Line not found error: "+nsee.toString());
}

httpResponseScanner.close();
return response.toString();
}

public String do_Patch(URL url, String patch_string, String authorization) throws Exception{

HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
con.setRequestProperty("Authorization", authorization);
con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
con.setRequestProperty("X-HTTP-Method-Override", "PATCH");
// Indicate that we want to write to the HTTP request body
con.setDoOutput(true);
con.setRequestMethod("POST");

// Writing the post data to the HTTP request body
BufferedWriter httpRequestBodyWriter = new BufferedWriter(new OutputStreamWriter(con.getOutputStream()));
httpRequestBodyWriter.write(patch_string);
httpRequestBodyWriter.close();

int responseCode = con.getResponseCode();
System.out.println("\nSending 'PATCH' request to URL : " + url + System.lineSeparator() + "Send data: " + patch_string);
System.out.println("Response Code : " + responseCode+"\n");

// Reading from the HTTP response body
Scanner httpResponseScanner = new Scanner(con.getInputStream());
String inputLine;
StringBuilder response = new StringBuilder();
try{
while ((inputLine = httpResponseScanner.nextLine()) != null) {
response.append(inputLine);
}
}catch (NoSuchElementException nsee){
System.out.println("Line not found error: "+nsee.toString());
}

httpResponseScanner.close();
return response.toString();
}

public String do_Post(URL url, String authorization) throws Exception{
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();

// optional default is GET
con.setRequestMethod("POST");

//add request header
con.setRequestProperty("Authorization",authorization);

int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Response Code : " + responseCode+"\n");

BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();

while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
return response.toString();
}

public String do_Delete(URL url, String authorization) throws Exception{
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
con.setRequestMethod("DELETE");
con.setRequestProperty("Authorization", authorization);

int responseCode = con.getResponseCode();
System.out.println("\nSending 'DELETE' request to URL : " + url);
System.out.println("Response Code : " + responseCode+"\n");

BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();

while ((inputLine = in.readLine()) != null)
{
response.append(inputLine);
}
in.close();
return response.toString();
}
public String do_Get(URL url, String authorization) throws Exception{
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpget = new HttpGet(url.toURI());
httpget.addHeader("Authorization", authorization);
CloseableHttpResponse response = httpclient. execute(httpget);

InputStream entityStream = null;
try {
int responseCode = response.getStatusLine().getStatusCode();
System.out.println("\nSending 'GET' request to URL : " + url + System.lineSeparator());
System.out.println("Response Code : " + responseCode+"\n");

HttpEntity entity = response.getEntity();
long length = entity.getContentLength();
entityStream = entity.getContent();
StringBuilder strbuilder = new StringBuilder(length > 16 && length < Integer.MAX_VALUE ? (int)length : 200);
try (Reader reader = new BufferedReader(new InputStreamReader
(entityStream, Charset.forName(StandardCharsets.UTF_8.name())))) {
int c = 0;
while ((c = reader.read()) != -1) {
strbuilder.append((char) c);
}
}
return strbuilder.toString();
} finally {
if (entityStream != null) {entityStream.close();}
response.close();
}
}

public String do_Post(URL url, String post_string, String authorization) throws Exception{
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost(url.toURI());
httppost.addHeader("Authorization", authorization);
httppost.addHeader("Content-Type", "application/json; charset=UTF-8");
httppost.setEntity(EntityBuilder.create().setText(post_string).build());
CloseableHttpResponse response = httpclient. execute(httppost);

InputStream entityStream = null;
try {
int responseCode = response.getStatusLine().getStatusCode();
System.out.println("\nSending 'POST' request to URL : " + url + System.lineSeparator() + "Send data: " + post_string);
System.out.println("Response Code : " + responseCode+"\n");

HttpEntity entity = response.getEntity();
long length = entity.getContentLength();
entityStream = entity.getContent();
StringBuilder strbuilder = new StringBuilder(length > 16 && length < Integer.MAX_VALUE ? (int)length : 200);
try (Reader reader = new BufferedReader(new InputStreamReader
(entityStream, Charset.forName(StandardCharsets.UTF_8.name())))) {
int c = 0;
while ((c = reader.read()) != -1) {
strbuilder.append((char) c);
}
}
return strbuilder.toString();
} finally {
if (entityStream != null) {entityStream.close();}
response.close();
}
}

public String do_Patch(URL url, String patch_string, String authorization) throws Exception{
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPatch httppatch = new HttpPatch(url.toURI());
httppatch.addHeader("Authorization", authorization);
httppatch.addHeader("Content-Type", "application/json; charset=UTF-8");
httppatch.setEntity(EntityBuilder.create().setText(patch_string).build());
CloseableHttpResponse response = httpclient. execute(httppatch);

InputStream entityStream = null;
try {
int responseCode = response.getStatusLine().getStatusCode();
System.out.println("\nSending 'PATCH' request to URL : " + url + System.lineSeparator() + "Send data: " + patch_string);
System.out.println("Response Code : " + responseCode+"\n");

HttpEntity entity = response.getEntity();
long length = entity.getContentLength();
entityStream = entity.getContent();
StringBuilder strbuilder = new StringBuilder(length > 16 && length < Integer.MAX_VALUE ? (int)length : 200);
try (Reader reader = new BufferedReader(new InputStreamReader
(entityStream, Charset.forName(StandardCharsets.UTF_8.name())))) {
int c = 0;
while ((c = reader.read()) != -1) {
strbuilder.append((char) c);
}
}
return strbuilder.toString();
} finally {
if (entityStream != null) {entityStream.close();}
response.close();
}
}

public String do_Put(URL url, String patch_string, String authorization) throws Exception{
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPut httpput = new HttpPut(url.toURI());
httpput.addHeader("Authorization", authorization);
httpput.addHeader("Content-Type", "application/json; charset=UTF-8");
httpput.setEntity(EntityBuilder.create().setText(patch_string).build());
CloseableHttpResponse response = httpclient. execute(httpput);

InputStream entityStream = null;
try {
int responseCode = response.getStatusLine().getStatusCode();
System.out.println("\nSending 'PUT' request to URL : " + url + System.lineSeparator() + "Send data: " + patch_string);
System.out.println("Response Code : " + responseCode+"\n");

HttpEntity entity = response.getEntity();
long length = entity.getContentLength();
entityStream = entity.getContent();
StringBuilder strbuilder = new StringBuilder(length > 16 && length < Integer.MAX_VALUE ? (int)length : 200);
try (Reader reader = new BufferedReader(new InputStreamReader
(entityStream, Charset.forName(StandardCharsets.UTF_8.name())))) {
int c = 0;
while ((c = reader.read()) != -1) {
strbuilder.append((char) c);
}
}
return strbuilder.toString();
} finally {
if (entityStream != null) {entityStream.close();}
response.close();
}
}

public String do_Post(URL url, String authorization) throws Exception{
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost(url.toURI());
httppost.addHeader("Authorization", authorization);
httppost.addHeader("Content-Type", "application/json; charset=UTF-8");
CloseableHttpResponse response = httpclient. execute(httppost);

InputStream entityStream = null;
try {
int responseCode = response.getStatusLine().getStatusCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Response Code : " + responseCode+"\n");

HttpEntity entity = response.getEntity();
long length = entity.getContentLength();
entityStream = entity.getContent();
StringBuilder strbuilder = new StringBuilder(length > 16 && length < Integer.MAX_VALUE ? (int)length : 200);
try (Reader reader = new BufferedReader(new InputStreamReader
(entityStream, Charset.forName(StandardCharsets.UTF_8.name())))) {
int c = 0;
while ((c = reader.read()) != -1) {
strbuilder.append((char) c);
}
}
return strbuilder.toString();
} finally {
if (entityStream != null) {entityStream.close();}
response.close();
}
}

public String do_Delete(URL url, String authorization) throws Exception{
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpDelete httpdelete = new HttpDelete(url.toURI());
httpdelete.addHeader("Authorization", authorization);
httpdelete.addHeader("Content-Type", "application/json; charset=UTF-8");
CloseableHttpResponse response = httpclient. execute(httpdelete);

InputStream entityStream = null;
try {
int responseCode = response.getStatusLine().getStatusCode();
System.out.println("\nSending 'DELETE' request to URL : " + url);
System.out.println("Response Code : " + responseCode+"\n");

HttpEntity entity = response.getEntity();
long length = entity.getContentLength();
entityStream = entity.getContent();
StringBuilder strbuilder = new StringBuilder(length > 16 && length < Integer.MAX_VALUE ? (int)length : 200);
try (Reader reader = new BufferedReader(new InputStreamReader
(entityStream, Charset.forName(StandardCharsets.UTF_8.name())))) {
int c = 0;
while ((c = reader.read()) != -1) {
strbuilder.append((char) c);
}
}
return strbuilder.toString();
} finally {
if (entityStream != null) {entityStream.close();}
response.close();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ protected CampaignContent(String plain_text, String html, Campaign campaign) {
public void changeHTMLContent(String htmlContent) throws Exception{
JSONObject content = new JSONObject();
content.put("html", htmlContent);
getCampaign().getConnection().do_Post(new URL(getCampaign().getConnection().getCampaignendpoint()+"/"+this.getCampaign().getId()+"/content"),content.toString(), this.getCampaign().getConnection().getApikey());
this.html = htmlContent;
String response = getCampaign().getConnection().do_Put(new URL(getCampaign().getConnection().getCampaignendpoint()+"/"+this.getCampaign().getId()+"/content"),content.toString(), this.getCampaign().getConnection().getApikey());
content = new JSONObject(response);
this.plain_text = content.has("plain_text") ? content.getString("plain_text") : null;
this.html = content.has("html") ? content.getString("html") : null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ private Boolean getBoolean(JSONObject settings, String key) {
public void changeSubjectLine(String newSubject) throws Exception{
JSONObject updatedCampaign = new JSONObject();
JSONObject updatedSettings = new JSONObject();
updatedSettings.put("subject", newSubject);
updatedSettings.put("subject_line", newSubject);
updatedCampaign.put("settings", updatedSettings);
this.getConnection().do_Patch(new URL(this.getConnection().getCampaignendpoint()+"/"+this.getCampaignId()),updatedCampaign.toString(),this.getConnection().getApikey());
this.subject_line = newSubject;
Expand Down

0 comments on commit de4e4fc

Please sign in to comment.