From c72d565bb58226b20625b2bce5f0019046bee649 Mon Sep 17 00:00:00 2001 From: "Sonsino, Ofir (os0695)" Date: Tue, 10 Jul 2018 14:20:54 +0300 Subject: Merge 1806 code of vid-common Change-Id: I75d52abed4a24dfe3827d79edc4a2938726aa87a Issue-ID: VID-208 Signed-off-by: Sonsino, Ofir (os0695) --- .../onap/vid/scheduler/SchedulerProperties.java | 2 + .../onap/vid/scheduler/SchedulerRestInterface.java | 186 +++++++-------------- .../scheduler/SchedulerRestInterfaceFactory.java | 15 -- .../vid/scheduler/SchedulerRestInterfaceIfc.java | 11 +- 4 files changed, 61 insertions(+), 153 deletions(-) delete mode 100644 vid-app-common/src/main/java/org/onap/vid/scheduler/SchedulerRestInterfaceFactory.java (limited to 'vid-app-common/src/main/java/org/onap/vid/scheduler') diff --git a/vid-app-common/src/main/java/org/onap/vid/scheduler/SchedulerProperties.java b/vid-app-common/src/main/java/org/onap/vid/scheduler/SchedulerProperties.java index 8f0b6b0e..4b45f4b4 100644 --- a/vid-app-common/src/main/java/org/onap/vid/scheduler/SchedulerProperties.java +++ b/vid-app-common/src/main/java/org/onap/vid/scheduler/SchedulerProperties.java @@ -15,5 +15,7 @@ public class SchedulerProperties extends SystemProperties { public static final String SCHEDULER_DELETE_SCHEDULE = "scheduler.delete.schedule"; + public static final String SCHEDULER_BASIC_AUTH = "scheduler.basic.auth"; + } diff --git a/vid-app-common/src/main/java/org/onap/vid/scheduler/SchedulerRestInterface.java b/vid-app-common/src/main/java/org/onap/vid/scheduler/SchedulerRestInterface.java index 77fc5c2c..abd03f3e 100644 --- a/vid-app-common/src/main/java/org/onap/vid/scheduler/SchedulerRestInterface.java +++ b/vid-app-common/src/main/java/org/onap/vid/scheduler/SchedulerRestInterface.java @@ -1,43 +1,42 @@ package org.onap.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 com.att.eelf.configuration.EELFLogger; import org.apache.commons.codec.binary.Base64; import org.eclipse.jetty.util.security.Password; -import org.json.simple.JSONObject; +import org.onap.vid.aai.util.HttpClientMode; +import org.onap.vid.aai.util.HttpsAuthClient; +import org.onap.vid.client.HttpBasicClient; +import org.onap.vid.exceptions.GenericUncheckedException; +import org.onap.vid.utils.Logging; import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate; import org.onap.portalsdk.core.util.SystemProperties; -import org.onap.vid.client.HttpBasicClient; -import org.onap.vid.client.HttpsBasicClient; -import org.onap.vid.scheduler.SchedulerProperties; -import org.onap.vid.scheduler.RestObjects.RestObject; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpMethod; import org.springframework.stereotype.Service; -import static org.onap.vid.utils.Logging.getHttpServletRequest; -import static org.onap.vid.utils.Logging.requestIdHeaderKey; +import javax.ws.rs.client.Client; +import javax.ws.rs.core.MultivaluedHashMap; +import javax.ws.rs.core.Response; +import java.util.Collections; + +import static org.onap.vid.utils.Logging.REQUEST_ID_HEADER_KEY; @Service public class SchedulerRestInterface implements SchedulerRestInterfaceIfc { - private static Client client = null; - + private Client client = null; + + @Autowired + HttpsAuthClient httpsAuthClient; + private MultivaluedHashMap commonHeaders; /** The logger. */ - static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SchedulerRestInterface.class); - - public SchedulerRestInterface() { - super(); - } + private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SchedulerRestInterface.class); + final private static EELFLogger outgoingRequestsLogger = Logging.getRequestsLogger("scheduler"); - public void initRestClient() - { - System.out.println( "\t <== Starting to initialize rest client "); + public void initRestClient() { + logger.info("Starting to initialize rest client "); final String username; final String password; @@ -68,42 +67,36 @@ public class SchedulerRestInterface implements SchedulerRestInterfaceIfc { String authStringEnc = new String(authEncBytes); commonHeaders = new MultivaluedHashMap (); - commonHeaders.put("Authorization", Collections.singletonList((Object) ("Basic " + authStringEnc))); + commonHeaders.put("Authorization", Collections.singletonList("Basic " + authStringEnc)); try { if ( !username.isEmpty() ) { - - client = HttpsBasicClient.getClient(); + client = httpsAuthClient.getClient(HttpClientMode.WITHOUT_KEYSTORE); } else { client = HttpBasicClient.getClient(); } } catch (Exception e) { - System.out.println( " <== Unable to initialize rest client "); + logger.error(" <== Unable to initialize rest client ", e); } - - System.out.println( "\t<== Client Initialized \n"); + + logger.info("\t<== Client Initialized \n"); } - @SuppressWarnings("unchecked") - public void Get (T t, String sourceId, String path, org.onap.vid.scheduler.RestObject restObject ) throws Exception { + public void Get (T t, String sourceId, String path, org.onap.vid.scheduler.RestObject restObject ) { String methodName = "Get"; String url = SystemProperties.getProperty(SchedulerProperties.SCHEDULER_SERVER_URL_VAL) + path; - - - System.out.println( "<== URL FOR GET : " + url + "\n"); - - initRestClient(); - + initRestClient(); + Logging.logRequest(outgoingRequestsLogger, HttpMethod.GET, url); final Response cres = client.target(url) .request() .accept("application/json") .headers(commonHeaders) - .header(requestIdHeaderKey, getHttpServletRequest().getHeader(requestIdHeaderKey)) + .header(REQUEST_ID_HEADER_KEY, Logging.extractOrGenerateRequestId()) .get(); - + Logging.logResponse(outgoingRequestsLogger, HttpMethod.GET, url, cres); int status = cres.getStatus(); restObject.setStatusCode (status); @@ -112,100 +105,33 @@ public class SchedulerRestInterface implements SchedulerRestInterfaceIfc { restObject.set(t); } else { - throw new Exception(methodName + " with status="+ status + ", url= " + url ); + throw new GenericUncheckedException(methodName + " with status="+ status + ", url= " + url ); } - return; } - - @SuppressWarnings("unchecked") - public void Post(T t, JSONObject requestDetails, String path, RestObject 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(requestIdHeaderKey, getHttpServletRequest().getHeader(requestIdHeaderKey)) - .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 void Delete(T t, String sourceID, String path, org.onap.vid.scheduler.RestObject 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) - .header(requestIdHeaderKey, getHttpServletRequest().getHeader(requestIdHeaderKey)) - //.entity(r) - .delete(); - // .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 void Delete(T t, String sourceID, String path, org.onap.vid.scheduler.RestObject restObject) { + + initRestClient(); + + String url = SystemProperties.getProperty(SchedulerProperties.SCHEDULER_SERVER_URL_VAL) + path; + Logging.logRequest(outgoingRequestsLogger, HttpMethod.DELETE, url); + Response cres = client.target(url) + .request() + .accept("application/json") + .headers(commonHeaders) + .header(REQUEST_ID_HEADER_KEY, Logging.extractOrGenerateRequestId()) + .delete(); + Logging.logResponse(outgoingRequestsLogger, HttpMethod.DELETE, url, cres); + + int status = cres.getStatus(); + restObject.setStatusCode(status); + + t = (T) cres.readEntity(t.getClass()); + restObject.set(t); + } - + public T getInstance(Class clazz) throws IllegalAccessException, InstantiationException { return clazz.newInstance(); diff --git a/vid-app-common/src/main/java/org/onap/vid/scheduler/SchedulerRestInterfaceFactory.java b/vid-app-common/src/main/java/org/onap/vid/scheduler/SchedulerRestInterfaceFactory.java deleted file mode 100644 index 1c3bea9f..00000000 --- a/vid-app-common/src/main/java/org/onap/vid/scheduler/SchedulerRestInterfaceFactory.java +++ /dev/null @@ -1,15 +0,0 @@ - -package org.onap.vid.scheduler; - -public class SchedulerRestInterfaceFactory { - - - public static SchedulerRestInterfaceIfc getInstance () { - SchedulerRestInterfaceIfc obj = null; - - obj = new SchedulerRestInterface(); - - return ( obj ); - } - -} diff --git a/vid-app-common/src/main/java/org/onap/vid/scheduler/SchedulerRestInterfaceIfc.java b/vid-app-common/src/main/java/org/onap/vid/scheduler/SchedulerRestInterfaceIfc.java index 7be8480f..d39e938b 100644 --- a/vid-app-common/src/main/java/org/onap/vid/scheduler/SchedulerRestInterfaceIfc.java +++ b/vid-app-common/src/main/java/org/onap/vid/scheduler/SchedulerRestInterfaceIfc.java @@ -1,23 +1,18 @@ package org.onap.vid.scheduler; -import org.json.simple.JSONObject; -import org.onap.vid.scheduler.RestObjects.RestObject; import org.springframework.stereotype.Service; @Service public interface SchedulerRestInterfaceIfc { - public void initRestClient(); + void initRestClient(); - public void Get (T t, String sourceId, String path, org.onap.vid.scheduler.RestObject restObject ) throws Exception; + void Get(T t, String sourceId, String path, org.onap.vid.scheduler.RestObject restObject); - public void Delete(T t, String sourceID, String path, org.onap.vid.scheduler.RestObject restObject) + void Delete(T t, String sourceID, String path, org.onap.vid.scheduler.RestObject restObject) throws Exception; - public void Post(T t, JSONObject r, String path, RestObject restObject) throws Exception; - - public void logRequest(JSONObject requestDetails); } -- cgit 1.2.3-korg