X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=ecomp-portal-BE-common%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fportalapp%2Fportal%2Fscheduler%2FSchedulerRestInterface.java;h=0f6f3cea862367dcd990345aeab281c624ab595a;hb=0ee99992f35411273073c19e22367b641869d651;hp=1886b8b5d9d09e5e248cb57e4d8459fde01f2abc;hpb=9d6c52ce215710a86f73fc5ea2ea4b95aa44d020;p=portal.git diff --git a/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/scheduler/SchedulerRestInterface.java b/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/scheduler/SchedulerRestInterface.java index 1886b8b5..0f6f3cea 100644 --- a/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/scheduler/SchedulerRestInterface.java +++ b/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/scheduler/SchedulerRestInterface.java @@ -37,76 +37,60 @@ */ package org.onap.portalapp.portal.scheduler; -import java.lang.reflect.Type; import java.util.Collections; import java.util.Date; import javax.security.auth.login.CredentialException; -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 org.apache.commons.codec.binary.Base64; import org.apache.commons.lang.StringUtils; +import org.apache.cxf.jaxrs.client.WebClient; import org.apache.cxf.jaxrs.impl.ResponseImpl; import org.eclipse.jetty.util.security.Password; import org.json.simple.JSONObject; import org.onap.portalapp.portal.logging.format.EPAppMessagesEnum; import org.onap.portalapp.portal.logging.logic.EPLogUtil; -import org.onap.portalapp.portal.scheduler.client.HttpBasicClient; -import org.onap.portalapp.portal.scheduler.client.HttpsBasicClient; import org.onap.portalapp.portal.scheduler.restobjects.RestObject; import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Service; import org.springframework.web.client.HttpClientErrorException; -import com.fasterxml.jackson.databind.ObjectMapper; import com.google.gson.Gson; import com.google.gson.GsonBuilder; -import com.google.gson.JsonDeserializationContext; import com.google.gson.JsonDeserializer; -import com.google.gson.JsonElement; -import com.google.gson.JsonParseException; +import lombok.NoArgsConstructor; + +@SuppressWarnings("MalformedFormatString") @Service +@NoArgsConstructor public class SchedulerRestInterface implements SchedulerRestInterfaceIfc { - + private static final String APPLICATION_JSON = "application/json"; private static final String PASSWORD_IS_EMPTY = "Password is Empty"; + private static final String HTTP_CLIENT_ERROR = " HttpClientErrorException: Exception For the POST ." + + " MethodName: %APPLICATION_JSON, Url: %APPLICATION_JSON"; - private static Client client = null; - - private MultivaluedHashMap commonHeaders; - - /** The logger. */ - static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SchedulerRestInterface.class); - - public SchedulerRestInterface() { - super(); - } - - Gson gson = null; + private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(SchedulerRestInterface.class); + private static WebClient client = null; + private static Gson gson = null; - private final ObjectMapper mapper = new ObjectMapper(); + private MultivaluedHashMap commonHeaders; - private void init() { + private static void init() { logger.debug(EELFLoggerDelegate.debugLogger, "initializing"); GsonBuilder builder = new GsonBuilder(); // Register an adapter to manage the date types as long values - builder.registerTypeAdapter(Date.class, new JsonDeserializer() { - public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) - throws JsonParseException { - return new Date(json.getAsJsonPrimitive().getAsLong()); - } - }); + builder.registerTypeAdapter(Date.class, + (JsonDeserializer) (json, typeOfT, context) -> new Date(json.getAsJsonPrimitive().getAsLong())); gson = builder.create(); } - public void initRestClient() { + public void initRestClient(String URI) { logger.debug(EELFLoggerDelegate.debugLogger, "Starting to initialize rest client"); init(); @@ -114,7 +98,6 @@ public class SchedulerRestInterface implements SchedulerRestInterfaceIfc { final String username; final String password; - String methodName = "initRestClient"; /* Setting user name based on properties */ String retrievedUsername = SchedulerProperties.getProperty(SchedulerProperties.SCHEDULER_USER_NAME_VAL); if (retrievedUsername.isEmpty()) { @@ -145,45 +128,54 @@ public class SchedulerRestInterface implements SchedulerRestInterfaceIfc { byte[] authEncBytes = Base64.encodeBase64(authString.getBytes()); String authStringEnc = new String(authEncBytes); - commonHeaders = new MultivaluedHashMap(); - commonHeaders.put("Authorization", Collections.singletonList((Object) ("Basic " + authStringEnc))); - - try { - if (!username.isEmpty()) { - - client = HttpBasicClient.getClient(); - } else { - - client = HttpsBasicClient.getClient(); - } - } catch (Exception e) { - logger.debug(EELFLoggerDelegate.debugLogger, "Unable to initialize rest client",e.getMessage()); - - } + commonHeaders = new MultivaluedHashMap<>(); + commonHeaders.put("Authorization", Collections.singletonList(("Basic " + authStringEnc))); + + // try { + // if (!username.isEmpty()) { + // + // client = HttpBasicClient.getClient(); + // } else { + // + // client = HttpsBasicClient.getClient(); + // } + // } catch (Exception e) { + // logger.debug(EELFLoggerDelegate.debugLogger, "Unable to initialize rest client",e.getMessage()); + // + // } + + client = WebClient.create(URI); + client.type(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON); + //client.path(""); + client.headers(commonHeaders); + logger.debug(EELFLoggerDelegate.debugLogger, "Client Initialized"); - + } @SuppressWarnings("unchecked") public void Get(T t, String sourceId, String path, - org.onap.portalapp.portal.scheduler.restobjects.RestObject restObject) throws Exception { + org.onap.portalapp.portal.scheduler.restobjects.RestObject restObject) { String methodName = "Get"; String url = SchedulerProperties.getProperty(SchedulerProperties.SCHEDULER_SERVER_URL_VAL) + path; logger.debug(EELFLoggerDelegate.debugLogger, "URL FOR GET : ", url); try { - initRestClient(); - - final Response cres = client.target(url).request().accept("application/json").headers(commonHeaders).get(); + initRestClient(url); + + + //final Response cres = client.target(url).request().accept(APPLICATION_JSON).headers(commonHeaders).get(); + final ResponseImpl cres = (ResponseImpl)client.get(); + logger.debug(EELFLoggerDelegate.debugLogger, "The implemenation class of Response : ", cres.getClass().getName()); int status = cres.getStatus(); restObject.setStatusCode(status); - if (cres != null && cres.getEntity() != null) { + if (cres.getEntity() != null) { try { - String str = ((ResponseImpl) cres).readEntity(String.class); - if (t.getClass().getName().equals(String.class.getName())) { + String str = (cres).readEntity(String.class); + if (t.getClass().isAssignableFrom(String.class)) { t = (T) str; } else { @@ -195,23 +187,21 @@ public class SchedulerRestInterface implements SchedulerRestInterfaceIfc { } } else { t = null; - restObject.set(t); + restObject.set(null); } - // t = (T) cres.readEntity(t.getClass()); - - if (t.equals("")) { + if ("".equals(t)) { restObject.set(null); } else { restObject.set(t); } } catch (HttpClientErrorException e) { String message = String.format( - " HttpClientErrorException: Exception For the POST . MethodName: %s, Url: %s", methodName, url); + HTTP_CLIENT_ERROR, methodName, url); logger.error(EELFLoggerDelegate.errorLogger, message, e); EPLogUtil.schedulerAccessAlarm(logger, e.getStatusCode().value()); } catch (Exception e) { - String message = String.format("Exception For the POST . MethodName: %s, Url: %s", methodName, url); + String message = String.format("Exception For the POST . MethodName: %APPLICATION_JSON, Url: %APPLICATION_JSON", methodName, url); logger.error(EELFLoggerDelegate.errorLogger, message, e); EPLogUtil.schedulerAccessAlarm(logger, HttpStatus.INTERNAL_SERVER_ERROR.value()); @@ -223,7 +213,7 @@ public class SchedulerRestInterface implements SchedulerRestInterfaceIfc { } @SuppressWarnings("unchecked") - public void Post(T t, JSONObject requestDetails, String path, RestObject restObject) throws Exception { + public void Post(T t, JSONObject requestDetails, String path, RestObject restObject) { String methodName = "Post"; String url = SchedulerProperties.getProperty(SchedulerProperties.SCHEDULER_SERVER_URL_VAL) + path; @@ -231,17 +221,16 @@ public class SchedulerRestInterface implements SchedulerRestInterfaceIfc { try { - initRestClient(); + initRestClient(url); // Change the content length - final Response cres = client.target(url).request().accept("application/json").headers(commonHeaders) - .post(Entity.entity(requestDetails, MediaType.APPLICATION_JSON)); + final ResponseImpl cres = (ResponseImpl)client.post(requestDetails.toJSONString()); if (cres != null && cres.getEntity() != null) { try { - String str = ((ResponseImpl) cres).readEntity(String.class); - if (t.getClass().getName().equals(String.class.getName())) { + String str = (cres).readEntity(String.class); + if (t.getClass().isAssignableFrom(String.class)) { t = (T) str; } else { @@ -251,14 +240,12 @@ public class SchedulerRestInterface implements SchedulerRestInterfaceIfc { } catch (Exception e) { EPLogUtil.logEcompError(logger, EPAppMessagesEnum.BeInvalidJsonInput, e); } - // t = (T) cres.readEntity(t.getClass()); restObject.set(t); } else { - t = null; - restObject.set(t); + restObject.set(null); } - int status = cres.getStatus(); + int status = cres != null ? cres.getStatus() : 0; restObject.setStatusCode(status); if (status >= 200 && status <= 299) { @@ -266,19 +253,19 @@ public class SchedulerRestInterface implements SchedulerRestInterfaceIfc { logger.debug(EELFLoggerDelegate.debugLogger, message); } else { - String message = String.format(" FAILED with http status . MethodName: %s, Status: %s, Url: %s", + String message = String.format(" FAILED with http status . MethodName: %APPLICATION_JSON, Status: %APPLICATION_JSON, Url: %APPLICATION_JSON", methodName, status, url); logger.debug(EELFLoggerDelegate.debugLogger, message); } } catch (HttpClientErrorException e) { String message = String.format( - " HttpClientErrorException: Exception For the POST . MethodName: %s, Url: %s", methodName, url); + HTTP_CLIENT_ERROR, methodName, url); logger.error(EELFLoggerDelegate.errorLogger, message, e); EPLogUtil.schedulerAccessAlarm(logger, e.getStatusCode().value()); } catch (Exception e) { String message = String.format( - " HttpClientErrorException: Exception For the POST . MethodName: %s, Url: %s", methodName, url); + HTTP_CLIENT_ERROR, methodName, url); logger.error(EELFLoggerDelegate.errorLogger, message, e); EPLogUtil.schedulerAccessAlarm(logger, HttpStatus.INTERNAL_SERVER_ERROR.value()); throw e; @@ -287,46 +274,8 @@ public class SchedulerRestInterface implements SchedulerRestInterfaceIfc { @Override public void logRequest(JSONObject requestDetails) { + throw new UnsupportedOperationException(); } - @SuppressWarnings("unchecked") - public void Delete(T t, JSONObject requestDetails, String sourceID, String path, RestObject restObject) { - - String methodName = "Delete"; - String url = ""; - Response cres = null; - - try { - initRestClient(); - - url = SchedulerProperties.getProperty(SchedulerProperties.SCHEDULER_SERVER_URL_VAL) + path; - - cres = client.target(url).request().accept("application/json").headers(commonHeaders) - // .entity(r) - .build("DELETE", Entity.entity(requestDetails, MediaType.APPLICATION_JSON)).invoke(); - // .method("DELETE", Entity.entity(r, MediaType.APPLICATION_JSON)); - // .delete(Entity.entity(r, MediaType.APPLICATION_JSON)); - - int status = cres.getStatus(); - restObject.setStatusCode(status); - if (cres.getEntity() != null) { - t = (T) cres.readEntity(t.getClass()); - restObject.set(t); - } - - } catch (HttpClientErrorException e) { - logger.error(EELFLoggerDelegate.errorLogger, " HttpClientErrorException:Exception For the Delete", - methodName, url, e); - EPLogUtil.schedulerAccessAlarm(logger, e.getStatusCode().value()); - } catch (Exception e) { - logger.error(EELFLoggerDelegate.errorLogger, "Exception For the Delete", methodName, url, e); - EPLogUtil.schedulerAccessAlarm(logger, HttpStatus.INTERNAL_SERVER_ERROR.value()); - throw e; - } - } - - public T getInstance(Class clazz) throws IllegalAccessException, InstantiationException { - return clazz.newInstance(); - } }