blob: 1deacad37ee97d6fb6aed23b6c66316955156b42 (
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
|
package org.onap.so.adapters.cnf.client;
import com.google.gson.Gson;
import org.onap.so.security.SoUserCredentialConfiguration;
import org.onap.so.security.UserCredentials;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import java.util.ArrayList;
import java.util.List;
import static org.springframework.http.HttpMethod.POST;
@Component
public class SoCallbackClient {
private final static Gson gson = new Gson();
private final RestTemplate restTemplate;
private final SoUserCredentialConfiguration userCredentialConfiguration;
private final String role = "ACTUATOR";
private final UserCredentials credentials;
@Autowired
public SoCallbackClient(RestTemplate restTemplate, SoUserCredentialConfiguration userCredentialConfiguration) {
this.restTemplate = restTemplate;
this.userCredentialConfiguration = userCredentialConfiguration;
if (!userCredentialConfiguration.getRoles().contains(role))
throw new RuntimeException("Missing authentication role: " + role);
credentials = userCredentialConfiguration.getUsercredentials().stream().filter(
creds -> role.equals(creds.getRole())).findAny().orElse(null);
}
public ResponseEntity<String> sendPostCallback(String url, Object body) {
return restTemplate.exchange(url, POST, httpEntity(body), String.class);
}
private HttpEntity<?> httpEntity(Object body) {
HttpHeaders headers = new HttpHeaders();
List<MediaType> acceptableMediaTypes = new ArrayList<>();
acceptableMediaTypes.add(MediaType.APPLICATION_JSON);
headers.setAccept(acceptableMediaTypes);
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setBasicAuth(credentials.getUsername(), credentials.getPassword());
return new HttpEntity<>(gson.toJson(body), headers);
}
}
|