summaryrefslogtreecommitdiffstats
path: root/ecomp-portal-BE-common/src/main/java/org/openecomp/portalapp/portal/scheduler/SchedulerRestInterface.java
blob: 337c1fcfeb6eb9f23f5f8149b18a7bf4588ddbd1 (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
package org.openecomp.portalapp.portal.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.json.simple.JSONObject;
import org.openecomp.portalapp.portal.scheduler.client.HttpBasicClient;
import org.openecomp.portalapp.portal.scheduler.client.HttpsBasicClient;
import org.openecomp.portalsdk.core.util.SystemProperties;
import org.springframework.stereotype.Service;
import org.openecomp.portalapp.portal.scheduler.restobjects.RestObject;


@Service
public class SchedulerRestInterface implements SchedulerRestInterfaceIfc {

	private static Client client = null;
		
	private MultivaluedHashMap<String, Object> commonHeaders;
	
	public SchedulerRestInterface() {
		super();
	}
	
	public void initRestClient()
	{
		final String methodname = "initRestClient()";
		
		final String username = "";//SystemProperties.getProperty(SchedulerProperties.SCHEDULER_USER_NAME_VAL);
		//final String password = "";//SystemProperties.getProperty(SchedulerProperties.SCHEDULER_PASSWORD_VAL);
		final String scheduler_url = "";//SystemProperties.getProperty(SchedulerProperties.SCHEDULER_SERVER_URL_VAL);
		final String decrypted_password = "";//Password.deobfuscate(password);
		
		String authString = username + ":" + decrypted_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)));
		
		boolean use_ssl = true;
		if ( (scheduler_url != null) && ( !(scheduler_url.isEmpty()) ) ) {
			if ( scheduler_url.startsWith("https")) {
				use_ssl = true;
			}
			else {
				use_ssl = false;
			}
		}
		if (client == null) {
			
			try {
				if ( use_ssl ) { 
					
					client = HttpsBasicClient.getClient();
				}
				else {
					
					client = HttpBasicClient.getClient();
				}
			} catch (Exception e) {
				System.out.println(  methodname + " Unable to get the SSL client");
			}
		}
	}
		
	@SuppressWarnings("unchecked")
	public <T> void Get (T t, String sourceId, String path, org.openecomp.portalapp.portal.scheduler.restobjects.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)
                 //.header("content-length", 201)
                 //.header("X-FromAppId",  sourceID)
                 .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;        
        }
    }
	
	@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();
	}

	@Override
	public void logRequest(JSONObject requestDetails) {
		// TODO Auto-generated method stub
		
	}	
}