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 final static String HEADER_TRANSACTION_ID = "transactionid";
53 private final static String JSON_RESULT = "result";
54 private final static String JSON_STATUS = "status";
56 private final static String AUTH_FAILED = "Authentication Failed: Username/password/AuthKey/AuthDate parameter(s) cannot be null or empty.";
57 private final static String LOG_TRANSACTION_ID = "TransactionId : ";
59 private ClientConfig clientConfig = null;
61 protected MRBaseClient(Collection<String> hosts) throws MalformedURLException {
62 super(ConnectionType.HTTP, hosts, MRConstants.STD_MR_SERVICE_PORT);
64 fLog = LoggerFactory.getLogger(this.getClass().getName());
67 protected MRBaseClient(Collection<String> hosts, int stdSvcPort) throws MalformedURLException {
68 super(ConnectionType.HTTP, hosts, stdSvcPort);
70 fLog = LoggerFactory.getLogger(this.getClass().getName());
73 protected MRBaseClient(Collection<String> hosts, String clientSignature) throws MalformedURLException {
74 super(ConnectionType.HTTP, hosts, MRConstants.STD_MR_SERVICE_PORT, clientSignature, CacheUse.NONE, 1, 1L,
75 TimeUnit.MILLISECONDS, 32, 32, 600000);
77 fLog = LoggerFactory.getLogger(this.getClass().getName());
80 public ClientConfig getClientConfig1() {
84 public void setClientConfig(ClientConfig config) {
85 this.clientConfig = config;
93 protected Set<String> jsonArrayToSet(JSONArray a) {
97 final TreeSet<String> set = new TreeSet<>();
98 for (int i = 0; i < a.length(); i++) {
99 set.add(a.getString(i));
104 public void logTo(Logger log) {
109 protected Logger getLog() {
115 public JSONObject post(final String path, final byte[] data, final String contentType, final String username,
116 final String password, final String protocalFlag) throws HttpException, JSONException {
117 if ((null != username && null != password)) {
118 WebTarget target = null;
119 Response response = null;
120 target = DmaapClientUtil.getTarget(clientConfig,path, username, password);
121 String encoding = Base64.encodeAsString(username + ":" + password);
123 response = DmaapClientUtil.postResponsewtBasicAuth(target, encoding, data, contentType);
125 return getResponseDataInJson(response);
127 throw new HttpException(AUTH_FAILED);
131 public JSONObject postNoAuth(final String path, final byte[] data, String contentType)
132 throws HttpException, JSONException {
133 WebTarget target = null;
134 Response response = null;
135 if (contentType == null) {
136 contentType = "text/pain";
138 target = DmaapClientUtil.getTarget(clientConfig,path);
140 response = DmaapClientUtil.postResponsewtNoAuth(target, data, contentType);
142 return getResponseDataInJson(response);
145 public String postWithResponse(final String path, final byte[] data, final String contentType,
146 final String username, final String password, final String protocolFlag)
147 throws HttpException, JSONException {
148 String responseData = null;
149 if ((null != username && null != password)) {
150 WebTarget target = null;
151 Response response = null;
152 target = DmaapClientUtil.getTarget(clientConfig,path, username, password);
153 String encoding = Base64.encodeAsString(username + ":" + password);
155 response = DmaapClientUtil.postResponsewtBasicAuth(target, encoding, data, contentType);
157 responseData = (String) response.readEntity(String.class);
160 throw new HttpException(AUTH_FAILED);
164 public String postNoAuthWithResponse(final String path, final byte[] data, String contentType)
165 throws HttpException, JSONException {
167 String responseData = null;
168 WebTarget target = null;
169 Response response = null;
170 if (contentType == null) {
171 contentType = "text/pain";
173 target = DmaapClientUtil.getTarget(clientConfig,path);
175 response = DmaapClientUtil.postResponsewtNoAuth(target, data, contentType);
176 responseData = (String) response.readEntity(String.class);
180 public JSONObject postAuth(PostAuthDataObject postAuthDO) throws HttpException, JSONException {
181 if ((null != postAuthDO.getUsername() && null != postAuthDO.getPassword())) {
182 WebTarget target = null;
183 Response response = null;
184 target = DmaapClientUtil.getTarget(clientConfig,postAuthDO.getPath(), postAuthDO.getUsername(),
185 postAuthDO.getPassword());
186 response = DmaapClientUtil.postResponsewtCambriaAuth(target, postAuthDO.getAuthKey(),
187 postAuthDO.getAuthDate(), postAuthDO.getData(), postAuthDO.getContentType());
188 return getResponseDataInJson(response);
190 throw new HttpException(AUTH_FAILED);
194 public String postAuthwithResponse(final String path, final byte[] data, final String contentType,
195 final String authKey, final String authDate, final String username, final String password,
196 final String protocolFlag) throws HttpException, JSONException {
197 String responseData = null;
198 if ((null != username && null != password)) {
199 WebTarget target = null;
200 Response response = null;
201 target = DmaapClientUtil.getTarget(clientConfig,path, username, password);
202 response = DmaapClientUtil.postResponsewtCambriaAuth(target, authKey, authDate, data, contentType);
203 responseData = (String) response.readEntity(String.class);
207 throw new HttpException(AUTH_FAILED);
211 public JSONObject get(final String path, final String username, final String password, final String protocolFlag)
212 throws HttpException, JSONException {
213 if (null != username && null != password) {
215 WebTarget target = null;
216 Response response = null;
218 if (ProtocolTypeConstants.AUTH_KEY.getValue().equalsIgnoreCase(protocolFlag)) {
219 target = DmaapClientUtil.getTarget(clientConfig,path);
220 response = DmaapClientUtil.getResponsewtCambriaAuth(target, username, password);
222 target = DmaapClientUtil.getTarget(clientConfig,path, username, password);
223 String encoding = Base64.encodeAsString(username + ":" + password);
225 response = DmaapClientUtil.getResponsewtBasicAuth(target, encoding);
228 return getResponseDataInJson(response);
230 throw new HttpException(AUTH_FAILED);
234 public String getResponse(final String path, final String username, final String password,
235 final String protocolFlag) throws HttpException, JSONException {
236 String responseData = null;
237 if (null != username && null != password) {
238 WebTarget target = null;
239 Response response = null;
240 if (ProtocolTypeConstants.AUTH_KEY.getValue().equalsIgnoreCase(protocolFlag)) {
241 target = DmaapClientUtil.getTarget(clientConfig,path);
242 response = DmaapClientUtil.getResponsewtCambriaAuth(target, username, password);
244 target = DmaapClientUtil.getTarget(clientConfig,path, username, password);
245 String encoding = Base64.encodeAsString(username + ":" + password);
246 response = DmaapClientUtil.getResponsewtBasicAuth(target, encoding);
248 MRClientFactory.setHTTPHeadersMap(response.getHeaders());
250 String transactionid = response.getHeaderString(HEADER_TRANSACTION_ID);
251 if (transactionid != null && !transactionid.equalsIgnoreCase("")) {
252 fLog.info(LOG_TRANSACTION_ID + transactionid);
255 responseData = (String) response.readEntity(String.class);
258 throw new HttpException(AUTH_FAILED);
262 public JSONObject getAuth(final String path, final String authKey, final String authDate, final String username,
263 final String password, final String protocolFlag) throws HttpException, JSONException {
264 if (null != username && null != password) {
265 WebTarget target = null;
266 Response response = null;
267 target = DmaapClientUtil.getTarget(clientConfig,path, username, password);
268 response = DmaapClientUtil.getResponsewtCambriaAuth(target, authKey, authDate);
270 return getResponseDataInJson(response);
272 throw new HttpException(AUTH_FAILED);
276 public JSONObject getNoAuth(final String path) throws HttpException, JSONException {
278 WebTarget target = null;
279 Response response = null;
280 target = DmaapClientUtil.getTarget(clientConfig,path);
281 response = DmaapClientUtil.getResponsewtNoAuth(target);
283 return getResponseDataInJson(response);
286 public String getAuthResponse(final String path, final String authKey, final String authDate, final String username,
287 final String password, final String protocolFlag) throws HttpException, JSONException {
288 String responseData = null;
289 if (null != username && null != password) {
290 WebTarget target = null;
291 Response response = null;
292 target = DmaapClientUtil.getTarget(clientConfig,path, username, password);
293 response = DmaapClientUtil.getResponsewtCambriaAuth(target, authKey, authDate);
295 MRClientFactory.setHTTPHeadersMap(response.getHeaders());
297 String transactionid = response.getHeaderString(HEADER_TRANSACTION_ID);
298 if (transactionid != null && !transactionid.equalsIgnoreCase("")) {
299 fLog.info(LOG_TRANSACTION_ID + transactionid);
302 responseData = (String) response.readEntity(String.class);
305 throw new HttpException(AUTH_FAILED);
309 public String getNoAuthResponse(String path, final String username, final String password,
310 final String protocolFlag) throws HttpException, JSONException {
311 String responseData = null;
312 WebTarget target = null;
313 Response response = null;
314 target = DmaapClientUtil.getTarget(clientConfig,path, username, password);
315 response = DmaapClientUtil.getResponsewtNoAuth(target);
317 MRClientFactory.setHTTPHeadersMap(response.getHeaders());
319 String transactionid = response.getHeaderString(HEADER_TRANSACTION_ID);
320 if (transactionid != null && !transactionid.equalsIgnoreCase("")) {
321 fLog.info(LOG_TRANSACTION_ID + transactionid);
324 responseData = (String) response.readEntity(String.class);
329 private JSONObject getResponseDataInJson(Response response) throws JSONException {
331 MRClientFactory.setHTTPHeadersMap(response.getHeaders());
333 // MultivaluedMap<String, Object> headersMap =
334 // for(String key : headersMap.keySet()) {
335 String transactionid = response.getHeaderString(HEADER_TRANSACTION_ID);
336 if (transactionid != null && !transactionid.equalsIgnoreCase("")) {
337 fLog.info(LOG_TRANSACTION_ID + transactionid);
340 if (response.getStatus() == 403) {
341 JSONObject jsonObject = null;
342 jsonObject = new JSONObject();
343 JSONArray jsonArray = new JSONArray();
344 jsonArray.put(response.getEntity());
345 jsonObject.put(JSON_RESULT, jsonArray);
346 jsonObject.put(JSON_STATUS, response.getStatus());
349 String responseData = (String) response.readEntity(String.class);
351 JSONTokener jsonTokener = new JSONTokener(responseData);
352 JSONObject jsonObject = null;
353 final char firstChar = jsonTokener.next();
355 if ('[' == firstChar) {
356 JSONArray jsonArray = new JSONArray(jsonTokener);
357 jsonObject = new JSONObject();
358 jsonObject.put(JSON_RESULT, jsonArray);
359 jsonObject.put(JSON_STATUS, response.getStatus());
361 jsonObject = new JSONObject(jsonTokener);
362 jsonObject.put(JSON_STATUS, response.getStatus());
366 } catch (JSONException excp) {
367 fLog.error("DMAAP - Error reading response data.", excp);
373 public String getHTTPErrorResponseMessage(String responseString) {
375 String response = null;
378 if (responseString.contains("<body>")) {
380 beginIndex = responseString.indexOf("body>") + 5;
381 endIndex = responseString.indexOf("</body");
382 response = responseString.substring(beginIndex, endIndex);
389 public String getHTTPErrorResponseCode(String responseString) {
391 String response = null;
394 if (responseString.contains("<title>")) {
395 beginIndex = responseString.indexOf("title>") + 6;
396 endIndex = responseString.indexOf("</title");
397 response = responseString.substring(beginIndex, endIndex);