1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ============LICENSE_END=========================================================
19 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21 *******************************************************************************/
22 package org.onap.dmaap.mr.client.impl;
24 import java.net.MalformedURLException;
25 import java.util.Collection;
27 import java.util.TreeSet;
28 import java.util.concurrent.TimeUnit;
30 import javax.ws.rs.client.WebTarget;
31 import javax.ws.rs.core.Response;
33 import org.apache.http.HttpException;
34 import org.glassfish.jersey.client.ClientConfig;
35 import org.glassfish.jersey.internal.util.Base64;
36 import org.json.JSONArray;
37 import org.json.JSONException;
38 import org.json.JSONObject;
39 import org.json.JSONTokener;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 import com.att.nsa.apiClient.http.CacheUse;
44 import com.att.nsa.apiClient.http.HttpClient;
45 import org.onap.dmaap.mr.client.MRClient;
46 import org.onap.dmaap.mr.client.MRClientFactory;
47 import org.onap.dmaap.mr.test.clients.ProtocolTypeConstants;
49 public class MRBaseClient extends HttpClient implements MRClient {
51 private ClientConfig clientConfig = null;
53 protected MRBaseClient(Collection<String> hosts) throws MalformedURLException {
54 super(ConnectionType.HTTP, hosts, MRConstants.kStdMRServicePort);
56 fLog = LoggerFactory.getLogger(this.getClass().getName());
59 protected MRBaseClient(Collection<String> hosts, int stdSvcPort) throws MalformedURLException {
60 super(ConnectionType.HTTP, hosts, stdSvcPort);
62 fLog = LoggerFactory.getLogger(this.getClass().getName());
65 protected MRBaseClient(Collection<String> hosts, String clientSignature) throws MalformedURLException {
66 super(ConnectionType.HTTP, hosts, MRConstants.kStdMRServicePort, clientSignature, CacheUse.NONE, 1, 1L,
67 TimeUnit.MILLISECONDS, 32, 32, 600000);
69 fLog = LoggerFactory.getLogger(this.getClass().getName());
72 public ClientConfig getClientConfig1() {
76 public void setClientConfig(ClientConfig config) {
77 this.clientConfig = config;
84 protected Set<String> jsonArrayToSet(JSONArray a) {
88 final TreeSet<String> set = new TreeSet<>();
89 for (int i = 0; i < a.length(); i++) {
90 set.add(a.getString(i));
95 public void logTo(Logger log) {
100 protected Logger getLog() {
106 public JSONObject post(final String path, final byte[] data, final String contentType, final String username,
107 final String password, final String protocalFlag) throws HttpException, JSONException {
108 if ((null != username && null != password)) {
109 WebTarget target = null;
110 Response response = null;
111 target = DmaapClientUtil.getTarget(clientConfig,path, username, password);
112 String encoding = Base64.encodeAsString(username + ":" + password);
114 response = DmaapClientUtil.postResponsewtBasicAuth(target, encoding, data, contentType);
116 return getResponseDataInJson(response);
118 throw new HttpException(
119 "Authentication Failed: Username/password/AuthKey/AuthDate parameter(s) cannot be null or empty.");
123 public JSONObject postNoAuth(final String path, final byte[] data, String contentType)
124 throws HttpException, JSONException {
125 WebTarget target = null;
126 Response response = null;
127 if (contentType == null) {
128 contentType = "text/pain";
130 target = DmaapClientUtil.getTarget(clientConfig,path);
132 response = DmaapClientUtil.postResponsewtNoAuth(target, data, contentType);
134 return getResponseDataInJson(response);
137 public String postWithResponse(final String path, final byte[] data, final String contentType,
138 final String username, final String password, final String protocolFlag)
139 throws HttpException, JSONException {
140 String responseData = null;
141 if ((null != username && null != password)) {
142 WebTarget target = null;
143 Response response = null;
144 target = DmaapClientUtil.getTarget(clientConfig,path, username, password);
145 String encoding = Base64.encodeAsString(username + ":" + password);
147 response = DmaapClientUtil.postResponsewtBasicAuth(target, encoding, data, contentType);
149 responseData = (String) response.readEntity(String.class);
152 throw new HttpException(
153 "Authentication Failed: Username/password/AuthKey/AuthDate parameter(s) cannot be null or empty.");
157 public String postNoAuthWithResponse(final String path, final byte[] data, String contentType)
158 throws HttpException, JSONException {
160 String responseData = null;
161 WebTarget target = null;
162 Response response = null;
163 if (contentType == null) {
164 contentType = "text/pain";
166 target = DmaapClientUtil.getTarget(clientConfig,path);
168 response = DmaapClientUtil.postResponsewtNoAuth(target, data, contentType);
169 responseData = (String) response.readEntity(String.class);
173 public JSONObject postAuth(PostAuthDataObject postAuthDO) throws HttpException, JSONException {
174 if ((null != postAuthDO.getUsername() && null != postAuthDO.getPassword())) {
175 WebTarget target = null;
176 Response response = null;
177 target = DmaapClientUtil.getTarget(clientConfig,postAuthDO.getPath(), postAuthDO.getUsername(),
178 postAuthDO.getPassword());
179 response = DmaapClientUtil.postResponsewtCambriaAuth(target, postAuthDO.getAuthKey(),
180 postAuthDO.getAuthDate(), postAuthDO.getData(), postAuthDO.getContentType());
181 return getResponseDataInJson(response);
183 throw new HttpException(
184 "Authentication Failed: Username/password/AuthKey/AuthDate parameter(s) cannot be null or empty.");
188 public String postAuthwithResponse(final String path, final byte[] data, final String contentType,
189 final String authKey, final String authDate, final String username, final String password,
190 final String protocolFlag) throws HttpException, JSONException {
191 String responseData = null;
192 if ((null != username && null != password)) {
193 WebTarget target = null;
194 Response response = null;
195 target = DmaapClientUtil.getTarget(clientConfig,path, username, password);
196 response = DmaapClientUtil.postResponsewtCambriaAuth(target, authKey, authDate, data, contentType);
197 responseData = (String) response.readEntity(String.class);
201 throw new HttpException(
202 "Authentication Failed: Username/password/AuthKey/AuthDate parameter(s) cannot be null or empty.");
206 public JSONObject get(final String path, final String username, final String password, final String protocolFlag)
207 throws HttpException, JSONException {
208 if (null != username && null != password) {
210 WebTarget target = null;
211 Response response = null;
213 if (ProtocolTypeConstants.AUTH_KEY.getValue().equalsIgnoreCase(protocolFlag)) {
214 target = DmaapClientUtil.getTarget(clientConfig,path);
215 response = DmaapClientUtil.getResponsewtCambriaAuth(target, username, password);
217 target = DmaapClientUtil.getTarget(clientConfig,path, username, password);
218 String encoding = Base64.encodeAsString(username + ":" + password);
220 response = DmaapClientUtil.getResponsewtBasicAuth(target, encoding);
223 return getResponseDataInJson(response);
225 throw new HttpException(
226 "Authentication Failed: Username/password/AuthKey/Authdate parameter(s) cannot be null or empty.");
230 public String getResponse(final String path, final String username, final String password,
231 final String protocolFlag) throws HttpException, JSONException {
232 String responseData = null;
233 if (null != username && null != password) {
234 WebTarget target = null;
235 Response response = null;
236 if (ProtocolTypeConstants.AUTH_KEY.getValue().equalsIgnoreCase(protocolFlag)) {
237 target = DmaapClientUtil.getTarget(clientConfig,path);
238 response = DmaapClientUtil.getResponsewtCambriaAuth(target, username, password);
240 target = DmaapClientUtil.getTarget(clientConfig,path, username, password);
241 String encoding = Base64.encodeAsString(username + ":" + password);
242 response = DmaapClientUtil.getResponsewtBasicAuth(target, encoding);
244 MRClientFactory.HTTPHeadersMap = response.getHeaders();
246 String transactionid = response.getHeaderString("transactionid");
247 if (transactionid != null && !transactionid.equalsIgnoreCase("")) {
248 fLog.info("TransactionId : " + transactionid);
251 responseData = (String) response.readEntity(String.class);
254 throw new HttpException(
255 "Authentication Failed: Username/password/AuthKey/Authdate parameter(s) cannot be null or empty.");
259 public JSONObject getAuth(final String path, final String authKey, final String authDate, final String username,
260 final String password, final String protocolFlag) throws HttpException, JSONException {
261 if (null != username && null != password) {
262 WebTarget target = null;
263 Response response = null;
264 target = DmaapClientUtil.getTarget(clientConfig,path, username, password);
265 response = DmaapClientUtil.getResponsewtCambriaAuth(target, authKey, authDate);
267 return getResponseDataInJson(response);
269 throw new HttpException(
270 "Authentication Failed: Username/password/AuthKey/Authdate parameter(s) cannot be null or empty.");
274 public JSONObject getNoAuth(final String path) throws HttpException, JSONException {
276 WebTarget target = null;
277 Response response = null;
278 target = DmaapClientUtil.getTarget(clientConfig,path);
279 response = DmaapClientUtil.getResponsewtNoAuth(target);
281 return getResponseDataInJson(response);
284 public String getAuthResponse(final String path, final String authKey, final String authDate, final String username,
285 final String password, final String protocolFlag) throws HttpException, JSONException {
286 String responseData = null;
287 if (null != username && null != password) {
288 WebTarget target = null;
289 Response response = null;
290 target = DmaapClientUtil.getTarget(clientConfig,path, username, password);
291 response = DmaapClientUtil.getResponsewtCambriaAuth(target, authKey, authDate);
293 MRClientFactory.HTTPHeadersMap = response.getHeaders();
295 String transactionid = response.getHeaderString("transactionid");
296 if (transactionid != null && !transactionid.equalsIgnoreCase("")) {
297 fLog.info("TransactionId : " + transactionid);
300 responseData = (String) response.readEntity(String.class);
303 throw new HttpException(
304 "Authentication Failed: Username/password/AuthKey/Authdate parameter(s) cannot be null or empty.");
308 public String getNoAuthResponse(String path, final String username, final String password,
309 final String protocolFlag) throws HttpException, JSONException {
310 String responseData = null;
311 WebTarget target = null;
312 Response response = null;
313 target = DmaapClientUtil.getTarget(clientConfig,path, username, password);
314 response = DmaapClientUtil.getResponsewtNoAuth(target);
316 MRClientFactory.HTTPHeadersMap = response.getHeaders();
318 String transactionid = response.getHeaderString("transactionid");
319 if (transactionid != null && !transactionid.equalsIgnoreCase("")) {
320 fLog.info("TransactionId : " + transactionid);
323 responseData = (String) response.readEntity(String.class);
328 private JSONObject getResponseDataInJson(Response response) throws JSONException {
330 MRClientFactory.HTTPHeadersMap = response.getHeaders();
332 // MultivaluedMap<String, Object> headersMap =
333 // for(String key : headersMap.keySet()) {
334 String transactionid = response.getHeaderString("transactionid");
335 if (transactionid != null && !transactionid.equalsIgnoreCase("")) {
336 fLog.info("TransactionId : " + transactionid);
339 if (response.getStatus() == 403) {
340 JSONObject jsonObject = null;
341 jsonObject = new JSONObject();
342 JSONArray jsonArray = new JSONArray();
343 jsonArray.put(response.getEntity());
344 jsonObject.put("result", jsonArray);
345 jsonObject.put("status", response.getStatus());
348 String responseData = (String) response.readEntity(String.class);
350 JSONTokener jsonTokener = new JSONTokener(responseData);
351 JSONObject jsonObject = null;
352 final char firstChar = jsonTokener.next();
354 if ('[' == firstChar) {
355 JSONArray jsonArray = new JSONArray(jsonTokener);
356 jsonObject = new JSONObject();
357 jsonObject.put("result", jsonArray);
358 jsonObject.put("status", response.getStatus());
360 jsonObject = new JSONObject(jsonTokener);
361 jsonObject.put("status", response.getStatus());
365 } catch (JSONException excp) {
366 fLog.error("DMAAP - Error reading response data.", excp);
372 public String getHTTPErrorResponseMessage(String responseString) {
374 String response = null;
377 if (responseString.contains("<body>")) {
379 beginIndex = responseString.indexOf("body>") + 5;
380 endIndex = responseString.indexOf("</body");
381 response = responseString.substring(beginIndex, endIndex);
388 public String getHTTPErrorResponseCode(String responseString) {
390 String response = null;
393 if (responseString.contains("<title>")) {
394 beginIndex = responseString.indexOf("title>") + 6;
395 endIndex = responseString.indexOf("</title");
396 response = responseString.substring(beginIndex, endIndex);