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;
143 if ( errorcode/100 == 2) {
144 inputStream = conn.getInputStream();
146 inputStream = conn.getErrorStream();
149 InputStreamReader inputstreamreader = new InputStreamReader(inputStream);
150 BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
154 while ((response = bufferedreader.readLine()) != null) {
155 save = save + response;
157 if ( !save.isEmpty() ) {
158 if ( errorcode/100 == 2) {
159 resp.setResponse(strtomap(save));
161 resp.setErrorMessage(save);
165 } catch ( Exception e ) {
166 resp.setResponseCode(errorcode);
169 resp.setResponseCode(errorcode);
173 public SmsResponse createDomain(String dname) {
175 String t = baset + "/domain";
176 String input = "{\"name\":\"" + dname + "\"}";
178 SmsResponse resp = execute("POST", t, input, true);
179 int errcode = resp.getResponseCode();
181 if ( errcode > 0 && errcode/100 == 2 )
182 resp.setSuccess(true);
184 resp.setSuccess(false);
189 public SmsResponse deleteDomain(String dname) {
191 String t = baset + "/domain/" + dname;
193 SmsResponse resp = execute("DELETE", t, null, true);
194 int errcode = resp.getResponseCode();
196 if ( errcode > 0 && errcode/100 == 2 )
197 resp.setSuccess(true);
199 resp.setSuccess(false);
204 public SmsResponse storeSecret(String dname, String sname, Map<String, Object> values) {
206 String t = baset + "/domain/" + dname + "/secret";
207 Map<String, Object> cm = new HashMap<String, Object>();
208 cm.put("name", sname);
209 cm.put("values", values);
210 JSONObject jobj = new JSONObject(cm);
212 SmsResponse resp = execute("POST", t, jobj.toString(), false);
213 int errcode = resp.getResponseCode();
215 if ( errcode > 0 && errcode/100 == 2 )
216 resp.setSuccess(true);
218 resp.setSuccess(false);
223 public SmsResponse getSecretNames(String dname) {
225 String t = baset + "/domain/" + dname + "/secret";
227 SmsResponse resp = execute("GET", t, null, true);
228 int errcode = resp.getResponseCode();
230 if ( errcode > 0 && errcode/100 == 2 )
231 resp.setSuccess(true);
233 resp.setSuccess(false);
238 public SmsResponse getSecret(String dname, String sname) {
240 String t = baset + "/domain/" + dname + "/secret/" + sname;
242 SmsResponse resp = execute("GET", t, null, true);
243 int errcode = resp.getResponseCode();
245 if ( errcode > 0 && errcode/100 == 2 ) {
246 Map<String, Object> m = getSubmap(resp.getResponse(), "values");
248 resp.setSuccess(true);
252 resp.setSuccess(false);
256 resp.setSuccess(false);
263 public SmsResponse deleteSecret(String dname, String sname) {
265 String t = baset + "/domain/" + dname + "/secret/" + sname;
267 SmsResponse resp = execute("DELETE", t, null, true);
268 int errcode = resp.getResponseCode();
270 if ( errcode > 0 && errcode/100 == 2 )
271 resp.setSuccess(true);
273 resp.setSuccess(false);