1 package org.onap.vid.scheduler;
3 import com.att.eelf.configuration.EELFLogger;
4 import org.apache.commons.codec.binary.Base64;
5 import org.eclipse.jetty.util.security.Password;
6 import org.onap.vid.aai.util.HttpClientMode;
7 import org.onap.vid.aai.util.HttpsAuthClient;
8 import org.onap.vid.client.HttpBasicClient;
9 import org.onap.vid.exceptions.GenericUncheckedException;
10 import org.onap.vid.utils.Logging;
11 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
12 import org.onap.portalsdk.core.util.SystemProperties;
13 import org.springframework.beans.factory.annotation.Autowired;
14 import org.springframework.http.HttpMethod;
15 import org.springframework.stereotype.Service;
17 import javax.ws.rs.client.Client;
18 import javax.ws.rs.core.MultivaluedHashMap;
19 import javax.ws.rs.core.Response;
20 import java.util.Collections;
21 import java.util.function.Function;
23 import static org.onap.vid.utils.Logging.REQUEST_ID_HEADER_KEY;
26 public class SchedulerRestInterface implements SchedulerRestInterfaceIfc {
28 private Client client = null;
29 private Function<String, String> propertyGetter;
32 HttpsAuthClient httpsAuthClient;
34 private MultivaluedHashMap<String, Object> commonHeaders;
37 private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SchedulerRestInterface.class);
38 final private static EELFLogger outgoingRequestsLogger = Logging.getRequestsLogger("scheduler");
41 public SchedulerRestInterface(){
42 this.propertyGetter = SystemProperties::getProperty;
45 public SchedulerRestInterface(Function<String, String> propertyGetter){
46 this.propertyGetter = propertyGetter;
49 public void initRestClient() {
50 logger.info("Starting to initialize rest client ");
52 final String username;
53 final String password;
55 /*Setting user name based on properties*/
56 String retrievedUsername = propertyGetter.apply(SchedulerProperties.SCHEDULER_USER_NAME_VAL);
57 if(retrievedUsername.isEmpty()) {
60 username = retrievedUsername;
63 /*Setting password based on properties*/
64 String retrievedPassword = propertyGetter.apply(SchedulerProperties.SCHEDULER_PASSWORD_VAL);
65 if(retrievedPassword.isEmpty()) {
68 if (retrievedPassword.contains("OBF:")) {
69 password = Password.deobfuscate(retrievedPassword);
71 password = retrievedPassword;
75 String authString = username + ":" + password;
77 byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
78 String authStringEnc = new String(authEncBytes);
80 commonHeaders = new MultivaluedHashMap<> ();
81 commonHeaders.put("Authorization", Collections.singletonList("Basic " + authStringEnc));
84 if ( !username.isEmpty() ) {
85 client = httpsAuthClient.getClient(HttpClientMode.WITHOUT_KEYSTORE);
89 client = HttpBasicClient.getClient();
91 } catch (Exception e) {
92 logger.error(" <== Unable to initialize rest client ", e);
95 logger.info("\t<== Client Initialized \n");
98 public <T> void Get (T t, String sourceId, String path, org.onap.vid.scheduler.RestObject<T> restObject ) {
100 String methodName = "Get";
101 String url = String.format("%s%s",propertyGetter.apply(SchedulerProperties.SCHEDULER_SERVER_URL_VAL), path);
103 Logging.logRequest(outgoingRequestsLogger, HttpMethod.GET, url);
104 final Response cres = client.target(url)
106 .accept("application/json")
107 .headers(commonHeaders)
108 .header(REQUEST_ID_HEADER_KEY, Logging.extractOrGenerateRequestId())
110 Logging.logResponse(outgoingRequestsLogger, HttpMethod.GET, url, cres);
111 int status = cres.getStatus();
112 restObject.setStatusCode (status);
115 t = (T) cres.readEntity(t.getClass());
119 throw new GenericUncheckedException(String.format("%s with status=%d, url=%s", methodName, status,url));
124 public <T> void Delete(T t, String sourceID, String path, org.onap.vid.scheduler.RestObject<T> restObject) {
128 String url = String.format( "%s%s",propertyGetter.apply(SchedulerProperties.SCHEDULER_SERVER_URL_VAL), path);
129 Logging.logRequest(outgoingRequestsLogger, HttpMethod.DELETE, url);
130 Response cres = client.target(url)
132 .accept("application/json")
133 .headers(commonHeaders)
134 .header(REQUEST_ID_HEADER_KEY, Logging.extractOrGenerateRequestId())
136 Logging.logResponse(outgoingRequestsLogger, HttpMethod.DELETE, url, cres);
138 int status = cres.getStatus();
139 restObject.setStatusCode(status);
141 t = (T) cres.readEntity(t.getClass());
146 public <T> T getInstance(Class<T> clazz) throws IllegalAccessException, InstantiationException
148 return clazz.newInstance();