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 protected Map<String, Object> strtomap(String r) throws JSONException {
95 JSONObject jobj = null;
97 jobj = new JSONObject(r);
98 return(jsontomap(jobj));
103 Inputs reqtype - type of Request, POST, GET, DELETE, PUT
104 urlstr - url to connect to
105 body - json encoded data being sent to SMS server
106 output - expect a response data from SMS server
107 Return SmsResponse Object
109 response code if connection succeeded, otherwise -1
110 response string if expected.
112 protected SmsResponse execute(String reqtype, String urlstr, String body,
115 HttpsURLConnection conn;
117 SmsResponse resp = new SmsResponse();
120 URL url = new URL(urlstr);
121 conn = (HttpsURLConnection)url.openConnection();
122 conn.setSSLSocketFactory(ssf);
123 conn.setRequestMethod(reqtype);
124 conn.setRequestProperty("Content-Type", "application/json");
125 conn.setRequestProperty("Accept", "application/json");
127 // If we have any data in body write it out
128 if ( body != null ) {
129 conn.setDoOutput(true);
130 // Implicitly connects and writes data
131 OutputStream out = conn.getOutputStream();
132 OutputStreamWriter wr = new OutputStreamWriter(out);
138 // Parse the response from Server here
139 // An implicit connection happens here
140 errorcode = conn.getResponseCode();
141 if ( output && errorcode > 0 ) {
142 InputStream inputstream = conn.getInputStream();
143 InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
144 BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
148 while ((response = bufferedreader.readLine()) != null) {
149 save = save + response;
151 if ( !save.isEmpty() ) {
152 if ( errorcode/100 == 2 ) {
153 resp.setResponse(strtomap(save));
155 resp.setErrorMessage(save);
159 } catch ( Exception e ) {
161 resp.setResponseCode(errorcode);
164 resp.setResponseCode(errorcode);
168 public SmsResponse createDomain(String dname) {
170 String t = baset + "/domain";
171 String input = "{\"name\":\"" + dname + "\"}";
173 SmsResponse resp = execute("POST", t, input, true);
174 int errcode = resp.getResponseCode();
176 if ( errcode > 0 && errcode/100 == 2 )
177 resp.setSuccess(true);
179 resp.setSuccess(false);
184 public SmsResponse deleteDomain(String dname) {
186 String t = baset + "/domain/" + dname;
188 SmsResponse resp = execute("DELETE", t, null, true);
189 int errcode = resp.getResponseCode();
191 if ( errcode > 0 && errcode/100 == 2 )
192 resp.setSuccess(true);
194 resp.setSuccess(false);
199 public SmsResponse storeSecret(String dname, String sname, Map<String, Object> values) {
201 String t = baset + "/domain/" + dname + "/secret";
202 Map<String, Object> cm = new HashMap<String, Object>();
203 cm.put("name", sname);
204 cm.put("values", values);
205 JSONObject jobj = new JSONObject(cm);
207 SmsResponse resp = execute("POST", t, jobj.toString(), false);
208 int errcode = resp.getResponseCode();
210 if ( errcode > 0 && errcode/100 == 2 )
211 resp.setSuccess(true);
213 resp.setSuccess(false);
218 public SmsResponse getSecretNames(String dname) {
220 String t = baset + "/domain/" + dname + "/secret";
222 SmsResponse resp = execute("GET", t, null, true);
223 int errcode = resp.getResponseCode();
225 if ( errcode > 0 && errcode/100 == 2 )
226 resp.setSuccess(true);
228 resp.setSuccess(false);
233 public SmsResponse getSecret(String dname, String sname) {
235 String t = baset + "/domain/" + dname + "/secret/" + sname;
237 SmsResponse resp = execute("GET", t, null, true);
238 int errcode = resp.getResponseCode();
240 if ( errcode > 0 && errcode/100 == 2 ) {
241 Map<String, Object> m = getSubmap(resp.getResponse(), "values");
243 resp.setSuccess(true);
247 resp.setSuccess(false);
251 resp.setSuccess(false);
258 public SmsResponse deleteSecret(String dname, String sname) {
260 String t = baset + "/domain/" + dname + "/secret/" + sname;
262 SmsResponse resp = execute("DELETE", t, null, true);
263 int errcode = resp.getResponseCode();
265 if ( errcode > 0 && errcode/100 == 2 )
266 resp.setSuccess(true);
268 resp.setSuccess(false);