From a2f957f423bbae16a6b9ecb411de1f9ada2457ee Mon Sep 17 00:00:00 2001 From: vyshakhbabji Date: Mon, 3 Aug 2015 14:08:57 -0700 Subject: [PATCH] platform object ready --- .classpath | 9 ++ .project | 18 +++ ...springsource.ide.eclipse.gradle.core.prefs | 5 + .settings/org.eclipse.jdt.core.prefs | 11 ++ bin/core/SDK.class | Bin 0 -> 590 bytes bin/http/RCHeaders.class | Bin 0 -> 3698 bytes bin/http/RCRequest.class | Bin 0 -> 1262 bytes bin/http/RCResponse.class | Bin 0 -> 265 bytes bin/platform/Auth.class | Bin 0 -> 2924 bytes bin/platform/Platform$1.class | Bin 0 -> 608 bytes bin/platform/Platform.class | Bin 0 -> 5392 bytes src/core/SDK.java | 16 +++ src/http/RCHeaders.java | 78 ++++++++++++ src/http/RCRequest.java | 32 +++++ src/http/RCResponse.java | 5 + src/platform/Auth.java | 110 +++++++++++++++++ src/platform/Platform.java | 116 ++++++++++++++++++ 17 files changed, 400 insertions(+) create mode 100644 .classpath create mode 100644 .project create mode 100644 .settings/gradle/org.springsource.ide.eclipse.gradle.core.prefs create mode 100644 .settings/org.eclipse.jdt.core.prefs create mode 100644 bin/core/SDK.class create mode 100644 bin/http/RCHeaders.class create mode 100644 bin/http/RCRequest.class create mode 100644 bin/http/RCResponse.class create mode 100644 bin/platform/Auth.class create mode 100644 bin/platform/Platform$1.class create mode 100644 bin/platform/Platform.class create mode 100644 src/core/SDK.java create mode 100644 src/http/RCHeaders.java create mode 100644 src/http/RCRequest.java create mode 100644 src/http/RCResponse.java create mode 100644 src/platform/Auth.java create mode 100644 src/platform/Platform.java 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 0000000000000000000000000000000000000000..2db66f41fc07de4e712e7378e2317bc86060c74c GIT binary patch literal 590 zcma)3%TB^j5Is|nLV1Jue#9NrM>lQ^#*M}ejhbLcT$XyplG2h(A@Q#?(U5532l!FO zX(@4m3m13p%(-V~&dmM#e*Xk;j9nE9A#M9o810LeiX@@bwls*=LBbjle+nv-VUVW^_u@d8XZFN+`ktZ63FJDB1cHn8yyt| zLV2O5hCE6kEu8_OlEV^Ggrezq;%YqT3VCaFUBNG=Z(DB1l1>=MU@~}gMg+~A&mB%+ z4Tml96lN?8K*;cGTi8>s7)2?>?skp!Cr#VO>D&t4Zb5W`YX!MOj0-%tRe&pQR&=_5SJ!wN35M6 DaN>63 literal 0 HcmV?d00001 diff --git a/bin/http/RCHeaders.class b/bin/http/RCHeaders.class new file mode 100644 index 0000000000000000000000000000000000000000..278b69fd750ca952db92f817e905a9367e292811 GIT binary patch literal 3698 zcma)8XlkK$w6=7Hw_SxDpk;`5i1D68la`Nr9&KHFihyoM8VeX`@ZkX zN5Aw#SFG*RKJC~3g#L;CgSO|+ya^MM=c#-kGxvS(xyw24edo`Ae*O)>o%kw>8V${J zp10V0I(5P{&YF%JMV*Fq^Tq|EH)q(hy<;=;X4cc7r^eFbgX!_<@u^dT8WJO`TQZ(w z*|Yr`)(>aK($nP|DhGd1s-g z;N(m@n?EZE4RZ@Yy&5(Il@vTH*L%Wn=SGc1meQE9W^KbOIHrdEl{*G1%dfN?B2L3V zHfPzEcSu8RXV;{Lx)d{Lz$WQv-u6t}>lt5OG#jDeMja88XZDKQy#5zlx=_B?grry9k9vI7!ClO_-7 zI0zw{p)`~Q2~JUexFTiZ_VeblhFB>-QqxwfJ91zLP9CVM)euJ<$6!##Ia67; z)N)+My*NRwxYi|-LG2ESd059uj8N9pt+SkwI@g!wL{ong(l`~x7(2HrMlR^M52xvC zd8T7{c}Mi7I^x$kr0fJ3$7B=}6#gneaaG6tcz}u7<`Scqt3>rQiU)bFS6xZnjzLK{ ziib3G_zNMW94%Nmj=m;L;Sn9vf?m&7oivwY*bhU;46@Yl1sPyt=l-8rrN0=?z?6eY zm#X@lY`~lb?SO2Y*Kr;>2IlZGZBD43>(Z!7ogJHkts^hFH!yd9*b44rIvn)S=;ed7 zq_Y`&&p!XGD>N@kT^6BMvtOrBqgWED zJ+0#zJgcE8%Q0zKwo5+NG_rWAn0nlDL(@ZedqKyGc!`x4{W)O6Jx$FSPR4w!z&pY0 zm+JjMtScBl^_Dx#t4T)4Yd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 z+|_Xp_t}0+I-X6L{!8lZQnI9yZt4hPKwzj~TJmN4pe*e@qugLjywI!|jgn!T3VW9U zr*8J{kGKA6nd|OyakB*y=R--s3m+^xM&%uSJRB)D+jd1hGgZOZ+1s?HG{GY2`T5EP z-QNI_#*>e|LyeSAsDbdwQDD%;oUV)65Qgd7jTk|kPK(T@^0iW@7)^ae;>{^0JD5tP z<1;6i{f?_0Tqjui3vQ<4$rGeL(%U_CUDl2fPQ`C^u$GRmd*;uWa)I(V!N$rDL{lpr zJUB)uCHVh|WdnG~XO6@`x&bc25I?h|nZyXzN%1j8v5hh8P|DYsfPpDUOk*E2IH0GE zd1M*A&}{=-ctpg8l!P3DI^}rsF%y`>=h;GMwh-^0xVGSLmhx*58s_-rlqaJB`aPx9 GhwVRAwjjR% literal 0 HcmV?d00001 diff --git a/bin/http/RCResponse.class b/bin/http/RCResponse.class new file mode 100644 index 0000000000000000000000000000000000000000..a2ed6773b42b8d6f636379b97a510529265f647d GIT binary patch literal 265 zcmZ8bF>b;@5S%qO4vvYP{2*PR;F@%aXpmA_K_Zd+vvYz&=8Wuf{*?-ef(P&@#2!M5 zL^HE9tKFI7X@3CliZp~rNLJQv#bUNl?Y1_p3gHvdwfvQ$l4dF9#afk?(0|uP+bN;< zGRg`5to~6EdKknA(C0syHtMt66sq}_MWqN&SzXF1myLG%YWjAiTf$>@6Z=MpzUr?- literal 0 HcmV?d00001 diff --git a/bin/platform/Auth.class b/bin/platform/Auth.class new file mode 100644 index 0000000000000000000000000000000000000000..1b272c04b71c4c940be541750dce4fa5b7828200 GIT binary patch literal 2924 zcma)8S#uOs7(I8g%#zMRCxl%<1Tq=I$R@#oXb4**B#?vwqA+wOP10nR*xdufeP15* zS$*)87phze2A3+zSN#RPTRvOmyO!tP?#c8_1~rxRUB2zy?>qOq`Q`5)9sxLrkHZKF zv@hih_eRmqCx=V!LKw{gT{n$eMlxp<=93e%H_faopc&b$={Om;xM&sx^s%bSlxtgs z`IJDLd1uM8&5Ud1O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Wu2D8$Ih>-$l-=a zn|9aHCvU;0>MC>iTn?X~#!aNYr7bv(LlL@jGKbsRs_KI7eqB`#U%3-^wuPFg)w;S+ z4&OM4P+NuY`p(U?O9W5gDb~Ay>%Ob$B;X91eH|TlCf0CuJ;!tCs7^Ylo8xA>X_)qm zaCL-FMTa|Ap^IH%C$1&M0c^x|bnf+}ypiJ}^swfi%R9E>Ui9K89Ph(6{2JTwTaFK- zk20KxXE1>0FvJ_cHdEsH$V^Ba!T0ceN*koCKfn*^!&7GtI>-{|G-dv(!`<$l+hJdAW=R z04|`)5h{NnGjJ!}cM*+q30E(5jlR|sX2$3dJVwJw9!BsiXOf2zJk41-`SiOk3ApCc zN#(6PKsP9PAoEv%CP`WWvIak~PVp1#2-AlVxx&k$-c>pLXIt}aiXEaPs36G#_goaq z_?%o0%ayR+YSW{@rUhiu6GV{hSKT^7gcfyF5j90a_1z zl+u-esK{>k=PTrV@!|1;!xj0%(r!`+7iH7}m&wKa76ViXljfXSIO~%Tx4@Tf5O#rf oRWtUlqD`e93)D-Hmd)7OTX~QxWoilc%hWMyDMz6`#F2mh14L(yTmS$7 literal 0 HcmV?d00001 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