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");
40 public SchedulerRestInterface(){
41 this.propertyGetter = SystemProperties::getProperty;
44 public SchedulerRestInterface(Function<String, String> propertyGetter){
45 this.propertyGetter = propertyGetter;
48 public void initRestClient() {
49 logger.info("Starting to initialize rest client ");
51 final String username;
52 final String password;
54 /*Setting user name based on properties*/
55 String retrievedUsername = propertyGetter.apply(SchedulerProperties.SCHEDULER_USER_NAME_VAL);
56 if(retrievedUsername.isEmpty()) {
59 username = retrievedUsername;
62 /*Setting password based on properties*/
63 String retrievedPassword = propertyGetter.apply(SchedulerProperties.SCHEDULER_PASSWORD_VAL);
64 if(retrievedPassword.isEmpty()) {
67 if (retrievedPassword.contains("OBF:")) {
68 password = Password.deobfuscate(retrievedPassword);
70 password = retrievedPassword;
74 String authString = username + ":" + password;
76 byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
77 String authStringEnc = new String(authEncBytes);
79 commonHeaders = new MultivaluedHashMap<> ();
80 commonHeaders.put("Authorization", Collections.singletonList("Basic " + authStringEnc));
83 if ( !username.isEmpty() ) {
84 client = httpsAuthClient.getClient(HttpClientMode.WITHOUT_KEYSTORE);
88 client = HttpBasicClient.getClient();
90 } catch (Exception e) {
91 logger.error(" <== Unable to initialize rest client ", e);
94 logger.info("\t<== Client Initialized \n");
97 public <T> void Get (T t, String sourceId, String path, org.onap.vid.scheduler.RestObject<T> restObject ) {
99 String methodName = "Get";
100 String url = String.format("%s%s",propertyGetter.apply(SchedulerProperties.SCHEDULER_SERVER_URL_VAL), path);
102 Logging.logRequest(outgoingRequestsLogger, HttpMethod.GET, url);
103 final Response cres = client.target(url)
105 .accept("application/json")
106 .headers(commonHeaders)
107 .header(REQUEST_ID_HEADER_KEY, Logging.extractOrGenerateRequestId())
109 Logging.logResponse(outgoingRequestsLogger, HttpMethod.GET, url, cres);
110 int status = cres.getStatus();
111 restObject.setStatusCode (status);
114 t = (T) cres.readEntity(t.getClass());
118 throw new GenericUncheckedException(String.format("%s with status=%d, url=%s", methodName, status,url));
123 public <T> void Delete(T t, String sourceID, String path, org.onap.vid.scheduler.RestObject<T> restObject) {
127 String url = String.format( "%s%s",propertyGetter.apply(SchedulerProperties.SCHEDULER_SERVER_URL_VAL), path);
128 Logging.logRequest(outgoingRequestsLogger, HttpMethod.DELETE, url);
129 Response cres = client.target(url)
131 .accept("application/json")
132 .headers(commonHeaders)
133 .header(REQUEST_ID_HEADER_KEY, Logging.extractOrGenerateRequestId())
135 Logging.logResponse(outgoingRequestsLogger, HttpMethod.DELETE, url, cres);
137 int status = cres.getStatus();
138 restObject.setStatusCode(status);
140 t = (T) cres.readEntity(t.getClass());
145 public <T> T getInstance(Class<T> clazz) throws IllegalAccessException, InstantiationException
147 return clazz.newInstance();