1 package org.onap.vid.scheduler;
3 import java.util.Collections;
4 import javax.ws.rs.client.Client;
5 import javax.ws.rs.client.Entity;
6 import javax.ws.rs.core.MediaType;
7 import javax.ws.rs.core.MultivaluedHashMap;
8 import javax.ws.rs.core.Response;
10 import org.apache.commons.codec.binary.Base64;
11 import org.eclipse.jetty.util.security.Password;
12 import org.json.simple.JSONObject;
13 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
14 import org.openecomp.portalsdk.core.util.SystemProperties;
15 import org.onap.vid.client.HttpBasicClient;
16 import org.onap.vid.client.HttpsBasicClient;
17 import org.onap.vid.scheduler.SchedulerProperties;
18 import org.onap.vid.scheduler.RestObjects.RestObject;
19 import org.springframework.stereotype.Service;
21 import static org.onap.vid.utils.Logging.getHttpServletRequest;
22 import static org.onap.vid.utils.Logging.requestIdHeaderKey;
25 public class SchedulerRestInterface implements SchedulerRestInterfaceIfc {
27 private static Client client = null;
29 private MultivaluedHashMap<String, Object> commonHeaders;
32 static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SchedulerRestInterface.class);
34 public SchedulerRestInterface() {
38 public void initRestClient()
40 System.out.println( "\t <== Starting to initialize rest client ");
42 final String username;
43 final String password;
45 /*Setting user name based on properties*/
46 String retrievedUsername = SystemProperties.getProperty(SchedulerProperties.SCHEDULER_USER_NAME_VAL);
47 if(retrievedUsername.isEmpty()) {
50 username = retrievedUsername;
53 /*Setting password based on properties*/
54 String retrievedPassword = SystemProperties.getProperty(SchedulerProperties.SCHEDULER_PASSWORD_VAL);
55 if(retrievedPassword.isEmpty()) {
58 if (retrievedPassword.contains("OBF:")) {
59 password = Password.deobfuscate(retrievedPassword);
61 password = retrievedPassword;
65 String authString = username + ":" + password;
67 byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
68 String authStringEnc = new String(authEncBytes);
70 commonHeaders = new MultivaluedHashMap<String, Object> ();
71 commonHeaders.put("Authorization", Collections.singletonList((Object) ("Basic " + authStringEnc)));
74 if ( !username.isEmpty() ) {
76 client = HttpsBasicClient.getClient();
80 client = HttpBasicClient.getClient();
82 } catch (Exception e) {
83 System.out.println( " <== Unable to initialize rest client ");
86 System.out.println( "\t<== Client Initialized \n");
89 @SuppressWarnings("unchecked")
90 public <T> void Get (T t, String sourceId, String path, org.onap.vid.scheduler.RestObject<T> restObject ) throws Exception {
92 String methodName = "Get";
93 String url = SystemProperties.getProperty(SchedulerProperties.SCHEDULER_SERVER_URL_VAL) + path;
96 System.out.println( "<== URL FOR GET : " + url + "\n");
100 final Response cres = client.target(url)
102 .accept("application/json")
103 .headers(commonHeaders)
104 .header(requestIdHeaderKey, getHttpServletRequest().getHeader(requestIdHeaderKey))
107 int status = cres.getStatus();
108 restObject.setStatusCode (status);
111 t = (T) cres.readEntity(t.getClass());
115 throw new Exception(methodName + " with status="+ status + ", url= " + url );
121 @SuppressWarnings("unchecked")
122 public <T> void Post(T t, JSONObject requestDetails, String path, RestObject<T> restObject) throws Exception {
124 String methodName = "Post";
125 String url = SystemProperties.getProperty(SchedulerProperties.SCHEDULER_SERVER_URL_VAL) + path;
127 System.out.println( "<== URL FOR POST : " + url + "\n");
133 // Change the content length
134 final Response cres = client.target(url)
136 .accept("application/json")
137 .headers(commonHeaders)
138 .header(requestIdHeaderKey, getHttpServletRequest().getHeader(requestIdHeaderKey))
139 .post(Entity.entity(requestDetails, MediaType.APPLICATION_JSON));
142 t = (T) cres.readEntity(t.getClass());
145 catch ( Exception e ) {
147 System.out.println("<== " + methodName + " : No response entity, this is probably ok, e=" + e.getMessage());
150 int status = cres.getStatus();
151 restObject.setStatusCode (status);
153 if ( status >= 200 && status <= 299 ) {
155 System.out.println( "<== " + methodName + " : REST api POST was successful!" + "\n");
158 System.out.println( "<== " + methodName + " : FAILED with http status : "+status+", url = " + url + "\n");
161 } catch (Exception e)
163 System.out.println( "<== " + methodName + " : with url="+url+ ", Exception: " + e.toString() + "\n");
169 public void logRequest(JSONObject requestDetails) {}
171 @SuppressWarnings("unchecked")
172 public <T> void Delete(T t, String sourceID, String path, org.onap.vid.scheduler.RestObject<T> restObject) {
175 Response cres = null;
180 url = SystemProperties.getProperty(SchedulerProperties.SCHEDULER_SERVER_URL_VAL) + path;
182 cres = client.target(url)
184 .accept("application/json")
185 .headers(commonHeaders)
186 .header(requestIdHeaderKey, getHttpServletRequest().getHeader(requestIdHeaderKey))
189 // .method("DELETE", Entity.entity(r, MediaType.APPLICATION_JSON));
190 //.delete(Entity.entity(r, MediaType.APPLICATION_JSON));
192 int status = cres.getStatus();
193 restObject.setStatusCode (status);
196 t = (T) cres.readEntity(t.getClass());
199 catch ( Exception e ) {
209 public <T> T getInstance(Class<T> clazz) throws IllegalAccessException, InstantiationException
211 return clazz.newInstance();