From 9c9f8c9643dcd6ccda3b765111a1b278267393c3 Mon Sep 17 00:00:00 2001 From: Parshad Patel Date: Mon, 20 Apr 2020 11:36:34 +0900 Subject: [PATCH] Fix NPE & other issues Fix A "NullPointerException" could be thrown Use "isAssignableFrom" instead Either log or rethrow this exception Issue-ID: PORTAL-562 Change-Id: Iabee6308bdca9c679323f8f496532d9431227772 Signed-off-by: Parshad Patel --- .../portal/scheduler/SchedulerRestInterface.java | 7 ++--- .../portalapp/portal/scheduler/SchedulerUtil.java | 34 ++++++++++----------- .../portal/scheduleraux/SchedulerAuxRestInt.java | 35 ++++++++++------------ .../portal/service/EPRoleServiceImpl.java | 9 +++--- 4 files changed, 39 insertions(+), 46 deletions(-) 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 93310a97..afcb7abb 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 @@ -152,9 +152,6 @@ public class SchedulerRestInterface implements SchedulerRestInterfaceIfc { logger.debug(EELFLoggerDelegate.debugLogger, "Client Initialized"); - - - } @SuppressWarnings("unchecked") @@ -179,7 +176,7 @@ public class SchedulerRestInterface implements SchedulerRestInterfaceIfc { if (cres.getEntity() != null) { try { String str = (cres).readEntity(String.class); - if (t.getClass().getName().equals(String.class.getName())) { + if (t.getClass().isAssignableFrom(String.class)) { t = (T) str; } else { @@ -234,7 +231,7 @@ public class SchedulerRestInterface implements SchedulerRestInterfaceIfc { try { String str = (cres).readEntity(String.class); - if (t.getClass().getName().equals(String.class.getName())) { + if (t.getClass().isAssignableFrom(String.class)) { t = (T) str; } else { diff --git a/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/scheduler/SchedulerUtil.java b/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/scheduler/SchedulerUtil.java index c1ca8735..675cfe14 100644 --- a/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/scheduler/SchedulerUtil.java +++ b/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/scheduler/SchedulerUtil.java @@ -57,17 +57,17 @@ public class SchedulerUtil { public static GetTimeSlotsWrapper getTimeSlotsWrapResponse (GetTimeSlotsRestObject rs) { - String resp_str = ""; + String respStr = ""; int status = 0; if ( rs != null ) { - resp_str = rs.get(); + respStr = rs.get(); status = rs.getStatusCode(); } GetTimeSlotsWrapper w = new GetTimeSlotsWrapper(); - w.setEntity(resp_str); + w.setEntity(respStr); w.setStatus (status); return (w); @@ -75,19 +75,19 @@ public class SchedulerUtil { public static PostSubmitVnfChangeTimeSlotsWrapper postSubmitNewVnfWrapResponse (PostSubmitVnfChangeRestObject rs) { - String resp_str = ""; + String respStr = ""; int status = 0; String uuid = ""; if ( rs != null ) { - resp_str = rs.get(); + respStr = rs.get(); status = rs.getStatusCode(); uuid = rs.getUUID(); } PostSubmitVnfChangeTimeSlotsWrapper w = new PostSubmitVnfChangeTimeSlotsWrapper(); - w.setEntity(resp_str); + w.setEntity(respStr); w.setStatus (status); w.setUuid(uuid); @@ -96,19 +96,19 @@ public class SchedulerUtil { public static PostCreateNewVnfWrapper postCreateNewVnfWrapResponse (PostCreateNewVnfRestObject rs) { - String resp_str = ""; + String respStr = ""; int status = 0; String uuid = ""; if ( rs != null ) { - resp_str = rs.get(); + respStr = rs.get(); status = rs.getStatusCode(); uuid = rs.getUUID(); } PostCreateNewVnfWrapper w = new PostCreateNewVnfWrapper(); - w.setEntity(resp_str); + w.setEntity(respStr); w.setStatus (status); w.setUuid(uuid); @@ -121,14 +121,14 @@ public class SchedulerUtil { ObjectMapper mapper = new ObjectMapper(); String r_json_str = ""; if ( t != null ) { - try { - r_json_str = mapper.writeValueAsString(t); - } - catch ( com.fasterxml.jackson.core.JsonProcessingException j ) { - logger.debug(EELFLoggerDelegate.debugLogger, - DateUtil.getDateFormat().format(new Date()) + "<== " + methodName + " Unable " + "to " - + "parse object as json"); - } + try { + r_json_str = mapper.writeValueAsString(t); + } catch (com.fasterxml.jackson.core.JsonProcessingException j) { + logger.debug( + EELFLoggerDelegate.debugLogger, DateUtil.getDateFormat().format(new Date()) + + "<== " + methodName + " Unable " + "to " + "parse object as json", + j); + } } return (r_json_str); } diff --git a/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/scheduleraux/SchedulerAuxRestInt.java b/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/scheduleraux/SchedulerAuxRestInt.java index 75919eee..f120c4f0 100644 --- a/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/scheduleraux/SchedulerAuxRestInt.java +++ b/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/scheduleraux/SchedulerAuxRestInt.java @@ -63,24 +63,21 @@ public class SchedulerAuxRestInt { * * @param r the r */ - public void logRequest ( RequestDetails r ) { - SimpleDateFormat dateFormat = DateUtil.getDateFormat(); - String methodName = "logRequest"; - ObjectMapper mapper = new ObjectMapper(); - String r_json_str = ""; - if ( r != null ) { - r_json_str = r.toString(); - try { - r_json_str = mapper.writeValueAsString(r); - } - catch ( com.fasterxml.jackson.core.JsonProcessingException j ) { - logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " " - + "Unable to " - + "parse request as json"); - } - } - logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + " Request=" - + "(" + - r_json_str + ")"); + public void logRequest(RequestDetails r) { + SimpleDateFormat dateFormat = DateUtil.getDateFormat(); + String methodName = "logRequest"; + ObjectMapper mapper = new ObjectMapper(); + String rJsonStr = ""; + if (r != null) { + rJsonStr = r.toString(); + try { + rJsonStr = mapper.writeValueAsString(r); + } catch (com.fasterxml.jackson.core.JsonProcessingException j) { + logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + + methodName + " " + "Unable to " + "parse request as json", j); + } + } + logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + + methodName + " Request=" + "(" + rJsonStr + ")"); } } diff --git a/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/service/EPRoleServiceImpl.java b/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/service/EPRoleServiceImpl.java index 5d38317a..4adf1fdb 100644 --- a/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/service/EPRoleServiceImpl.java +++ b/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/service/EPRoleServiceImpl.java @@ -62,6 +62,9 @@ import org.onap.portalsdk.core.service.DataAccessService; public class EPRoleServiceImpl implements EPRoleService { EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EPRoleServiceImpl.class); + private static final String GET_APP_ROLE_SQL_FORMAT = + "SELECT * FROM fn_role where APP_ID = %s AND APP_ROLE_ID = %s"; + @Autowired private DataAccessService dataAccessService; @@ -123,10 +126,6 @@ public class EPRoleServiceImpl implements EPRoleService { return (EPRole) getDataAccessService().getDomainObject(EPRole.class, id, null); } - // TODO: refactor - private static final String GET_APP_ROLE_SQL_FORMAT = - "SELECT * FROM fn_role where APP_ID = %s AND APP_ROLE_ID = %s"; - @SuppressWarnings("unchecked") public EPRole getRole(Long appId, Long appRoleid) { if (appId == null || appRoleid == null) { @@ -185,7 +184,7 @@ public class EPRoleServiceImpl implements EPRoleService { if (roles != null) { return roles.get(0); } - } else if (resultsCount == 1) { + } else if (roles!=null && resultsCount == 1) { return roles.get(0); } return null; -- 2.16.6