Fix sonar issues in OnBoardingApiServiceImpl 14/85214/1
authorburdziak <olaf.burdziakowski@nokia.com>
Fri, 12 Apr 2019 13:01:19 +0000 (15:01 +0200)
committerburdziak <olaf.burdziakowski@nokia.com>
Fri, 12 Apr 2019 13:01:19 +0000 (15:01 +0200)
Change-Id: Ic01054b4051c89082e17b35a21f9030d5495b561
Issue-ID: PORTAL-523
Signed-off-by: burdziak <olaf.burdziakowski@nokia.com>
ecomp-sdk/epsdk-app-common/src/main/java/org/onap/portalapp/service/OnBoardingApiServiceImpl.java

index 139f69a..acf94ba 100644 (file)
@@ -38,7 +38,6 @@
 package org.onap.portalapp.service;
 
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
@@ -46,7 +45,6 @@ import java.util.Map;
 import java.util.Set;
 import java.util.SortedSet;
 import java.util.TreeSet;
-import java.util.stream.Collectors;
 
 import javax.servlet.http.HttpServletRequest;
 
@@ -81,7 +79,6 @@ import org.onap.portalsdk.core.web.support.UserUtils;
 import org.slf4j.MDC;
 import org.springframework.context.ApplicationContext;
 
-import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.type.TypeFactory;
 
@@ -107,10 +104,10 @@ public class OnBoardingApiServiceImpl implements IPortalRestAPIService, IPortalR
        private RestApiRequestBuilder restApiRequestBuilder;
        private AppService appServiceImpl;
 
-       private static final String isAccessCentralized = PortalApiProperties
+       private static final String IS_ACCESS_CENTRALIZED = PortalApiProperties
                        .getProperty(PortalApiConstants.ROLE_ACCESS_CENTRALIZED);
        
-       private static final String isCentralized = "remote";
+       private static final String IS_CENTRALIZED = "remote";
        private static String portalApiVersion = "/v3";
        
        public OnBoardingApiServiceImpl() {
@@ -118,7 +115,7 @@ public class OnBoardingApiServiceImpl implements IPortalRestAPIService, IPortalR
                // that was caused by a spurious Spring annotation on this class.
                ApplicationContext appContext = AppContextManager.getAppContext();
                if (appContext == null)
-                       throw new RuntimeException("OnBoardingApiServiceImpl ctor failed to get appContext");
+                       throw new IllegalStateException("OnBoardingApiServiceImpl ctor failed to get appContext");
                roleService = appContext.getBean(RoleService.class);
                userProfileService = appContext.getBean(UserProfileService.class);
                loginStrategy = appContext.getBean(LoginStrategy.class);
@@ -127,7 +124,7 @@ public class OnBoardingApiServiceImpl implements IPortalRestAPIService, IPortalR
                userService = appContext.getBean(UserService.class);
                appServiceImpl = appContext.getBean(AppService.class);
                
-               if(isCentralized.equals(isAccessCentralized)){
+               if(IS_CENTRALIZED.equals(IS_ACCESS_CENTRALIZED)){
                        restApiRequestBuilder = appContext.getBean(RestApiRequestBuilder.class);
                }
        }
@@ -148,7 +145,17 @@ public class OnBoardingApiServiceImpl implements IPortalRestAPIService, IPortalR
                user.setPhone(userJson.getPhone());
                user.setOrgUserId(userJson.getOrgUserId());
                user.setActive(userJson.isActive());
-//             user.setRoles(new TreeSet(userJson.getRoles()));
+       }
+
+       private void saveUserExtension(User user)
+       {
+               if (adminAuthExtensionServiceImpl != null) {
+                       try {
+                               adminAuthExtensionServiceImpl.saveUserExtension(user);
+                       } catch (Exception ex) {
+                               logger.error("pushUser: saveUserExtension failed", ex);
+                       }
+               }
        }
 
        @Override
