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;
22 import static org.onap.vid.utils.Logging.REQUEST_ID_HEADER_KEY;
25 public class SchedulerRestInterface implements SchedulerRestInterfaceIfc {
27 private Client client = null;
30 HttpsAuthClient httpsAuthClient;
32 private MultivaluedHashMap<String, Object> commonHeaders;
35 private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SchedulerRestInterface.class);
36 final private static EELFLogger outgoingRequestsLogger = Logging.getRequestsLogger("scheduler");
38 public void initRestClient() {
39 logger.info("Starting to initialize rest client ");
41 final String username;
42 final String password;
44 /*Setting user name based on properties*/
45 String retrievedUsername = SystemProperties.getProperty(SchedulerProperties.SCHEDULER_USER_NAME_VAL);
46 if(retrievedUsername.isEmpty()) {
49 username = retrievedUsername;
52 /*Setting password based on properties*/
53 String retrievedPassword = SystemProperties.getProperty(SchedulerProperties.SCHEDULER_PASSWORD_VAL);
54 if(retrievedPassword.isEmpty()) {
57 if (retrievedPassword.contains("OBF:")) {
58 password = Password.deobfuscate(retrievedPassword);
60 password = retrievedPassword;
64 String authString = username + ":" + password;
66 byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
67 String authStringEnc = new String(authEncBytes);
69 commonHeaders = new MultivaluedHashMap<String, Object> ();
70 commonHeaders.put("Authorization", Collections.singletonList("Basic " + authStringEnc));
73 if ( !username.isEmpty() ) {
74 client = httpsAuthClient.getClient(HttpClientMode.WITHOUT_KEYSTORE);
78 client = HttpBasicClient.getClient();
80 } catch (Exception e) {
81 logger.error(" <== Unable to initialize rest client ", e);
84 logger.info("\t<== Client Initialized \n");
87 public <T> void Get (T t, String sourceId, String path, org.onap.vid.scheduler.RestObject<T> restObject ) {
89 String methodName = "Get";
90 String url = SystemProperties.getProperty(SchedulerProperties.SCHEDULER_SERVER_URL_VAL) + path;
92 Logging.logRequest(outgoingRequestsLogger, HttpMethod.GET, url);
93 final Response cres = client.target(url)
95 .accept("application/json")
96 .headers(commonHeaders)
97 .header(REQUEST_ID_HEADER_KEY, Logging.extractOrGenerateRequestId())
99 Logging.logResponse(outgoingRequestsLogger, HttpMethod.GET, url, cres);
100 int status = cres.getStatus();
101 restObject.setStatusCode (status);
104 t = (T) cres.readEntity(t.getClass());
108 throw new GenericUncheckedException(methodName + " with status="+ status + ", url= " + url );
113 public <T> void Delete(T t, String sourceID, String path, org.onap.vid.scheduler.RestObject<T> restObject) {
117 String url = SystemProperties.getProperty(SchedulerProperties.SCHEDULER_SERVER_URL_VAL) + path;
118 Logging.logRequest(outgoingRequestsLogger, HttpMethod.DELETE, url);
119 Response cres = client.target(url)
121 .accept("application/json")
122 .headers(commonHeaders)
123 .header(REQUEST_ID_HEADER_KEY, Logging.extractOrGenerateRequestId())
125 Logging.logResponse(outgoingRequestsLogger, HttpMethod.DELETE, url, cres);
127 int status = cres.getStatus();
128 restObject.setStatusCode(status);
130 t = (T) cres.readEntity(t.getClass());
135 public <T> T getInstance(Class<T> clazz) throws IllegalAccessException, InstantiationException
137 return clazz.newInstance();