summaryrefslogtreecommitdiffstats
path: root/ecomp-portal-BE-common/src/test/java/org/onap/portalapp/portal/controller/SharedContextRestClient.java
blob: 427b4124c06815da09f2bfcb810a99f4a9b8512a (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
/*-
 * ============LICENSE_START==========================================
 * ONAP Portal
 * ===================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ===================================================================
 *
 * Unless otherwise specified, all software contained herein is licensed
 * under the Apache License, Version 2.0 (the "License");
 * you may not use this software 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.
 *
 * Unless otherwise specified, all documentation contained herein is licensed
 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
 * you may not use this documentation except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *             https://creativecommons.org/licenses/by/4.0/
 *
 * Unless required by applicable law or agreed to in writing, documentation
 * 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.onap.portalapp.portal.controller;

import java.net.URI;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.SSLContext;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
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.utils.URIBuilder;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
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.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.ssl.TrustStrategy;
import org.apache.http.util.EntityUtils;

/**
 * Provides reusable features for test cases to get or post from an REST
 * endpoint, allowing use of HTTPS connections to servers that use self-signed
 * certificates.
 */
public class SharedContextRestClient {

	private static final Log logger = LogFactory.getLog(SharedContextRestClient.class);

	/**
	 * Convenience method that builds and sends a GET request using properties
	 * to build the URI and populate header with credentials.
	 * 
	 * @param task
	 *            last component(s) of REST endpoint name; e.g., "get".
	 * @param contextId
	 * @param contextKey
	 * @return JSON string fetched
	 * @throws Exception
	 *             if the HTTP response code is anything other than OK.
	 */
	public static String getJson(final SharedContextTestProperties properties, final String task,
			final String contextId, final String contextKey) throws Exception {
		String requestPath = '/' + properties.getProperty(SharedContextTestProperties.APPNAME) //
				+ '/' + properties.getProperty(SharedContextTestProperties.RESTPATH) //
				+ '/' + task;
		return getJson(properties.getProperty(SharedContextTestProperties.HOSTNAME), //
				properties.getProperty(SharedContextTestProperties.PORT, -1), //
				properties.getProperty(SharedContextTestProperties.SECURE, true), //
				properties.getProperty(SharedContextTestProperties.UEBKEY), //
				properties.getProperty(SharedContextTestProperties.USERNAME), //
				properties.getProperty(SharedContextTestProperties.COUNTERSIGN), requestPath, //
				contextId, //
				contextKey);
	}

	/**
	 * Constructs and sends a GET request using the specified values.
	 * 
	 * @param hostname
	 * @param port
	 *            ignored if negative
	 * @param secure
	 *            If true, uses https; else http.
	 * @param headerUebkey
	 * @param headerUsername
	 * @param headerPassword
	 * @param requestPath
	 *            full path of the REST endpoint
	 * @param contextId
	 * @param contextKey
	 * Ignored if null
	 * @return JSON result
	 */
	public static String getJson(final String hostname, final int port, boolean secure, final String headerUebkey,
			final String headerUsername, final String headerPassword, final String requestPath, final String contextId,
			final String contextKey) throws Exception {

		URIBuilder uriBuilder = new URIBuilder();
		if (secure)
			uriBuilder.setScheme("https");
		else
			uriBuilder.setScheme("http");
		uriBuilder.setHost(hostname);
		if (port > 0)
			uriBuilder.setPort(port);
		uriBuilder.setPath(requestPath);
		uriBuilder.addParameter("context_id", contextId);
		if (contextKey != null)
			uriBuilder.addParameter("ckey", contextKey);
		final URI uri = uriBuilder.build();

		CloseableHttpClient httpClient;
		if (secure) {
			// Tell HttpClient to accept any server certificate for HTTPS.
			// http://stackoverflow.com/questions/24720013/apache-http-client-ssl-certificate-error
			SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() {
				@Override
				public boolean isTrusted(final X509Certificate[] chain, final String authType)
						throws CertificateException {
					return true;
				}
			}).build();
			SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext,
					NoopHostnameVerifier.INSTANCE);
			httpClient = HttpClientBuilder.create().setSSLSocketFactory(sslsf).build();
		} else {
			httpClient = HttpClients.createDefault();
		}

		HttpGet httpGet = new HttpGet(uri);
		httpGet.setHeader("uebkey", headerUebkey);
		httpGet.setHeader("username", headerUsername);
		httpGet.setHeader("password", headerPassword);

		String json = null;
		CloseableHttpResponse response = null;
		try {
			logger.debug("GET from " + uri);
			response = httpClient.execute(httpGet);
			logger.info("Status is " + response.getStatusLine());
			if (response.getStatusLine().getStatusCode() != HttpServletResponse.SC_OK)
				throw new Exception("Status is " + response.getStatusLine().toString());
			HttpEntity entity = response.getEntity();
			if (entity == null) {
				logger.warn("Entity is null!");
			} else {
				// entity content length is never set.
				// this naively tries to read everything.
				json = EntityUtils.toString(entity);
				EntityUtils.consume(entity);
			}
		} finally {
			if (response != null)
				response.close();
		}
		return json;
	}

	/**
	 * Convenience method that builds and sends a POST request using properties
	 * to build the URI and populate header with credentials.
	 * 
	 * @param path
	 *            last component(s) of REST endpoint name; e.g., "users" or
	 *            "user/ab1234/roles".
	 * @return JSON string fetched
	 * @throws Exception
	 *             if the HTTP response code is anything other than OK.
	 */
	public static String postJson(final SharedContextTestProperties properties, final String path, final String json)
			throws Exception {
		String requestPath = '/' + properties.getProperty(SharedContextTestProperties.APPNAME) //
				+ '/' + properties.getProperty(SharedContextTestProperties.RESTPATH) //
				+ '/' + path;
		return postJson(properties.getProperty(SharedContextTestProperties.HOSTNAME), //
				properties.getProperty(SharedContextTestProperties.PORT, -1), //
				properties.getProperty(SharedContextTestProperties.SECURE, true), //
				properties.getProperty(SharedContextTestProperties.UEBKEY), //
				properties.getProperty(SharedContextTestProperties.USERNAME), //
				properties.getProperty(SharedContextTestProperties.COUNTERSIGN), //
				requestPath, //
				json);
	}

	/**
	 * Constructs and sends a POST request using the specified values.
	 * 
	 * @param hostname
	 * @param port
	 * @param secure
	 *            If true, uses https; else http.
	 * @param requestPath
	 *            full path of the REST endpoint
	 * @param headerUebkey
	 * @param headerUsername
	 * @param headerPassword
	 * @param json
	 *            Content to post
	 * @return JSON result
	 * @throws Exception
	 */
	public static String postJson(final String hostname, final int port, boolean secure, final String headerUebkey,
			final String headerUsername, final String headerPassword, final String requestPath, final String json)
			throws Exception {

		URIBuilder builder = new URIBuilder();
		if (secure)
			builder.setScheme("https");
		else
			builder.setScheme("http");
		builder.setHost(hostname);
		if (port > 0)
			builder.setPort(port);
		builder.setPath(requestPath);
		final URI uri = builder.build();

		CloseableHttpClient httpClient;
		if (secure) {
			// Tell HttpClient to accept any server certificate for HTTPS.
			// http://stackoverflow.com/questions/24720013/apache-http-client-ssl-certificate-error
			SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() {
				@Override
				public boolean isTrusted(final X509Certificate[] chain, final String authType)
						throws CertificateException {
					return true;
				}
			}).build();
			SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext,
					NoopHostnameVerifier.INSTANCE);
			httpClient = HttpClientBuilder.create().setSSLSocketFactory(sslsf).build();
		} else {
			httpClient = HttpClients.createDefault();
		}
		HttpPost httpPost = new HttpPost(uri);
		httpPost.setHeader("uebkey", headerUebkey);
		httpPost.setHeader("username", headerUsername);
		httpPost.setHeader("password", headerPassword);

		StringEntity postEntity = new StringEntity(json, ContentType.create("application/json", Consts.UTF_8));
		httpPost.setEntity(postEntity);

		String responseJson = null;
		CloseableHttpResponse response = null;
		try {
			logger.debug("POST to " + uri);
			response = httpClient.execute(httpPost);
			logger.info("Status is " + response.getStatusLine());
			if (response.getStatusLine().getStatusCode() != HttpServletResponse.SC_OK)
				throw new Exception("Status is " + response.getStatusLine().toString());

			HttpEntity entity = response.getEntity();
			if (entity == null) {
				logger.warn("Entity is null!");
			} else {
				long len = entity.getContentLength();
				if (len < 0)
					logger.warn("Content length is -1");
				if (len < 2048) {
					responseJson = EntityUtils.toString(entity);
					logger.debug(responseJson);
				} else {
					logger.warn("Not implemented - stream content");
				}
				EntityUtils.consume(entity);
			}
		} finally {
			if (response != null)
				response.close();
		}
		return responseJson;
	}

}