diff --git a/bin/core/SDK.class b/bin/core/SDK.class index 2db66f4..b2fecbe 100644 Binary files a/bin/core/SDK.class and b/bin/core/SDK.class differ diff --git a/bin/platform/Auth.class b/bin/platform/Auth.class index 1b272c0..5d472ad 100644 Binary files a/bin/platform/Auth.class and b/bin/platform/Auth.class differ diff --git a/bin/platform/Platform$1.class b/bin/platform/Platform$1.class index 28ffdad..181fd01 100644 Binary files a/bin/platform/Platform$1.class and b/bin/platform/Platform$1.class differ diff --git a/bin/platform/Platform.class b/bin/platform/Platform.class index 538d12b..487854f 100644 Binary files a/bin/platform/Platform.class and b/bin/platform/Platform.class differ diff --git a/src/core/SDK.java b/src/core/SDK.java index ff15175..f87c698 100644 --- a/src/core/SDK.java +++ b/src/core/SDK.java @@ -6,7 +6,7 @@ public class SDK { Platform platform; - public SDK(String appKey, String appSecret, String server) { + public SDK(String appKey, String appSecret, Platform.Server server) { platform = new Platform(appKey, appSecret, server); } diff --git a/src/platform/Auth.java b/src/platform/Auth.java index 524acd5..2d2590b 100644 --- a/src/platform/Auth.java +++ b/src/platform/Auth.java @@ -102,9 +102,9 @@ public void setData(Map authData) { 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); +// 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 index 7a76041..02a8a06 100644 --- a/src/platform/Platform.java +++ b/src/platform/Platform.java @@ -5,9 +5,12 @@ import java.io.InputStreamReader; import java.lang.reflect.Type; import java.util.ArrayList; -import java.util.List; import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map.Entry; +import javax.net.ssl.HttpsURLConnection; import javax.xml.bind.DatatypeConverter; import org.apache.http.Consts; @@ -16,8 +19,11 @@ 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.client.methods.HttpUriRequest; import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.message.BasicNameValuePair; import com.google.gson.Gson; @@ -25,43 +31,59 @@ public class Platform { - public String accessToken; + public enum ContentTypeSelection { + FORM_TYPE_MARKDOWN("application/x-www-form-urlencoded"), JSON_TYPE_MARKDOWN( + "application/json"), MULTIPART_TYPE_MARKDOWN( + "multipart/mixed; boundary=Boundary_1_14413901_1361871080888"); + + private String value; + + private ContentTypeSelection(String contentType) { + this.value = contentType; + } + + } + + public enum Server { + + PRODUCTION( + "https://platform.ringcentral.com"), SANDBOX("https://platform.devtest.ringcentral.com"); + private String value; + + private Server(String url) { + this.value = url; + } + } + 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 Server server; - public Platform(String appKey, String appSecret, String server) { + public Platform(String appKey, String appSecret, Server server) { super(); this.appKey = appKey; this.appSecret = appSecret; - this.server = server.toString().equals("RC_PRODUCTION") ? RC_PRODUCTION - : RC_SANDBOX; + this.server = server; this.auth = new Auth(); - } public void authCall(List body) { HttpClient client = new DefaultHttpClient(); - HttpPost post = new HttpPost(this.server + authURL); + HttpPost post = new HttpPost(server.value + authURL); post.setHeader(HttpHeaders.AUTHORIZATION, "Basic " + encodeAPICredentialsToBase64()); post.setHeader(HttpHeaders.CONTENT_TYPE, - "application/x-www-form-urlencoded"); + ContentTypeSelection.FORM_TYPE_MARKDOWN.value); 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())); @@ -70,12 +92,12 @@ public void authCall(List body) { while ((line = rd.readLine()) != null) { result.append(line); } - System.out.println(result.toString()); + // System.out.println(result.toString()); Gson gson = new Gson(); Type HashMapType = new TypeToken>() { }.getType(); - HashMap authData = gson.fromJson(result.toString(), - HashMapType); + HashMap authData = gson.fromJson( + result.toString(), HashMapType); setAuth(auth, authData); } } catch (IOException e1) { @@ -101,6 +123,10 @@ public String encodeAPICredentialsToBase64() { return DatatypeConverter.printBase64Binary(message); } + public String getAuthHeader(){ + return this.auth.getTokenType() + " " + this.getAccessToken(); + } + public String getAccessToken() { return auth.getAccessToken(); } @@ -113,4 +139,177 @@ public void setAuth(Auth auth, HashMap authData) { this.auth.setData(authData); } -} \ No newline at end of file + + //apiCall("post", "/call-log", "{body}", header) + + //Additionals + + + + public HttpResponse apiCall(String method, String apiURL, LinkedHashMap body, HashMap headerMap) throws IOException { + + //this.isAuthorized(); + + String URL = server.value+apiURL; + + HttpClient client = HttpClientBuilder.create().build(); + HttpResponse response=null; + if(headerMap==null){ + headerMap= new HashMap(); + } + + headerMap.put("Authorization", getAuthHeader()); + if(method.equalsIgnoreCase("get")){ + HttpGet httpget = new HttpGet(URL); + for (Entry entry : headerMap.entrySet()) { + httpget.addHeader(entry.getKey(), entry.getValue()); + } + response = client.execute(httpget); + } + + return response; + } + + + + // + //BufferedReader in = null; + // + //String output = ""; + //try { + // + // StringBuilder data = new StringBuilder(); + // byte[] byteArray = data.toString().getBytes("UTF-8"); + // + // URL requests = new URL(urll); + // httpConn = (HttpsURLConnection) requests.openConnection(); + // httpConn.setRequestMethod("GET"); + // httpConn.setRequestProperty( + // "Authorization", + // "Bearer "+getAccessToken().toString()); + // httpConn.setDoOutput(true); + // System.out.println("Here"); + // InputStreamReader reader = new InputStreamReader( + // httpConn.getInputStream()); + // in = new BufferedReader(reader); + // System.out.println("Done"); + // StringBuffer content = new StringBuffer(); + // String line; + // while ((line = in.readLine()) != null) { + // content.append(line + "\n"); + // } + // in.close(); + // + // String json = content.toString(); + // Date date = new Date(); + // output = "--------------"+date+"-------------------\n"; + // output = output+ json; + // System.out.println("presence : "+ json); + // + //} catch (java.io.IOException e) { + // output = output+ e.getMessage(); + // System.out.println(e.getMessage()); + // + //} finally { + // if (in != null) + // in.close(); + // if (httpConn != null) + // httpConn.disconnect(); + //} + // + //output=output+"\n --------------------------------------------\n\n\n"; + // + // + //} + // + // + // + // + // + // + // + // + // + // + // + // + // + // + // + // + // + // + // + // + // + // + + + + + + + + + + + + // + // + // + // + // OkHttpClient client = new OkHttpClient(); + // //Check if the Platform is authorized, and add the authorization header + // this.isAuthorized(); + // headerMap.put("Authorization", this.getAuthHeader()); + // //Generate the proper url to be passed into the request + // HashMap options = new HashMap<>(); + // options.put("addServer", "true"); + // String apiUrl = apiURL(url, options); + // + // Request.Builder requestBuilder = new Request.Builder(); + // //Add all the headers to the Request.Builder from the headerMap + // for (Map.Entry entry : headerMap.entrySet()) { + // requestBuilder.addHeader(entry.getKey(), entry.getValue()); + // } + // Request request = null; + // if (method.toUpperCase().equals("GET")) { + // request = requestBuilder + // .url(apiUrl) + // .build(); + // } else if (method.toUpperCase().equals("DELETE")) { + // request = requestBuilder + // .url(apiUrl) + // .delete() + // .build(); + // } else { + // //For POST and PUT requests, find and set what MediaType the body is + // MediaType mediaType; + // if (headerMap.containsValue("application/json")) { + // mediaType = MediaType.parse(ContentTypeSelection.JSON_TYPE_MARKDOWN.toString()); + // } else if (headerMap.containsValue("multipart/mixed")) { + // mediaType = MediaType.parse(ContentTypeSelection.MULTIPART_TYPE_MARKDOWN.toString()); + // } else { + // mediaType =MediaType.parse(ContentTypeSelection.FORM_TYPE_MARKDOWN.toString()); + // } + // String bodyString = getBodyString(body, mediaType); + // if (method.toUpperCase().equals("POST")) { + // request = requestBuilder + // .url(apiUrl) + // .post(RequestBody.create(mediaType, bodyString)) + // .build(); + // } else if (method.toUpperCase().equals("PUT")) { + // request = requestBuilder + // .url(apiUrl) + // .put(RequestBody.create(mediaType, bodyString)) + // .build(); + // } + // } + // //Make OKHttp request call, that returns response to the callback + // client.newCall(request).enqueue(callback); + // } catch (Exception e) { + // e.printStackTrace(); + // } +} + +