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