Return only values to the caller of getSecret
[aaf/sms.git] / sms-client / src / java / main / 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     private 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     private SmsResponse execute(String reqtype, String t, String ins, boolean input, boolean output) {
102
103         HttpsURLConnection conn;
104         int errorcode = -1;
105         SmsResponse resp = new SmsResponse();
106
107         try {
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");
116
117             if ( input ) {
118                 OutputStream out = conn.getOutputStream();
119                 OutputStreamWriter wr = new OutputStreamWriter(out);
120                 wr.write(ins);
121                 wr.flush();
122                 wr.close();
123             }
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);
129
130                 String response;
131                 String save = "";
132                 while ((response = bufferedreader.readLine()) != null) {
133                     save = save + response;
134                 }
135                 if ( !save.isEmpty() ) {
136                     if ( errorcode/100 == 2 ) {
137                         resp.setResponse(strtomap(save));
138                     } else {
139                         resp.setErrorMessage(save);
140                     }
141                 }
142             }
143         } catch ( Exception e ) {
144             e.printStackTrace();
145             resp.setResponseCode(errorcode);
146             return(resp);
147         }
148         resp.setResponseCode(errorcode);
149         return resp;
150     }
151     @Override
152     public SmsResponse createDomain(String dname) {
153
154         String t = baset + "/domain";
155         String input = "{\"name\":\"" + dname + "\"}";
156
157         SmsResponse resp = execute("POST", t, input, true, true);
158         int errcode = resp.getResponseCode();
159
160         if ( errcode > 0 && errcode/100 == 2 )
161             resp.setSuccess(true);
162         else
163             resp.setSuccess(false);
164
165         return(resp);
166     }
167     @Override
168     public SmsResponse deleteDomain(String dname) {
169
170         String t = baset + "/domain/" + dname;
171
172         SmsResponse resp = execute("DELETE", t, null, false, true);
173         int errcode = resp.getResponseCode();
174
175         if ( errcode > 0 && errcode/100 == 2 )
176             resp.setSuccess(true);
177         else
178             resp.setSuccess(false);
179
180         return(resp);
181     }
182     @Override
183     public SmsResponse storeSecret(String dname, String sname, Map<String, Object> values) {
184
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);
190
191         SmsResponse resp = execute("POST", t, jobj.toString(), true, false);
192         int errcode = resp.getResponseCode();
193
194         if ( errcode > 0 && errcode/100 == 2 )
195             resp.setSuccess(true);
196         else
197             resp.setSuccess(false);
198
199         return(resp);
200     }
201     @Override
202     public SmsResponse getSecretNames(String dname) {
203
204         String t = baset + "/domain/" + dname + "/secret";
205
206         SmsResponse resp = execute("GET", t, null, false, true);
207         int errcode = resp.getResponseCode();
208
209         if ( errcode > 0 && errcode/100 == 2 )
210             resp.setSuccess(true);
211         else
212             resp.setSuccess(false);
213
214         return(resp);
215     }
216     @Override
217     public SmsResponse getSecret(String dname, String sname) {
218
219         String t = baset + "/domain/" + dname + "/secret/" + sname;
220
221         SmsResponse resp = execute("GET", t, null, false, true);
222         int errcode = resp.getResponseCode();
223
224         if ( errcode > 0 && errcode/100 == 2 ) {
225             Map<String, Object> m = getSubmap(resp.getResponse(), "values");
226             if ( m != null ) {
227                 resp.setSuccess(true);
228                 resp.setResponse(m);
229             }
230             else {
231                 resp.setSuccess(false);
232             }
233         }
234         else {
235             resp.setSuccess(false);
236         }
237
238         return(resp);
239
240     }
241     @Override
242     public SmsResponse deleteSecret(String dname, String sname) {
243
244         String t = baset + "/domain/" + dname + "/secret/" + sname;
245
246         SmsResponse resp = execute("DELETE", t, null, false, true);
247         int errcode = resp.getResponseCode();
248
249         if ( errcode > 0 && errcode/100 == 2 )
250             resp.setSuccess(true);
251         else
252             resp.setSuccess(false);
253
254         return(resp);
255     }
256 }