aboutsummaryrefslogtreecommitdiffstats
path: root/ccsdk-app-common/src/main/java/org/onap/ccsdk/dashboard/rest/RestClientBase.java
blob: c2acb334ee746b6ad96820d1f11ff22e551d5536 (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
/**
 * 
 */
package org.onap.ccsdk.dashboard.rest;

import java.net.URL;

import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.web.client.RestTemplate;

/**
 * Base class for all the Rest client implementations
 * 
 * @author rp5662
 *
 */
public class RestClientBase {
	protected RestTemplate restTemplate = null;
	
	protected void createRestTemplate(URL url, String user, String pass, String urlScheme) {
		RestTemplate restTempl = null;	
		final HttpHost httpHost = new HttpHost(url.getHost(), url.getPort(), urlScheme);

		// Build a client with a credentials provider
		CloseableHttpClient httpClient = null;

		if (user != null && pass != null) {
			CredentialsProvider credsProvider = new BasicCredentialsProvider();
			credsProvider.setCredentials(new AuthScope(httpHost), new UsernamePasswordCredentials(user, pass));
			httpClient = HttpClientBuilder.create().setDefaultCredentialsProvider(credsProvider).build();
		} else {
			httpClient = HttpClientBuilder.create().build();
		}
		// Create request factory
		HttpComponentsClientHttpRequestFactoryBasicAuth requestFactory = new HttpComponentsClientHttpRequestFactoryBasicAuth(
				httpHost);
		requestFactory.setHttpClient(httpClient);

		// Put the factory in the template
		restTempl = new RestTemplate();
		restTempl.setRequestFactory(requestFactory);
		this.restTemplate = restTempl;
	}
	
	/**
	 * Builds URL ensuring appropriate separators. The base comes from
	 * properties file so could have many problems.
	 * 
	 * @param base
	 * @param suffix
	 * @param queryParams
	 *            key-value pairs; i.e. must have an even number of entries.
	 *            Ignored if null.
	 * @return
	 */
	protected String buildUrl(final String[] path, final String[] queryParams) {
		StringBuilder sb = new StringBuilder(path[0]);
		for (int p = 1; p < path.length; ++p) {
			if (!path[p - 1].endsWith("/") && !path[p].startsWith("/"))
				sb.append('/');
			sb.append(path[p]);
		}
		if (queryParams != null && queryParams.length > 0) {
			sb.append('?');
			int i = 0;
			while (i < queryParams.length) {
				if (i > 0)
					sb.append('&');
				sb.append(queryParams[i]);
				sb.append('=');
				sb.append(queryParams[i + 1]);
				i += 2;
			}
		}
		return sb.toString();
	}
	/**
	 * Create Http Entity for the tenant header
	 * 
	 * @param tenant
	 * @return
	 */
	protected HttpEntity<String> getTenantHeader(String tenant) {
		HttpHeaders headers = new HttpHeaders();
		headers.set("Tenant", tenant);
		return new HttpEntity<String>("parameters", headers);
	}
}