diff options
author | 2018-03-08 14:43:37 -0800 | |
---|---|---|
committer | 2018-03-14 16:27:24 +0000 | |
commit | d30732db3572050948a6c90f6eb4aabfc15d8cc2 (patch) | |
tree | 1e20ff35057fbd9ab02d3fd5e6fcd59c1d0db200 /sms-client/src/java/main/SmsClient.java | |
parent | 2d5cceffb52baabe08c1e293531ba42c3a9b550a (diff) |
Initial structure and java code for sms client.
Change-Id: I245302bc125515269be948bb8b6874a7d3cb5367
Issue-ID: AAF-98
Signed-off-by: Manjunath Ranganathaiah <manjunath.ranganathaiah@intel.com>
Diffstat (limited to 'sms-client/src/java/main/SmsClient.java')
-rw-r--r-- | sms-client/src/java/main/SmsClient.java | 236 |
1 files changed, 236 insertions, 0 deletions
diff --git a/sms-client/src/java/main/SmsClient.java b/sms-client/src/java/main/SmsClient.java new file mode 100644 index 0000000..69d81e4 --- /dev/null +++ b/sms-client/src/java/main/SmsClient.java @@ -0,0 +1,236 @@ +/* + * Copyright 2018 Intel Corporation, Inc + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.aaf.sms; + +import javax.net.ssl.SSLSocketFactory; +import java.net.URL; +import javax.net.ssl.HttpsURLConnection; +import org.onap.aaf.sms.SmsResponse; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.InputStreamReader; +import java.io.BufferedReader; +import java.io.OutputStreamWriter; +import java.util.Map; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.ArrayList; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +public class SmsClient implements SmsInterface { + + private String baset; + private SSLSocketFactory ssf; + + public SmsClient(String host, int port, SSLSocketFactory s) { + baset = "https://"+ host + ":" + port + "/v1/sms"; + ssf = s; + } + public SmsClient(String host, int port, String version, SSLSocketFactory s) { + baset = "https://"+ host + ":" + port + "/" + version + "/sms"; + ssf = s; + } + + private List<Object> jsontolist(JSONArray a) throws JSONException { + List<Object> l = new ArrayList<Object>(); + for(int i=0;i<a.length();i++) { + Object v = a.get(i); + if ( v instanceof JSONArray ) { + v = jsontolist((JSONArray) v); + } else if (v instanceof JSONObject) { + v = jsontomap((JSONObject) v); + } + l.add(v); + } + return(l); + } + + private Map<String, Object> jsontomap(JSONObject j) throws JSONException { + Map<String, Object> m = new HashMap<String, Object>(); + + Iterator<?> ks = j.keys(); + while( ks.hasNext() ) { + String k = (String)ks.next(); + Object v = j.get(k); + + if ( v instanceof JSONArray ) { + v = jsontolist((JSONArray) v); + } else if ( v instanceof JSONObject ) { + v = jsontomap((JSONObject) v); + } + m.put(k, v); + } + return(m); + } + + private Map<String, Object> strtomap(String r) throws JSONException { + JSONObject jobj = null; + + jobj = new JSONObject(r); + return(jsontomap(jobj)); + + } + private SmsResponse execute(String reqtype, String t, String ins, boolean input, boolean output) { + + HttpsURLConnection conn; + int errorcode = -1; + SmsResponse resp = new SmsResponse(); + + try { + URL url = new URL(t); + conn = (HttpsURLConnection)url.openConnection(); + conn.setSSLSocketFactory(ssf); + conn.setRequestMethod(reqtype); + conn.setDoOutput(true); + conn.setDoInput(true); + conn.setRequestProperty("Content-Type", "application/json"); + conn.setRequestProperty("Accept", "application/json"); + + if ( input ) { + OutputStream out = conn.getOutputStream(); + OutputStreamWriter wr = new OutputStreamWriter(out); + wr.write(ins); + wr.flush(); + wr.close(); + } + errorcode = conn.getResponseCode(); + if ( output && errorcode > 0 ) { + InputStream inputstream = conn.getInputStream(); + InputStreamReader inputstreamreader = new InputStreamReader(inputstream); + BufferedReader bufferedreader = new BufferedReader(inputstreamreader); + + String response; + String save = ""; + while ((response = bufferedreader.readLine()) != null) { + save = save + response; + } + if ( !save.isEmpty() ) { + if ( errorcode/100 == 2 ) { + resp.setResponse(strtomap(save)); + } else { + resp.setErrorMessage(save); + } + } + } + } catch ( Exception e ) { + e.printStackTrace(); + resp.setResponseCode(errorcode); + return(resp); + } + resp.setResponseCode(errorcode); + return resp; + } + @Override + public SmsResponse createDomain(String dname) { + + String t = baset + "/domain"; + String input = "{\"name\":\"" + dname + "\"}"; + + SmsResponse resp = execute("POST", t, input, true, true); + int errcode = resp.getResponseCode(); + + if ( errcode > 0 && errcode/100 == 2 ) + resp.setSuccess(true); + else + resp.setSuccess(false); + + return(resp); + } + @Override + public SmsResponse deleteDomain(String dname) { + + String t = baset + "/domain/" + dname; + + SmsResponse resp = execute("DELETE", t, null, false, true); + int errcode = resp.getResponseCode(); + + if ( errcode > 0 && errcode/100 == 2 ) + resp.setSuccess(true); + else + resp.setSuccess(false); + + return(resp); + } + @Override + public SmsResponse storeSecret(String dname, String sname, Map<String, Object> values) { + + String t = baset + "/domain/" + dname + "/secret"; + Map<String, Object> cm = new HashMap<String, Object>(); + cm.put("name", sname); + cm.put("values", values); + JSONObject jobj = new JSONObject(cm); + + SmsResponse resp = execute("POST", t, jobj.toString(), true, false); + int errcode = resp.getResponseCode(); + + if ( errcode > 0 && errcode/100 == 2 ) + resp.setSuccess(true); + else + resp.setSuccess(false); + + return(resp); + } + @Override + public SmsResponse getSecretNames(String dname) { + + String t = baset + "/domain/" + dname + "/secret"; + + SmsResponse resp = execute("GET", t, null, false, true); + int errcode = resp.getResponseCode(); + + if ( errcode > 0 && errcode/100 == 2 ) + resp.setSuccess(true); + else + resp.setSuccess(false); + + return(resp); + } + @Override + public SmsResponse getSecret(String dname, String sname) { + + String t = baset + "/domain/" + dname + "/secret/" + sname; + + SmsResponse resp = execute("GET", t, null, false, true); + int errcode = resp.getResponseCode(); + + if ( errcode > 0 && errcode/100 == 2 ) + resp.setSuccess(true); + else + resp.setSuccess(false); + + return(resp); + + } + @Override + public SmsResponse deleteSecret(String dname, String sname) { + + String t = baset + "/domain/" + dname + "/secret/" + sname; + + SmsResponse resp = execute("DELETE", t, null, false, true); + int errcode = resp.getResponseCode(); + + if ( errcode > 0 && errcode/100 == 2 ) + resp.setSuccess(true); + else + resp.setSuccess(false); + + return(resp); + } +} |