diff --git a/README.md b/README.md index cc170c6..c097441 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ See full example in [samples](samples/) - This can be imported into Android Stud ```java // Set your credentials -Api tc = new Api("login", "password"); +Api tc = new Api(new LoginPasswordAuth("login", "password"), null); Hashtable param = new Hashtable(); param.put("flash_message", false); diff --git a/jars/callr-sdk-java-1.4.0.jar b/jars/callr-sdk-java-1.4.0.jar deleted file mode 100644 index 8ccc086..0000000 Binary files a/jars/callr-sdk-java-1.4.0.jar and /dev/null differ diff --git a/jars/callr-sdk-java-1.5.0.jar b/jars/callr-sdk-java-1.5.0.jar new file mode 100644 index 0000000..9f2cec1 Binary files /dev/null and b/jars/callr-sdk-java-1.5.0.jar differ diff --git a/samples/CallrSampleApp/app/build.gradle b/samples/CallrSampleApp/app/build.gradle index a5d1160..c32e8d1 100644 --- a/samples/CallrSampleApp/app/build.gradle +++ b/samples/CallrSampleApp/app/build.gradle @@ -1,31 +1,31 @@ -apply plugin: 'com.android.application' - -android { - compileSdkVersion 25 - buildToolsVersion "25.0.2" - defaultConfig { - applicationId "com.callr.samples.callrsampleapp" - minSdkVersion 17 - targetSdkVersion 25 - versionCode 1 - versionName "1.0" - testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" - } - buildTypes { - release { - minifyEnabled false - proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' - } - } -} - -dependencies { - compile fileTree(include: ['*.jar'], dir: 'libs') - androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { - exclude group: 'com.android.support', module: 'support-annotations' - }) - testCompile 'junit:junit:4.12' - compile files('libs/callr-sdk-java-1.4.0.jar') - compile 'com.google.code.gson:gson:2.8.0' - compile 'commons-codec:commons-codec:1.10' -} +apply plugin: 'com.android.application' + +android { + compileSdkVersion 25 + buildToolsVersion "25.0.2" + defaultConfig { + applicationId "com.callr.samples.callrsampleapp" + minSdkVersion 17 + targetSdkVersion 25 + versionCode 1 + versionName "1.0" + testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } +} + +dependencies { + compile fileTree(include: ['*.jar'], dir: 'libs') + androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { + exclude group: 'com.android.support', module: 'support-annotations' + }) + testCompile 'junit:junit:4.12' + compile files('libs/callr-sdk-java-1.5.0.jar') + compile 'com.google.code.gson:gson:2.8.0' + compile 'commons-codec:commons-codec:1.10' +} diff --git a/samples/CallrSampleApp/app/libs/callr-sdk-java-1.4.0.jar b/samples/CallrSampleApp/app/libs/callr-sdk-java-1.4.0.jar deleted file mode 100644 index 8ccc086..0000000 Binary files a/samples/CallrSampleApp/app/libs/callr-sdk-java-1.4.0.jar and /dev/null differ diff --git a/samples/CallrSampleApp/app/libs/callr-sdk-java-1.5.0.jar b/samples/CallrSampleApp/app/libs/callr-sdk-java-1.5.0.jar new file mode 100644 index 0000000..9f2cec1 Binary files /dev/null and b/samples/CallrSampleApp/app/libs/callr-sdk-java-1.5.0.jar differ diff --git a/samples/CallrSampleApp/app/src/main/java/com/callr/samples/callrsampleapp/MainActivity.java b/samples/CallrSampleApp/app/src/main/java/com/callr/samples/callrsampleapp/MainActivity.java index 7faf5a4..54d77f2 100644 --- a/samples/CallrSampleApp/app/src/main/java/com/callr/samples/callrsampleapp/MainActivity.java +++ b/samples/CallrSampleApp/app/src/main/java/com/callr/samples/callrsampleapp/MainActivity.java @@ -7,6 +7,7 @@ import android.widget.EditText; import com.callr.exceptions.CallrException; +import com.callr.auth.*; import com.callr.Api; public class MainActivity extends Activity { @@ -26,7 +27,7 @@ protected void onCreate(Bundle savedInstanceState) { public void sendSMS(View view){ String phoneNumber = ((EditText)findViewById(R.id.editPhonenumber)).getText().toString(); String message = ((EditText)findViewById(R.id.editMessage)).getText().toString(); - Api api = new Api("login", "password"); + Api api = new Api(new LoginPasswordAuth("login", "password"), null); try { api.call("sms.send","SMS", phoneNumber, message, null); } catch (CallrException e) { diff --git a/samples/CallrSampleApp/build.gradle b/samples/CallrSampleApp/build.gradle index cfce2ca..74b2ab0 100644 --- a/samples/CallrSampleApp/build.gradle +++ b/samples/CallrSampleApp/build.gradle @@ -1,23 +1,23 @@ -// Top-level build file where you can add configuration options common to all sub-projects/modules. - -buildscript { - repositories { - jcenter() - } - dependencies { - classpath 'com.android.tools.build:gradle:2.2.3' - - // NOTE: Do not place your application dependencies here; they belong - // in the individual module build.gradle files - } -} - -allprojects { - repositories { - jcenter() - } -} - -task clean(type: Delete) { - delete rootProject.buildDir -} +// Top-level build file where you can add configuration options common to all sub-projects/modules. + +buildscript { + repositories { + jcenter() + } + dependencies { + classpath 'com.android.tools.build:gradle:2.2.3' + + // NOTE: Do not place your application dependencies here; they belong + // in the individual module build.gradle files + } +} + +allprojects { + repositories { + jcenter() + } +} + +task clean(type: Delete) { + delete rootProject.buildDir +} diff --git a/samples/CallrSampleApp/settings.gradle b/samples/CallrSampleApp/settings.gradle index d3db109..e7b4def 100644 --- a/samples/CallrSampleApp/settings.gradle +++ b/samples/CallrSampleApp/settings.gradle @@ -1 +1 @@ -include ':app' +include ':app' diff --git a/src/main/java/com/callr/Api.java b/src/main/java/com/callr/Api.java index a7b6b4f..3c475b7 100644 --- a/src/main/java/com/callr/Api.java +++ b/src/main/java/com/callr/Api.java @@ -1,6 +1,7 @@ package com.callr; import com.callr.exceptions.*; +import com.callr.auth.*; import com.google.gson.JsonParser; import com.google.gson.JsonElement; @@ -26,33 +27,44 @@ import javax.net.ssl.HttpsURLConnection; public class Api { - private static final String SDK_VERSION = "1.4.0"; + private static final String SDK_VERSION = "1.5.0"; private String _apiUrl = "https://api.callr.com/json-rpc/v1.1/"; - private String _login = null; - private String _password = null; private Hashtable _config = null; + private LoginAs _logAs = null; + private CallrAuthentication _auth = null; /** - * Constructor + * Default Constructor + * @param auth CallrAuthentication CALLR authentication object + * @param config extra configuration options + */ + public Api(CallrAuthentication auth, Hashtable config){ + this._auth = auth; + this._config = config; + } + + /** + * Overloaded constructor * @param login CALLR login * @param password CALLR password * @param config extra configuration options + * @deprecated */ + @Deprecated public Api(String login, String password, Hashtable config) { - _login = login; - _password = password; - _config = config; + this(new LoginPasswordAuth(login, password), config); } - + // overload Api constructor for optional parameters /** * Constructor * @param login * @param password + * @deprecated */ + @Deprecated public Api(String login, String password) { - _login = login; - _password = password; + this(new LoginPasswordAuth(login, password), null); } /** @@ -63,6 +75,21 @@ public void setApiUrl(String url){ this._apiUrl = url; } + /** + * setLoginAs - set to null to clear + * @param logAs LoginAs + */ + public void setLoginAs(LoginAs logAs){ + this._logAs = logAs; + } + + /** + * setLoginAs overload for updating object + */ + public void setLoginAs(String type, String target) throws CallrClientException { + this._logAs = new LoginAs(type, target); + } + // Send a request to CALLR webservice /** * call @@ -72,7 +99,7 @@ public void setApiUrl(String url){ @SuppressWarnings({ "rawtypes", "unchecked" }) public JsonElement call(String method, Object... params) throws CallrException, CallrClientException { ArrayList array = new ArrayList(); - + for (Object el : params) { array.add(el); } @@ -132,9 +159,6 @@ public PasswordAuthentication getPasswordAuthentication() { proxy = Proxy.NO_PROXY; } - // encode credentials to base64 Basic Auth format - tmp = new String(Base64.encodeBase64((this._login + ":" + this._password).getBytes())); - try { postDataBytes = gson.toJson(createObject(method, params, id)).getBytes("UTF-8"); conn = (HttpsURLConnection) url.openConnection(proxy); @@ -143,9 +167,14 @@ public PasswordAuthentication getPasswordAuthentication() { conn.setRequestProperty("Content-Type", "application/json-rpc; charset=utf-8"); conn.setRequestProperty("User-Agent", "sdk=JAVA; sdk-version="+SDK_VERSION+"; lang-version="+System.getProperty("java.version")+"; platform="+System.getProperty("os.name")); conn.setRequestProperty("charset", "utf-8"); - conn.setRequestProperty("Authorization", "Basic " + tmp); + conn.setRequestProperty("Authorization", this._auth.toString()); conn.setRequestProperty("Content-Length", Integer.toString(postDataBytes.length)); + // Check for LoginAs + if(this._logAs != null){ + conn.setRequestProperty("CALLR-Login-As", this._logAs.toString()); + } + // Send request out = new DataOutputStream(conn.getOutputStream()); out.write(postDataBytes); @@ -174,7 +203,7 @@ public PasswordAuthentication getPasswordAuthentication() { } } } - + // overload send method for optional parameters @SuppressWarnings("rawtypes") /** @@ -199,13 +228,13 @@ private Hashtable createObject(String method, ArrayList params, // Response analysis private JsonElement parseResponse(String response) throws CallrException { JsonElement result = null; - + try { result = new JsonParser().parse(response); } catch (Exception e) { throw new CallrException("INVALID_RESPONSE", -1, response); } - + if (result != null && result.isJsonObject() && result.getAsJsonObject().has("result")) { return result.getAsJsonObject().get("result"); } else if (result.isJsonObject() && result.getAsJsonObject().has("error")) { diff --git a/src/main/java/com/callr/auth/ApiKeyAuth.java b/src/main/java/com/callr/auth/ApiKeyAuth.java new file mode 100644 index 0000000..8f43128 --- /dev/null +++ b/src/main/java/com/callr/auth/ApiKeyAuth.java @@ -0,0 +1,15 @@ +package com.callr.auth; + +public class ApiKeyAuth extends CallrAuthentication { + private String apiKey; + + public ApiKeyAuth(String apiKey){ + this.apiKey = apiKey; + } + + @Override + public String toString(){ + return String.format("Api-Key %s", this.apiKey); + } + +} \ No newline at end of file diff --git a/src/main/java/com/callr/auth/CallrAuthentication.java b/src/main/java/com/callr/auth/CallrAuthentication.java new file mode 100644 index 0000000..d65248a --- /dev/null +++ b/src/main/java/com/callr/auth/CallrAuthentication.java @@ -0,0 +1,3 @@ +package com.callr.auth; + +public abstract class CallrAuthentication {} \ No newline at end of file diff --git a/src/main/java/com/callr/auth/LoginAs.java b/src/main/java/com/callr/auth/LoginAs.java new file mode 100644 index 0000000..721d4f0 --- /dev/null +++ b/src/main/java/com/callr/auth/LoginAs.java @@ -0,0 +1,35 @@ +package com.callr.auth; + +import java.util.*; +import com.callr.exceptions.CallrClientException; + +public class LoginAs { + + private String type; + private String target; + + private static HashMap loginAsTypes = new HashMap() { + { + put("user", "user.login"); + put("account", "account.hash"); + } + }; + + public LoginAs(String type, String target) throws CallrClientException { + setLoginAs(type, target); + } + + @Override + public String toString(){ + return String.format("%s %s", loginAsTypes.get(this.type), this.target); + } + + public void setLoginAs(String type, String target) throws CallrClientException { + if(!loginAsTypes.containsKey(type.toLowerCase())){ + throw new CallrClientException("ERROR_IN_LOGINAS", -1, "type must be one of " + loginAsTypes.keySet().toString()); + } else { + this.type = type.toLowerCase(); + this.target = target; + } + } +} \ No newline at end of file diff --git a/src/main/java/com/callr/auth/LoginPasswordAuth.java b/src/main/java/com/callr/auth/LoginPasswordAuth.java new file mode 100644 index 0000000..e7b7716 --- /dev/null +++ b/src/main/java/com/callr/auth/LoginPasswordAuth.java @@ -0,0 +1,20 @@ +package com.callr.auth; + +import org.apache.commons.codec.binary.Base64; + + +public class LoginPasswordAuth extends CallrAuthentication { + private String login; + private String password; + + public LoginPasswordAuth(String login, String password){ + this.login = login; + this.password = password; + } + + @Override + public String toString(){ + String loginPassB64 = new String(Base64.encodeBase64((this.login + ":" + this.password).getBytes())); + return String.format("Basic %s", loginPassB64); + } +} \ No newline at end of file diff --git a/src/main/java/com/callr/auth/UserSessionAuth.java b/src/main/java/com/callr/auth/UserSessionAuth.java new file mode 100644 index 0000000..895cc57 --- /dev/null +++ b/src/main/java/com/callr/auth/UserSessionAuth.java @@ -0,0 +1,14 @@ +package com.callr.auth; + +public class UserSessionAuth extends CallrAuthentication { + private String userSessionToken; + + public UserSessionAuth(String userSessionToken){ + this.userSessionToken = userSessionToken; + } + + @Override + public String toString(){ + return String.format("Session %s", this.userSessionToken); + } +}