diff options
Diffstat (limited to 'vid-app-common/src/main/java/org/onap/vid/scheduler/SchedulerRestInterface.java')
-rw-r--r-- | vid-app-common/src/main/java/org/onap/vid/scheduler/SchedulerRestInterface.java | 186 |
1 files changed, 56 insertions, 130 deletions
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<String, Object> 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<String, Object> (); - 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 <T> void Get (T t, String sourceId, String path, org.onap.vid.scheduler.RestObject<T> restObject ) throws Exception { + public <T> void Get (T t, String sourceId, String path, org.onap.vid.scheduler.RestObject<T> 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 <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(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 <T> void Delete(T t, String sourceID, String path, org.onap.vid.scheduler.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) - .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 <T> void Delete(T t, String sourceID, String path, org.onap.vid.scheduler.RestObject<T> 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> T getInstance(Class<T> clazz) throws IllegalAccessException, InstantiationException { return clazz.newInstance(); |