337c1fcfeb6eb9f23f5f8149b18a7bf4588ddbd1
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / portal / scheduler / SchedulerRestInterface.java
1 package org.openecomp.portalapp.portal.scheduler;
2
3 import java.util.Collections;
4
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;
10
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;
18
19
20 @Service
21 public class SchedulerRestInterface implements SchedulerRestInterfaceIfc {
22
23         private static Client client = null;
24                 
25         private MultivaluedHashMap<String, Object> commonHeaders;
26         
27         public SchedulerRestInterface() {
28                 super();
29         }
30         
31         public void initRestClient()
32         {
33                 final String methodname = "initRestClient()";
34                 
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);
39                 
40                 String authString = username + ":" + decrypted_password;
41                 
42                 byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
43                 String authStringEnc = new String(authEncBytes);
44
45                 commonHeaders = new MultivaluedHashMap<String, Object> ();
46                 commonHeaders.put("Authorization",  Collections.singletonList((Object) ("Basic " + authStringEnc)));
47                 
48                 boolean use_ssl = true;
49                 if ( (scheduler_url != null) && ( !(scheduler_url.isEmpty()) ) ) {
50                         if ( scheduler_url.startsWith("https")) {
51                                 use_ssl = true;
52                         }
53                         else {
54                                 use_ssl = false;
55                         }
56                 }
57                 if (client == null) {
58                         
59                         try {
60                                 if ( use_ssl ) { 
61                                         
62                                         client = HttpsBasicClient.getClient();
63                                 }
64                                 else {
65                                         
66                                         client = HttpBasicClient.getClient();
67                                 }
68                         } catch (Exception e) {
69                                 System.out.println(  methodname + " Unable to get the SSL client");
70                         }
71                 }
72         }
73                 
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 {
76                 
77                 String methodName = "Get";
78                 String url = SystemProperties.getProperty(SchedulerProperties.SCHEDULER_SERVER_URL_VAL) + path;
79                 
80                 
81                 System.out.println( "<== URL FOR GET : " + url + "\n");
82
83         initRestClient();
84                 
85                 final Response cres = client.target(url)
86                          .request()
87                  .accept("application/json")
88                  .headers(commonHeaders)
89                  .get();
90                                 
91                 int status = cres.getStatus();
92                 restObject.setStatusCode (status);
93                 
94                 if (status == 200) {
95                          t = (T) cres.readEntity(t.getClass());
96                          restObject.set(t);
97                         
98                  } else {
99                      throw new Exception(methodName + " with status="+ status + ", url= " + url );
100                  }
101
102                 return;
103         }
104                 
105         @SuppressWarnings("unchecked")
106         public <T> void Post(T t, JSONObject requestDetails, String path, RestObject<T> restObject) throws Exception {
107                 
108         String methodName = "Post";
109         String url = SystemProperties.getProperty(SchedulerProperties.SCHEDULER_SERVER_URL_VAL) + path;
110                         
111         System.out.println( "<== URL FOR POST : " + url + "\n");
112      
113         try {
114             
115             initRestClient();    
116     
117             // Change the content length
118             final Response cres = client.target(url)
119                  .request()
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));
125             
126             try {
127                                 t = (T) cres.readEntity(t.getClass());
128                                 restObject.set(t);
129             }
130             catch ( Exception e ) {
131                 
132                 System.out.println("<== " + methodName + " : No response entity, this is probably ok, e=" + e.getMessage());
133             }
134
135             int status = cres.getStatus();
136                 restObject.setStatusCode (status);              
137                                 
138                 if ( status >= 200 && status <= 299 ) {
139                                                 
140                         System.out.println( "<== " + methodName + " : REST api POST was successful!" + "\n");
141                 
142              } else {
143                  System.out.println( "<== " + methodName + " : FAILED with http status : "+status+", url = " + url + "\n");
144              }    
145    
146         } catch (Exception e)
147         {
148                 System.out.println( "<== " + methodName + " : with url="+url+ ", Exception: " + e.toString() + "\n");
149                 throw e;        
150         }
151     }
152         
153         @SuppressWarnings("unchecked")
154         public <T> void Delete(T t, JSONObject requestDetails, String sourceID, String path, RestObject<T> restObject) {
155          
156                 String url="";
157                 Response cres = null;
158                 
159                 try {
160                         initRestClient();
161                         
162                         url = SystemProperties.getProperty(SchedulerProperties.SCHEDULER_SERVER_URL_VAL) + path;
163                 
164                         cres = client.target(url)
165                                          .request()
166                                  .accept("application/json")
167                                  .headers(commonHeaders)
168                                  //.entity(r)
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));
172                         
173                         int status = cres.getStatus();
174                 restObject.setStatusCode (status);
175                                 
176                         try {
177                                 t = (T) cres.readEntity(t.getClass());
178                                 restObject.set(t);
179             }
180             catch ( Exception e ) {
181             }
182    
183         } 
184                 catch (Exception e)
185         {       
186                  throw e;        
187         }
188         }
189         
190         public <T> T getInstance(Class<T> clazz) throws IllegalAccessException, InstantiationException
191         {
192                 return clazz.newInstance();
193         }
194
195         @Override
196         public void logRequest(JSONObject requestDetails) {
197                 // TODO Auto-generated method stub
198                 
199         }       
200 }