1 package org.openecomp.portalapp.portal.controller;
4 import java.security.cert.CertificateException;
5 import java.security.cert.X509Certificate;
7 import javax.net.ssl.SSLContext;
8 import javax.servlet.http.HttpServletResponse;
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12 import org.apache.http.Consts;
13 import org.apache.http.HttpEntity;
14 import org.apache.http.client.methods.CloseableHttpResponse;
15 import org.apache.http.client.methods.HttpGet;
16 import org.apache.http.client.methods.HttpPost;
17 import org.apache.http.client.utils.URIBuilder;
18 import org.apache.http.conn.ssl.NoopHostnameVerifier;
19 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
20 import org.apache.http.entity.ContentType;
21 import org.apache.http.entity.StringEntity;
22 import org.apache.http.impl.client.CloseableHttpClient;
23 import org.apache.http.impl.client.HttpClientBuilder;
24 import org.apache.http.impl.client.HttpClients;
25 import org.apache.http.ssl.SSLContexts;
26 import org.apache.http.ssl.TrustStrategy;
27 import org.apache.http.util.EntityUtils;
30 * Provides reusable features for test cases to get or post from an REST
31 * endpoint, allowing use of HTTPS connections to servers that use self-signed
34 public class SharedContextRestClient {
36 private static final Log logger = LogFactory.getLog(SharedContextRestClient.class);
39 * Convenience method that builds and sends a GET request using properties
40 * to build the URI and populate header with credentials.
43 * last component(s) of REST endpoint name; e.g., "get".
46 * @return JSON string fetched
48 * if the HTTP response code is anything other than OK.
50 public static String getJson(final SharedContextTestProperties properties, final String task,
51 final String contextId, final String contextKey) throws Exception {
52 String requestPath = '/' + properties.getProperty(SharedContextTestProperties.APPNAME) //
53 + '/' + properties.getProperty(SharedContextTestProperties.RESTPATH) //
55 return getJson(properties.getProperty(SharedContextTestProperties.HOSTNAME), //
56 properties.getProperty(SharedContextTestProperties.PORT, -1), //
57 properties.getProperty(SharedContextTestProperties.SECURE, true), //
58 properties.getProperty(SharedContextTestProperties.UEBKEY), //
59 properties.getProperty(SharedContextTestProperties.USERNAME), //
60 properties.getProperty(SharedContextTestProperties.PASSWORD), requestPath, //
66 * Constructs and sends a GET request using the specified values.
72 * If true, uses https; else http.
74 * @param headerUsername
75 * @param headerPassword
77 * full path of the REST endpoint
83 public static String getJson(final String hostname, final int port, boolean secure, final String headerUebkey,
84 final String headerUsername, final String headerPassword, final String requestPath, final String contextId,
85 final String contextKey) throws Exception {
87 URIBuilder uriBuilder = new URIBuilder();
89 uriBuilder.setScheme("https");
91 uriBuilder.setScheme("http");
92 uriBuilder.setHost(hostname);
94 uriBuilder.setPort(port);
95 uriBuilder.setPath(requestPath);
96 uriBuilder.addParameter("context_id", contextId);
97 if (contextKey != null)
98 uriBuilder.addParameter("ckey", contextKey);
99 final URI uri = uriBuilder.build();
101 CloseableHttpClient httpClient;
103 // Tell HttpClient to accept any server certificate for HTTPS.
104 // http://stackoverflow.com/questions/24720013/apache-http-client-ssl-certificate-error
105 SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() {
107 public boolean isTrusted(final X509Certificate[] chain, final String authType)
108 throws CertificateException {
112 SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext,
113 NoopHostnameVerifier.INSTANCE);
114 httpClient = HttpClientBuilder.create().setSSLSocketFactory(sslsf).build();
116 httpClient = HttpClients.createDefault();
119 HttpGet httpGet = new HttpGet(uri);
120 httpGet.setHeader("uebkey", headerUebkey);
121 httpGet.setHeader("username", headerUsername);
122 httpGet.setHeader("password", headerPassword);
125 CloseableHttpResponse response = null;
127 logger.debug("GET from " + uri);
128 response = httpClient.execute(httpGet);
129 logger.info("Status is " + response.getStatusLine());
130 if (response.getStatusLine().getStatusCode() != HttpServletResponse.SC_OK)
131 throw new Exception("Status is " + response.getStatusLine().toString());
132 HttpEntity entity = response.getEntity();
133 if (entity == null) {
134 logger.warn("Entity is null!");
136 // entity content length is never set.
137 // this naively tries to read everything.
138 json = EntityUtils.toString(entity);
139 EntityUtils.consume(entity);
142 if (response != null)
149 * Convenience method that builds and sends a POST request using properties
150 * to build the URI and populate header with credentials.
153 * last component(s) of REST endpoint name; e.g., "users" or
154 * "user/{userid}/roles".
155 * @return JSON string fetched
157 * if the HTTP response code is anything other than OK.
159 public static String postJson(final SharedContextTestProperties properties, final String path, final String json)
161 String requestPath = '/' + properties.getProperty(SharedContextTestProperties.APPNAME) //
162 + '/' + properties.getProperty(SharedContextTestProperties.RESTPATH) //
164 return postJson(properties.getProperty(SharedContextTestProperties.HOSTNAME), //
165 properties.getProperty(SharedContextTestProperties.PORT, -1), //
166 properties.getProperty(SharedContextTestProperties.SECURE, true), //
167 properties.getProperty(SharedContextTestProperties.UEBKEY), //
168 properties.getProperty(SharedContextTestProperties.USERNAME), //
169 properties.getProperty(SharedContextTestProperties.PASSWORD), //
175 * Constructs and sends a POST request using the specified values.
180 * If true, uses https; else http.
182 * full path of the REST endpoint
183 * @param headerUebkey
184 * @param headerUsername
185 * @param headerPassword
188 * @return JSON result
191 public static String postJson(final String hostname, final int port, boolean secure, final String headerUebkey,
192 final String headerUsername, final String headerPassword, final String requestPath, final String json)
195 URIBuilder builder = new URIBuilder();
197 builder.setScheme("https");
199 builder.setScheme("http");
200 builder.setHost(hostname);
202 builder.setPort(port);
203 builder.setPath(requestPath);
204 final URI uri = builder.build();
206 CloseableHttpClient httpClient;
208 // Tell HttpClient to accept any server certificate for HTTPS.
209 // http://stackoverflow.com/questions/24720013/apache-http-client-ssl-certificate-error
210 SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() {
212 public boolean isTrusted(final X509Certificate[] chain, final String authType)
213 throws CertificateException {
217 SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext,
218 NoopHostnameVerifier.INSTANCE);
219 httpClient = HttpClientBuilder.create().setSSLSocketFactory(sslsf).build();
221 httpClient = HttpClients.createDefault();
223 HttpPost httpPost = new HttpPost(uri);
224 httpPost.setHeader("uebkey", headerUebkey);
225 httpPost.setHeader("username", headerUsername);
226 httpPost.setHeader("password", headerPassword);
228 StringEntity postEntity = new StringEntity(json, ContentType.create("application/json", Consts.UTF_8));
229 httpPost.setEntity(postEntity);
231 String responseJson = null;
232 CloseableHttpResponse response = null;
234 logger.debug("POST to " + uri);
235 response = httpClient.execute(httpPost);
236 logger.info("Status is " + response.getStatusLine());
237 if (response.getStatusLine().getStatusCode() != HttpServletResponse.SC_OK)
238 throw new Exception("Status is " + response.getStatusLine().toString());
240 HttpEntity entity = response.getEntity();
241 if (entity == null) {
242 logger.warn("Entity is null!");
244 long len = entity.getContentLength();
246 logger.warn("Content length is -1");
248 responseJson = EntityUtils.toString(entity);
249 logger.debug(responseJson);
251 logger.warn("Not implemented - stream content");
253 EntityUtils.consume(entity);
256 if (response != null)