1 package org.onap.vid.policy;
3 import org.apache.commons.codec.binary.Base64;
4 import org.eclipse.jetty.util.security.Password;
5 import org.json.simple.JSONObject;
6 import org.onap.vid.client.HttpBasicClient;
7 import org.onap.vid.exceptions.GenericUncheckedException;
8 import org.onap.vid.policy.rest.RequestDetails;
9 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
10 import org.onap.portalsdk.core.util.SystemProperties;
12 import javax.ws.rs.client.Client;
13 import javax.ws.rs.client.Entity;
14 import javax.ws.rs.core.MediaType;
15 import javax.ws.rs.core.MultivaluedHashMap;
16 import javax.ws.rs.core.Response;
17 import java.text.DateFormat;
18 import java.text.SimpleDateFormat;
19 import java.util.Collections;
20 import java.util.Date;
22 public class PolicyRestInterface extends PolicyRestInt implements PolicyRestInterfaceIfc {
25 private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(PolicyRestInterface.class);
27 public static final String APPLICATION_JSON = "application/json";
30 private static Client client = null;
32 /** The common headers. */
33 private MultivaluedHashMap<String, Object> commonHeaders;
34 public PolicyRestInterface() {
38 /** The Constant dateFormat. */
39 static final DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSSS");
41 public void initRestClient()
43 final String methodname = "initRestClient()";
45 final String mechId = SystemProperties.getProperty(PolicyProperties.POLICY_CLIENT_MECHID_VAL);
46 final String clientPassword = SystemProperties.getProperty(PolicyProperties.POLICY_CLIENT_PASSWORD_VAL);
47 final String username = SystemProperties.getProperty(PolicyProperties.POLICY_USERNAME_VAL);
48 final String password = SystemProperties.getProperty(PolicyProperties.POLICY_PASSWORD_VAL);
49 final String environment = SystemProperties.getProperty(PolicyProperties.POLICY_ENVIRONMENT_VAL);
51 final String decrypted_client_password = Password.deobfuscate(clientPassword);
52 String mechAuthString = mechId + ":" + decrypted_client_password;
53 byte[] mechAuthEncBytes = Base64.encodeBase64(mechAuthString.getBytes());
54 String clientAuth = new String(mechAuthEncBytes);
56 final String decrypted_password = Password.deobfuscate(password);
57 String authString = username + ":" + decrypted_password;
58 byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
59 String authorization = new String(authEncBytes);
61 commonHeaders = new MultivaluedHashMap<> ();
62 commonHeaders.put("ClientAuth", Collections.singletonList((Object) ("Basic " + clientAuth)));
63 commonHeaders.put("Authorization", Collections.singletonList((Object) ("Basic " + authorization)));
64 commonHeaders.put("Environment", Collections.singletonList((Object) (environment)));
69 client = HttpBasicClient.getClient();
70 } catch (Exception e) {
71 System.out.println( methodname + " Unable to get the SSL client");
76 @SuppressWarnings("unchecked")
77 public <T> void Get (T t, String sourceId, String path, RestObject<T> restObject ) {
78 String methodName = "Get";
80 logger.debug(EELFLoggerDelegate.debugLogger, methodName + " start");
85 url = SystemProperties.getProperty(PolicyProperties.POLICY_SERVER_URL_VAL) + path;
86 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " sending request to url= " + url);
90 final Response cres = client.target(url)
92 .accept(APPLICATION_JSON)
93 .headers(commonHeaders)
96 int status = cres.getStatus();
97 restObject.setStatusCode (status);
100 t = (T) cres.readEntity(t.getClass());
102 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + methodName + " REST api was successfull!");
105 throw new GenericUncheckedException(methodName + " with status="+ status + ", url= " + url );
108 logger.debug(EELFLoggerDelegate.debugLogger,methodName + " received status=" + status );
113 @SuppressWarnings("unchecked")
114 public <T> void Delete(T t, RequestDetails r, String sourceID, String path, RestObject<T> restObject) {
116 String methodName = "Delete";
118 Response cres = null;
120 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + " start");
126 url = SystemProperties.getProperty(PolicyProperties.POLICY_SERVER_URL_VAL) + path;
127 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + " methodName sending request to: " + url);
129 cres = client.target(url)
131 .accept(APPLICATION_JSON)
132 .headers(commonHeaders)
134 .build("DELETE", Entity.entity(r, MediaType.APPLICATION_JSON)).invoke();
136 int status = cres.getStatus();
137 restObject.setStatusCode (status);
139 if (status == 404) { // resource not found
140 String msg = "Resource does not exist...: " + cres.getStatus();
141 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + msg);
142 } else if (status == 200 || status == 204){
143 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + "Resource " + url + " deleted");
144 } else if (status == 202) {
145 String msg = "Delete in progress: " + status;
146 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + msg);
149 String msg = "Deleting Resource failed: " + status;
150 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + msg);
154 t = (T) cres.readEntity(t.getClass());
157 catch ( Exception e ) {
158 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + " No response entity, this is probably ok, e="
165 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + " with url="+url+ ", Exception: " + e.toString());
171 @SuppressWarnings("unchecked")
172 public <T> void Post(T t, JSONObject requestDetails, String uuid, String path, RestObject<T> restObject) {
174 String methodName = "Post";
177 System.out.println( "POST policy rest interface");
183 url = SystemProperties.getProperty(PolicyProperties.POLICY_SERVER_URL_VAL) + path;
184 System.out.println( "<== " + methodName + " sending request to url= " + url);
185 // Change the content length
186 final Response cres = client.target(url)
188 .accept(APPLICATION_JSON)
189 .headers(commonHeaders)
190 //.header("content-length", 201)
191 //.header("X-FromAppId", sourceID)
192 .post(Entity.entity(requestDetails, MediaType.APPLICATION_JSON));
195 t = (T) cres.readEntity(t.getClass());
198 catch ( Exception e ) {
200 System.out.println("<== " + methodName + " No response entity, this is probably ok, e=" + e.getMessage());
203 int status = cres.getStatus();
204 restObject.setStatusCode (status);
206 if ( status >= 200 && status <= 299 ) {
207 System.out.println( "<== " + methodName + " REST api POST was successful!");
210 System.out.println( "<== " + methodName + " with status="+status+", url="+url);
213 } catch (Exception e)
215 System.out.println( "<== " + methodName + " with url="+url+ ", Exception: " + e.toString());
221 public <T> T getInstance(Class<T> clazz) throws IllegalAccessException, InstantiationException
223 return clazz.newInstance();
227 public void logRequest(RequestDetails r) {
228 // TODO Auto-generated method stub