diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..a9f1212 --- /dev/null +++ b/.classpath @@ -0,0 +1,9 @@ + + + + + + + diff --git a/.project b/.project new file mode 100644 index 0000000..bcd58eb --- /dev/null +++ b/.project @@ -0,0 +1,18 @@ + + + RingCentral-Java + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.springsource.ide.eclipse.gradle.core.nature + org.eclipse.jdt.core.javanature + + diff --git a/.settings/gradle/org.springsource.ide.eclipse.gradle.core.prefs b/.settings/gradle/org.springsource.ide.eclipse.gradle.core.prefs new file mode 100644 index 0000000..446f268 --- /dev/null +++ b/.settings/gradle/org.springsource.ide.eclipse.gradle.core.prefs @@ -0,0 +1,5 @@ +#org.springsource.ide.eclipse.gradle.core.preferences.GradleProjectPreferences +#Wed Jul 29 13:15:58 PDT 2015 +build.family.org.gradle.tooling.model.eclipse.HierarchicalEclipseProject=; +org.springsource.ide.eclipse.gradle.linkedresources= +org.springsource.ide.eclipse.gradle.rootprojectloc= diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..3a21537 --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/bin/core/SDK.class b/bin/core/SDK.class new file mode 100644 index 0000000..2db66f4 Binary files /dev/null and b/bin/core/SDK.class differ diff --git a/bin/http/RCHeaders.class b/bin/http/RCHeaders.class new file mode 100644 index 0000000..278b69f Binary files /dev/null and b/bin/http/RCHeaders.class differ diff --git a/bin/http/RCRequest.class b/bin/http/RCRequest.class new file mode 100644 index 0000000..3a8cce9 Binary files /dev/null and b/bin/http/RCRequest.class differ diff --git a/bin/http/RCResponse.class b/bin/http/RCResponse.class new file mode 100644 index 0000000..a2ed677 Binary files /dev/null and b/bin/http/RCResponse.class differ diff --git a/bin/platform/Auth.class b/bin/platform/Auth.class new file mode 100644 index 0000000..1b272c0 Binary files /dev/null and b/bin/platform/Auth.class differ diff --git a/bin/platform/Platform$1.class b/bin/platform/Platform$1.class new file mode 100644 index 0000000..28ffdad Binary files /dev/null and b/bin/platform/Platform$1.class differ diff --git a/bin/platform/Platform.class b/bin/platform/Platform.class new file mode 100644 index 0000000..538d12b Binary files /dev/null and b/bin/platform/Platform.class differ diff --git a/src/core/SDK.java b/src/core/SDK.java new file mode 100644 index 0000000..ff15175 --- /dev/null +++ b/src/core/SDK.java @@ -0,0 +1,16 @@ +package core; + +import platform.Platform; + +public class SDK { + + Platform platform; + + public SDK(String appKey, String appSecret, String server) { + platform = new Platform(appKey, appSecret, server); + } + + public Platform getPlatform() { + return this.platform; + } +} diff --git a/src/http/RCHeaders.java b/src/http/RCHeaders.java new file mode 100644 index 0000000..e140ef8 --- /dev/null +++ b/src/http/RCHeaders.java @@ -0,0 +1,78 @@ +package http; + +import java.util.HashMap; +import java.util.Map; + +public class RCHeaders { + + public static String CONTENT_TYPE = "Content-Type"; + + public static final String JSON_CONTENT_TYPE = "application/json"; + public static final String MULTIPART_CONTENT_TYPE = "multipart/mixed"; + public static final String URL_ENCODED_CONTENT_TYPE = "application/x-www-form-urlencoded"; + HashMap hmHeader; + + public RCHeaders() { + this.hmHeader = new HashMap<>(); + } + + public RCHeaders(HashMap headers) { + this.hmHeader = headers; + } + + public String getContentType() { + return hasHeader(CONTENT_TYPE) ? getHeader(CONTENT_TYPE) : ""; + } + + public String getHeader(String key) { + return this.hmHeader.get(key); + } + + public HashMap getHeaders() { + return hmHeader; + } + + public String[] getHeadersArray() { + String[] array = new String[this.hmHeader.size()]; + int count = 0; + for (Map.Entry entry : this.hmHeader.entrySet()) { + array[count] = entry.getKey() + ":" + entry.getValue(); + count++; + } + return array; + } + + public boolean hasHeader(String key) { + return this.hmHeader.containsKey(key); + } + + public boolean isContentType(String contentType) { + return (this.hmHeader.get(CONTENT_TYPE).contains(contentType)); + } + + public boolean isJson() { + return isContentType(JSON_CONTENT_TYPE); + } + + public boolean isMultipart() { + return isContentType(MULTIPART_CONTENT_TYPE); + } + + public boolean isURLEncoded() { + return isContentType(URL_ENCODED_CONTENT_TYPE); + } + + public void setContentType(String contentType) { + this.hmHeader.put(CONTENT_TYPE, contentType); + } + + public void setHeader(String key, String val) { + this.hmHeader.put(key, val); + } + + public void setHeaders(HashMap headers) { + for (Map.Entry entry : headers.entrySet()) { + setHeader(entry.getKey(), entry.getValue()); + } + } +} diff --git a/src/http/RCRequest.java b/src/http/RCRequest.java new file mode 100644 index 0000000..e00ffe3 --- /dev/null +++ b/src/http/RCRequest.java @@ -0,0 +1,32 @@ +package http; + +import java.util.HashMap; + +public class RCRequest { + HashMap body; + String method; + String query; + public RCHeaders RCHeaders; + String url; + + public RCRequest(HashMap body, + HashMap headerMap) { + RCHeaders = new RCHeaders(); + this.method = headerMap.get("method"); + this.url = headerMap.get("url"); + if (headerMap.containsKey("query")) { + this.query = headerMap.get("query"); + } else { + this.query = ""; + } + if (headerMap.containsKey("Content-Type")) { + this.RCHeaders.setContentType(headerMap.get("Content-Type")); + } + if (body != null) { + this.body = body; + } else { + this.body = null; + } + } + +} diff --git a/src/http/RCResponse.java b/src/http/RCResponse.java new file mode 100644 index 0000000..dc0183c --- /dev/null +++ b/src/http/RCResponse.java @@ -0,0 +1,5 @@ +package http; + +public class RCResponse { + +} diff --git a/src/platform/Auth.java b/src/platform/Auth.java new file mode 100644 index 0000000..524acd5 --- /dev/null +++ b/src/platform/Auth.java @@ -0,0 +1,110 @@ +package platform; + +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.Map; + +public class Auth { + + String access_token; + Date expire_time; + String expires_in; + String owner_id; + String refresh_token; + Date refresh_token_expire_time; + String refresh_token_expires_in; + String scope; + String token_type; + +// public Auth() { +// token_type = ""; +// access_token = ""; +// expires_in = ""; +// expire_time = null; +// refresh_token = ""; +// refresh_token_expires_in = ""; +// refresh_token_expire_time = null; +// scope = ""; +// owner_id = ""; +// } + + public String getAccessToken() { + return this.access_token; + } + + public Auth getData() { + return this; + } + + public String getRefreshToken() { + return this.refresh_token; + } + + public String getTokenType() { + return this.token_type; + } + + public boolean isAccessTokenValid() { + GregorianCalendar cal = new GregorianCalendar(); + cal.setTime(this.expire_time); + return isTokenDateValid(cal); + } + + public boolean isRefreshTokenValid() { + GregorianCalendar cal = new GregorianCalendar(); + cal.setTime(this.refresh_token_expire_time); + return isTokenDateValid(cal); + } + + public boolean isTokenDateValid(GregorianCalendar token_date) { + return (token_date.compareTo(new GregorianCalendar()) > 0); + } + + public void setData(Map authData) { + + if (authData.containsKey("token_type")) { + this.token_type = authData.get("token_type"); + } + if (authData.containsKey("scope")) { + this.scope = authData.get("scope"); + } + if (authData.containsKey("owner_id")) { + this.owner_id = authData.get("owner_id"); + } + if (authData.containsKey("access_token")) { + this.access_token = authData.get("access_token"); + } + if (authData.containsKey("expires_in")) { + this.expires_in = authData.get("expires_in"); + } + if (!authData.containsKey("expire_time") + && authData.containsKey("expires_in")) { + int expiresIn = Integer.parseInt(authData.get("expires_in")); + Calendar calendar = new GregorianCalendar(); + calendar.add(Calendar.SECOND, expiresIn); + this.expire_time = calendar.getTime(); + + } + if (authData.containsKey("refresh_token")) { + this.refresh_token = authData.get("refresh_token"); + } + if (authData.containsKey("refresh_token_expires_in")) { + this.refresh_token_expires_in = authData + .get("refresh_token_expires_in"); + } + if (!authData.containsKey("refresh_token_expire_time") + && authData.containsKey("refresh_token_expires_in")) { + int expiresIn = Integer.parseInt(authData + .get("refresh_token_expires_in")); + Calendar calendar = new GregorianCalendar(); + calendar.add(Calendar.SECOND, expiresIn); + this.refresh_token_expire_time = calendar.getTime(); + } + + System.out.println(this.expire_time); + System.out.println(this.expires_in); + System.out.println(this.refresh_token_expires_in); + System.out.println(this.refresh_token_expire_time); + } +} diff --git a/src/platform/Platform.java b/src/platform/Platform.java new file mode 100644 index 0000000..7a76041 --- /dev/null +++ b/src/platform/Platform.java @@ -0,0 +1,116 @@ +package platform; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.List; +import java.util.HashMap; + +import javax.xml.bind.DatatypeConverter; + +import org.apache.http.Consts; +import org.apache.http.HttpHeaders; +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.HttpPost; +import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.message.BasicNameValuePair; + +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; + +public class Platform { + + public String accessToken; + + public String appKey; + public String appSecret; + + Auth auth; + + final String authURL = "/restapi/oauth/token"; + public String RC_PRODUCTION = "https://platform.ringcentral.com"; + + public String RC_SANDBOX = "https://platform.devtest.ringcentral.com"; + public String server; + + public Platform(String appKey, String appSecret, String server) { + super(); + this.appKey = appKey; + this.appSecret = appSecret; + this.server = server.toString().equals("RC_PRODUCTION") ? RC_PRODUCTION + : RC_SANDBOX; + this.auth = new Auth(); + + } + + public void authCall(List body) { + + HttpClient client = new DefaultHttpClient(); + HttpPost post = new HttpPost(this.server + authURL); + post.setHeader(HttpHeaders.AUTHORIZATION, "Basic " + + encodeAPICredentialsToBase64()); + post.setHeader(HttpHeaders.CONTENT_TYPE, + "application/x-www-form-urlencoded"); + post.setEntity(new UrlEncodedFormEntity(body, Consts.UTF_8)); + + HttpResponse response = null; + try { + response = client.execute(post); + + if (response.getStatusLine().getStatusCode() == 200) { + BufferedReader rd = new BufferedReader(new InputStreamReader( + response.getEntity().getContent())); + StringBuffer result = new StringBuffer(); + String line = ""; + while ((line = rd.readLine()) != null) { + result.append(line); + } + System.out.println(result.toString()); + Gson gson = new Gson(); + Type HashMapType = new TypeToken>() { + }.getType(); + HashMap authData = gson.fromJson(result.toString(), + HashMapType); + setAuth(auth, authData); + } + } catch (IOException e1) { + e1.printStackTrace(); + } + } + + public void authorize(String username, String extension, String password) { + + List body = new ArrayList(); + body.add(new BasicNameValuePair("username", username)); + body.add(new BasicNameValuePair("password", password)); + body.add(new BasicNameValuePair("extension", extension)); + body.add(new BasicNameValuePair("grant_type", "password")); + + authCall(body); + + } + + public String encodeAPICredentialsToBase64() { + String apiCredentials = appKey + ":" + appSecret; + byte[] message = apiCredentials.getBytes(); + return DatatypeConverter.printBase64Binary(message); + } + + public String getAccessToken() { + return auth.getAccessToken(); + } + + public Auth getAuth() { + return auth; + } + + public void setAuth(Auth auth, HashMap authData) { + this.auth.setData(authData); + } + +} \ No newline at end of file