Skip to content
This repository was archived by the owner on Apr 3, 2025. It is now read-only.

Add raw encoding and custom api base path options #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,23 @@
<url>https://spryng.nl/</url>
</organization>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.1</version>
</dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.8.47</version>
<scope>test</scope>
</dependency>
</dependencies>
<licenses>
<license>
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/Spryng/SpryngJavaSDK/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ public interface Constants
/**
* The URI used to send SMS messages.
*/
String API_SEND_URI = URL_PATH_SEPARATOR + "send.php";
String API_SEND_URI = "send.php";

/**
* The URI used to check the credit balance.
*/
String API_BALANCE_URI = URL_PATH_SEPARATOR + "check.php";
String API_BALANCE_URI = "check.php";

/**
* User agent with client information.
Expand Down
18 changes: 3 additions & 15 deletions src/main/java/com/Spryng/SpryngJavaSDK/Credits.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.Spryng.SpryngJavaSDK;

import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.message.BasicNameValuePair;

Expand All @@ -17,30 +18,17 @@ public class Credits implements Constants, APIResponses
public Credits(Spryng api)
{
this.api = api;
this.http = new RequestHandler();
this.http = new RequestHandler(api, API_BALANCE_URI);
}

public float check() throws SpryngException
{
List<BasicNameValuePair> queryParameters = new ArrayList<BasicNameValuePair>();
List<NameValuePair> queryParameters = new ArrayList<NameValuePair>();
// Add auth information to query string
String secretPasswordKey = (this.api.isSecretIsAPIKey()) ? "secret" : "password";
queryParameters.add(new BasicNameValuePair("username", this.api.getUsername()));
queryParameters.add(new BasicNameValuePair(secretPasswordKey, this.api.getSecret()));
queryParameters.add(new BasicNameValuePair("sender", this.api.getSender()));

URI uri;
try
{
uri = new URI(HTTP_SCHEME, null, API_HOST, -1, API_PATH + API_BALANCE_URI,
URLEncodedUtils.format(queryParameters, URL_ENCODING), null);
}
catch (URISyntaxException ex)
{
throw new SpryngException("Error occurred while trying to initiate URI for credit balance request.");
}

this.http.setUri(uri);
this.http.setQueryParameters(queryParameters);

return (float) Float.valueOf(this.http.send());
Expand Down
45 changes: 43 additions & 2 deletions src/main/java/com/Spryng/SpryngJavaSDK/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ public class Message implements Constants
* The Reference parameter, used to identify a message when receiving a delivery report.
*/
protected String reference;

/**
* Enable rawEncoding to send UTF-8 strings directly to API without converting first.
*/
protected boolean rawEncoding = false;

/**
* Instantiates a new Message.
Expand All @@ -47,18 +52,35 @@ public class Message implements Constants
* @param body the body
* @param service the service
* @param route the route
* @param allowLong the allow long
* @param allowLong to allow long
* @param rawEncoding to enable raw encoding
* @param reference the reference
*/
public Message(String destination, String body, String service, String route, boolean allowLong, String reference)
public Message(String destination, String body, String service, String route, boolean allowLong, boolean rawEncoding, String reference)
{
this.destination = destination;
this.body = body;
this.service = service;
this.route = route;
this.allowLong = allowLong;
this.rawEncoding = rawEncoding;
this.reference = reference;
}

/**
* Instantiates a new Message.
*
* @param the destination
* @param the body
* @param the service
* @param the route
* @param to allowLong
* @param the reference
*/
public Message(String destination, String body, String service, String route, boolean allowLong, String reference)
{
this(destination, body, service, route, allowLong, false, reference);
}

/**
* Empty constructor
Expand Down Expand Up @@ -231,4 +253,23 @@ public void setReference(String reference)
{
this.reference = reference;
}

/**
* Gets rawEncoding setting
*
* @return
*/
public boolean isRawEncoding() {
return rawEncoding;
}

/**
* Sets if rawEncoding should be used
*
* @param rawEncoding
*/
public void setRawEncoding(boolean rawEncoding) {
this.rawEncoding = rawEncoding;
}

}
86 changes: 43 additions & 43 deletions src/main/java/com/Spryng/SpryngJavaSDK/RequestHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
Expand All @@ -13,51 +15,58 @@

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

