Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
888453f
commit 001a7ca
Showing
6 changed files
with
357 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Make sure to ignore Eclipse files | ||
.classpath | ||
.project | ||
.settings | ||
|
||
# Macisms | ||
.DS_Store | ||
# Where maven compiles by default | ||
target | ||
|
||
#IntelliJ project files | ||
*.iml | ||
.idea | ||
|
||
# Java Files that should be ignored | ||
*.class | ||
|
||
# Package Files | ||
*.jar | ||
*.war | ||
*.ear | ||
|
||
# Backup files | ||
*.bak | ||
scratch | ||
|
||
src/main/java/com/iamraphson/jusibe/core/example/App.java |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.iamraphson.jusibe.core</groupId> | ||
<artifactId>jusibe-java-lib</artifactId> | ||
<packaging>jar</packaging> | ||
<version>1.0-SNAPSHOT</version> | ||
<name>jusibe-java-lib</name> | ||
<url>http://maven.apache.org</url> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>3.8.1</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.httpcomponents</groupId> | ||
<artifactId>httpclient</artifactId> | ||
<version>4.5.2</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>net.iharder</groupId> | ||
<artifactId>base64</artifactId> | ||
<version>2.3.8</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.guava</groupId> | ||
<artifactId>guava</artifactId> | ||
<version>19.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.googlecode.json-simple</groupId> | ||
<artifactId>json-simple</artifactId> | ||
<version>1.1.1</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* To change this license header, choose License Headers in Project Properties. | ||
* To change this template file, choose Tools | Templates | ||
* and open the template in the editor. | ||
*/ | ||
package com.iamraphson.jusibe.core; | ||
|
||
import com.google.common.base.Strings; | ||
import com.iamraphson.jusibe.core.connection.HttpURLConnection; | ||
import com.iamraphson.jusibe.core.exceptions.IsNullException; | ||
import java.io.IOException; | ||
import java.net.MalformedURLException; | ||
import java.util.Map; | ||
|
||
/** | ||
* | ||
* @author Raphson | ||
*/ | ||
public class Jusibe { | ||
|
||
/** | ||
* public key | ||
* @var String | ||
*/ | ||
private String publicKey; | ||
|
||
/** | ||
* Access Token | ||
* @var String | ||
*/ | ||
private String accessToken; | ||
|
||
/** | ||
* Instance of HttpURLConnection | ||
* @var object | ||
*/ | ||
private HttpURLConnection client = null; | ||
|
||
|
||
public Jusibe(String publicKey, String accessToken) throws IsNullException{ | ||
if(Strings.isNullOrEmpty(publicKey)) | ||
throw new IsNullException("The Public Key can not be null or empty. Please pass it to the constructor"); | ||
|
||
if(Strings.isNullOrEmpty(accessToken)) | ||
throw new IsNullException("The Access Token can not be null or empty. Please pass it to the constructor"); | ||
|
||
|
||
this.publicKey = publicKey; | ||
this.accessToken = accessToken; | ||
} | ||
|
||
/** | ||
* Send SMS using the Jusibe API | ||
* @param payload | ||
* @return $this | ||
* @throws com.iamraphson.jusibe.core.exceptions.IsNullException | ||
* @throws java.net.MalformedURLException | ||
*/ | ||
public String sendSMS(Map<String, String> payload) | ||
throws IsNullException, MalformedURLException, IOException{ | ||
if (this.isNullOrEmpty(payload)) { | ||
throw new IsNullException("Message Payload can not be empty. Please fill the appropriate details"); | ||
} | ||
|
||
client = new HttpURLConnection(this.publicKey, this.accessToken); | ||
return client.performPostRequest("/smsapi/send_sms", payload); | ||
} | ||
|
||
|
||
public boolean isNullOrEmpty( final Map< ?, ? > m ) { | ||
return m == null || m.isEmpty(); | ||
} | ||
|
||
} |
147 changes: 147 additions & 0 deletions
147
src/main/java/com/iamraphson/jusibe/core/connection/HttpURLConnection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
/* | ||
* To change this license header, choose License Headers in Project Properties. | ||
* To change this template file, choose Tools | Templates | ||
* and open the template in the editor. | ||
*/ | ||
package com.iamraphson.jusibe.core.connection; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.io.UnsupportedEncodingException; | ||
import java.net.MalformedURLException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import org.apache.commons.codec.binary.Base64; | ||
import org.apache.http.HttpResponse; | ||
import org.apache.http.NameValuePair; | ||
import org.apache.http.client.HttpClient; | ||
import org.apache.http.client.entity.UrlEncodedFormEntity; | ||
import org.apache.http.client.methods.HttpGet; | ||
import org.apache.http.client.methods.HttpPost; | ||
import org.apache.http.impl.client.HttpClientBuilder; | ||
import org.apache.http.message.BasicNameValuePair; | ||
|
||
/** | ||
* | ||
* @author Raphson | ||
*/ | ||
public class HttpURLConnection { | ||
|
||
/** | ||
* Jusibe API Base Url | ||
*/ | ||
private final String baseURL = "https://jusibe.com"; | ||
|
||
|
||
/** | ||
* HttpClient User Agent | ||
*/ | ||
private final String USER_AGENT = "Mozilla/5.0"; | ||
|
||
|
||
/** | ||
* Instance of HttpClient | ||
*/ | ||
private HttpClient client = null; | ||
|
||
|
||
/** | ||
* Instance of HttpResponse | ||
*/ | ||
private HttpResponse response = null; | ||
|
||
|
||
/** | ||
* Response Code | ||
*/ | ||
private int responseCode; | ||
|
||
|
||
/** | ||
* Instance of StringBuffer | ||
*/ | ||
private StringBuffer result = null; | ||
|
||
|
||
/** | ||
* Auth Encoding | ||
*/ | ||
private String encoding = null; | ||
|
||
|
||
/** | ||
* class constructor | ||
* @param publicKey | ||
* @param accessToken | ||
* @throws java.net.MalformedURLException | ||
*/ | ||
public HttpURLConnection(String publicKey, String accessToken) throws MalformedURLException{ | ||
encoding = Base64.encodeBase64String((publicKey + ":" + accessToken).getBytes()); | ||
client = HttpClientBuilder.create().build(); | ||
} | ||
|
||
|
||
/** | ||
* Perform a GET request | ||
* @param relativeUrl | ||
* @return | ||
* @throws java.io.IOException | ||
*/ | ||
public String performGetRequest(String relativeUrl) throws IOException{ | ||
HttpGet request = new HttpGet(baseURL + relativeUrl); | ||
|
||
// add request header | ||
request.addHeader("User-Agent", USER_AGENT); | ||
request.setHeader("Authorization", "Basic " + encoding); | ||
response = client.execute(request); | ||
responseCode = response.getStatusLine().getStatusCode(); | ||
|
||
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); | ||
result = new StringBuffer(); | ||
|
||
String line = ""; | ||
while ((line = rd.readLine()) != null) { | ||
result.append(line); | ||
} | ||
|
||
return result.toString(); | ||
} | ||
|
||
|
||
/** | ||
* Perform a POST request | ||
* @param relativeUrl | ||
* @param data | ||
* @return | ||
* @throws java.io.UnsupportedEncodingException | ||
*/ | ||
public String performPostRequest(String relativeUrl, Map<String, String> data) | ||
throws UnsupportedEncodingException, IOException { | ||
HttpPost request = new HttpPost(baseURL + relativeUrl); | ||
|
||
// add header | ||
request.setHeader("User-Agent", USER_AGENT); | ||
request.setHeader("Authorization", "Basic " + encoding); | ||
|
||
List<NameValuePair> urlParameters = new ArrayList<NameValuePair>(); | ||
urlParameters.add(new BasicNameValuePair("to", data.get("to"))); | ||
urlParameters.add(new BasicNameValuePair("from", data.get("from"))); | ||
urlParameters.add(new BasicNameValuePair("message", data.get("message"))); | ||
request.setEntity(new UrlEncodedFormEntity(urlParameters)); | ||
|
||
response = client.execute(request); | ||
responseCode = response.getStatusLine().getStatusCode(); | ||
|
||
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); | ||
|
||
result = new StringBuffer(); | ||
String line = ""; | ||
while ((line = rd.readLine()) != null) | ||
result.append(line); | ||
|
||
|
||
return result.toString(); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/com/iamraphson/jusibe/core/exceptions/IsNullException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* To change this license header, choose License Headers in Project Properties. | ||
* To change this template file, choose Tools | Templates | ||
* and open the template in the editor. | ||
*/ | ||
package com.iamraphson.jusibe.core.exceptions; | ||
|
||
/** | ||
* | ||
* @author Raphson | ||
*/ | ||
public class IsNullException extends Exception { | ||
|
||
public IsNullException(){} | ||
|
||
public IsNullException(String message){ | ||
super(message); | ||
} | ||
|
||
public IsNullException(Throwable cause){ | ||
super(cause); | ||
} | ||
|
||
public IsNullException(String message, Throwable cause){ | ||
super(message, cause); | ||
} | ||
|
||
public IsNullException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { | ||
super(message, cause, enableSuppression, writableStackTrace); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.iamraphson.jusibe.core; | ||
|
||
import junit.framework.Test; | ||
import junit.framework.TestCase; | ||
import junit.framework.TestSuite; | ||
|
||
/** | ||
* Unit test for simple App. | ||
*/ | ||
public class AppTest | ||
extends TestCase | ||
{ | ||
/** | ||
* Create the test case | ||
* | ||
* @param testName name of the test case | ||
*/ | ||
public AppTest( String testName ) | ||
{ | ||
super( testName ); | ||
} | ||
|
||
/** | ||
* @return the suite of tests being tested | ||
*/ | ||
public static Test suite() | ||
{ | ||
return new TestSuite( AppTest.class ); | ||
} | ||
|
||
/** | ||
* Rigourous Test :-) | ||
*/ | ||
public void testApp() | ||
{ | ||
assertTrue( true ); | ||
} | ||
} |