1 package org.openecomp.portalapp.portal.scheduler.policy;
3 import java.text.DateFormat;
4 import java.text.SimpleDateFormat;
5 import java.util.Collections;
8 import javax.ws.rs.client.Client;
9 import javax.ws.rs.client.Entity;
10 import javax.ws.rs.core.MediaType;
11 import javax.ws.rs.core.MultivaluedHashMap;
12 import javax.ws.rs.core.Response;
14 import org.apache.commons.codec.binary.Base64;
15 import org.eclipse.jetty.util.security.Password;
16 import org.json.simple.JSONObject;
17 import org.openecomp.portalapp.portal.scheduler.client.HttpBasicClient;
18 import org.openecomp.portalapp.portal.scheduler.policy.rest.RequestDetails;
19 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
20 import org.openecomp.portalsdk.core.util.SystemProperties;
23 public class PolicyRestInterface extends PolicyRestInt implements PolicyRestInterfaceIfc {
26 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(PolicyRestInterface.class);
28 /** The Constant dateFormat. */
29 final static DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSSS");
32 private static Client client = null;
34 /** The common headers. */
35 private MultivaluedHashMap<String, Object> commonHeaders;
37 public PolicyRestInterface() {
41 public void initRestClient()
43 final String methodname = "initRestClient()";
45 //final String clientAuth = SystemProperties.getProperty(PolicyProperties.POLICY_CLIENTAUTH_VAL);
46 //final String authorization = SystemProperties.getProperty(PolicyProperties.POLICY_AUTHORIZATION_VAL);
47 final String mechId = SystemProperties.getProperty(PolicyProperties.POLICY_CLIENT_MECHID_VAL);
48 final String clientPassword = SystemProperties.getProperty(PolicyProperties.POLICY_CLIENT_PASSWORD_VAL);
49 final String username = SystemProperties.getProperty(PolicyProperties.POLICY_USERNAME_VAL);
50 final String password = SystemProperties.getProperty(PolicyProperties.POLICY_PASSWORD_VAL);
51 final String environment = SystemProperties.getProperty(PolicyProperties.POLICY_ENVIRONMENT_VAL);
53 final String decrypted_client_password = Password.deobfuscate(clientPassword);
54 String mechAuthString = mechId + ":" + decrypted_client_password;
55 byte[] mechAuthEncBytes = Base64.encodeBase64(mechAuthString.getBytes());
56 String clientAuth = new String(mechAuthEncBytes);
58 final String decrypted_password = Password.deobfuscate(password);
59 String authString = username + ":" + decrypted_password;
60 byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
61 String authorization = new String(authEncBytes);
63 commonHeaders = new MultivaluedHashMap<String, Object> ();
64 commonHeaders.put("ClientAuth", Collections.singletonList((Object) ("Basic " + clientAuth)));
65 commonHeaders.put("Authorization", Collections.singletonList((Object) ("Basic " + authorization)));
66 commonHeaders.put("Environment", Collections.singletonList((Object) (environment)));
71 client = HttpBasicClient.getClient();
72 } catch (Exception e) {
73 System.out.println( methodname + " Unable to get the SSL client");
78 @SuppressWarnings("unchecked")
79 public <T> void Get (T t, String sourceId, String path, RestObject<T> restObject ) throws Exception {
80 String methodName = "Get";
82 logger.debug(EELFLoggerDelegate.debugLogger, methodName + " start");
87 url = SystemProperties.getProperty(PolicyProperties.POLICY_SERVER_URL_VAL) + path;
88 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " sending request to url= " + url);
92 final Response cres = client.target(url)
94 .accept("application/json")
95 .headers(commonHeaders)
98 int status = cres.getStatus();
99 restObject.setStatusCode (status);
102 t = (T) cres.readEntity(t.getClass());
104 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + methodName + " REST api was successfull!");
107 throw new Exception(methodName + " with status="+ status + ", url= " + url );
110 logger.debug(EELFLoggerDelegate.debugLogger,methodName + " received status=" + status );
115 @SuppressWarnings("unchecked")
116 public <T> void Delete(T t, RequestDetails r, String sourceID, String path, RestObject<T> restObject) {
118 String methodName = "Delete";
120 Response cres = null;
122 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + " start");
128 url = SystemProperties.getProperty(PolicyProperties.POLICY_SERVER_URL_VAL) + path;
129 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + " methodName sending request to: " + url);
131 cres = client.target(url)
133 .accept("application/json")
134 .headers(commonHeaders)
136 .build("DELETE", Entity.entity(r, MediaType.APPLICATION_JSON)).invoke();
137 // .method("DELETE", Entity.entity(r, MediaType.APPLICATION_JSON));
138 //.delete(Entity.entity(r, MediaType.APPLICATION_JSON));
140 int status = cres.getStatus();
141 restObject.setStatusCode (status);
143 if (status == 404) { // resource not found
144 String msg = "Resource does not exist...: " + cres.getStatus();
145 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + msg);
146 } else if (status == 200 || status == 204){
147 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + "Resource " + url + " deleted");
148 } else if (status == 202) {
149 String msg = "Delete in progress: " + status;
150 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + msg);
153 String msg = "Deleting Resource failed: " + status;
154 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + msg);
158 t = (T) cres.readEntity(t.getClass());
161 catch ( Exception e ) {
162 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + " No response entity, this is probably ok, e="
169 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + " with url="+url+ ", Exception: " + e.toString());
175 @SuppressWarnings("unchecked")
176 public <T> void Post(T t, JSONObject requestDetails, String uuid, String path, RestObject<T> restObject) throws Exception {
178 String methodName = "Post";
181 System.out.println( "POST policy rest interface");
183 // logRequest (requestDetails);
188 url = SystemProperties.getProperty(PolicyProperties.POLICY_SERVER_URL_VAL) + path;
189 System.out.println( "<== " + methodName + " sending request to url= " + url);
190 // Change the content length
191 final Response cres = client.target(url)
193 .accept("application/json")
194 .headers(commonHeaders)
195 //.header("content-length", 201)
196 //.header("X-FromAppId", sourceID)
197 .post(Entity.entity(requestDetails, MediaType.APPLICATION_JSON));
200 t = (T) cres.readEntity(t.getClass());
203 catch ( Exception e ) {
205 System.out.println("<== " + methodName + " No response entity, this is probably ok, e=" + e.getMessage());
208 int status = cres.getStatus();
209 restObject.setStatusCode (status);
211 if ( status >= 200 && status <= 299 ) {
212 System.out.println( "<== " + methodName + " REST api POST was successful!");
215 System.out.println( "<== " + methodName + " with status="+status+", url="+url);
218 } catch (Exception e)
220 System.out.println( "<== " + methodName + " with url="+url+ ", Exception: " + e.toString());
226 public <T> T getInstance(Class<T> clazz) throws IllegalAccessException, InstantiationException
228 return clazz.newInstance();
232 public void logRequest(RequestDetails r) {
233 // TODO Auto-generated method stub