@@ -156,7 +163,6 @@ public class OnBoardingApiServiceImpl implements IPortalRestAPIService, IPortalR
                if (logger.isDebugEnabled())
                        logger.debug(EELFLoggerDelegate.debugLogger, "pushUser was invoked: {}", userJson);
                User user = new User();
-               String response = "";
                try {
                        // Set input attributes to the object obout to be saved
                        setCurrentAttributes(user, userJson);
@@ -185,17 +191,9 @@ public class OnBoardingApiServiceImpl implements IPortalRestAPIService, IPortalR
                                roles.add(convertToRole(epRole));
                        }
                        user.setRoles(roles);
-                       if (adminAuthExtensionServiceImpl != null) {
-                               try {
-                                       adminAuthExtensionServiceImpl.saveUserExtension(user);
-                               } catch (Exception ex) {
-                                       logger.error("pushUser: saveUserExtension failed", ex);
-                               }
-                       }
-                       response = "push user success.";
-                       response = JSONUtil.convertResponseToJSON(response);
+                       saveUserExtension(user);
                } catch (Exception e) {
-                       response = "OnboardingApiService.pushUser failed";
+                       String response = "OnboardingApiService.pushUser failed";
                        logger.error(EELFLoggerDelegate.errorLogger, response, e);
                        throw new PortalAPIException(response, e);
                } finally {
@@ -230,7 +228,17 @@ public class OnBoardingApiServiceImpl implements IPortalRestAPIService, IPortalR
                roleFunction.setAction(rolefun.getAction());
                return roleFunction;
        }
