diff options
author | Dominik Mizyn <d.mizyn@samsung.com> | 2019-07-16 13:00:07 +0200 |
---|---|---|
committer | Dominik Mizyn <d.mizyn@samsung.com> | 2019-07-16 13:17:57 +0200 |
commit | 5e036e48759741ad72dd4878314ad9c016ac9ba6 (patch) | |
tree | 18f886ef1847c45768f77b123d80e82630055abb /ecomp-portal-BE-common | |
parent | b382121ad0475b9d730c81d47f93f0db8d50c0f0 (diff) |
EPUserUtils class fix
Sonar errors EPUserUtils class fix
Issue-ID: PORTAL-667
Change-Id: I7a8a39ac52ee2da5d2c2ab0016a8bbea0acb4d21
Signed-off-by: Dominik Mizyn <d.mizyn@samsung.com>
Diffstat (limited to 'ecomp-portal-BE-common')
3 files changed, 30 insertions, 50 deletions
diff --git a/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/service/EPRoleFunctionServiceImpl.java b/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/service/EPRoleFunctionServiceImpl.java index 4a3cf63c..c0881640 100644 --- a/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/service/EPRoleFunctionServiceImpl.java +++ b/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/portal/service/EPRoleFunctionServiceImpl.java @@ -86,7 +86,7 @@ public class EPRoleFunctionServiceImpl implements EPRoleFunctionService { .getAttribute(SystemProperties.getProperty(SystemProperties.ROLE_FUNCTIONS_ATTRIBUTE_NAME)); if (roleFunctions == null) { - HashMap roles = EPUserUtils.getRoles(request); + HashMap roles = (HashMap) EPUserUtils.getRoles(request); roleFunctions = new HashSet(); Iterator i = roles.keySet().iterator(); diff --git a/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/util/EPUserUtils.java b/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/util/EPUserUtils.java index 99a29116..80db8c86 100644 --- a/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/util/EPUserUtils.java +++ b/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/util/EPUserUtils.java @@ -2,7 +2,7 @@ * ============LICENSE_START========================================== * ONAP Portal * =================================================================== - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. * =================================================================== * * Unless otherwise specified, all software contained herein is licensed @@ -41,18 +41,16 @@ import java.util.ArrayList; import java.util.Enumeration; import java.util.HashMap; import java.util.HashSet; -import java.util.Iterator; import java.util.List; +import java.util.Map; import java.util.Set; import java.util.UUID; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; - import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; - import org.apache.commons.codec.DecoderException; import org.apache.commons.codec.binary.Hex; import org.onap.portalapp.portal.domain.EPRole; @@ -70,18 +68,14 @@ import org.onap.portalsdk.core.web.support.AppUtils; import org.springframework.beans.factory.annotation.Autowired; public class EPUserUtils { - - private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EPUserUtils.class); - - private final static Long ACCOUNT_ADMIN_ROLE_ID = 999L; - public static final String ALL_ROLE_FUNCTIONS = "allRoleFunctions"; - - // These decode values are based on HexDecoder + private static final String decodeValueOfForwardSlash = "2f"; private static final String decodeValueOfHyphen = "2d"; private static final String decodeValueOfAsterisk = "2a"; + private static final Long ACCOUNT_ADMIN_ROLE_ID = 999L; + private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EPUserUtils.class); private static DataAccessService dataAccessService; /** @@ -111,15 +105,13 @@ public class EPUserUtils { * Menu data * @param businessDirectMenuData * Menu data - * @param loginMethod_ignored - * How the user authenticated; ignored * @param ePRoleFunctionService * role function service * @throws DecoderException */ @SuppressWarnings("rawtypes") public static void setUserSession(HttpServletRequest request, EPUser user, Set applicationMenuData, - Set businessDirectMenuData, String loginMethod_ignored, EPRoleFunctionService ePRoleFunctionService) throws RoleFunctionException { + Set businessDirectMenuData, EPRoleFunctionService ePRoleFunctionService) throws RoleFunctionException { HttpSession session = request.getSession(true); // clear the current user session to avoid any conflicts @@ -136,9 +128,8 @@ public class EPUserUtils { session.setAttribute(SystemProperties.getProperty(SystemProperties.USER_NAME), user.getFullName()); ServletContext context = session.getServletContext(); - int licenseVerificationFlag = 3; try { - licenseVerificationFlag = (Integer) context.getAttribute("licenseVerification"); + context.getAttribute("licenseVerification"); } catch (Exception e) { logger.error(EELFLoggerDelegate.errorLogger, "setUserSession failed to get licenseVerification attribute", e); @@ -163,7 +154,7 @@ public class EPUserUtils { private static void setAllRoleFunctions(List<RoleFunction> allRoleFunctions, HttpSession session) throws RoleFunctionException { if (allRoleFunctions == null) return; - Set<String> roleFnSet = new HashSet<String>(); + Set<String> roleFnSet = new HashSet<>(); for (RoleFunction roleFn : allRoleFunctions){ roleFnSet.add(decodeFunctionCode(roleFn.getCode())); } @@ -221,8 +212,8 @@ public class EPUserUtils { * @return Map of role ID to role object */ @SuppressWarnings("rawtypes") - public static HashMap getRoles(HttpServletRequest request) { - HashMap roles = null; + public static Map getRoles(HttpServletRequest request) { + HashMap roles; HttpSession session = AppUtils.getSession(request); roles = (HashMap) session.getAttribute(SystemProperties.getProperty(SystemProperties.ROLES_ATTRIBUTE_NAME)); @@ -251,11 +242,8 @@ public class EPUserUtils { @SuppressWarnings({ "rawtypes", "unchecked" }) private static HashMap getAllUserRoles(EPUser user) { HashMap roles = new HashMap(); - Iterator i = user.getEPRoles().iterator(); - - while (i.hasNext()) { - EPRole role = (EPRole) i.next(); + for (EPRole role : user.getEPRoles()) { if (role.getActive()) { roles.put(role.getId(), role); @@ -267,9 +255,8 @@ public class EPUserUtils { // Additionally; the account admin role is overloaded between onap // portal and partners; lets also include that - Iterator<EPUserApp> appRolesIterator = user.getEPUserApps().iterator(); - while (appRolesIterator.hasNext()) { - EPRole role = (EPRole) appRolesIterator.next().getRole(); + for (EPUserApp epUserApp : user.getEPUserApps()) { + EPRole role = epUserApp.getRole(); if (role.getActive() && role.getId().equals(ACCOUNT_ADMIN_ROLE_ID)) { roles.put(role.getId(), role); @@ -295,10 +282,9 @@ public class EPUserUtils { private static void addChildRoles(EPRole role, HashMap roles) { Set childRoles = role.getChildRoles(); - if (childRoles != null && childRoles.size() > 0) { - Iterator j = childRoles.iterator(); - while (j.hasNext()) { - EPRole childRole = (EPRole) j.next(); + if (childRoles != null && !childRoles.isEmpty()) { + for (Object o : childRoles) { + EPRole childRole = (EPRole) o; if (childRole.getActive()) { roles.put(childRole.getId(), childRole); @@ -319,7 +305,7 @@ public class EPUserUtils { } @Autowired - public void setDataAccessService(DataAccessService dataAccessService) { + public static void setDataAccessService(DataAccessService dataAccessService) { EPUserUtils.dataAccessService = dataAccessService; } @@ -341,12 +327,10 @@ public class EPUserUtils { * HttpServletREquest * @return Long ID of current user */ - public static Long getUserIdAsLong(HttpServletRequest request) { + static Long getUserIdAsLong(HttpServletRequest request) { Long userId = new Long(SystemProperties.getProperty(SystemProperties.APPLICATION_USER_ID)); - if (request != null) { - if (getUserSession(request) != null) { + if (request != null && getUserSession(request) != null) { userId = getUserSession(request).getId(); - } } return userId; } @@ -364,7 +348,7 @@ public class EPUserUtils { String requestId = ""; try { while (headerNames.hasMoreElements()) { - String headerName = (String) headerNames.nextElement(); + String headerName = headerNames.nextElement(); logger.debug(EELFLoggerDelegate.debugLogger, "One header is " + headerName + " : " + request.getHeader(headerName)); if (headerName.equalsIgnoreCase(SystemProperties.ECOMP_REQUEST_ID)) { @@ -386,7 +370,7 @@ public class EPUserUtils { * HttpServletRequest * @return Full URL */ - public static String getFullURL(HttpServletRequest request) { + static String getFullURL(HttpServletRequest request) { if (request != null) { StringBuffer requestURL = request.getRequestURL(); String queryString = request.getQueryString(); @@ -402,7 +386,7 @@ public class EPUserUtils { public static Boolean matchRoleFunctions(String portalApiPath, Set<? extends String> roleFunctions) { String[] path = portalApiPath.split("/"); - List<String> roleFunList = new ArrayList<>(); + List<String> roleFunList; if (path.length > 1) { roleFunList = roleFunctions.stream().filter(item -> item.startsWith(path[0])).collect(Collectors.toList()); if (roleFunList.size() >= 1) { @@ -411,17 +395,13 @@ public class EPUserUtils { boolean b = true; if (roleFunctionArray.length == path.length) { for (int i = 0; i < roleFunctionArray.length; i++) { - if (b) { if (!roleFunctionArray[i].equals("*")) { Pattern p = Pattern.compile(Pattern.quote(path[i]), Pattern.CASE_INSENSITIVE); Matcher m = p.matcher(roleFunctionArray[i]); b = m.matches(); - } } - } - if (b) - return b; + if (b) return true; } } } diff --git a/ecomp-portal-BE-common/src/test/java/org/onap/portalapp/util/EPUserUtilsTest.java b/ecomp-portal-BE-common/src/test/java/org/onap/portalapp/util/EPUserUtilsTest.java index 7383330d..80ca1424 100644 --- a/ecomp-portal-BE-common/src/test/java/org/onap/portalapp/util/EPUserUtilsTest.java +++ b/ecomp-portal-BE-common/src/test/java/org/onap/portalapp/util/EPUserUtilsTest.java @@ -256,7 +256,7 @@ public class EPUserUtilsTest { PowerMockito.when(AppUtils.getSession(mockedRequest)).thenReturn(session); PowerMockito.when(SystemProperties.getProperty(Matchers.anyString())).thenReturn("12"); Mockito.when(session.getAttribute(Matchers.anyString())).thenReturn(roles); - roles=EPUserUtils.getRoles(mockedRequest); + roles= (HashMap) EPUserUtils.getRoles(mockedRequest); assertEquals(roles,expected); @@ -279,7 +279,7 @@ public class EPUserUtilsTest { Mockito.when(session.getAttribute("attr_name")).thenReturn(user); Mockito.when(user.getEPRoles()).thenReturn(role); Mockito.when(session.getAttribute("12")).thenReturn(null); - roles=EPUserUtils.getRoles(mockedRequest); + roles= (HashMap) EPUserUtils.getRoles(mockedRequest); assertEquals(roles,expected); } @@ -320,7 +320,7 @@ public class EPUserUtilsTest { Mockito.when(epRole.getChildRoles()).thenReturn(childRoles); Mockito.when(user.getEPUserApps()).thenReturn(epUserApps); Mockito.when(session.getAttribute("12")).thenReturn(null); - roles= EPUserUtils.getRoles(mockedRequest); + roles= (HashMap) EPUserUtils.getRoles(mockedRequest); assertNotNull(roles); } @@ -358,7 +358,7 @@ public class EPUserUtilsTest { Mockito.when(epRoleFunctionService.getRoleFunctions()).thenReturn(roleFunctions); Mockito.when(MenuBuilder.filterMenu(applicationMenuData, mockedRequest)).thenReturn(applicationMenuData); PowerMockito.when(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME)).thenReturn("12"); - EPUserUtils.setUserSession(mockedRequest, user, applicationMenuData, businessDirectMenuData, "login_method", epRoleFunctionService); + EPUserUtils.setUserSession(mockedRequest, user, applicationMenuData, businessDirectMenuData, epRoleFunctionService); assertNotNull(session); @@ -383,7 +383,7 @@ public class EPUserUtilsTest { Mockito.when(epRoleFunctionService.getRoleFunctions()).thenReturn(null); Mockito.when(MenuBuilder.filterMenu(applicationMenuData, mockedRequest)).thenReturn(applicationMenuData); PowerMockito.when(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME)).thenReturn("12"); - EPUserUtils.setUserSession(mockedRequest, user, applicationMenuData, businessDirectMenuData, "login_method", epRoleFunctionService); + EPUserUtils.setUserSession(mockedRequest, user, applicationMenuData, businessDirectMenuData, epRoleFunctionService); assertNotNull(session); } @@ -408,7 +408,7 @@ public class EPUserUtilsTest { Mockito.when(epRoleFunctionService.getRoleFunctions()).thenReturn(null); Mockito.when(MenuBuilder.filterMenu(applicationMenuData, mockedRequest)).thenReturn(applicationMenuData); PowerMockito.when(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME)).thenReturn("12"); - EPUserUtils.setUserSession(mockedRequest, user, applicationMenuData, businessDirectMenuData, "login_method", epRoleFunctionService); + EPUserUtils.setUserSession(mockedRequest, user, applicationMenuData, businessDirectMenuData, epRoleFunctionService); assertNotNull(session); |