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 com.att.nsa.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.internal.util.Base64;
35 import org.json.JSONArray;
36 import org.json.JSONException;
37 import org.json.JSONObject;
38 import org.json.JSONTokener;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 import com.att.nsa.apiClient.http.CacheUse;
43 import com.att.nsa.apiClient.http.HttpClient;
44 import com.att.nsa.mr.client.MRClient;
45 import com.att.nsa.mr.client.MRClientFactory;
46 import com.att.nsa.mr.test.clients.ProtocolTypeConstants;
48 public class MRBaseClient extends HttpClient implements MRClient {
51 protected MRBaseClient(Collection<String> hosts) throws MalformedURLException {
52 super(ConnectionType.HTTP, hosts, MRConstants.kStdMRServicePort);
54 fLog = LoggerFactory.getLogger(this.getClass().getName());
57 protected MRBaseClient(Collection<String> hosts, int stdSvcPort) throws MalformedURLException {
58 super(ConnectionType.HTTP, hosts, stdSvcPort);
60 fLog = LoggerFactory.getLogger(this.getClass().getName());
63 protected MRBaseClient(Collection<String> hosts, String clientSignature) throws MalformedURLException {
64 super(ConnectionType.HTTP, hosts, MRConstants.kStdMRServicePort, clientSignature, CacheUse.NONE, 1, 1L,
65 TimeUnit.MILLISECONDS, 32, 32, 600000);
67 fLog = LoggerFactory.getLogger(this.getClass().getName());
74 protected Set<String> jsonArrayToSet(JSONArray a) {
78 final TreeSet<String> set = new TreeSet<String>();
79 for (int i = 0; i < a.length(); i++) {
80 set.add(a.getString(i));
85 public void logTo(Logger log) {
90 protected Logger getLog() {
96 public JSONObject post(final String path, final byte[] data, final String contentType, final String username,
97 final String password, final String protocolFlag) throws HttpException, JSONException {
98 if ((null != username && null != password)) {
99 WebTarget target=null;
100 Response response=null;
101 target = DmaapClientUtil.getTarget(path, username, password);
102 String encoding = Base64.encodeAsString(username + ":" + password);
104 response = DmaapClientUtil.getResponsewtBasicAuth(target, encoding);
106 return getResponseDataInJson(response);
108 throw new HttpException(
109 "Authentication Failed: Username/password/AuthKey/AuthDate parameter(s) cannot be null or empty.");
113 public String postWithResponse(final String path, final byte[] data, final String contentType,
114 final String username, final String password, final String protocolFlag)
115 throws HttpException, JSONException {
116 String responseData = null;
117 if ((null != username && null != password)) {
118 WebTarget target=null;
119 Response response=null;
120 target = DmaapClientUtil.getTarget(path, username, password);
121 String encoding = Base64.encodeAsString(username + ":" + password);
123 response = DmaapClientUtil.getResponsewtBasicAuth(target, encoding);
125 responseData = (String)response.getEntity();
128 throw new HttpException(
129 "Authentication Failed: Username/password/AuthKey/AuthDate parameter(s) cannot be null or empty.");
133 public JSONObject postAuth(final String path, final byte[] data, final String contentType, final String authKey,
134 final String authDate, final String username, final String password, final String protocolFlag)
135 throws HttpException, JSONException {
136 if ((null != username && null != password)) {
137 WebTarget target=null;
138 Response response=null;
139 target = DmaapClientUtil.getTarget(path, username, password);
140 response =DmaapClientUtil.postResponsewtCambriaAuth(target, authKey, authDate, data, contentType);
141 return getResponseDataInJson(response);
143 throw new HttpException(
144 "Authentication Failed: Username/password/AuthKey/AuthDate parameter(s) cannot be null or empty.");
148 public String postAuthwithResponse(final String path, final byte[] data, final String contentType,
149 final String authKey, final String authDate, final String username, final String password,
150 final String protocolFlag) throws HttpException, JSONException {
151 String responseData = null;
152 if ((null != username && null != password)) {
153 WebTarget target=null;
154 Response response=null;
155 target = DmaapClientUtil.getTarget(path, username, password);
156 response = DmaapClientUtil.postResponsewtCambriaAuth(target, authKey, authDate, data, contentType);
157 responseData = (String)response.getEntity();
161 throw new HttpException(
162 "Authentication Failed: Username/password/AuthKey/AuthDate parameter(s) cannot be null or empty.");
166 public JSONObject get(final String path, final String username, final String password, final String protocolFlag)
167 throws HttpException, JSONException {
168 if (null != username && null != password) {
170 WebTarget target=null;
171 Response response=null;
173 if (ProtocolTypeConstants.AUTH_KEY.getValue().equalsIgnoreCase(protocolFlag)) {
174 target = DmaapClientUtil.getTarget(path);
175 response = DmaapClientUtil.getResponsewtCambriaAuth(target, username, password);
177 target = DmaapClientUtil.getTarget(path, username, password);
178 String encoding = Base64.encodeAsString(username + ":" + password);
180 response = DmaapClientUtil.getResponsewtBasicAuth(target, encoding);
183 return getResponseDataInJson(response);
185 throw new HttpException(
186 "Authentication Failed: Username/password/AuthKey/Authdate parameter(s) cannot be null or empty.");
190 public String getResponse(final String path, final String username, final String password,
191 final String protocolFlag) throws HttpException, JSONException {
192 String responseData = null;
193 if (null != username && null != password) {
194 WebTarget target=null;
195 Response response=null;
196 if (ProtocolTypeConstants.AUTH_KEY.getValue().equalsIgnoreCase(protocolFlag)) {
197 target = DmaapClientUtil.getTarget(path);
198 response = DmaapClientUtil.getResponsewtCambriaAuth(target, username, password);
200 target = DmaapClientUtil.getTarget(path, username, password);
201 String encoding = Base64.encodeAsString(username + ":" + password);
202 response = DmaapClientUtil.getResponsewtBasicAuth(target, encoding);
204 MRClientFactory.HTTPHeadersMap = response.getHeaders();
206 String transactionid = response.getHeaderString("transactionid");
207 if (transactionid != null && !transactionid.equalsIgnoreCase("")) {
208 fLog.info("TransactionId : " + transactionid);
211 responseData = (String)response.getEntity();
214 throw new HttpException(
215 "Authentication Failed: Username/password/AuthKey/Authdate parameter(s) cannot be null or empty.");
219 public JSONObject getAuth(final String path, final String authKey, final String authDate, final String username,
220 final String password, final String protocolFlag) throws HttpException, JSONException {
221 if (null != username && null != password) {
222 WebTarget target=null;
223 Response response=null;
224 target = DmaapClientUtil.getTarget(path, username, password);
225 response = DmaapClientUtil.getResponsewtCambriaAuth(target, authKey, authDate);
227 return getResponseDataInJson(response);
229 throw new HttpException(
230 "Authentication Failed: Username/password/AuthKey/Authdate parameter(s) cannot be null or empty.");
234 public JSONObject getNoAuth(final String path, final String username, final String password,
235 final String protocolFlag) throws HttpException, JSONException {
236 if (null != username && null != password) {
237 WebTarget target=null;
238 Response response=null;
239 target = DmaapClientUtil.getTarget(path, username, password);
240 response = DmaapClientUtil.getResponsewtNoAuth(target);
242 return getResponseDataInJson(response);
244 throw new HttpException(
245 "Authentication Failed: Username/password/AuthKey/Authdate parameter(s) cannot be null or empty.");
249 public String getAuthResponse(final String path, final String authKey, final String authDate, final String username,
250 final String password, final String protocolFlag) throws HttpException, JSONException {
251 String responseData = null;
252 if (null != username && null != password) {
253 WebTarget target=null;
254 Response response=null;
255 target = DmaapClientUtil.getTarget(path, username, password);
256 response = DmaapClientUtil.getResponsewtCambriaAuth(target, authKey, authDate);
258 MRClientFactory.HTTPHeadersMap = response.getHeaders();
260 String transactionid = response.getHeaderString("transactionid");
261 if (transactionid != null && !transactionid.equalsIgnoreCase("")) {
262 fLog.info("TransactionId : " + transactionid);
265 responseData = (String)response.getEntity();
268 throw new HttpException(
269 "Authentication Failed: Username/password/AuthKey/Authdate parameter(s) cannot be null or empty.");
273 public String getNoAuthResponse(String path, final String username, final String password,
274 final String protocolFlag) throws HttpException, JSONException {
275 String responseData = null;
276 WebTarget target=null;
277 Response response=null;
278 target = DmaapClientUtil.getTarget(path, username, password);
279 response = DmaapClientUtil.getResponsewtNoAuth(target);
281 MRClientFactory.HTTPHeadersMap = response.getHeaders();
283 String transactionid = response.getHeaderString("transactionid");
284 if (transactionid != null && !transactionid.equalsIgnoreCase("")) {
285 fLog.info("TransactionId : " + transactionid);
288 responseData = (String)response.getEntity();
294 private JSONObject getResponseDataInJson(Response response) throws JSONException {
296 MRClientFactory.HTTPHeadersMap = response.getHeaders();
297 // fLog.info("DMAAP response status: " + response.getStatus());
299 // MultivaluedMap<String, Object> headersMap =
300 // response.getHeaders();
301 // for(String key : headersMap.keySet()) {
302 String transactionid = response.getHeaderString("transactionid");
303 if (transactionid != null && !transactionid.equalsIgnoreCase("")) {
304 fLog.info("TransactionId : " + transactionid);
308 * final String responseData = response.readEntity(String.class);
309 * JSONTokener jsonTokener = new JSONTokener(responseData);
310 * JSONObject jsonObject = null; final char firstChar =
311 * jsonTokener.next(); jsonTokener.back(); if ('[' == firstChar) {
312 * JSONArray jsonArray = new JSONArray(jsonTokener); jsonObject =
313 * new JSONObject(); jsonObject.put("result", jsonArray); } else {
314 * jsonObject = new JSONObject(jsonTokener); }
319 if (response.getStatus() == 403) {
320 JSONObject jsonObject = null;
321 jsonObject = new JSONObject();
322 JSONArray jsonArray = new JSONArray();
323 jsonArray.put(response.getEntity());
324 jsonObject.put("result", jsonArray);
325 jsonObject.put("status", response.getStatus());
328 String responseData = (String)response.getEntity();
330 JSONTokener jsonTokener = new JSONTokener(responseData);
331 JSONObject jsonObject = null;
332 final char firstChar = jsonTokener.next();
334 if ('[' == firstChar) {
335 JSONArray jsonArray = new JSONArray(jsonTokener);
336 jsonObject = new JSONObject();
337 jsonObject.put("result", jsonArray);
338 jsonObject.put("status", response.getStatus());
340 jsonObject = new JSONObject(jsonTokener);
341 jsonObject.put("status", response.getStatus());
345 } catch (JSONException excp) {
346 fLog.error("DMAAP - Error reading response data.", excp);
352 public String getHTTPErrorResponseMessage(String responseString) {
354 String response = null;
357 if (responseString.contains("<body>")) {
359 beginIndex = responseString.indexOf("body>") + 5;
360 endIndex = responseString.indexOf("</body");
361 response = responseString.substring(beginIndex, endIndex);
368 public String getHTTPErrorResponseCode(String responseString) {
370 String response = null;
373 if (responseString.contains("<title>")) {
374 beginIndex = responseString.indexOf("title>") + 6;
375 endIndex = responseString.indexOf("</title");
376 response = responseString.substring(beginIndex, endIndex);