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 Map<String, Object> getSubmap(Map<String, Object> raw, String k) {
52 Object v = raw.get(k);
54 Map<String, Object> r = (Map<String, Object>)v;
62 private List<Object> jsontolist(JSONArray a) throws JSONException {
63 List<Object> l = new ArrayList<Object>();
64 for(int i=0;i<a.length();i++) {
66 if ( v instanceof JSONArray ) {
67 v = jsontolist((JSONArray) v);
68 } else if (v instanceof JSONObject) {
69 v = jsontomap((JSONObject) v);
76 private Map<String, Object> jsontomap(JSONObject j) throws JSONException {
77 Map<String, Object> m = new HashMap<String, Object>();
79 Iterator<?> ks = j.keys();
80 while( ks.hasNext() ) {
81 String k = (String)ks.next();
84 if ( v instanceof JSONArray ) {
85 v = jsontolist((JSONArray) v);
86 } else if ( v instanceof JSONObject ) {
87 v = jsontomap((JSONObject) v);
94 private Map<String, Object> strtomap(String r) throws JSONException {
95 JSONObject jobj = null;
97 jobj = new JSONObject(r);
98 return(jsontomap(jobj));
101 private SmsResponse execute(String reqtype, String t, String ins, boolean input, boolean output) {
103 HttpsURLConnection conn;
105 SmsResponse resp = new SmsResponse();
108 URL url = new URL(t);
109 conn = (HttpsURLConnection)url.openConnection();
110 conn.setSSLSocketFactory(ssf);
111 conn.setRequestMethod(reqtype);
112 conn.setDoOutput(true);
113 conn.setDoInput(true);
114 conn.setRequestProperty("Content-Type", "application/json");
115 conn.setRequestProperty("Accept", "application/json");
118 OutputStream out = conn.getOutputStream();
119 OutputStreamWriter wr = new OutputStreamWriter(out);
124 errorcode = conn.getResponseCode();
125 if ( output && errorcode > 0 ) {
126 InputStream inputstream = conn.getInputStream();
127 InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
128 BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
132 while ((response = bufferedreader.readLine()) != null) {
133 save = save + response;
135 if ( !save.isEmpty() ) {
136 if ( errorcode/100 == 2 ) {
137 resp.setResponse(strtomap(save));
139 resp.setErrorMessage(save);
143 } catch ( Exception e ) {
145 resp.setResponseCode(errorcode);
148 resp.setResponseCode(errorcode);
152 public SmsResponse createDomain(String dname) {
154 String t = baset + "/domain";
155 String input = "{\"name\":\"" + dname + "\"}";
157 SmsResponse resp = execute("POST", t, input, true, true);
158 int errcode = resp.getResponseCode();
160 if ( errcode > 0 && errcode/100 == 2 )
161 resp.setSuccess(true);
163 resp.setSuccess(false);
168 public SmsResponse deleteDomain(String dname) {
170 String t = baset + "/domain/" + dname;
172 SmsResponse resp = execute("DELETE", t, null, false, true);
173 int errcode = resp.getResponseCode();
175 if ( errcode > 0 && errcode/100 == 2 )
176 resp.setSuccess(true);
178 resp.setSuccess(false);
183 public SmsResponse storeSecret(String dname, String sname, Map<String, Object> values) {
185 String t = baset + "/domain/" + dname + "/secret";
186 Map<String, Object> cm = new HashMap<String, Object>();
187 cm.put("name", sname);
188 cm.put("values", values);
189 JSONObject jobj = new JSONObject(cm);
191 SmsResponse resp = execute("POST", t, jobj.toString(), true, false);
192 int errcode = resp.getResponseCode();
194 if ( errcode > 0 && errcode/100 == 2 )
195 resp.setSuccess(true);
197 resp.setSuccess(false);
202 public SmsResponse getSecretNames(String dname) {
204 String t = baset + "/domain/" + dname + "/secret";
206 SmsResponse resp = execute("GET", t, null, false, true);
207 int errcode = resp.getResponseCode();
209 if ( errcode > 0 && errcode/100 == 2 )
210 resp.setSuccess(true);
212 resp.setSuccess(false);
217 public SmsResponse getSecret(String dname, String sname) {
219 String t = baset + "/domain/" + dname + "/secret/" + sname;
221 SmsResponse resp = execute("GET", t, null, false, true);
222 int errcode = resp.getResponseCode();
224 if ( errcode > 0 && errcode/100 == 2 ) {
225 Map<String, Object> m = getSubmap(resp.getResponse(), "values");
227 resp.setSuccess(true);
231 resp.setSuccess(false);
235 resp.setSuccess(false);
242 public SmsResponse deleteSecret(String dname, String sname) {
244 String t = baset + "/domain/" + dname + "/secret/" + sname;
246 SmsResponse resp = execute("DELETE", t, null, false, true);
247 int errcode = resp.getResponseCode();
249 if ( errcode > 0 && errcode/100 == 2 )
250 resp.setSuccess(true);
252 resp.setSuccess(false);