8bc88fe37c71e1feee4f075f8d43c39c900b5ec3
[aaf/sms.git] / sms-client / java / src / main / java / org / onap / aaf / sms / SmsClient.java
1 /*
2  * Copyright 2018 Intel Corporation, Inc
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.aaf.sms;
18
19 import javax.net.ssl.SSLSocketFactory;
20 import java.net.URL;
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;
28 import java.util.Map;
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;
36
37 public class SmsClient implements SmsInterface {
38
39     private String baset;
40     private SSLSocketFactory ssf;
41
42     public SmsClient(String host, int port, SSLSocketFactory s) {
43         baset = "https://"+ host + ":" + port + "/v1/sms";
44         ssf = s;
45     }
46     public SmsClient(String host, int port, String version, SSLSocketFactory s) {
47         baset = "https://"+ host + ":" + port + "/" + version + "/sms";
48         ssf = s;
49     }
50
51     private  Map<String, Object> getSubmap(Map<String, Object> raw, String k) {
52         Object v = raw.get(k);
53         if ( v != null ) {
54             Map<String, Object> r = (Map<String, Object>)v;
55             return(r);
56         }
57         else {
58             return(null);
59         }
60     }
61
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++) {
65             Object v = a.get(i);
66             if ( v instanceof JSONArray ) {
67                 v = jsontolist((JSONArray) v);
68             } else if (v instanceof JSONObject) {
69                 v = jsontomap((JSONObject) v);
70             }
71             l.add(v);
72         }
73         return(l);
74     }
75
76     private Map<String, Object> jsontomap(JSONObject j) throws JSONException {
77         Map<String, Object> m = new HashMap<String, Object>();
78
79         Iterator<?> ks = j.keys();
80         while( ks.hasNext() ) {
81             String k = (String)ks.next();
82             Object v = j.get(k);
83
84             if ( v instanceof JSONArray ) {
85                 v = jsontolist((JSONArray) v);
86             } else if ( v instanceof JSONObject ) {
87                 v = jsontomap((JSONObject) v);
88             }
89             m.put(k, v);
90         }
91         return(m);
92     }
93
94     protected Map<String, Object> strtomap(String r) throws JSONException {
95         JSONObject jobj = null;
96
97         jobj = new JSONObject(r);
98         return(jsontomap(jobj));
99
100     }
101
102     /*
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
108             success or failure
109             response code if connection succeeded, otherwise -1
110             response string if expected.
111     */
112     protected SmsResponse execute(String reqtype, String urlstr, String body,
113         boolean output) {
114
115         HttpsURLConnection conn;
116         int errorcode = -1;
117         SmsResponse resp = new SmsResponse();
118
119         try {
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");
126
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);
133                 wr.write(body);
134                 wr.flush();
135                 wr.close();
136             }
137
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);
145
146                 String response;
147                 String save = "";
148                 while ((response = bufferedreader.readLine()) != null) {
149                     save = save + response;
150                 }
151                 if ( !save.isEmpty() ) {
152                     if ( errorcode/100 == 2 ) {
153                         resp.setResponse(strtomap(save));
154                     } else {
155                         resp.setErrorMessage(save);
156                     }
157                 }
158             }
159         } catch ( Exception e ) {
160             e.printStackTrace();
161             resp.setResponseCode(errorcode);
162             return(resp);
163         }
164         resp.setResponseCode(errorcode);
165         return resp;
166     }
167     @Override
168     public SmsResponse createDomain(String dname) {
169
170         String t = baset + "/domain";
171         String input = "{\"name\":\"" + dname + "\"}";
172
173         SmsResponse resp = execute("POST", t, input, true);
174         int errcode = resp.getResponseCode();
175
176         if ( errcode > 0 && errcode/100 == 2 )
177             resp.setSuccess(true);
178         else
179             resp.setSuccess(false);
180
181         return(resp);
182     }
183     @Override
184     public SmsResponse deleteDomain(String dname) {
185
186         String t = baset + "/domain/" + dname;
187
188         SmsResponse resp = execute("DELETE", t, null, true);
189         int errcode = resp.getResponseCode();
190
191         if ( errcode > 0 && errcode/100 == 2 )
192             resp.setSuccess(true);
193         else
194             resp.setSuccess(false);
195
196         return(resp);
197     }
198     @Override
199     public SmsResponse storeSecret(String dname, String sname, Map<String, Object> values) {
200
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);
206
207         SmsResponse resp = execute("POST", t, jobj.toString(), false);
208         int errcode = resp.getResponseCode();
209
210         if ( errcode > 0 && errcode/100 == 2 )
211             resp.setSuccess(true);
212         else
213             resp.setSuccess(false);
214
215         return(resp);
216     }
217     @Override
218     public SmsResponse getSecretNames(String dname) {
219
220         String t = baset + "/domain/" + dname + "/secret";
221
222         SmsResponse resp = execute("GET", t, null, true);
223         int errcode = resp.getResponseCode();
224
225         if ( errcode > 0 && errcode/100 == 2 )
226             resp.setSuccess(true);
227         else
228             resp.setSuccess(false);
229
230         return(resp);
231     }
232     @Override
233     public SmsResponse getSecret(String dname, String sname) {
234
235         String t = baset + "/domain/" + dname + "/secret/" + sname;
236
237         SmsResponse resp = execute("GET", t, null, true);
238         int errcode = resp.getResponseCode();
239
240         if ( errcode > 0 && errcode/100 == 2 ) {
241             Map<String, Object> m = getSubmap(resp.getResponse(), "values");
242             if ( m != null ) {
243                 resp.setSuccess(true);
244                 resp.setResponse(m);
245             }
246             else {
247                 resp.setSuccess(false);
248             }
249         }
250         else {
251             resp.setSuccess(false);
252         }
253
254         return(resp);
255
256     }
257     @Override
258     public SmsResponse deleteSecret(String dname, String sname) {
259
260         String t = baset + "/domain/" + dname + "/secret/" + sname;
261
262         SmsResponse resp = execute("DELETE", t, null, true);
263         int errcode = resp.getResponseCode();
264
265         if ( errcode > 0 && errcode/100 == 2 )
266             resp.setSuccess(true);
267         else
268             resp.setSuccess(false);
269
270         return(resp);
271     }
272 }