2 * Copyright 2018 Intel Corporation, Inc
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.aaf.sms;
19 import javax.net.ssl.SSLSocketFactory;
21 import javax.net.ssl.HttpsURLConnection;
22 import org.onap.aaf.sms.SmsResponse;
23 import java.io.InputStream;
24 import java.io.OutputStream;
25 import java.io.InputStreamReader;
26 import java.io.BufferedReader;
27 import java.io.OutputStreamWriter;
29 import java.util.HashMap;
30 import java.util.Iterator;
31 import java.util.List;
32 import java.util.ArrayList;
33 import org.json.JSONArray;
34 import org.json.JSONException;
35 import org.json.JSONObject;
37 public class SmsClient implements SmsInterface {
40 private SSLSocketFactory ssf;
42 public SmsClient(String host, int port, SSLSocketFactory s) {
43 baset = "https://"+ host + ":" + port + "/v1/sms";
46 public SmsClient(String host, int port, String version, SSLSocketFactory s) {
47 baset = "https://"+ host + ":" + port + "/" + version + "/sms";
51 private List<Object> jsontolist(JSONArray a) throws JSONException {
52 List<Object> l = new ArrayList<Object>();
53 for(int i=0;i<a.length();i++) {
55 if ( v instanceof JSONArray ) {
56 v = jsontolist((JSONArray) v);
57 } else if (v instanceof JSONObject) {
58 v = jsontomap((JSONObject) v);
65 private Map<String, Object> jsontomap(JSONObject j) throws JSONException {
66 Map<String, Object> m = new HashMap<String, Object>();
68 Iterator<?> ks = j.keys();
69 while( ks.hasNext() ) {
70 String k = (String)ks.next();
73 if ( v instanceof JSONArray ) {
74 v = jsontolist((JSONArray) v);
75 } else if ( v instanceof JSONObject ) {
76 v = jsontomap((JSONObject) v);
83 private Map<String, Object> strtomap(String r) throws JSONException {
84 JSONObject jobj = null;
86 jobj = new JSONObject(r);
87 return(jsontomap(jobj));
90 private SmsResponse execute(String reqtype, String t, String ins, boolean input, boolean output) {
92 HttpsURLConnection conn;
94 SmsResponse resp = new SmsResponse();
98 conn = (HttpsURLConnection)url.openConnection();
99 conn.setSSLSocketFactory(ssf);
100 conn.setRequestMethod(reqtype);
101 conn.setDoOutput(true);
102 conn.setDoInput(true);
103 conn.setRequestProperty("Content-Type", "application/json");
104 conn.setRequestProperty("Accept", "application/json");
107 OutputStream out = conn.getOutputStream();
108 OutputStreamWriter wr = new OutputStreamWriter(out);
113 errorcode = conn.getResponseCode();
114 if ( output && errorcode > 0 ) {
115 InputStream inputstream = conn.getInputStream();
116 InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
117 BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
121 while ((response = bufferedreader.readLine()) != null) {
122 save = save + response;
124 if ( !save.isEmpty() ) {
125 if ( errorcode/100 == 2 ) {
126 resp.setResponse(strtomap(save));
128 resp.setErrorMessage(save);
132 } catch ( Exception e ) {
134 resp.setResponseCode(errorcode);
137 resp.setResponseCode(errorcode);
141 public SmsResponse createDomain(String dname) {
143 String t = baset + "/domain";
144 String input = "{\"name\":\"" + dname + "\"}";
146 SmsResponse resp = execute("POST", t, input, true, true);
147 int errcode = resp.getResponseCode();
149 if ( errcode > 0 && errcode/100 == 2 )
150 resp.setSuccess(true);
152 resp.setSuccess(false);
157 public SmsResponse deleteDomain(String dname) {
159 String t = baset + "/domain/" + dname;
161 SmsResponse resp = execute("DELETE", t, null, false, true);
162 int errcode = resp.getResponseCode();
164 if ( errcode > 0 && errcode/100 == 2 )
165 resp.setSuccess(true);
167 resp.setSuccess(false);
172 public SmsResponse storeSecret(String dname, String sname, Map<String, Object> values) {
174 String t = baset + "/domain/" + dname + "/secret";
175 Map<String, Object> cm = new HashMap<String, Object>();
176 cm.put("name", sname);
177 cm.put("values", values);
178 JSONObject jobj = new JSONObject(cm);
180 SmsResponse resp = execute("POST", t, jobj.toString(), true, false);
181 int errcode = resp.getResponseCode();
183 if ( errcode > 0 && errcode/100 == 2 )
184 resp.setSuccess(true);
186 resp.setSuccess(false);
191 public SmsResponse getSecretNames(String dname) {
193 String t = baset + "/domain/" + dname + "/secret";
195 SmsResponse resp = execute("GET", t, null, false, true);
196 int errcode = resp.getResponseCode();
198 if ( errcode > 0 && errcode/100 == 2 )
199 resp.setSuccess(true);
201 resp.setSuccess(false);
206 public SmsResponse getSecret(String dname, String sname) {
208 String t = baset + "/domain/" + dname + "/secret/" + sname;
210 SmsResponse resp = execute("GET", t, null, false, true);
211 int errcode = resp.getResponseCode();
213 if ( errcode > 0 && errcode/100 == 2 )
214 resp.setSuccess(true);
216 resp.setSuccess(false);
222 public SmsResponse deleteSecret(String dname, String sname) {
224 String t = baset + "/domain/" + dname + "/secret/" + sname;
226 SmsResponse resp = execute("DELETE", t, null, false, true);
227 int errcode = resp.getResponseCode();
229 if ( errcode > 0 && errcode/100 == 2 )
230 resp.setSuccess(true);
232 resp.setSuccess(false);