Merge "Generic REST client for external services"
[vid.git] / vid-app-common / src / main / java / org / onap / vid / scheduler / SchedulerRestInterface.java
1 package org.onap.vid.scheduler;
2
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;
16
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;
22
23 import static org.onap.vid.utils.Logging.REQUEST_ID_HEADER_KEY;
24
25 @Service
26 public class SchedulerRestInterface implements SchedulerRestInterfaceIfc {
27
28     private Client client = null;
29     private Function<String, String> propertyGetter;
30
31     @Autowired
32     HttpsAuthClient httpsAuthClient;
33
34     private MultivaluedHashMap<String, Object> commonHeaders;
35
36     /** The logger. */
37     private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SchedulerRestInterface.class);
38     final private static EELFLogger outgoingRequestsLogger = Logging.getRequestsLogger("scheduler");
39
40     public SchedulerRestInterface(){
41         this.propertyGetter = SystemProperties::getProperty;
42     }
43
44     public SchedulerRestInterface(Function<String, String> propertyGetter){
45         this.propertyGetter = propertyGetter;
46     }
47
48     public void initRestClient() {
49         logger.info("Starting to initialize rest client ");
50
51         final String username;
52         final String password;
53
54                 /*Setting user name based on properties*/
55         String retrievedUsername = propertyGetter.apply(SchedulerProperties.SCHEDULER_USER_NAME_VAL);
56         if(retrievedUsername.isEmpty()) {
57             username = "";
58         } else {
59             username = retrievedUsername;
60         }
61
62                 /*Setting password based on properties*/
63         String retrievedPassword = propertyGetter.apply(SchedulerProperties.SCHEDULER_PASSWORD_VAL);
64         if(retrievedPassword.isEmpty()) {
65             password = "";
66         } else {
67             if (retrievedPassword.contains("OBF:")) {
68                 password = Password.deobfuscate(retrievedPassword);
69             } else {
70                 password = retrievedPassword;
71             }
72         }
73
74         String authString = username + ":" + password;
75
76         byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
77         String authStringEnc = new String(authEncBytes);
78
79         commonHeaders = new MultivaluedHashMap<> ();
80         commonHeaders.put("Authorization", Collections.singletonList("Basic " + authStringEnc));
81
82         try {
83             if ( !username.isEmpty() ) {
84                 client = httpsAuthClient.getClient(HttpClientMode.WITHOUT_KEYSTORE);
85             }
86             else {
87
88                 client = HttpBasicClient.getClient();
89             }
90         } catch (Exception e) {
91             logger.error(" <== Unable to initialize rest client ", e);
92         }
93
94         logger.info("\t<== Client Initialized \n");
95     }
96
97     public <T> void Get (T t, String sourceId, String path, org.onap.vid.scheduler.RestObject<T> restObject ) {
98
99         String methodName = "Get";
100         String url = String.format("%s%s",propertyGetter.apply(SchedulerProperties.SCHEDULER_SERVER_URL_VAL), path);
101         initRestClient();
102         Logging.logRequest(outgoingRequestsLogger, HttpMethod.GET, url);
103         final Response cres = client.target(url)
104                 .request()
105                 .accept("application/json")
106                 .headers(commonHeaders)
107                 .header(REQUEST_ID_HEADER_KEY, Logging.extractOrGenerateRequestId())
108                 .get();
109         Logging.logResponse(outgoingRequestsLogger, HttpMethod.GET, url, cres);
110         int status = cres.getStatus();
111         restObject.setStatusCode (status);
112
113         if (status == 200) {
114             t = (T) cres.readEntity(t.getClass());
115             restObject.set(t);
116
117         } else {
118             throw new GenericUncheckedException(String.format("%s with status=%d, url=%s", methodName, status,url));
119         }
120
121     }
122
123     public <T> void Delete(T t, String sourceID, String path, org.onap.vid.scheduler.RestObject<T> restObject) {
124
125         initRestClient();
126
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)
130                 .request()
131                 .accept("application/json")
132                 .headers(commonHeaders)
133                 .header(REQUEST_ID_HEADER_KEY, Logging.extractOrGenerateRequestId())
134                 .delete();
135         Logging.logResponse(outgoingRequestsLogger, HttpMethod.DELETE, url, cres);
136
137         int status = cres.getStatus();
138         restObject.setStatusCode(status);
139
140         t = (T) cres.readEntity(t.getClass());
141         restObject.set(t);
142
143     }
144
145     public <T> T getInstance(Class<T> clazz) throws IllegalAccessException, InstantiationException
146     {
147         return clazz.newInstance();
148     }
149
150 }