1 package org.openecomp.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.openecomp.vid.client.HttpBasicClient;
16 import org.openecomp.vid.client.HttpsBasicClient;
17 import org.openecomp.vid.scheduler.SchedulerProperties;
18 import org.openecomp.vid.scheduler.RestObjects.RestObject;
19 import org.springframework.stereotype.Service;
22 public class SchedulerRestInterface implements SchedulerRestInterfaceIfc {
24 private static Client client = null;
26 private MultivaluedHashMap<String, Object> commonHeaders;
29 static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SchedulerRestInterface.class);
31 public SchedulerRestInterface() {
35 public void initRestClient()
37 System.out.println( "\t <== Starting to initialize rest client ");
39 final String username;
40 final String password;
42 /*Setting user name based on properties*/
43 String retrievedUsername = SystemProperties.getProperty(SchedulerProperties.SCHEDULER_USER_NAME_VAL);
44 if(retrievedUsername.isEmpty()) {
47 username = retrievedUsername;
50 /*Setting password based on properties*/
51 String retrievedPassword = SystemProperties.getProperty(SchedulerProperties.SCHEDULER_PASSWORD_VAL);
52 if(retrievedPassword.isEmpty()) {
55 if (retrievedPassword.contains("OBF:")) {
56 password = Password.deobfuscate(retrievedPassword);
58 password = retrievedPassword;
62 String authString = username + ":" + password;
64 byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
65 String authStringEnc = new String(authEncBytes);
67 commonHeaders = new MultivaluedHashMap<String, Object> ();
68 commonHeaders.put("Authorization", Collections.singletonList((Object) ("Basic " + authStringEnc)));
71 if ( !username.isEmpty() ) {
73 client = HttpsBasicClient.getClient();
77 client = HttpBasicClient.getClient();
79 } catch (Exception e) {
80 System.out.println( " <== Unable to initialize rest client ");
83 System.out.println( "\t<== Client Initialized \n");
86 @SuppressWarnings("unchecked")
87 public <T> void Get (T t, String sourceId, String path, org.openecomp.vid.scheduler.RestObject<T> restObject ) throws Exception {
89 String methodName = "Get";
90 String url = SystemProperties.getProperty(SchedulerProperties.SCHEDULER_SERVER_URL_VAL) + path;
93 System.out.println( "<== URL FOR GET : " + url + "\n");
97 final Response cres = client.target(url)
99 .accept("application/json")
100 .headers(commonHeaders)
103 int status = cres.getStatus();
104 restObject.setStatusCode (status);
107 t = (T) cres.readEntity(t.getClass());
111 throw new Exception(methodName + " with status="+ status + ", url= " + url );
117 @SuppressWarnings("unchecked")
118 public <T> void Post(T t, JSONObject requestDetails, String path, RestObject<T> restObject) throws Exception {
120 String methodName = "Post";
121 String url = SystemProperties.getProperty(SchedulerProperties.SCHEDULER_SERVER_URL_VAL) + path;
123 System.out.println( "<== URL FOR POST : " + url + "\n");
129 // Change the content length
130 final Response cres = client.target(url)
132 .accept("application/json")
133 .headers(commonHeaders)
134 .post(Entity.entity(requestDetails, MediaType.APPLICATION_JSON));
137 t = (T) cres.readEntity(t.getClass());
140 catch ( Exception e ) {
142 System.out.println("<== " + methodName + " : No response entity, this is probably ok, e=" + e.getMessage());
145 int status = cres.getStatus();
146 restObject.setStatusCode (status);
148 if ( status >= 200 && status <= 299 ) {
150 System.out.println( "<== " + methodName + " : REST api POST was successful!" + "\n");
153 System.out.println( "<== " + methodName + " : FAILED with http status : "+status+", url = " + url + "\n");
156 } catch (Exception e)
158 System.out.println( "<== " + methodName + " : with url="+url+ ", Exception: " + e.toString() + "\n");
164 public void logRequest(JSONObject requestDetails) {}
166 @SuppressWarnings("unchecked")
167 public <T> void Delete(T t, JSONObject requestDetails, String sourceID, String path, RestObject<T> restObject) {
170 Response cres = null;
175 url = SystemProperties.getProperty(SchedulerProperties.SCHEDULER_SERVER_URL_VAL) + path;
177 cres = client.target(url)
179 .accept("application/json")
180 .headers(commonHeaders)
182 .build("DELETE", Entity.entity(requestDetails, MediaType.APPLICATION_JSON)).invoke();
183 // .method("DELETE", Entity.entity(r, MediaType.APPLICATION_JSON));
184 //.delete(Entity.entity(r, MediaType.APPLICATION_JSON));
186 int status = cres.getStatus();
187 restObject.setStatusCode (status);
190 t = (T) cres.readEntity(t.getClass());
193 catch ( Exception e ) {
203 public <T> T getInstance(Class<T> clazz) throws IllegalAccessException, InstantiationException
205 return clazz.newInstance();