1 package org.openecomp.dcae.dmaapbc.dbcapp.rest;
3 import java.net.MalformedURLException;
7 import org.apache.http.HttpHost;
8 import org.apache.http.auth.AuthScope;
9 import org.apache.http.auth.UsernamePasswordCredentials;
10 import org.apache.http.client.CredentialsProvider;
11 import org.apache.http.impl.client.BasicCredentialsProvider;
12 import org.apache.http.impl.client.CloseableHttpClient;
13 import org.apache.http.impl.client.HttpClientBuilder;
14 import org.openecomp.dcae.dmaapbc.dbcapp.domain.DmaapAccess;
15 import org.openecomp.dcae.dmaapbc.dbcapp.domain.ManifestTransportModel;
16 import org.openecomp.dcae.dmaapbc.dbcapp.service.DmaapAccessService;
17 import org.springframework.core.ParameterizedTypeReference;
18 import org.springframework.http.HttpMethod;
19 import org.springframework.http.ResponseEntity;
20 import org.springframework.web.client.RestTemplate;
23 * Provides methods for accessing the DBC microservice via REST using basic HTTP
27 public class DbcUsvcRestClient implements DmaapAccessService {
29 public static final String endpointPath = "/dmaap_access";
30 private final String baseUrl;
31 private final RestTemplate restTemplate;
34 * Builds a restTemplate that uses basic HTTP authentication for use by all
35 * methods in this class.
38 * URL of the web endpoint
44 public DbcUsvcRestClient(String webapiUrl, String user, String pass) {
45 if (webapiUrl == null || user == null || pass == null)
46 throw new IllegalArgumentException("Nulls not permitted");
51 url = new URL(baseUrl);
52 } catch (MalformedURLException ex) {
53 throw new RuntimeException("Failed to parse URL", ex);
55 final HttpHost httpHost = new HttpHost(url.getHost(), url.getPort());
57 // Build a client with a credentials provider
58 CredentialsProvider credsProvider = new BasicCredentialsProvider();
59 credsProvider.setCredentials(new AuthScope(httpHost), new UsernamePasswordCredentials(user, pass));
60 HttpClientBuilder clientBuilder = HttpClientBuilder.create();
61 CloseableHttpClient httpClient = clientBuilder.setDefaultCredentialsProvider(credsProvider).build();
63 // Create request factory with our superpower client
64 HttpComponentsClientHttpRequestFactoryBasicAuth requestFactory = new HttpComponentsClientHttpRequestFactoryBasicAuth(
66 requestFactory.setHttpClient(httpClient);
68 // Put the factory in the template
69 this.restTemplate = new RestTemplate();
70 restTemplate.setRequestFactory(requestFactory);
74 * Gets the manifest from the micro service.
77 public ManifestTransportModel getManifest() {
78 String url = baseUrl + "/manifest";
79 ResponseEntity<ManifestTransportModel> daResponse = restTemplate.exchange(url, HttpMethod.GET, null,
80 ManifestTransportModel.class);
81 ManifestTransportModel response = daResponse.getBody();
86 * Gets the count of DMaaP access profiles.
88 * @return Number of access profiles in the database.
90 public int getDmaapAccessCount() {
91 String url = baseUrl + "/count_dmaap_access";
92 ResponseEntity<DbcUsvcRestResponse> daResponse = restTemplate.exchange(url, HttpMethod.GET, null,
93 DbcUsvcRestResponse.class);
94 DbcUsvcRestResponse response = daResponse.getBody();
95 return response.getStatus();
99 * Gets the DMaaP access profiles for the specified userId.
103 * @return List of access profiles
106 public List<DmaapAccess> getDmaapAccessList(final String userId) {
107 String url = baseUrl + endpointPath + "?userId=" + userId;
108 ResponseEntity<List<DmaapAccess>> daResponse = restTemplate.exchange(url, HttpMethod.GET, null,
109 new ParameterizedTypeReference<List<DmaapAccess>>() {
111 List<DmaapAccess> daList = daResponse.getBody();
116 * Gets the specified DMaaP access profile.
119 public DmaapAccess getDmaapAccess(Long dmaapId) {
120 String url = baseUrl + endpointPath + "?dmaapId=" + dmaapId;
121 ResponseEntity<DmaapAccess> daResponse = restTemplate.exchange(url, HttpMethod.GET, null,
122 new ParameterizedTypeReference<DmaapAccess>() {
124 DmaapAccess da = daResponse.getBody();
129 * POSTs or PUTs the DMaaP access profile as needed, based on whether the
130 * object's ID field is set. If not set it creates a new row; if set, it
131 * updates a row in the remote service table.
137 public void saveDmaapAccess(final DmaapAccess dmaapAccess) {
138 if (dmaapAccess.getId() == null) {
139 String url = baseUrl + endpointPath;
140 restTemplate.postForObject(url, dmaapAccess, String.class);
142 String url = baseUrl + endpointPath + "/" + Long.toString(dmaapAccess.getId());
143 restTemplate.put(url, dmaapAccess);
148 * Deletes the DMaaP access profile row in the remote service table with the
155 public void deleteDmaapAccess(final Long id) {
156 String url = baseUrl + endpointPath + "/" + Long.toString(id);
157 restTemplate.delete(url);
168 public static void main(String[] args) throws Exception {
169 if (args.length != 1)
170 throw new IllegalArgumentException("Single argument expected: userid");
171 DbcUsvcRestClient client = new DbcUsvcRestClient("http://localhost:8081/dbus", "dbus_user", "dbus_pass");
172 final String userId = args[0];
173 System.out.println("Requesting profiles for user " + userId);
174 List<DmaapAccess> access = client.getDmaapAccessList(userId);
176 System.err.println("Received null");
178 for (DmaapAccess da : access)
179 System.out.println(da);