summaryrefslogtreecommitdiffstats
path: root/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/rest/HttpRestClient.java
blob: fb09131b421da80a1197a622f32fbc5fc350b4ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.be.dao.rest;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map.Entry;
import java.util.Properties;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;
import org.openecomp.sdc.be.config.BeEcompErrorManager;
import org.openecomp.sdc.be.config.BeEcompErrorManager.ErrorSeverity;
import org.openecomp.sdc.common.rest.api.RestResponse;
import org.openecomp.sdc.common.rest.api.RestResponseAsByteArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HttpRestClient {

	public final static int DEFAULT_CONNECTION_POOL_SIZE = 10;

	public final static int DEFAULT_READ_TIMEOUT_IN_SEC = 30;

	public final static int DEFAULT_CONNECT_TIMEOUT = 30;

	public final static int DEFAULT_SOCKET_TIMEOUT = 30;

	private CloseableHttpClient httpClient = null;

	private PoolingHttpClientConnectionManager cm = null;

	private static Logger logger = LoggerFactory.getLogger(HttpRestClient.class.getName());

	private RestResponse errorRestResponse = new RestResponse("internal server error", null, 500);
	private RestResponseAsByteArray errorRestResponseAsByteArray = new RestResponseAsByteArray(
			"internal server error".getBytes(), null, 500);

	boolean isInitialized = false;

	// public static void main(String[] argv) {
	// try {
	// RestClientService restClientService =
	// RestClientServiceFactory.createHttpRestClientService(new
	// RestConfigurationInfo());
	//
	// String uriCreateCell =
	// "http://172.20.37.245:9082/topology/management/cell/update";
	// String jsonStr = " { \"cellName\" : \"mycell118\" }";
	// String jsonUpdateStr =
	// " { \"cellName\" : \"mycell118\", \"hostName\" : \"myhost333\" }";
	//
	//
	// // jsonStr = " <note>dfd</note>";
	//
	// Properties headers = new Properties();
	// headers.put("Content-type", "application/json");
	// headers.put("Accept", "*/*");
	//
	// // RestResponse restResponse = restClientService.doPOST(uriCreateCell,
	// headers, jsonStr);
	//
	// RestResponse restResponse = restClientService.doPUT(uriCreateCell,
	// headers, jsonUpdateStr);
	//
	// System.out.println(restResponse);
	//
	//
	// } catch (RestClientServiceExeption e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	//
	// }

	public HttpRestClient() {
		super();
		isInitialized = init(new RestConfigurationInfo());
	}

	public HttpRestClient(RestConfigurationInfo restConfigurationInfo) {
		super();
		isInitialized = init(restConfigurationInfo);
	}

	public boolean init(RestConfigurationInfo restConfigurationInfo) {

		logger.debug("create HttpRestClient: restConfigurationInfo={}", restConfigurationInfo);
		boolean result = false;
		// try {
		createHttpClient(restConfigurationInfo);
		result = true;
		// } catch (KeyManagementException e) {
		// String msg = "Failed creating client config for rest. "
		// + e.getMessage();
		// logger.debug(msg, e);
		// //TODO: log. error
		// } catch (NoSuchAlgorithmException e) {
		// String msg = "Failed creating client config for rest. "
		// + e.getMessage();
		// logger.debug(msg, e);
		// //TODO: log. error
		// }

		logger.debug("Finish creating HttpRestClient. Result is {}", result);
		return result;
	}

	public void destroy() {

		try {
			httpClient.close();
			logger.debug("Http client closed");
		} catch (Exception e) {
			logger.trace("Failed to close http client", e);
		}

	}

	private void createHttpClient(RestConfigurationInfo restConfigurationInfo) {
		// throws KeyManagementException, NoSuchAlgorithmException {

		PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();

		int connPoolSize = getConnectionPoolSize(restConfigurationInfo);
		connManager.setMaxTotal(connPoolSize);
		connManager.setDefaultMaxPerRoute(10);
		connManager.setValidateAfterInactivity(15000);

		// Create common default configuration
		int socketTimeout = getTimeout(restConfigurationInfo.getSocketTimeoutInSec(), DEFAULT_SOCKET_TIMEOUT);

		int connectTimeoutInSec = getTimeout(restConfigurationInfo.getConnectTimeoutInSec(), DEFAULT_CONNECT_TIMEOUT);

		int readTimeOut = getTimeout(restConfigurationInfo.getReadTimeoutInSec(), DEFAULT_READ_TIMEOUT_IN_SEC);

		RequestConfig clientConfig = RequestConfig.custom().setConnectTimeout(connectTimeoutInSec)
				.setSocketTimeout(socketTimeout).setConnectionRequestTimeout(readTimeOut).build();

		this.cm = connManager;

		this.httpClient = HttpClients.custom().setDefaultRequestConfig(clientConfig).setConnectionManager(connManager)
				.build();

	}

	private int getConnectionPoolSize(RestConfigurationInfo restConfigurationInfo) {
		Integer connPoolSizeObj = restConfigurationInfo.getConnectionPoolSize();
		int connPoolSize = DEFAULT_CONNECTION_POOL_SIZE;
		if (connPoolSizeObj != null) {
			connPoolSize = connPoolSizeObj.intValue();
			if (connPoolSize <= 0) {
				connPoolSize = DEFAULT_CONNECTION_POOL_SIZE;
			}
		}
		return connPoolSize;
	}

	private int getTimeout(Integer value, Integer defaultValue) {

		int defaultTimeout = defaultValue != null ? defaultValue.intValue() * 1000 : 0;

		int timeout = defaultTimeout;

		if (value != null) {
			timeout = defaultValue.intValue() * 1000;
			if (timeout <= 0) {
				timeout = defaultTimeout;
			}
		}

		return timeout;
	}

	/**
	 * Executes RS-GET to perform FIND.
	 * 
	 * @param headerParameterKey
	 *            String
	 * @param headerParameterValue
	 *            String
	 * @return String
	 */
	public RestResponse doGET(String uri, Properties headers) {

		logger.debug("Before executing uri {}. headers = {}", uri, headers);

		HttpGet httpGet = new HttpGet(uri);

		RestResponse response = execute(httpGet, headers);

		return response;
	}

	public RestResponse doPUT(String uri, Properties headers, String body) {

		logger.debug("Before executing uri {}. headers = {}. body = ", uri, headers, body);

		HttpPut httpPut = new HttpPut(uri);
		StringEntity data = new StringEntity(body, ContentType.APPLICATION_JSON);
		httpPut.setEntity(data);
		RestResponse response = execute(httpPut, headers);

		return response;
	}

	public RestResponse doPOST(String uri, Properties headers, String body) {

		logger.debug("Before executing uri {}. headers = {}. body = {}", uri, headers, body);

		HttpPost httpPost = new HttpPost(uri);
		StringEntity data = new StringEntity(body, ContentType.APPLICATION_JSON);
		httpPost.setEntity(data);
		RestResponse response = execute(httpPost, headers);

		return response;
	}

	public RestResponseAsByteArray doGetAsByteArray(String uri, Properties headers) {

		logger.debug("Before executing uri {}. headers = {}.", uri, headers);

		HttpGet httpGet = new HttpGet(uri);

		RestResponseAsByteArray response = executeAndReturnByteArray(httpGet, headers);

		return response;
	}

	private void addHeadersToRequest(HttpRequestBase httpRequestBase, Properties headers) {

		if (headers != null) {
			for (Entry<Object, Object> entry : headers.entrySet()) {
				httpRequestBase.addHeader(entry.getKey().toString(), entry.getValue().toString());
			}
		}

	}

	private RestResponse execute(HttpRequestBase httpRequestBase, Properties headers) {

		RestResponse restResponse = null;

		CloseableHttpResponse httpResponse = null;

		try {

			addHeadersToRequest(httpRequestBase, headers);

			httpResponse = this.httpClient.execute(httpRequestBase);

			restResponse = buildRestResponseFromResult(httpResponse);

			logger.debug("After executing uri {}. response = {}.", httpRequestBase.getURI().toString(), restResponse);
		} catch (Exception exception) {
			httpRequestBase.abort();

			String description = "Failed executing http request " + httpRequestBase.getURI().toString() + "("
					+ httpRequestBase.getMethod() + ")";
			BeEcompErrorManager.getInstance().logInternalFlowError("ExecuteRestRequest", description,
					ErrorSeverity.ERROR);
			restResponse = errorRestResponse;
		} finally {
			// ensure the connection gets released to the manager
			releaseResource(httpResponse);
		}

		return restResponse;
	}

	private RestResponse buildRestResponseFromResult(CloseableHttpResponse httpResponse) throws IOException {

		int statusCode = httpResponse.getStatusLine().getStatusCode();
		String statusDesc = httpResponse.getStatusLine().getReasonPhrase();

		HttpEntity entity = httpResponse.getEntity();
		String response = null;
		if (entity != null) {
			response = EntityUtils.toString(entity);
		}

		RestResponse restResponse = new RestResponse(response, statusDesc, statusCode);

		return restResponse;
	}

	private RestResponseAsByteArray buildRestResponseByteArrayFromResult(CloseableHttpResponse httpResponse)
			throws IOException {

		int statusCode = httpResponse.getStatusLine().getStatusCode();
		String statusDesc = httpResponse.getStatusLine().getReasonPhrase();

		HttpEntity entity = httpResponse.getEntity();

		byte[] response = null;
		if (entity != null) {
			InputStream content = entity.getContent();
			if (content != null) {
				response = IOUtils.toByteArray(content);
			}
		}

		RestResponseAsByteArray restResponse = new RestResponseAsByteArray(response, statusDesc, statusCode);

		return restResponse;
	}

	private RestResponseAsByteArray executeAndReturnByteArray(HttpRequestBase httpRequestBase, Properties headers) {

		RestResponseAsByteArray restResponse = null;

		CloseableHttpResponse httpResponse = null;

		try {

			addHeadersToRequest(httpRequestBase, headers);

			httpResponse = this.httpClient.execute(httpRequestBase);

			restResponse = buildRestResponseByteArrayFromResult(httpResponse);

			if (restResponse != null) {
				logger.debug("After executing uri {}. Response: {}.", httpRequestBase.getURI().toString(), restResponse.toPrettyString());
			}

		} catch (Exception exception) {
			httpRequestBase.abort();
			String description = "Failed executing http request " + httpRequestBase.getURI().toString() + "("
					+ httpRequestBase.getMethod() + ")";
			BeEcompErrorManager.getInstance().logInternalFlowError("ExecuteRestRequest", description,
					ErrorSeverity.ERROR);
			logger.debug(description, exception);
			restResponse = errorRestResponseAsByteArray;
		} finally {
			// ensure the connection gets released to the manager
			releaseResource(httpResponse);
		}

		return restResponse;
	}

	/**
	 * This method print the JSON response from the REST Server
	 * 
	 * @param response
	 *            the JSON response from the REST server
	 * @param method
	 *            name of method
	 */
	private void logResponse(String response, String method) {
		logger.trace(method + " response = " + response);
	}

	private void releaseResource(CloseableHttpResponse response) {
		if (response != null) {
			try {
				HttpEntity entity = response.getEntity();
				if (entity != null) {
					EntityUtils.consume(entity);
				}
				response.close();
			} catch (Exception e) {
				logger.error("failed to close connection exception", e);
			}
		}
	}

	public boolean isInitialized() {
		return isInitialized;
	}

}