aboutsummaryrefslogtreecommitdiffstats
path: root/vid-app-common/src/main/java/org/openecomp/vid/scheduler/SchedulerRestInterface.java
blob: 5f529f390c31a8e98249c451c5b0e9efae166963 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package org.openecomp.vid.scheduler;

import java.util.Collections;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedHashMap;
import javax.ws.rs.core.Response;

import org.apache.commons.codec.binary.Base64;
import org.eclipse.jetty.util.security.Password;
import org.json.simple.JSONObject;
import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
import org.openecomp.portalsdk.core.util.SystemProperties;
import org.openecomp.vid.client.HttpBasicClient;
import org.openecomp.vid.client.HttpsBasicClient;
import org.openecomp.vid.scheduler.SchedulerProperties;
import org.openecomp.vid.scheduler.RestObjects.RestObject;
import org.springframework.stereotype.Service;

@Service
public class SchedulerRestInterface implements SchedulerRestInterfaceIfc {

	private static Client client = null;
		
	private MultivaluedHashMap<String, Object> commonHeaders;
	
	/** The logger. */
	static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SchedulerRestInterface.class);
	
	public SchedulerRestInterface() {
		super();
	}
	
	public void initRestClient()
	{	
		System.out.println( "\t <== Starting to initialize rest client ");
		
		final String username;
		final String password;
		
		/*Setting user name based on properties*/
		String retrievedUsername = SystemProperties.getProperty(SchedulerProperties.SCHEDULER_USER_NAME_VAL);
		if(retrievedUsername.isEmpty()) {
			username = "";
		} else {
			username = retrievedUsername;
		}
		
		/*Setting password based on properties*/
		String retrievedPassword = SystemProperties.getProperty(SchedulerProperties.SCHEDULER_PASSWORD_VAL);
		if(retrievedPassword.isEmpty()) {
			password = "";
		} else {
			if (retrievedPassword.contains("OBF:")) {
				password = Password.deobfuscate(retrievedPassword);
			} else {
				password = retrievedPassword;
			}
		}
		
		String authString = username + ":" + password;
				
		byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
		String authStringEnc = new String(authEncBytes);

		commonHeaders = new MultivaluedHashMap<String, Object> ();
		commonHeaders.put("Authorization",  Collections.singletonList((Object) ("Basic " + authStringEnc)));		
					
		try {
			if ( !username.isEmpty() ) { 
				
				client = HttpsBasicClient.getClient();
			}
			else {
				
				client = HttpBasicClient.getClient();
			}
		} catch (Exception e) {
			System.out.println( " <== Unable to initialize rest client ");
		}
		
		System.out.println( "\t<== Client Initialized \n");
	}
		
	@SuppressWarnings("unchecked")
	public <T> void Get (T t, String sourceId, String path, org.openecomp.vid.scheduler.RestObject<T> restObject ) throws Exception {
		
		String methodName = "Get";
		String url = SystemProperties.getProperty(SchedulerProperties.SCHEDULER_SERVER_URL_VAL) + path;
		
		
		System.out.println( "<== URL FOR GET : " + url + "\n");

        initRestClient();
		
		final Response cres = client.target(url)
			 .request()
	         .accept("application/json")
	         .headers(commonHeaders)
	         .get();
				
		int status = cres.getStatus();
		restObject.setStatusCode (status);
		
		if (status == 200) {
			 t = (T) cres.readEntity(t.getClass());
			 restObject.set(t);
			
		 } else {
		     throw new Exception(methodName + " with status="+ status + ", url= " + url );
		 }

		return;
	}
		
	@SuppressWarnings("unchecked")
	public <T> void Post(T t, JSONObject requestDetails, String path, RestObject<T> restObject) throws Exception {
		
        String methodName = "Post";
        String url = SystemProperties.getProperty(SchedulerProperties.SCHEDULER_SERVER_URL_VAL) + path;
		        
        System.out.println( "<== URL FOR POST : " + url + "\n");
     
        try {
            
            initRestClient();    
    
            // Change the content length
            final Response cres = client.target(url)
            	 .request()
                 .accept("application/json")
	        	 .headers(commonHeaders)
                 .post(Entity.entity(requestDetails, MediaType.APPLICATION_JSON));
            
            try {
	   			t = (T) cres.readEntity(t.getClass());
	   			restObject.set(t);
            }
            catch ( Exception e ) {
            	
            	System.out.println("<== " + methodName + " : No response entity, this is probably ok, e=" + e.getMessage());
            }

            int status = cres.getStatus();
    		restObject.setStatusCode (status);    		
    		    		
    		if ( status >= 200 && status <= 299 ) {
    			    			
    			System.out.println( "<== " + methodName + " : REST api POST was successful!" + "\n");
    		
             } else {
            	 System.out.println( "<== " + methodName + " : FAILED with http status : "+status+", url = " + url + "\n");
             }    
   
        } catch (Exception e)
        {
        	System.out.println( "<== " + methodName + " : with url="+url+ ", Exception: " + e.toString() + "\n");
        	throw e;        
        }
    }

	@Override
	public void logRequest(JSONObject requestDetails) {}

	@SuppressWarnings("unchecked")
   	public <T> void Delete(T t, JSONObject requestDetails, String sourceID, String path, RestObject<T> restObject) {
	 
		String url="";
		Response cres = null;
		
		try {
			initRestClient();
			
			url = SystemProperties.getProperty(SchedulerProperties.SCHEDULER_SERVER_URL_VAL) + path;
		
			cres = client.target(url)
					 .request()
			         .accept("application/json")
	        		 .headers(commonHeaders)
			         //.entity(r)
			         .build("DELETE", Entity.entity(requestDetails, MediaType.APPLICATION_JSON)).invoke();
			       //  .method("DELETE", Entity.entity(r, MediaType.APPLICATION_JSON));
			         //.delete(Entity.entity(r, MediaType.APPLICATION_JSON));
			
			int status = cres.getStatus();
	    	restObject.setStatusCode (status);
	    			
			try {
	   			t = (T) cres.readEntity(t.getClass());
	   			restObject.set(t);
            }
            catch ( Exception e ) {
            }
   
        } 
		catch (Exception e)
        {     	
        	 throw e;        
        }
	}
	
	public <T> T getInstance(Class<T> clazz) throws IllegalAccessException, InstantiationException
	{
		return clazz.newInstance();
	}

}