diff options
Diffstat (limited to 'ecomp-sdk/epsdk-app-common/src')
7 files changed, 140 insertions, 491 deletions
diff --git a/ecomp-sdk/epsdk-app-common/src/main/java/org/onap/portalapp/controller/core/FuncMenuController.java b/ecomp-sdk/epsdk-app-common/src/main/java/org/onap/portalapp/controller/core/FuncMenuController.java deleted file mode 100644 index fa88c5c9..00000000 --- a/ecomp-sdk/epsdk-app-common/src/main/java/org/onap/portalapp/controller/core/FuncMenuController.java +++ /dev/null @@ -1,197 +0,0 @@ -/* - * ============LICENSE_START========================================== - * ONAP Portal SDK - * =================================================================== - * Copyright © 2017 AT&T Intellectual Property. All rights reserved. - * =================================================================== - * - * Unless otherwise specified, all software contained herein is licensed - * under the Apache License, Version 2.0 (the "License"); - * you may not use this software except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * Unless otherwise specified, all documentation contained herein is licensed - * under the Creative Commons License, Attribution 4.0 Intl. (the "License"); - * you may not use this documentation except in compliance with the License. - * You may obtain a copy of the License at - * - * https://creativecommons.org/licenses/by/4.0/ - * - * Unless required by applicable law or agreed to in writing, documentation - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ============LICENSE_END============================================ - * - * - */ -package org.onap.portalapp.controller.core; - -import static com.att.eelf.configuration.Configuration.MDC_KEY_REQUEST_ID; - -import java.io.IOException; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.json.JSONArray; -import org.json.JSONObject; -import org.onap.portalsdk.core.controller.RestrictedBaseController; -import org.onap.portalsdk.core.domain.App; -import org.onap.portalsdk.core.domain.User; -import org.onap.portalsdk.core.logging.aspect.AuditLog; -import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate; -import org.onap.portalsdk.core.onboarding.rest.FunctionalMenuClient; -import org.onap.portalsdk.core.onboarding.ueb.UebException; -import org.onap.portalsdk.core.onboarding.ueb.UebManager; -import org.onap.portalsdk.core.onboarding.ueb.UebMsg; -import org.onap.portalsdk.core.onboarding.ueb.UebMsgTypes; -import org.onap.portalsdk.core.onboarding.util.CipherUtil; -import org.onap.portalsdk.core.onboarding.util.PortalApiConstants; -import org.onap.portalsdk.core.onboarding.util.PortalApiProperties; -import org.onap.portalsdk.core.service.AppService; -import org.onap.portalsdk.core.util.SystemProperties; -import org.onap.portalsdk.core.web.support.UserUtils; -import org.slf4j.MDC; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.EnableAspectJAutoProxy; -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; - -@Controller -@RequestMapping("/") -@org.springframework.context.annotation.Configuration -@EnableAspectJAutoProxy -public class FuncMenuController extends RestrictedBaseController { - - private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(FuncMenuController.class); - - @Autowired - private AppService appService; - - @AuditLog - @RequestMapping(value = { "/get_functional_menu" }, method = RequestMethod.GET) - public void functionalMenu(HttpServletRequest request, HttpServletResponse response) { - - User user = UserUtils.getUserSession(request); - - try { - if (user != null) { - String useRestForFunctionalMenu = PortalApiProperties - .getProperty(PortalApiConstants.USE_REST_FOR_FUNCTIONAL_MENU); - String funcMenuJsonString; - if (useRestForFunctionalMenu == null || "".equals(useRestForFunctionalMenu) - || "false".equalsIgnoreCase(useRestForFunctionalMenu)) { - logger.debug(EELFLoggerDelegate.debugLogger, - "Making use of UEB communication and Requesting functional menu for user " - + user.getOrgUserId()); - funcMenuJsonString = getFunctionalMenu(user.getOrgUserId()); - } else { - funcMenuJsonString = getFunctionalMenuViaREST(user.getOrgUserId()); - } - response.setContentType("application/json"); - response.getWriter().write(funcMenuJsonString); - } else { - logger.error(EELFLoggerDelegate.errorLogger, - "Http request did not contain user info, cannot retrieve functional menu"); - response.setContentType("application/json"); - JSONArray jsonResponse = new JSONArray(); - JSONObject error = new JSONObject(); - error.put("error", "Http request did not contain user info, cannot retrieve functional menu"); - jsonResponse.put(error); - response.getWriter().write(jsonResponse.toString()); - } - } catch (Exception e) { - logger.error(EELFLoggerDelegate.errorLogger, "functionalMenu failed", e); - response.setCharacterEncoding("UTF-8"); - response.setContentType("application/json"); - JSONArray jsonResponse = new JSONArray(); - JSONObject error = new JSONObject(); - try { - if (null == e.getMessage()) { - error.put("error", "No menu data"); - } else { - error.put("error", e.getMessage()); - } - jsonResponse.put(error); - response.getWriter().write(jsonResponse.toString()); - } catch (IOException e1) { - logger.error(EELFLoggerDelegate.errorLogger, "Error getting functional_menu", e1); - } - } - - } - - // -------------------------------------------------------------------------- - // Makes a synchronous call to ECOMP Portal to get the JSON file that - // contains the contents of the functional menu. The JSON file will be - // in the payload of the returned UEB message. - // -------------------------------------------------------------------------- - private String getFunctionalMenu(String userId) throws UebException { - String returnString = null; - UebMsg msg = new UebMsg(); - msg.putMsgType(UebMsgTypes.UEB_MSG_TYPE_GET_FUNC_MENU); - msg.putUserId(userId); - UebMsg funcMenuUebMsg = UebManager.getInstance().requestReply(msg); - if (funcMenuUebMsg != null) { - if (funcMenuUebMsg.getPayload().startsWith("Error:")) { - logger.error(EELFLoggerDelegate.errorLogger, - "getFunctionalMenu received an error in UEB msg = " + funcMenuUebMsg.getPayload()); - } else { - returnString = funcMenuUebMsg.getPayload(); - } - } - - logger.debug(EELFLoggerDelegate.debugLogger, "FunctionalMenu response: " + returnString); - return returnString; - } - - private String getFunctionalMenuViaREST(String userId) { - String appName; - String requestId; - String appUserName = ""; - String decryptedPwd = null; - - logger.debug(EELFLoggerDelegate.debugLogger, - "Making use of REST API communication and Requesting functional menu for user " + userId); - - App app = appService.getDefaultApp(); - if (app != null) { - appName = app.getName(); - appUserName = app.getUsername(); - try { - decryptedPwd = CipherUtil.decryptPKC(app.getAppPassword(), - SystemProperties.getProperty(SystemProperties.Decryption_Key)); - } catch (Exception e) { - logger.error(EELFLoggerDelegate.errorLogger, - "getFunctionalMenuViaREST failed while decrypting the password", e); - } - } else { - logger.error(EELFLoggerDelegate.errorLogger, "Unable to locate the app information from the database."); - appName = SystemProperties.SDK_NAME; - } - requestId = MDC.get(MDC_KEY_REQUEST_ID); - - String fnMenu = null; - try { - fnMenu = FunctionalMenuClient.getFunctionalMenu(userId, appName, requestId, appUserName, decryptedPwd); - } catch (Exception ex) { - logger.error(EELFLoggerDelegate.errorLogger, "getFunctionalMenuViaREST failed", ex); - fnMenu = "Failed to get functional menu: " + ex.toString(); - } - - logger.debug(EELFLoggerDelegate.debugLogger, "FunctionalMenu response: {}", fnMenu); - return fnMenu; - } -}
\ No newline at end of file diff --git a/ecomp-sdk/epsdk-app-common/src/main/java/org/onap/portalapp/controller/core/PostSearchController.java b/ecomp-sdk/epsdk-app-common/src/main/java/org/onap/portalapp/controller/core/PostSearchController.java index d5137c2c..a8c3a5d6 100644 --- a/ecomp-sdk/epsdk-app-common/src/main/java/org/onap/portalapp/controller/core/PostSearchController.java +++ b/ecomp-sdk/epsdk-app-common/src/main/java/org/onap/portalapp/controller/core/PostSearchController.java @@ -78,6 +78,9 @@ public class PostSearchController extends RestrictedBaseController { private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(PostSearchController.class); + private static final String POST_SEARCH_BEAN = "postSearchBean"; + private static final String UTF8 = "UTF-8"; + @SuppressWarnings("rawtypes") private static List sortByList = null; @@ -91,13 +94,13 @@ public class PostSearchController extends RestrictedBaseController { private ProfileService profileService; @RequestMapping(value = { "/post_search" }, method = RequestMethod.GET) - public ModelAndView welcome(@ModelAttribute("postSearchBean") PostSearchBean postSearchBean) { + public ModelAndView welcome(@ModelAttribute(POST_SEARCH_BEAN) PostSearchBean postSearchBean) { Map<String, Object> model = new HashMap<>(); ObjectMapper mapper = new ObjectMapper(); try { - postSearchBean = new PostSearchBean(); - model.put("profileList", mapper.writeValueAsString(postSearchBean.getSearchResult())); - model.put("postSearchBean", mapper.writeValueAsString(postSearchBean)); + PostSearchBean postSearchBeanLocal = new PostSearchBean(); + model.put("profileList", mapper.writeValueAsString(postSearchBeanLocal.getSearchResult())); + model.put(POST_SEARCH_BEAN, mapper.writeValueAsString(postSearchBeanLocal)); model.put("existingUsers", mapper.writeValueAsString(getExistingUsers())); model.put("sortByList", mapper.writeValueAsString(getSortByList())); } catch (Exception ex) { @@ -109,13 +112,13 @@ public class PostSearchController extends RestrictedBaseController { @RequestMapping(value = { "/post_search_sample" }, method = RequestMethod.GET) public void getPostSearchProfile(HttpServletResponse response, - @ModelAttribute("postSearchBean") PostSearchBean postSearchBean) { + @ModelAttribute(POST_SEARCH_BEAN) PostSearchBean postSearchBean) { Map<String, Object> model = new HashMap<>(); ObjectMapper mapper = new ObjectMapper(); try { - postSearchBean = new PostSearchBean(); - model.put("profileList", mapper.writeValueAsString(postSearchBean.getSearchResult())); - model.put("postSearchBean", mapper.writeValueAsString(postSearchBean)); + PostSearchBean postSearchBeanLocal = new PostSearchBean(); + model.put("profileList", mapper.writeValueAsString(postSearchBeanLocal.getSearchResult())); + model.put(POST_SEARCH_BEAN, mapper.writeValueAsString(postSearchBeanLocal)); model.put("existingUsers", mapper.writeValueAsString(getExistingUsers())); model.put("sortByList", mapper.writeValueAsString(getSortByList())); JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model)); @@ -152,14 +155,14 @@ public class PostSearchController extends RestrictedBaseController { ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); JsonNode root = mapper.readTree(request.getReader()); - PostSearchBean postSearchBean = mapper.readValue(root.get("postSearchBean").toString(), + PostSearchBean postSearchBean = mapper.readValue(root.get(POST_SEARCH_BEAN).toString(), PostSearchBean.class); postSearchBean.setSearchResult(loadSearchResultData(request, postSearchBean)); - response.setCharacterEncoding("UTF-8"); + response.setCharacterEncoding(UTF8); response.setContentType("application / json"); - request.setCharacterEncoding("UTF-8"); + request.setCharacterEncoding(UTF8); PrintWriter out = response.getWriter(); String responseString = mapper.writeValueAsString(postSearchBean); @@ -201,7 +204,7 @@ public class PostSearchController extends RestrictedBaseController { mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); JsonNode root = mapper.readTree(request.getReader()); - PostSearchBean postSearch = mapper.readValue(root.get("postSearchBean").toString(), PostSearchBean.class); + PostSearchBean postSearch = mapper.readValue(root.get(POST_SEARCH_BEAN).toString(), PostSearchBean.class); String errorMsg = "{}"; try { postSearchService.process(request, postSearch); @@ -212,9 +215,9 @@ public class PostSearchController extends RestrictedBaseController { "Exception occurred while performing PostSearchController.process. Details:", e); } logger.info(EELFLoggerDelegate.auditLogger, "Import new user from webphone "); - response.setCharacterEncoding("UTF-8"); + response.setCharacterEncoding(UTF8); response.setContentType("application / json"); - request.setCharacterEncoding("UTF-8"); + request.setCharacterEncoding(UTF8); PrintWriter out = response.getWriter(); String postSearchString = mapper.writeValueAsString(postSearch); diff --git a/ecomp-sdk/epsdk-app-common/src/main/java/org/onap/portalapp/controller/core/ProfileController.java b/ecomp-sdk/epsdk-app-common/src/main/java/org/onap/portalapp/controller/core/ProfileController.java index c7edfb81..24ea203f 100644 --- a/ecomp-sdk/epsdk-app-common/src/main/java/org/onap/portalapp/controller/core/ProfileController.java +++ b/ecomp-sdk/epsdk-app-common/src/main/java/org/onap/portalapp/controller/core/ProfileController.java @@ -75,6 +75,17 @@ public class ProfileController extends RestrictedBaseController { private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ProfileController.class); + private static final String PROFILE_ID = "profile_id"; + private static final String PROFILEID = "profileId"; + private static final String STATE_LIST = "stateList"; + private static final String COUNTRIES = "countries"; + private static final String TIME_ZONES = "timeZones"; + private static final String AVAILABLE_ROLES= "availableRoles"; + private static final String PROFILE = "profile"; + private static final String PROFILE_CONTAINER = "ProfileController.save"; + private static final String UTF8 = "UTF-8"; + private static final String APPLICATION_JSON = "application / json"; + @Autowired private UserProfileService service; @@ -83,8 +94,6 @@ public class ProfileController extends RestrictedBaseController { @Autowired private RoleService roleService; - - private String viewName; @RequestMapping(value = { "/profile" }, method = RequestMethod.GET) public ModelAndView profile(HttpServletRequest request) throws IOException { @@ -98,25 +107,25 @@ public class ProfileController extends RestrictedBaseController { profile = UserUtils.getUserSession(request); profileId = profile.getId(); } else { - profileId = Long.parseLong(request.getParameter("profile_id")); + profileId = Long.parseLong(request.getParameter(PROFILE_ID)); profile = userService.getUser(String.valueOf(profileId)); } try { - model.put("stateList", mapper.writeValueAsString(getStates())); - model.put("countries", mapper.writeValueAsString(getCountries())); - model.put("timeZones", mapper.writeValueAsString(getTimeZones())); - model.put("availableRoles", mapper.writeValueAsString(getAvailableRoles(user.getOrgUserId()))); - model.put("profile", mapper.writeValueAsString(profile)); - model.put("profileId", mapper.writeValueAsString(profileId)); + model.put(STATE_LIST, mapper.writeValueAsString(getStates())); + model.put(COUNTRIES, mapper.writeValueAsString(getCountries())); + model.put(TIME_ZONES, mapper.writeValueAsString(getTimeZones())); + model.put(AVAILABLE_ROLES, mapper.writeValueAsString(getAvailableRoles(user.getOrgUserId()))); + model.put(PROFILE, mapper.writeValueAsString(profile)); + model.put(PROFILEID, mapper.writeValueAsString(profileId)); } catch (Exception e) { logger.error(EELFLoggerDelegate.errorLogger, "profile: failed to write JSON", e); } - return new ModelAndView("profile", "model", model); + return new ModelAndView(PROFILE, "model", model); } @RequestMapping(value = { "/self_profile" }, method = RequestMethod.GET) - public ModelAndView selfProfile(HttpServletRequest request) throws Exception{ + public ModelAndView selfProfile(HttpServletRequest request) { Map<String, Object> model = new HashMap<>(); ObjectMapper mapper = new ObjectMapper(); @@ -124,16 +133,16 @@ public class ProfileController extends RestrictedBaseController { User user = UserUtils.getUserSession(request); User profile = UserUtils.getUserSession(request); try { - model.put("stateList", mapper.writeValueAsString(getStates())); - model.put("countries", mapper.writeValueAsString(getCountries())); - model.put("timeZones", mapper.writeValueAsString(getTimeZones())); - model.put("availableRoles", mapper.writeValueAsString(getAvailableRoles(user.getOrgUserId()))); - model.put("profile", mapper.writeValueAsString(profile)); - model.put("profileId", mapper.writeValueAsString(profileId)); + model.put(STATE_LIST, mapper.writeValueAsString(getStates())); + model.put(COUNTRIES, mapper.writeValueAsString(getCountries())); + model.put(TIME_ZONES, mapper.writeValueAsString(getTimeZones())); + model.put(AVAILABLE_ROLES, mapper.writeValueAsString(getAvailableRoles(user.getOrgUserId()))); + model.put(PROFILE, mapper.writeValueAsString(profile)); + model.put(PROFILEID, mapper.writeValueAsString(profileId)); } catch (Exception e) { logger.error(EELFLoggerDelegate.errorLogger, "self_profile: failed to write JSON", e); } - return new ModelAndView("profile", "model", model); + return new ModelAndView(PROFILE, "model", model); } @RequestMapping(value = { "/get_self_profile" }, method = RequestMethod.GET) @@ -145,12 +154,12 @@ public class ProfileController extends RestrictedBaseController { Long profileId = null; User profile = UserUtils.getUserSession(request); try { - model.put("stateList", mapper.writeValueAsString(getStates())); - model.put("countries", mapper.writeValueAsString(getCountries())); - model.put("timeZones", mapper.writeValueAsString(getTimeZones())); - model.put("availableRoles", mapper.writeValueAsString(getAvailableRoles(user.getOrgUserId()))); - model.put("profile", mapper.writeValueAsString(profile)); - model.put("profileId", mapper.writeValueAsString(profileId)); + model.put(STATE_LIST, mapper.writeValueAsString(getStates())); + model.put(COUNTRIES, mapper.writeValueAsString(getCountries())); + model.put(TIME_ZONES, mapper.writeValueAsString(getTimeZones())); + model.put(AVAILABLE_ROLES, mapper.writeValueAsString(getAvailableRoles(user.getOrgUserId()))); + model.put(PROFILE, mapper.writeValueAsString(profile)); + model.put(PROFILEID, mapper.writeValueAsString(profileId)); JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model)); JSONObject j = new JSONObject(msg); response.getWriter().write(j.toString()); @@ -172,15 +181,15 @@ public class ProfileController extends RestrictedBaseController { profile = UserUtils.getUserSession(request); profileId = profile.getId(); } else { - profileId = Long.parseLong(request.getParameter("profile_id")); + profileId = Long.parseLong(request.getParameter(PROFILE_ID)); profile = userService.getUser(String.valueOf(profileId)); } - model.put("stateList", mapper.writeValueAsString(getStates())); - model.put("countries", mapper.writeValueAsString(getCountries())); - model.put("timeZones", mapper.writeValueAsString(getTimeZones())); - model.put("availableRoles", mapper.writeValueAsString(getAvailableRoles(user.getOrgUserId()))); - model.put("profile", mapper.writeValueAsString(profile)); - model.put("profileId", mapper.writeValueAsString(profileId)); + model.put(STATE_LIST, mapper.writeValueAsString(getStates())); + model.put(COUNTRIES, mapper.writeValueAsString(getCountries())); + model.put(TIME_ZONES, mapper.writeValueAsString(getTimeZones())); + model.put(AVAILABLE_ROLES, mapper.writeValueAsString(getAvailableRoles(user.getOrgUserId()))); + model.put(PROFILE, mapper.writeValueAsString(profile)); + model.put(PROFILEID, mapper.writeValueAsString(profileId)); JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model)); JSONObject j = new JSONObject(msg); response.getWriter().write(j.toString()); @@ -192,41 +201,41 @@ public class ProfileController extends RestrictedBaseController { @RequestMapping(value = { "/profile/saveProfile" }, method = RequestMethod.POST) public ModelAndView saveProfile(HttpServletRequest request, HttpServletResponse response) { - logger.info(EELFLoggerDelegate.debugLogger, "ProfileController.save"); + logger.info(EELFLoggerDelegate.debugLogger, PROFILE_CONTAINER); try { ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); JsonNode root = mapper.readTree(request.getReader()); - User user = mapper.readValue(root.get("profile").toString(), User.class); + User user = mapper.readValue(root.get(PROFILE).toString(), User.class); String selectedCountry = mapper.readValue(root.get("selectedCountry").toString(), String.class); String selectedState = mapper.readValue(root.get("selectedState").toString(), String.class); String selectedTimeZone = mapper.readValue(root.get("selectedTimeZone").toString(), String.class); - Long profileId = Long.parseLong(request.getParameter("profile_id")); + Long profileId = Long.parseLong(request.getParameter(PROFILE_ID)); - User domainUser = (User) userService.getUser(String.valueOf(profileId)); + User domainUser = userService.getUser(String.valueOf(profileId)); user.setPseudoRoles(domainUser.getPseudoRoles()); user.setUserApps(domainUser.getUserApps()); - if (!selectedCountry.equals("")) { + if (!selectedCountry.isEmpty()) { user.setCountry(selectedCountry); } - if (!selectedState.equals("")) { + if (!selectedState.isEmpty()) { user.setState(selectedState); } - if (!selectedTimeZone.equals("")) { + if (!selectedTimeZone.isEmpty()) { user.setTimeZoneId(Long.parseLong(selectedTimeZone)); } service.saveUser(user); logger.info(EELFLoggerDelegate.auditLogger, "Save profile for user {}", profileId); - response.setCharacterEncoding("UTF-8"); - response.setContentType("application / json"); + response.setCharacterEncoding(UTF8); + response.setContentType(APPLICATION_JSON); PrintWriter out = response.getWriter(); out.write("" + profileId); return null; } catch (Exception e) { logger.error(EELFLoggerDelegate.errorLogger, "saveProfile failed", e); - response.setCharacterEncoding("UTF-8"); + response.setCharacterEncoding(UTF8); try { PrintWriter out = response.getWriter(); out.write("An error occurred in the saveProfile ()"); @@ -240,7 +249,7 @@ public class ProfileController extends RestrictedBaseController { @RequestMapping(value = { "/profile/removeRole" }, method = RequestMethod.POST) public ModelAndView removeRole(HttpServletRequest request, HttpServletResponse response) throws IOException { - logger.info(EELFLoggerDelegate.debugLogger, "ProfileController.save"); + logger.info(EELFLoggerDelegate.debugLogger, PROFILE_CONTAINER); try { ObjectMapper mapper = new ObjectMapper(); @@ -248,7 +257,7 @@ public class ProfileController extends RestrictedBaseController { JsonNode root = mapper.readTree(request.getReader()); Role role = mapper.readValue(root.get("role").toString(), Role.class); - String profileId = request.getParameter("profile_id"); + String profileId = request.getParameter(PROFILE_ID); User domainUser = userService.getUser(profileId); @@ -262,14 +271,14 @@ public class ProfileController extends RestrictedBaseController { } logger.info(EELFLoggerDelegate.auditLogger, "Remove role " + role.getId() + " from user " + profileId); - response.setCharacterEncoding("UTF-8"); - response.setContentType("application / json"); - request.setCharacterEncoding("UTF-8"); + response.setCharacterEncoding(UTF8); + response.setContentType(APPLICATION_JSON); + request.setCharacterEncoding(UTF8); PrintWriter out = response.getWriter(); Map<String, Object> model = new HashMap<>(); - model.put("profile", mapper.writeValueAsString(domainUser)); + model.put(PROFILE, mapper.writeValueAsString(domainUser)); JSONObject j = new JSONObject(mapper.writeValueAsString(domainUser)); out.write(j.toString()); @@ -277,7 +286,7 @@ public class ProfileController extends RestrictedBaseController { return null; } catch (Exception e) { logger.error(EELFLoggerDelegate.errorLogger, "removeRole failed", e); - response.setCharacterEncoding("UTF-8"); + response.setCharacterEncoding(UTF8); PrintWriter out = response.getWriter(); out.write("An error occurred in the removeRole ()"); return null; @@ -287,14 +296,14 @@ public class ProfileController extends RestrictedBaseController { @RequestMapping(value = { "/profile/addNewRole" }, method = RequestMethod.POST) public ModelAndView addNewRole(HttpServletRequest request, HttpServletResponse response) throws IOException { - logger.info(EELFLoggerDelegate.debugLogger, "ProfileController.save" ); + logger.info(EELFLoggerDelegate.debugLogger, PROFILE_CONTAINER ); try { ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); JsonNode root = mapper.readTree(request.getReader()); Role role = mapper.readValue(root.get("role").toString(), Role.class); - String profileId = request.getParameter("profile_id"); + String profileId = request.getParameter(PROFILE_ID); User domainUser = userService.getUser(profileId); domainUser.addRole(role); service.saveUser(domainUser); @@ -305,13 +314,13 @@ public class ProfileController extends RestrictedBaseController { } logger.info(EELFLoggerDelegate.auditLogger, "Add new role " + role.getName() + " to user " + profileId); - response.setCharacterEncoding("UTF-8"); - response.setContentType("application / json"); - request.setCharacterEncoding("UTF-8"); + response.setCharacterEncoding(UTF8); + response.setContentType(APPLICATION_JSON); + request.setCharacterEncoding(UTF8); PrintWriter out = response.getWriter(); Map<String, Object> model = new HashMap<>(); - model.put("profile", mapper.writeValueAsString(domainUser)); + model.put(PROFILE, mapper.writeValueAsString(domainUser)); JSONObject j = new JSONObject(mapper.writeValueAsString(domainUser)); out.write(j.toString()); @@ -319,8 +328,8 @@ public class ProfileController extends RestrictedBaseController { return null; } catch (Exception e) { logger.error(EELFLoggerDelegate.errorLogger, "addNewRole failed", e); - response.setCharacterEncoding("UTF-8"); - request.setCharacterEncoding("UTF-8"); + response.setCharacterEncoding(UTF8); + request.setCharacterEncoding(UTF8); PrintWriter out = response.getWriter(); out.write("An error occurred in the addNewRole ()"); return null; @@ -328,16 +337,6 @@ public class ProfileController extends RestrictedBaseController { } - @Override - public String getViewName() { - return viewName; - } - - @Override - public void setViewName(String viewName) { - this.viewName = viewName; - } - @SuppressWarnings("rawtypes") public List getStates() { return AppUtils.getLookupList("FN_LU_STATE", "STATE_CD", "STATE", null, "STATE_CD"); @@ -358,4 +357,13 @@ public class ProfileController extends RestrictedBaseController { return roleService.getAvailableRoles(requestedLoginId); } + @Override + public String getViewName() { + return viewName; + } + + @Override + public void setViewName(String viewName) { + this.viewName = viewName; + } }
\ No newline at end of file diff --git a/ecomp-sdk/epsdk-app-common/src/main/java/org/onap/portalapp/controller/core/RoleController.java b/ecomp-sdk/epsdk-app-common/src/main/java/org/onap/portalapp/controller/core/RoleController.java index 52dedd46..d555ba46 100644 --- a/ecomp-sdk/epsdk-app-common/src/main/java/org/onap/portalapp/controller/core/RoleController.java +++ b/ecomp-sdk/epsdk-app-common/src/main/java/org/onap/portalapp/controller/core/RoleController.java @@ -84,8 +84,6 @@ public class RoleController extends RestrictedBaseController { @Autowired private RoleService roleService; - private String viewName; - @RequestMapping(value = { "/role" }, method = RequestMethod.GET) public ModelAndView role(HttpServletRequest request) throws IOException { Map<String, Object> model = new HashMap<>(); @@ -133,6 +131,14 @@ public class RoleController extends RestrictedBaseController { } + private void trowIfUserRoleExist(String roleName, String userId) throws IOException, SecurityException + { + List<Role> roles = roleService.getAvailableRoles(userId); + for (Role existRole : roles) + if (existRole.getName().equalsIgnoreCase(roleName)) + throw new SecurityException("role already exists: " + existRole.getName()); + } + /** * Creates a new role or updates an existing role. * @@ -167,11 +173,8 @@ public class RoleController extends RestrictedBaseController { domainRole.setPriority(role.getPriority()); } else { doAuditLog("saveRole: creating new role", role.getName()); - // check for existing role of same name - List<Role> roles = roleService.getAvailableRoles(user.getOrgUserId()); - for (Role existRole : roles) - if (existRole.getName().equalsIgnoreCase(role.getName())) - throw new Exception("role already exists: " + existRole.getName()); + + trowIfUserRoleExist(role.getName(),user.getOrgUserId()); domainRole = new Role(); domainRole.setName(role.getName()); @@ -371,13 +374,12 @@ public class RoleController extends RestrictedBaseController { } @Override - public String getViewName() { - return viewName; - } - - @Override - public void setViewName(String viewName) { - this.viewName = viewName; - } - + public String getViewName() { + return viewName; + } + + @Override + public void setViewName(String viewName) { + this.viewName = viewName; + } } diff --git a/ecomp-sdk/epsdk-app-common/src/main/java/org/onap/portalapp/controller/sample/BroadcastController.java b/ecomp-sdk/epsdk-app-common/src/main/java/org/onap/portalapp/controller/sample/BroadcastController.java index ba90f46f..600deeb3 100644 --- a/ecomp-sdk/epsdk-app-common/src/main/java/org/onap/portalapp/controller/sample/BroadcastController.java +++ b/ecomp-sdk/epsdk-app-common/src/main/java/org/onap/portalapp/controller/sample/BroadcastController.java @@ -37,7 +37,9 @@ */ package org.onap.portalapp.controller.sample; +import java.io.IOException; import java.io.PrintWriter; +import java.io.UnsupportedEncodingException; import java.util.HashMap; import java.util.Map; @@ -67,6 +69,10 @@ import com.fasterxml.jackson.databind.ObjectMapper; public class BroadcastController extends RestrictedBaseController { private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(BroadcastController.class); + + private static final String BROADCAST_MESSAGE = "broadcastMessage"; + private static final String BROADCAST_SITIES = "broadcastSites"; + private static final String UTF8 = "UTF-8"; @Autowired private BroadcastService broadcastService; @@ -77,8 +83,8 @@ public class BroadcastController extends RestrictedBaseController { ObjectMapper mapper = new ObjectMapper(); try { - model.put("broadcastMessage", mapper.writeValueAsString(broadcastService.getBroadcastMessage(request))); - model.put("broadcastSites", mapper.writeValueAsString(referenceData(request).get("broadcastSites"))); + model.put(BROADCAST_MESSAGE, mapper.writeValueAsString(broadcastService.getBroadcastMessage(request))); + model.put(BROADCAST_SITIES, mapper.writeValueAsString(referenceData(request).get(BROADCAST_SITIES))); } catch (Exception e) { logger.error(EELFLoggerDelegate.errorLogger, "broadcast() failed", e); } @@ -92,8 +98,8 @@ public class BroadcastController extends RestrictedBaseController { try { - model.put("broadcastMessage", mapper.writeValueAsString(broadcastService.getBroadcastMessage(request))); - model.put("broadcastSites", mapper.writeValueAsString(referenceData(request).get("broadcastSites"))); + model.put(BROADCAST_MESSAGE, mapper.writeValueAsString(broadcastService.getBroadcastMessage(request))); + model.put(BROADCAST_SITIES, mapper.writeValueAsString(referenceData(request).get(BROADCAST_SITIES))); JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model)); JSONObject j = new JSONObject(msg); response.getWriter().write(j.toString()); @@ -109,7 +115,7 @@ public class BroadcastController extends RestrictedBaseController { Map lookupData = new HashMap(); if ("true".equals(SystemProperties.getProperty(SystemProperties.CLUSTERED))) { - lookupData.put("broadcastSites", AppUtils.getLookupList("fn_lu_broadcast_site", "broadcast_site_cd", + lookupData.put(BROADCAST_SITIES, AppUtils.getLookupList("fn_lu_broadcast_site", "broadcast_site_cd", "broadcast_site_descr", "", "broadcast_site_descr")); } @@ -117,21 +123,21 @@ public class BroadcastController extends RestrictedBaseController { } @RequestMapping(value = { "/broadcast/save" }, method = RequestMethod.POST) - public ModelAndView save(HttpServletRequest request, HttpServletResponse response) throws Exception { + public ModelAndView save(HttpServletRequest request, HttpServletResponse response) throws IOException { try { ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); JsonNode root = mapper.readTree(request.getReader()); - BroadcastMessage broadcastMessage = mapper.readValue(root.get("broadcastMessage").toString(), + BroadcastMessage broadcastMessage = mapper.readValue(root.get(BROADCAST_MESSAGE).toString(), BroadcastMessage.class); broadcastService.saveBroadcastMessage(broadcastMessage); - response.setCharacterEncoding("UTF-8"); + response.setCharacterEncoding(UTF8); response.setContentType("application / json"); - request.setCharacterEncoding("UTF-8"); + request.setCharacterEncoding(UTF8); PrintWriter out = response.getWriter(); String responseString = mapper.writeValueAsString(broadcastMessage); @@ -141,8 +147,8 @@ public class BroadcastController extends RestrictedBaseController { return null; } catch (Exception e) { - response.setCharacterEncoding("UTF-8"); - request.setCharacterEncoding("UTF-8"); + response.setCharacterEncoding(UTF8); + request.setCharacterEncoding(UTF8); PrintWriter out = response.getWriter(); out.write("An error occurred while saving the BroadcastMessage in the save () mapping-/broadcast/save "); logger.error(EELFLoggerDelegate.errorLogger, "save() failed", e); diff --git a/ecomp-sdk/epsdk-app-common/src/main/java/org/onap/portalapp/controller/sample/BroadcastListController.java b/ecomp-sdk/epsdk-app-common/src/main/java/org/onap/portalapp/controller/sample/BroadcastListController.java index 8b740d0b..c9c666fb 100644 --- a/ecomp-sdk/epsdk-app-common/src/main/java/org/onap/portalapp/controller/sample/BroadcastListController.java +++ b/ecomp-sdk/epsdk-app-common/src/main/java/org/onap/portalapp/controller/sample/BroadcastListController.java @@ -37,6 +37,7 @@ */ package org.onap.portalapp.controller.sample; +import java.io.IOException; import java.io.PrintWriter; import java.util.HashMap; import java.util.Map; @@ -65,6 +66,8 @@ import com.fasterxml.jackson.databind.ObjectMapper; public class BroadcastListController extends RestrictedBaseController { private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(BroadcastListController.class); + private static final String UTF8 = "UTF-8"; + @Autowired private BroadcastService broadcastService; @@ -78,7 +81,7 @@ public class BroadcastListController extends RestrictedBaseController { @RequestMapping(value = { "/get_broadcast_list" }, method = RequestMethod.GET) public void getBroadcast(HttpServletRequest request, HttpServletResponse response) { - Map<String, Object> model = new HashMap<String, Object>(); + Map<String, Object> model = new HashMap<>(); ObjectMapper mapper = new ObjectMapper(); try { model.put("model", broadcastService.getBcModel(request)); @@ -94,7 +97,7 @@ public class BroadcastListController extends RestrictedBaseController { } @RequestMapping(value = { "/broadcast_list/remove" }, method = RequestMethod.POST) - public ModelAndView remove(HttpServletRequest request, HttpServletResponse response) throws Exception { + public ModelAndView remove(HttpServletRequest request, HttpServletResponse response) throws IOException { try { @@ -106,9 +109,9 @@ public class BroadcastListController extends RestrictedBaseController { broadcastService.removeBroadcastMessage(broadcastMessage); - response.setCharacterEncoding("UTF-8"); + response.setCharacterEncoding(UTF8); response.setContentType("application / json"); - request.setCharacterEncoding("UTF-8"); + request.setCharacterEncoding(UTF8); PrintWriter out = response.getWriter(); String responseString = mapper.writeValueAsString(broadcastMessage); @@ -118,8 +121,8 @@ public class BroadcastListController extends RestrictedBaseController { return null; } catch (Exception e) { - response.setCharacterEncoding("UTF-8"); - request.setCharacterEncoding("UTF-8"); + response.setCharacterEncoding(UTF8); + request.setCharacterEncoding(UTF8); PrintWriter out = response.getWriter(); out.write("An error occurred while removing the BroadcastMessage in the remove ()"); logger.error(EELFLoggerDelegate.errorLogger, "remove() failed", e); @@ -129,7 +132,7 @@ public class BroadcastListController extends RestrictedBaseController { } @RequestMapping(value = { "/broadcast_list/toggleActive" }, method = RequestMethod.POST) - public ModelAndView toggleActive(HttpServletRequest request, HttpServletResponse response) throws Exception { + public ModelAndView toggleActive(HttpServletRequest request, HttpServletResponse response) throws IOException { try { @@ -141,9 +144,9 @@ public class BroadcastListController extends RestrictedBaseController { broadcastService.saveBroadcastMessage(broadcastMessage); - response.setCharacterEncoding("UTF-8"); + response.setCharacterEncoding(UTF8); response.setContentType("application / json"); - request.setCharacterEncoding("UTF-8"); + request.setCharacterEncoding(UTF8); PrintWriter out = response.getWriter(); String responseString = mapper.writeValueAsString(broadcastMessage); @@ -153,8 +156,8 @@ public class BroadcastListController extends RestrictedBaseController { return null; } catch (Exception e) { - response.setCharacterEncoding("UTF-8"); - request.setCharacterEncoding("UTF-8"); + response.setCharacterEncoding(UTF8); + request.setCharacterEncoding(UTF8); PrintWriter out = response.getWriter(); out.write("An error occurred while saving the BroadcastMessage in the toggleActive () "); logger.error(EELFLoggerDelegate.errorLogger, "toggleActive() failed", e); diff --git a/ecomp-sdk/epsdk-app-common/src/test/java/org/onap/portalapp/controller/core/FuncMenuControllerTest.java b/ecomp-sdk/epsdk-app-common/src/test/java/org/onap/portalapp/controller/core/FuncMenuControllerTest.java deleted file mode 100644 index f4cea85c..00000000 --- a/ecomp-sdk/epsdk-app-common/src/test/java/org/onap/portalapp/controller/core/FuncMenuControllerTest.java +++ /dev/null @@ -1,176 +0,0 @@ -/* - * ============LICENSE_START========================================== - * ONAP Portal SDK - * =================================================================== - * Copyright © 2017 AT&T Intellectual Property. All rights reserved. - * =================================================================== - * - * Unless otherwise specified, all software contained herein is licensed - * under the Apache License, Version 2.0 (the "License"); - * you may not use this software except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * Unless otherwise specified, all documentation contained herein is licensed - * under the Creative Commons License, Attribution 4.0 Intl. (the "License"); - * you may not use this documentation except in compliance with the License. - * You may obtain a copy of the License at - * - * https://creativecommons.org/licenses/by/4.0/ - * - * Unless required by applicable law or agreed to in writing, documentation - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ============LICENSE_END============================================ - * - * - */ -package org.onap.portalapp.controller.core; - - -import java.io.IOException; -import java.io.PrintWriter; -import java.io.StringWriter; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.InjectMocks; -import org.mockito.Matchers; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.MockitoAnnotations; -import org.onap.portalapp.framework.MockitoTestSuite; -import org.onap.portalsdk.core.domain.App; -import org.onap.portalsdk.core.domain.User; -import org.onap.portalsdk.core.onboarding.ueb.UebException; -import org.onap.portalsdk.core.onboarding.ueb.UebManager; -import org.onap.portalsdk.core.onboarding.ueb.UebMsg; -import org.onap.portalsdk.core.onboarding.util.PortalApiConstants; -import org.onap.portalsdk.core.onboarding.util.PortalApiProperties; -import org.onap.portalsdk.core.service.AppService; -import org.onap.portalsdk.core.web.support.UserUtils; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; - -import com.fasterxml.jackson.databind.ObjectMapper; - -@RunWith(PowerMockRunner.class) -@PrepareForTest({UebManager.class, PortalApiProperties.class, PortalApiConstants.class}) -public class FuncMenuControllerTest { - - @InjectMocks - FuncMenuController funcMenuController = new FuncMenuController(); - - @Mock - AppService appService; - - @Mock - UebManager uebManager; - - @Before - public void setup() { - MockitoAnnotations.initMocks(this); - } - - MockitoTestSuite mockitoTestSuite = new MockitoTestSuite(); - - HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest(); - HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse(); - - NullPointerException nullPointerException = new NullPointerException(); - - User user = new User(); - - @Mock - UserUtils userUtils = new UserUtils(); - - @Mock - ObjectMapper mapper = new ObjectMapper(); - - @Test - public void functionalMenuUserExistsTest() throws IOException, UebException{ - User user = new User(); - user.setOrgUserId("test12"); - Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user); - StringWriter sw = new StringWriter(); - PrintWriter writer = new PrintWriter(sw); - Mockito.when(mockedResponse.getWriter()).thenReturn(writer); - PowerMockito.mockStatic(UebManager.class); - - Mockito.when(UebManager.getInstance()).thenReturn(uebManager); - Mockito.when(uebManager.requestReply(Matchers.anyObject())).thenReturn(new UebMsg()); - funcMenuController.functionalMenu(mockedRequest, mockedResponse); - } - - @Test - public void functionalMenuUserNotExistsTest() throws IOException, UebException{ - StringWriter sw = new StringWriter(); - PrintWriter writer = new PrintWriter(sw); - Mockito.when(mockedResponse.getWriter()).thenReturn(writer); - - funcMenuController.functionalMenu(mockedRequest, mockedResponse); - } - - @Test - public void functionalMenuViaRestTest() throws IOException, UebException{ - User user = new User(); - user.setOrgUserId("test12"); - Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user); - StringWriter sw = new StringWriter(); - PrintWriter writer = new PrintWriter(sw); - Mockito.when(mockedResponse.getWriter()).thenReturn(writer); - PowerMockito.mockStatic(UebManager.class); - PowerMockito.mockStatic(PortalApiProperties.class); - PowerMockito.mockStatic(PortalApiConstants.class); - Mockito.when(PortalApiProperties - .getProperty(PortalApiConstants.USE_REST_FOR_FUNCTIONAL_MENU)).thenReturn("test"); - Mockito.when(UebManager.getInstance()).thenReturn(uebManager); - funcMenuController.functionalMenu(mockedRequest, mockedResponse); - } - - @Test - public void functionalMenuViaRestAppNullTest() throws IOException, UebException{ - App app = new App(); - User user = new User(); - user.setOrgUserId("test12"); - Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user); - StringWriter sw = new StringWriter(); - PrintWriter writer = new PrintWriter(sw); - Mockito.when(mockedResponse.getWriter()).thenReturn(writer); - Mockito.when(appService.getDefaultApp()).thenReturn(app); - PowerMockito.mockStatic(UebManager.class); - PowerMockito.mockStatic(PortalApiProperties.class); - PowerMockito.mockStatic(PortalApiConstants.class); - Mockito.when(PortalApiProperties - .getProperty(PortalApiConstants.USE_REST_FOR_FUNCTIONAL_MENU)).thenReturn("test"); - Mockito.when(UebManager.getInstance()).thenReturn(uebManager); - funcMenuController.functionalMenu(mockedRequest, mockedResponse); - } - - @Test - public void functionalMenuExceptionTest() throws IOException{ - User user = new User(); - user.setOrgUserId("test12"); - Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user); - StringWriter sw = new StringWriter(); - PrintWriter writer = new PrintWriter(sw); - Mockito.when(mockedResponse.getWriter()).thenReturn(writer); - funcMenuController.functionalMenu(mockedRequest, mockedResponse); - } - -} |