diff options
author | Sonsino, Ofir (os0695) <os0695@intl.att.com> | 2018-07-10 14:20:54 +0300 |
---|---|---|
committer | Sonsino, Ofir (os0695) <os0695@intl.att.com> | 2018-07-10 14:20:54 +0300 |
commit | c72d565bb58226b20625b2bce5f0019046bee649 (patch) | |
tree | 8658e49595705b02e47ddc14afa20d6bb7123547 /vid-app-common/src/main/java/org/onap/vid/mso/RestMsoImplementation.java | |
parent | ef8a6b47847012fd59ea20da21d8d3d7c4a301ed (diff) |
Merge 1806 code of vid-common
Change-Id: I75d52abed4a24dfe3827d79edc4a2938726aa87a
Issue-ID: VID-208
Signed-off-by: Sonsino, Ofir (os0695) <os0695@intl.att.com>
Diffstat (limited to 'vid-app-common/src/main/java/org/onap/vid/mso/RestMsoImplementation.java')
-rw-r--r-- | vid-app-common/src/main/java/org/onap/vid/mso/RestMsoImplementation.java | 129 |
1 files changed, 69 insertions, 60 deletions
diff --git a/vid-app-common/src/main/java/org/onap/vid/mso/RestMsoImplementation.java b/vid-app-common/src/main/java/org/onap/vid/mso/RestMsoImplementation.java index 7924a7d7..0494facd 100644 --- a/vid-app-common/src/main/java/org/onap/vid/mso/RestMsoImplementation.java +++ b/vid-app-common/src/main/java/org/onap/vid/mso/RestMsoImplementation.java @@ -4,17 +4,17 @@ import com.att.eelf.configuration.EELFLogger; import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.commons.codec.binary.Base64; import org.eclipse.jetty.util.security.Password; -import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate; -import org.onap.portalsdk.core.util.SystemProperties; +import org.onap.vid.aai.util.HttpClientMode; +import org.onap.vid.aai.util.HttpsAuthClient; import org.onap.vid.client.HttpBasicClient; -import org.onap.vid.client.HttpsBasicClient; -import org.onap.vid.mso.rest.RequestDetails; +import org.onap.vid.exceptions.GenericUncheckedException; import org.onap.vid.mso.rest.RestInterface; import org.onap.vid.utils.Logging; +import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate; +import org.onap.portalsdk.core.util.SystemProperties; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpMethod; -import javax.servlet.http.HttpServletRequest; import javax.ws.rs.client.Client; import javax.ws.rs.client.Entity; import javax.ws.rs.client.Invocation; @@ -25,6 +25,7 @@ import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Collections; import java.util.Date; +import java.util.UUID; import static org.onap.vid.utils.Logging.*; @@ -33,22 +34,33 @@ import static org.onap.vid.utils.Logging.*; */ public class RestMsoImplementation implements RestInterface { + public static final String START_LOG = " start"; + public static final String APPLICATION_JSON = "application/json"; + public static final String WITH_STATUS = " with status="; + public static final String URL_LOG = ", url="; + public static final String NO_RESPONSE_ENTITY_LOG = " No response entity, this is probably ok, e="; + public static final String WITH_URL_LOG = " with url="; + public static final String EXCEPTION_LOG = ", Exception: "; + public static final String REST_API_SUCCESSFULL_LOG = " REST api was successfull!"; + public static final String REST_API_POST_WAS_SUCCESSFUL_LOG = " REST api POST was successful!"; /** * The logger. */ EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RestMsoImplementation.class); - final private EELFLogger outgoingRequestsLogger = Logging.getRequestsLogger("mso"); + private final EELFLogger outgoingRequestsLogger = Logging.getRequestsLogger("mso"); /** * The Constant dateFormat. */ - final static DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSSS"); + static final DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSSS"); /** The client. */ - private static Client client = null; + private Client client = null; + + @Autowired + HttpsAuthClient httpsAuthClient; /** The common headers. */ - //private MultivaluedHashMap<String, Object> commonHeaders; /** * Instantiates a new mso rest interface. */ @@ -71,30 +83,28 @@ public class RestMsoImplementation implements RestInterface { MultivaluedHashMap<String, Object> commonHeaders = new MultivaluedHashMap(); commonHeaders.put("Authorization", Collections.singletonList(("Basic " + authStringEnc))); - commonHeaders.put(requestIdHeaderKey, Collections.singletonList(getHttpServletRequest().getHeader(requestIdHeaderKey))); - //Pass calling application identifier to SO + //Pass calling application identifier to SO commonHeaders.put("X-FromAppId", Collections.singletonList(SystemProperties.getProperty(SystemProperties.APP_DISPLAY_NAME))); + try { + commonHeaders.put(REQUEST_ID_HEADER_KEY, Collections.singletonList(Logging.extractOrGenerateRequestId())); + } + catch (IllegalStateException e){ + //in async jobs we don't have any HttpServletRequest + commonHeaders.put(REQUEST_ID_HEADER_KEY, Collections.singletonList(UUID.randomUUID().toString())); + } + - boolean use_ssl = true; + boolean useSsl = true; if ( (mso_url != null) && ( !(mso_url.isEmpty()) ) ) { - if ( mso_url.startsWith("https")) { - use_ssl = true; - } - else { - use_ssl = false; - } + useSsl = mso_url.startsWith("https"); } if (client == null) { try { - if ( use_ssl ) { - //logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + methodname + " getting HttpsBasicClient with username=" + username - // + " password=" + password); - client = HttpsBasicClient.getClient(); + if ( useSsl ) { + client = httpsAuthClient.getClient(HttpClientMode.WITHOUT_KEYSTORE); } else { - //logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + methodname + " getting HttpsBasicClient with username=" + username - // + " password=" + password); client = HttpBasicClient.getClient(); } } catch (Exception e) { @@ -105,10 +115,10 @@ public class RestMsoImplementation implements RestInterface { return commonHeaders; } - public <T> void Get (T t, String sourceId, String path, RestObject<T> restObject ) throws Exception { + public <T> void Get (T t, String sourceId, String path, RestObject<T> restObject ) { String methodName = "Get"; - logger.debug(EELFLoggerDelegate.debugLogger, methodName + " start"); + logger.debug(EELFLoggerDelegate.debugLogger, methodName + START_LOG); String url=""; restObject.set(t); @@ -119,7 +129,7 @@ public class RestMsoImplementation implements RestInterface { Logging.logRequest(outgoingRequestsLogger, HttpMethod.GET, url); final Response cres = client.target(url) .request() - .accept("application/json") + .accept(APPLICATION_JSON) .headers(commonHeaders) .get(); Logging.logResponse(outgoingRequestsLogger, HttpMethod.GET, url, cres); @@ -129,10 +139,10 @@ public class RestMsoImplementation implements RestInterface { if (status == 200 || status == 202) { t = (T) cres.readEntity(t.getClass()); restObject.set(t); - logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + methodName + " REST api was successfull!"); + logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + methodName + REST_API_SUCCESSFULL_LOG); } else { - throw new Exception(methodName + " with status="+ status + ", url= " + url ); + throw new GenericUncheckedException(methodName + WITH_STATUS + status + ", url= " + url ); } logger.debug(EELFLoggerDelegate.debugLogger,methodName + " received status=" + status ); @@ -140,7 +150,7 @@ public class RestMsoImplementation implements RestInterface { return; } - public <T> RestObject<T> GetForObject(String sourceID, String path, Class<T> clazz) throws Exception { + public <T> RestObject<T> GetForObject(String sourceID, String path, Class<T> clazz) { final String methodName = getMethodName(); logger.debug(EELFLoggerDelegate.debugLogger, "start {}->{}({}, {}, {})", getMethodCallerName(), methodName, sourceID, path, clazz); @@ -151,7 +161,7 @@ public class RestMsoImplementation implements RestInterface { Logging.logRequest(outgoingRequestsLogger, HttpMethod.GET, url); final Response cres = client.target(url) .request() - .accept("application/json") + .accept(APPLICATION_JSON) .headers(commonHeaders) .get(); Logging.logResponse(outgoingRequestsLogger, HttpMethod.GET, url, cres); @@ -159,9 +169,9 @@ public class RestMsoImplementation implements RestInterface { int status = cres.getStatus(); if (status == 200 || status == 202) { - logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + methodName + " REST api was successfull!"); + logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + methodName + REST_API_SUCCESSFULL_LOG); } else { - logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + " with status="+status+", url="+url); + logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + WITH_STATUS +status+ URL_LOG +url); } logger.debug(EELFLoggerDelegate.debugLogger,methodName + " received status=" + status ); @@ -176,7 +186,7 @@ public class RestMsoImplementation implements RestInterface { String url=""; Response cres = null; - logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + " start"); + logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + START_LOG); try { MultivaluedHashMap<String, Object> commonHeaders = initMsoClient(); @@ -185,7 +195,8 @@ public class RestMsoImplementation implements RestInterface { Logging.logRequest(outgoingRequestsLogger, HttpMethod.DELETE, url, r); cres = client.target(url) .request() - .accept("application/json") + + .accept(APPLICATION_JSON) .headers(commonHeaders) //.entity(r) .build("DELETE", Entity.entity(r, MediaType.APPLICATION_JSON)).invoke(); @@ -212,30 +223,30 @@ public class RestMsoImplementation implements RestInterface { restObject.set(t); } catch ( Exception e ) { - logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + " No response entity, this is probably ok, e=" + logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + NO_RESPONSE_ENTITY_LOG + e.getMessage()); } } catch (Exception e) { - logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + " with url="+url+ ", Exception: " + e.toString()); + logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + WITH_URL_LOG +url+ EXCEPTION_LOG + e.toString()); throw e; } } - public <T> RestObject<T> PostForObject(Object requestDetails, String sourceID, String path, Class<T> clazz) throws RuntimeException { + public <T> RestObject<T> PostForObject(Object requestDetails, String sourceID, String path, Class<T> clazz) { logger.debug(EELFLoggerDelegate.debugLogger, "start {}->{}({}, {}, {}, {})", getMethodCallerName(), getMethodName(), requestDetails, sourceID, path, clazz); RestObject<T> restObject = new RestObject<>(); - Post(clazz, requestDetails, sourceID, path, restObject); + Post(clazz, requestDetails, path, restObject); return restObject; } @Override - public <T> void Post(T t, Object r, String sourceID, String path, RestObject<T> restObject) throws RuntimeException { + public <T> void Post(T t, Object r, String sourceID, String path, RestObject<T> restObject) { logger.debug(EELFLoggerDelegate.debugLogger, "start {}->{}({}, {}, {}, {})", getMethodCallerName(), getMethodName(), t.getClass(), r, sourceID, path); - Post(t.getClass(), r, sourceID, path, restObject); + Post(t.getClass(), r, path, restObject); } public Invocation.Builder prepareClient(String path, String methodName) { @@ -246,13 +257,13 @@ public class RestMsoImplementation implements RestInterface { // Change the content length return client.target(url) .request() - .accept("application/json") + .accept(APPLICATION_JSON) .headers(commonHeaders); } - public <T> void Post(Class<?> tClass, Object requestDetails, String sourceID, String path, RestObject<T> restObject) throws RuntimeException { + public <T> void Post(Class<?> tClass, Object requestDetails, String path, RestObject<T> restObject) { String methodName = "Post"; String url=""; @@ -265,11 +276,10 @@ public class RestMsoImplementation implements RestInterface { // Change the content length final Response cres = client.target(url) .request() - .accept("application/json") + .accept(APPLICATION_JSON) .headers(commonHeaders) .post(Entity.entity(requestDetails, MediaType.APPLICATION_JSON)); Logging.logResponse(outgoingRequestsLogger, HttpMethod.POST, url, cres); - final RestObject<T> cresToRestObject = cresToRestObject(cres, tClass); restObject.set(cresToRestObject.get()); restObject.setStatusCode(cresToRestObject.getStatusCode()); @@ -279,16 +289,16 @@ public class RestMsoImplementation implements RestInterface { restObject.setStatusCode (status); if ( status >= 200 && status <= 299 ) { - logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== " + methodName + " REST api POST was successful!"); - logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + " REST api POST was successful!"); + logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== " + methodName + REST_API_POST_WAS_SUCCESSFUL_LOG); + logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + REST_API_POST_WAS_SUCCESSFUL_LOG); } else { - logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + " with status="+status+", url="+url); + logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + WITH_STATUS +status+ URL_LOG +url); } } catch (Exception e) { - logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + " with url="+url+ ", Exception: " + e.toString()); + logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + WITH_URL_LOG +url+ EXCEPTION_LOG + e.toString()); throw e; } @@ -303,16 +313,16 @@ public class RestMsoImplementation implements RestInterface { try { cres.bufferEntity(); rawEntity = cres.readEntity(String.class); + restObject.setRaw(rawEntity); T t = (T) new ObjectMapper().readValue(rawEntity, tClass); restObject.set(t); } catch ( Exception e ) { try { - restObject.setRaw(rawEntity); logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + getMethodCallerName() + " Error reading response entity as " + tClass + ": , e=" + e.getMessage() + ", Entity=" + rawEntity); } catch (Exception e2) { - logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + getMethodCallerName() + " No response entity, this is probably ok, e=" + logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + getMethodCallerName() + NO_RESPONSE_ENTITY_LOG + e.getMessage()); } } @@ -325,14 +335,13 @@ public class RestMsoImplementation implements RestInterface { } @Override - public <T> void Put(T t, org.onap.vid.changeManagement.RequestDetailsWrapper r, String sourceID, String path, RestObject<T> restObject) throws Exception { + public <T> void Put(T t, org.onap.vid.changeManagement.RequestDetailsWrapper r, String sourceID, String path, RestObject<T> restObject) { String methodName = "Put"; String url=""; - logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + " start"); + logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + START_LOG); -// logRequest (r); try { MultivaluedHashMap<String, Object> commonHeaders = initMsoClient(); @@ -342,7 +351,7 @@ public class RestMsoImplementation implements RestInterface { // Change the content length final Response cres = client.target(url) .request() - .accept("application/json") + .accept(APPLICATION_JSON) .headers(commonHeaders) //.header("content-length", 201) //.header("X-FromAppId", sourceID) @@ -355,7 +364,7 @@ public class RestMsoImplementation implements RestInterface { restObject.set(t); } catch ( Exception e ) { - logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + " No response entity, this is probably ok, e=" + logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + NO_RESPONSE_ENTITY_LOG + e.getMessage()); } @@ -363,16 +372,16 @@ public class RestMsoImplementation implements RestInterface { restObject.setStatusCode (status); if ( status >= 200 && status <= 299 ) { - logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== " + methodName + " REST api POST was successful!"); - logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + " REST api POST was successful!"); + logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== " + methodName + REST_API_POST_WAS_SUCCESSFUL_LOG); + logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + REST_API_POST_WAS_SUCCESSFUL_LOG); } else { - logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + " with status="+status+", url="+url); + logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + WITH_STATUS +status+ URL_LOG +url); } } catch (Exception e) { - logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + " with url="+url+ ", Exception: " + e.toString()); + logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + WITH_URL_LOG +url+ EXCEPTION_LOG + e.toString()); throw e; } |