Upgrade sonar plugin
[vid.git] / vid-app-common / src / main / java / org / openecomp / vid / scheduler / SchedulerRestInterface.java
1 package org.openecomp.vid.scheduler;
2
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;
9
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;
20
21 @Service
22 public class SchedulerRestInterface implements SchedulerRestInterfaceIfc {
23
24         private static Client client = null;
25                 
26         private MultivaluedHashMap<String, Object> commonHeaders;
27         
28         /** The logger. */
29         static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SchedulerRestInterface.class);
30         
31         public SchedulerRestInterface() {
32                 super();
33         }
34         
35         public void initRestClient()
36         {       
37                 System.out.println( "\t <== Starting to initialize rest client ");
38                 
39                 final String username;
40                 final String password;
41                 
42                 /*Setting user name based on properties*/
43                 String retrievedUsername = SystemProperties.getProperty(SchedulerProperties.SCHEDULER_USER_NAME_VAL);
44                 if(retrievedUsername.isEmpty()) {
45                         username = "";
46                 } else {
47                         username = retrievedUsername;
48                 }
49                 
50                 /*Setting password based on properties*/
51                 String retrievedPassword = SystemProperties.getProperty(SchedulerProperties.SCHEDULER_PASSWORD_VAL);
52                 if(retrievedPassword.isEmpty()) {
53                         password = "";
54                 } else {
55                         if (retrievedPassword.contains("OBF:")) {
56                                 password = Password.deobfuscate(retrievedPassword);
57                         } else {
58                                 password = retrievedPassword;
59                         }
60                 }
61                 
62                 String authString = username + ":" + password;
63                                 
64                 byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
65                 String authStringEnc = new String(authEncBytes);
66
67                 commonHeaders = new MultivaluedHashMap<String, Object> ();
68                 commonHeaders.put("Authorization",  Collections.singletonList((Object) ("Basic " + authStringEnc)));            
69                                         
70                 try {
71                         if ( !username.isEmpty() ) { 
72                                 
73                                 client = HttpsBasicClient.getClient();
74                         }
75                         else {
76                                 
77                                 client = HttpBasicClient.getClient();
78                         }
79                 } catch (Exception e) {
80                         System.out.println( " <== Unable to initialize rest client ");
81                 }
82                 
83                 System.out.println( "\t<== Client Initialized \n");
84         }
85                 
86         @SuppressWarnings("unchecked")
87         public <T> void Get (T t, String sourceId, String path, org.openecomp.vid.scheduler.RestObject<T> restObject ) throws Exception {
88                 
89                 String methodName = "Get";
90                 String url = SystemProperties.getProperty(SchedulerProperties.SCHEDULER_SERVER_URL_VAL) + path;
91                 
92                 
93                 System.out.println( "<== URL FOR GET : " + url + "\n");
94
95         initRestClient();
96                 
97                 final Response cres = client.target(url)
98                          .request()
99                  .accept("application/json")
100                  .headers(commonHeaders)
101                  .get();
102                                 
103                 int status = cres.getStatus();
104                 restObject.setStatusCode (status);
105                 
106                 if (status == 200) {
107                          t = (T) cres.readEntity(t.getClass());
108                          restObject.set(t);
109                         
110                  } else {
111                      throw new Exception(methodName + " with status="+ status + ", url= " + url );
112                  }
113
114                 return;
115         }
116                 
117         @SuppressWarnings("unchecked")
118         public <T> void Post(T t, JSONObject requestDetails, String path, RestObject<T> restObject) throws Exception {
119                 
120         String methodName = "Post";
121         String url = SystemProperties.getProperty(SchedulerProperties.SCHEDULER_SERVER_URL_VAL) + path;
122                         
123         System.out.println( "<== URL FOR POST : " + url + "\n");
124      
125         try {
126             
127             initRestClient();    
128     
129             // Change the content length
130             final Response cres = client.target(url)
131                  .request()
132                  .accept("application/json")
133                          .headers(commonHeaders)
134                  .post(Entity.entity(requestDetails, MediaType.APPLICATION_JSON));
135             
136             try {
137                                 t = (T) cres.readEntity(t.getClass());
138                                 restObject.set(t);
139             }
140             catch ( Exception e ) {
141                 
142                 System.out.println("<== " + methodName + " : No response entity, this is probably ok, e=" + e.getMessage());
143             }
144
145             int status = cres.getStatus();
146                 restObject.setStatusCode (status);              
147                                 
148                 if ( status >= 200 && status <= 299 ) {
149                                                 
150                         System.out.println( "<== " + methodName + " : REST api POST was successful!" + "\n");
151                 
152              } else {
153                  System.out.println( "<== " + methodName + " : FAILED with http status : "+status+", url = " + url + "\n");
154              }    
155    
156         } catch (Exception e)
157         {
158                 System.out.println( "<== " + methodName + " : with url="+url+ ", Exception: " + e.toString() + "\n");
159                 throw e;        
160         }
161     }
162
163         @Override
164         public void logRequest(JSONObject requestDetails) {}
165
166         @SuppressWarnings("unchecked")
167         public <T> void Delete(T t, JSONObject requestDetails, String sourceID, String path, RestObject<T> restObject) {
168          
169                 String url="";
170                 Response cres = null;
171                 
172                 try {
173                         initRestClient();
174                         
175                         url = SystemProperties.getProperty(SchedulerProperties.SCHEDULER_SERVER_URL_VAL) + path;
176                 
177                         cres = client.target(url)
178                                          .request()
179                                  .accept("application/json")
180                                  .headers(commonHeaders)
181                                  //.entity(r)
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));
185                         
186                         int status = cres.getStatus();
187                 restObject.setStatusCode (status);
188                                 
189                         try {
190                                 t = (T) cres.readEntity(t.getClass());
191                                 restObject.set(t);
192             }
193             catch ( Exception e ) {
194             }
195    
196         } 
197                 catch (Exception e)
198         {       
199                  throw e;        
200         }
201         }
202         
203         public <T> T getInstance(Class<T> clazz) throws IllegalAccessException, InstantiationException
204         {
205                 return clazz.newInstance();
206         }
207
208 }