1 package org.openecomp.portalapp.portal.scheduler;
3 import java.util.Collections;
5 import javax.ws.rs.client.Client;
6 import javax.ws.rs.client.Entity;
7 import javax.ws.rs.core.MediaType;
8 import javax.ws.rs.core.MultivaluedHashMap;
9 import javax.ws.rs.core.Response;
11 import org.apache.commons.codec.binary.Base64;
12 import org.json.simple.JSONObject;
13 import org.openecomp.portalapp.portal.scheduler.client.HttpBasicClient;
14 import org.openecomp.portalapp.portal.scheduler.client.HttpsBasicClient;
15 import org.openecomp.portalsdk.core.util.SystemProperties;
16 import org.springframework.stereotype.Service;
17 import org.openecomp.portalapp.portal.scheduler.restobjects.RestObject;
21 public class SchedulerRestInterface implements SchedulerRestInterfaceIfc {
23 private static Client client = null;
25 private MultivaluedHashMap<String, Object> commonHeaders;
27 public SchedulerRestInterface() {
31 public void initRestClient()
33 final String methodname = "initRestClient()";
35 final String username = "";//SystemProperties.getProperty(SchedulerProperties.SCHEDULER_USER_NAME_VAL);
36 //final String password = "";//SystemProperties.getProperty(SchedulerProperties.SCHEDULER_PASSWORD_VAL);
37 final String scheduler_url = "";//SystemProperties.getProperty(SchedulerProperties.SCHEDULER_SERVER_URL_VAL);
38 final String decrypted_password = "";//Password.deobfuscate(password);
40 String authString = username + ":" + decrypted_password;
42 byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
43 String authStringEnc = new String(authEncBytes);
45 commonHeaders = new MultivaluedHashMap<String, Object> ();
46 commonHeaders.put("Authorization", Collections.singletonList((Object) ("Basic " + authStringEnc)));
48 boolean use_ssl = true;
49 if ( (scheduler_url != null) && ( !(scheduler_url.isEmpty()) ) ) {
50 if ( scheduler_url.startsWith("https")) {
62 client = HttpsBasicClient.getClient();
66 client = HttpBasicClient.getClient();
68 } catch (Exception e) {
69 System.out.println( methodname + " Unable to get the SSL client");
74 @SuppressWarnings("unchecked")
75 public <T> void Get (T t, String sourceId, String path, org.openecomp.portalapp.portal.scheduler.restobjects.RestObject<T> restObject ) throws Exception {
77 String methodName = "Get";
78 String url = SystemProperties.getProperty(SchedulerProperties.SCHEDULER_SERVER_URL_VAL) + path;
81 System.out.println( "<== URL FOR GET : " + url + "\n");
85 final Response cres = client.target(url)
87 .accept("application/json")
88 .headers(commonHeaders)
91 int status = cres.getStatus();
92 restObject.setStatusCode (status);
95 t = (T) cres.readEntity(t.getClass());
99 throw new Exception(methodName + " with status="+ status + ", url= " + url );
105 @SuppressWarnings("unchecked")
106 public <T> void Post(T t, JSONObject requestDetails, String path, RestObject<T> restObject) throws Exception {
108 String methodName = "Post";
109 String url = SystemProperties.getProperty(SchedulerProperties.SCHEDULER_SERVER_URL_VAL) + path;
111 System.out.println( "<== URL FOR POST : " + url + "\n");
117 // Change the content length
118 final Response cres = client.target(url)
120 .accept("application/json")
121 .headers(commonHeaders)
122 //.header("content-length", 201)
123 //.header("X-FromAppId", sourceID)
124 .post(Entity.entity(requestDetails, MediaType.APPLICATION_JSON));
127 t = (T) cres.readEntity(t.getClass());
130 catch ( Exception e ) {
132 System.out.println("<== " + methodName + " : No response entity, this is probably ok, e=" + e.getMessage());
135 int status = cres.getStatus();
136 restObject.setStatusCode (status);
138 if ( status >= 200 && status <= 299 ) {
140 System.out.println( "<== " + methodName + " : REST api POST was successful!" + "\n");
143 System.out.println( "<== " + methodName + " : FAILED with http status : "+status+", url = " + url + "\n");
146 } catch (Exception e)
148 System.out.println( "<== " + methodName + " : with url="+url+ ", Exception: " + e.toString() + "\n");
153 @SuppressWarnings("unchecked")
154 public <T> void Delete(T t, JSONObject requestDetails, String sourceID, String path, RestObject<T> restObject) {
157 Response cres = null;
162 url = SystemProperties.getProperty(SchedulerProperties.SCHEDULER_SERVER_URL_VAL) + path;
164 cres = client.target(url)
166 .accept("application/json")
167 .headers(commonHeaders)
169 .build("DELETE", Entity.entity(requestDetails, MediaType.APPLICATION_JSON)).invoke();
170 // .method("DELETE", Entity.entity(r, MediaType.APPLICATION_JSON));
171 //.delete(Entity.entity(r, MediaType.APPLICATION_JSON));
173 int status = cres.getStatus();
174 restObject.setStatusCode (status);
177 t = (T) cres.readEntity(t.getClass());
180 catch ( Exception e ) {
190 public <T> T getInstance(Class<T> clazz) throws IllegalAccessException, InstantiationException
192 return clazz.newInstance();
196 public void logRequest(JSONObject requestDetails) {
197 // TODO Auto-generated method stub