-        
+
+       private void editUserExtension(User domainUser)
+       {
+               if (adminAuthExtensionServiceImpl != null) {
+                       try {
+                               adminAuthExtensionServiceImpl.editUserExtension(domainUser);
+                       } catch (Exception ex) {
+                               logger.error("editUser: editUserExtension failed", ex);
+                       }
+               }
+       }
 
        @Override
        public void editUser(String loginId, EcompUser userJson) throws PortalAPIException {
@@ -239,7 +247,6 @@ public class OnBoardingApiServiceImpl implements IPortalRestAPIService, IPortalR
                        logger.debug(EELFLoggerDelegate.debugLogger, "OnboardingApi editUser was invoked with loginID {}, JSON {}",
                                        loginId, userJson);
                User editUser = new User();
-               String response = "";
                try {
                        setCurrentAttributes(editUser, userJson);
                        if (editUser.getOrgUserId() != null) {
@@ -266,25 +273,15 @@ public class OnBoardingApiServiceImpl implements IPortalRestAPIService, IPortalR
                        domainUser.setRoles(roles);
                        
                        // After successful edit, call the admin auth extension
-                       if (adminAuthExtensionServiceImpl != null) {
-                               try {
-                                       adminAuthExtensionServiceImpl.editUserExtension(domainUser);
-                               } catch (Exception ex) {
-                                       logger.error("editUser: editUserExtension failed", ex);
-                               }
-                       }
+                       editUserExtension(domainUser);
 
-                       response = "edit user success.";
-                       response = JSONUtil.convertResponseToJSON(response);
                } catch (Exception e) {
-                       response = "OnboardingApiService.editUser failed";
+                       String response = "OnboardingApiService.editUser failed";
                        logger.error(EELFLoggerDelegate.errorLogger, response, e);
                        throw new PortalAPIException(response, e);
                } finally {
                        MDC.remove(SystemProperties.MDC_TIMER);
                }
-
-               // return response;
        }
 
        @Override
@@ -294,14 +291,14 @@ public class OnBoardingApiServiceImpl implements IPortalRestAPIService, IPortalR
                                logger.debug(EELFLoggerDelegate.debugLogger, "## REST API ## loginId: {}", loginId);
 
                        User user = null;
-                       if (isCentralized.equals(isAccessCentralized)) {
+                       if (IS_CENTRALIZED.equals(IS_ACCESS_CENTRALIZED)) {
                                String responseString = restApiRequestBuilder.getViaREST(portalApiVersion+"/user/" + loginId, true,
                                                loginId);
                                user = userService.userMapper(responseString);
                        }
                        else{
                           user = userProfileService.getUserByLoginId(loginId);
-                          user.getRoles().removeIf(role -> (role.getActive() == false));
+                          user.getRoles().removeIf(role -> (!role.getActive()));
                        }
                        if (user == null) {
                                logger.info(EELFLoggerDelegate.debugLogger, "User + " + loginId + " doesn't exist");
@@ -326,20 +323,20 @@ public class OnBoardingApiServiceImpl implements IPortalRestAPIService, IPortalR
 
        @Override
        public List<EcompUser> getUsers() throws PortalAPIException {
-               String users_List = "";
+               String usersList = "";
                try {
-                       if (isCentralized.equals(isAccessCentralized)) {
-                               List<EcompUser> UsersList = new ArrayList<>();
+                       if (IS_CENTRALIZED.equals(IS_ACCESS_CENTRALIZED)) {
+                               List<EcompUser> ecompUsers;
                                List<EcompUser> finalUsersList = new ArrayList<>();
-                                       users_List = restApiRequestBuilder.getViaREST(portalApiVersion + "/users", true, null);
+                                       usersList = restApiRequestBuilder.getViaREST(portalApiVersion + "/users", true, null);
                                        ObjectMapper mapper = new ObjectMapper();
-                                       UsersList = mapper.readValue(users_List,
+                                       ecompUsers = mapper.readValue(usersList,
                                                        TypeFactory.defaultInstance().constructCollectionType(List.class, EcompUser.class));
-                                       for (EcompUser userString : UsersList) {
+                                       for (EcompUser userString : ecompUsers) {
                                                EcompUser ecompUser = mapper.convertValue(userString, EcompUser.class);
                                                finalUsersList.add(ecompUser);
                                        }
-                                       return UsersList;
+                                       return ecompUsers;
                                }
                         else {
                                List<User> users = userProfileService.findAllActive();
@@ -351,7 +348,7 @@ public class OnBoardingApiServiceImpl implements IPortalRestAPIService, IPortalR
                } catch (Exception e) {
                        String response = "OnboardingApiService.getUsers failed";
                        logger.error(EELFLoggerDelegate.errorLogger, response, e);
-                       if (users_List.equals("")) {
+                       if (usersList.isEmpty()) {
                                throw new PortalAPIException("Application is Inactive");
                        } else {
                                throw new PortalAPIException(response, e);
@@ -374,9 +371,19 @@ public class OnBoardingApiServiceImpl implements IPortalRestAPIService, IPortalR
                }
        }
 
+       private void saveUserRoleExtension(Set<Role> roles, User user)
+  {
+    if (adminAuthExtensionServiceImpl != null) {
+      try {
+        adminAuthExtensionServiceImpl.saveUserRoleExtension(roles, user);
+      } catch (Exception ex) {
+        logger.error("pushUserRole: saveUserRoleExtension failed", ex);
+      }
+    }
+  }
+
        @Override
        public void pushUserRole(String loginId, List<EcompRole> rolesJson) throws PortalAPIException {
-               String response = "";
                try {
                        if (logger.isDebugEnabled())
                                logger.debug(EELFLoggerDelegate.debugLogger, "## REST API ## loginId: {}, roles Json {}", loginId,
@@ -397,26 +404,23 @@ public class OnBoardingApiServiceImpl implements IPortalRestAPIService, IPortalR
                        logger.debug(EELFLoggerDelegate.debugLogger, "push user role success.");
 
                        // After successful creation, call admin auth extension
-                       if (adminAuthExtensionServiceImpl != null) {
-                               try {
-                                       adminAuthExtensionServiceImpl.saveUserRoleExtension(roles, user);
-                               } catch (Exception ex) {
-                                       logger.error("pushUserRole: saveUserRoleExtension failed", ex);
-                               }
-                       }
-                       response = "push user role success.";
-                       response = JSONUtil.convertResponseToJSON(response);
-
+      saveUserRoleExtension(roles,user);
                } catch (Exception e) {
-                       response = "OnboardingApiService.pushUserRole failed";
+                       String response = "OnboardingApiService.pushUserRole failed";
                        logger.error(EELFLoggerDelegate.errorLogger, response, e);
                        throw new PortalAPIException(response, e);
                } finally {
                        MDC.remove(SystemProperties.MDC_TIMER);
                }
-
        }
 
+       private static void addRoles2Ecomp(SortedSet<Role> currentRoles,List<EcompRole> ecompRoles)
+  {
+    if (currentRoles != null && ecompRoles!=null)
+      for (Role role : currentRoles)
+        ecompRoles.add(UserUtils.convertToEcompRole(role));
+  }
+
        @Override
        public List<EcompRole> getUserRoles(String loginId) throws PortalAPIException {
                if (logger.isDebugEnabled())
@@ -424,7 +428,7 @@ public class OnBoardingApiServiceImpl implements IPortalRestAPIService, IPortalR
                List<EcompRole> ecompRoles = new ArrayList<>();
                try {
 
-                       if (isCentralized.equals(isAccessCentralized)) {
+                       if (IS_CENTRALIZED.equals(IS_ACCESS_CENTRALIZED)) {
                                User user = null;
                                String responseString = restApiRequestBuilder.getViaREST(portalApiVersion+"/user/" + loginId, true,
                                                loginId);
@@ -432,19 +436,15 @@ public class OnBoardingApiServiceImpl implements IPortalRestAPIService, IPortalR
                                SortedSet<Role> currentRoles = null;
                                if (user != null) {
                                        currentRoles = user.getRoles();
-                                       if (currentRoles != null)
-                                               for (Role role : currentRoles)
-                                                       ecompRoles.add(UserUtils.convertToEcompRole(role));
+          addRoles2Ecomp(currentRoles,ecompRoles);
                                }
                        } else {
                                User user = userProfileService.getUserByLoginId(loginId);
                                SortedSet<Role> currentRoles = null;
                                if (user != null) {
                                        currentRoles = user.getRoles();
-                                       currentRoles.removeIf(role -> (role.getActive() == false));
-                                       if (currentRoles != null)
-                                               for (Role role : currentRoles)
-                                                       ecompRoles.add(UserUtils.convertToEcompRole(role));
+                                       currentRoles.removeIf(role -> (!role.getActive()));
+          addRoles2Ecomp(currentRoles,ecompRoles);
                                }
                        }
                        return ecompRoles;
@@ -486,13 +486,7 @@ public class OnBoardingApiServiceImpl implements IPortalRestAPIService, IPortalR
                try {
                        String appUser = request.getHeader("username");
                        String password = request.getHeader("password");
-                       // System.out.println("username = " + appUser);
-                       // System.out.println("password = " + password);
-                       boolean flag = securityService.verifyRESTCredential(null, appUser, password);
-                       // System.out.println("username = " + appUser);
-                       // System.out.println("password = " + password);
-                       return flag;
-
+                       return securityService.verifyRESTCredential(null, appUser, password);
                } catch (Exception e) {
                        String response = "OnboardingApiService.isAppAuthenticated failed";
                        logger.error(EELFLoggerDelegate.errorLogger, response, e);
@@ -500,11 +494,11 @@ public class OnBoardingApiServiceImpl implements IPortalRestAPIService, IPortalR
                }
        }
 
-       public String getSessionTimeOuts() throws Exception {
+       public String getSessionTimeOuts() {
                return PortalTimeoutHandler.gatherSessionExtensions();
        }
 
-       public void updateSessionTimeOuts(String sessionMap) throws Exception {
+       public void updateSessionTimeOuts(String sessionMap) {
                PortalTimeoutHandler.updateSessionExtensions(sessionMap);
        }
 
@@ -514,7 +508,7 @@ public class OnBoardingApiServiceImpl implements IPortalRestAPIService, IPortalR
        }
        
        @Override
-       public Map<String, String> getAppCredentials() throws PortalAPIException{
+       public Map<String, String> getAppCredentials() {
                Map<String, String> credentialsMap = new HashMap<>();
                String appName = null;
                String appUserName = null;