public class RequestHandler implements Constants
{
protected CloseableHttpClient http;

protected List<BasicNameValuePair> queryParameters;
protected List<NameValuePair> queryParameters;

protected URI uri;

protected String endpoint;


protected int httpResponseCode;

public RequestHandler()
public RequestHandler(Spryng api, String endpoint)
{
this.http = HttpClients.createDefault();
this.uri = api.getBasePath().resolve(endpoint);
}

public String send() throws SpryngException
{
HttpGet get = new HttpGet(this.getUri());

CloseableHttpResponse httpResponse;
String response;

try
{
httpResponse = this.http.execute(get);

this.setHttpResponseCode(httpResponse.getStatusLine().getStatusCode());
if (this.getHttpResponseCode() == HttpStatus.SC_OK)
{
response = this.httpResponseToString(httpResponse);
}
else
{
throw new SpryngException("Got non-OK HTTP status for request. Code: " + this.getHttpResponseCode());
}
}
catch (IOException ioEx)
{
throw new SpryngException("HTTP Request failed. Message: " + ioEx.getMessage());
}

return response;

try {

URIBuilder builder = new URIBuilder(this.getUri());
builder.setCharset(Charset.forName(URL_ENCODING));
builder.addParameters(this.queryParameters);
HttpGet get = new HttpGet(builder.build());

CloseableHttpResponse httpResponse = this.http.execute(get);
try {
int httpResponseCode = httpResponse.getStatusLine().getStatusCode();
if (httpResponseCode == HttpStatus.SC_OK)
{
return this.httpResponseToString(httpResponse);
}
else
{
throw new SpryngException("Got non-OK HTTP status for request. Code: " + httpResponseCode);
}
} finally {
httpResponse.close();
}

} catch (IOException e) {
throw new SpryngException("HTTP Request failed. Message: " + e.getMessage());
} catch (URISyntaxException e) {
throw new SpryngException("Error occurred while trying to initiate URI for request. Message: " + e.getMessage());
}
}

public String httpResponseToString(HttpResponse httpResponse) throws SpryngException
Expand All @@ -69,12 +78,12 @@ public String httpResponseToString(HttpResponse httpResponse) throws SpryngExcep
{
response = EntityUtils.toString(httpResponse.getEntity(), URL_ENCODING);
}
catch (ParseException pEx)
catch (ParseException e)
{
throw new SpryngException("Could not properly parse HTTP response to a string. Additional Message: "
+ pEx.getMessage());
+ e.getMessage());
}
catch (IOException ioEx)
catch (IOException e)
{
throw new SpryngException("Could not parse the HTTP response to a string.");
}
Expand All @@ -83,12 +92,12 @@ public String httpResponseToString(HttpResponse httpResponse) throws SpryngExcep
return response;
}

public List<BasicNameValuePair> getQueryParameters()
public List<NameValuePair> getQueryParameters()
{
return queryParameters;
}

public void setQueryParameters(List<BasicNameValuePair> queryParameters)
public void setQueryParameters(List<NameValuePair> queryParameters)
{
this.queryParameters = queryParameters;
}
Expand All @@ -103,13 +112,4 @@ public void setUri(URI uri)
this.uri = uri;
}

private void setHttpResponseCode(int httpResponseCode)
{
this.httpResponseCode = httpResponseCode;
}

public int getHttpResponseCode()
{
return httpResponseCode;
}
}
21 changes: 7 additions & 14 deletions src/main/java/com/Spryng/SpryngJavaSDK/SMS.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.Spryng.SpryngJavaSDK;

import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.message.BasicNameValuePair;

Expand All @@ -26,7 +27,7 @@ public class SMS implements Constants, APIResponses
public SMS(Spryng api)
{
this.api = api;
this.http = new RequestHandler();
this.http = new RequestHandler(api, API_SEND_URI);
}

/**
Expand All @@ -43,7 +44,7 @@ public SpryngResponse send(Message message) throws SpryngException
throw new SpryngException("The message you provided is invalid. Please refer to the API documentation");
}

List<BasicNameValuePair> queryParameters = new ArrayList<BasicNameValuePair>();
List<NameValuePair> queryParameters = new ArrayList<NameValuePair>();

// Add auth information to query string
String secretPasswordKey = (this.api.isSecretIsAPIKey()) ? "secret" : "password";
Expand All @@ -69,23 +70,15 @@ public SpryngResponse send(Message message) throws SpryngException
queryParameters.add(new BasicNameValuePair("allowlong", "0"));
}

if (message.isRawEncoding()) {
queryParameters.add(new BasicNameValuePair("rawencoding", "1"));
}

if (message.getReference() != null)
{
queryParameters.add(new BasicNameValuePair("reference", message.getReference()));
}

URI uri;
try
{
uri = new URI(HTTP_SCHEME, null, API_HOST, -1, API_PATH + API_SEND_URI,
URLEncodedUtils.format(queryParameters, URL_ENCODING), null);
}
catch (URISyntaxException ex)
{
throw new SpryngException("Error occurred while trying to initiate URI for SMS request.");
}

this.http.setUri(uri);
this.http.setQueryParameters(queryParameters);

String response = this.http.send();
Expand Down
Loading