@Test public final void testHandle() { this.testSetAgencyUserManager(); this.testSetAuthorityManager(); this.testSetProfileManager(); performTestHandle(ProfileListCommand.ACTION_LIST, -1, "", false); performTestHandle(ProfileListCommand.ACTION_LIST, -1, "", true); performTestHandle(ProfileListCommand.ACTION_LIST, Privilege.SCOPE_AGENCY, Privilege.VIEW_PROFILES, false); performTestHandle(ProfileListCommand.ACTION_LIST, Privilege.SCOPE_AGENCY, Privilege.VIEW_PROFILES, true); performTestHandle(ProfileListCommand.ACTION_LIST, Privilege.SCOPE_AGENCY, Privilege.MANAGE_PROFILES, false); performTestHandle(ProfileListCommand.ACTION_LIST, Privilege.SCOPE_AGENCY, Privilege.MANAGE_PROFILES, true); performTestHandle(ProfileListCommand.ACTION_LIST, Privilege.SCOPE_ALL, Privilege.VIEW_PROFILES, false); performTestHandle(ProfileListCommand.ACTION_LIST, Privilege.SCOPE_ALL, Privilege.VIEW_PROFILES, true); performTestHandle(ProfileListCommand.ACTION_LIST, Privilege.SCOPE_ALL, Privilege.MANAGE_PROFILES, false); performTestHandle(ProfileListCommand.ACTION_LIST, Privilege.SCOPE_ALL, Privilege.MANAGE_PROFILES, true); performTestHandle(ProfileListCommand.ACTION_FILTER, -1, "", false); performTestHandle(ProfileListCommand.ACTION_FILTER, -1, "", true); performTestHandle(ProfileListCommand.ACTION_FILTER, Privilege.SCOPE_AGENCY, Privilege.VIEW_PROFILES, false); performTestHandle(ProfileListCommand.ACTION_FILTER, Privilege.SCOPE_AGENCY, Privilege.VIEW_PROFILES, true); performTestHandle(ProfileListCommand.ACTION_FILTER, Privilege.SCOPE_AGENCY, Privilege.MANAGE_PROFILES, false); performTestHandle(ProfileListCommand.ACTION_FILTER, Privilege.SCOPE_AGENCY, Privilege.MANAGE_PROFILES, true); performTestHandle(ProfileListCommand.ACTION_FILTER, Privilege.SCOPE_ALL, Privilege.VIEW_PROFILES, false); performTestHandle(ProfileListCommand.ACTION_FILTER, Privilege.SCOPE_ALL, Privilege.VIEW_PROFILES, true); performTestHandle(ProfileListCommand.ACTION_FILTER, Privilege.SCOPE_ALL, Privilege.MANAGE_PROFILES, false); performTestHandle(ProfileListCommand.ACTION_FILTER, Privilege.SCOPE_ALL, Privilege.MANAGE_PROFILES, true); }
@Override protected ModelAndView handle(HttpServletRequest req, HttpServletResponse res, Object comm, BindException errors) throws Exception { ProfileListCommand command = (ProfileListCommand) comm; req.getSession().setAttribute(ProfileListController.SESSION_KEY_SHOW_INACTIVE, command.isShowInactive()); if (command.getActionCommand().equals(ProfileListCommand.ACTION_LIST)) { String defaultAgency = (String) req.getSession().getAttribute(ProfileListController.SESSION_AGENCY_FILTER); if (defaultAgency == null) { defaultAgency = AuthUtil.getRemoteUserObject().getAgency().getName(); } command.setDefaultAgency(defaultAgency); String harvesterType = (String) req.getSession().getAttribute(ProfileListController.SESSION_HARVESTER_TYPE_FILTER); if (harvesterType != null) { command.setHarvesterType(harvesterType); } } else if (command.getActionCommand().equals(ProfileListCommand.ACTION_FILTER)) { req.getSession().setAttribute(ProfileListController.SESSION_AGENCY_FILTER, command.getDefaultAgency()); req.getSession().setAttribute(ProfileListController.SESSION_HARVESTER_TYPE_FILTER, command.getHarvesterType()); } else if (command.getActionCommand().equals(ProfileListCommand.ACTION_IMPORT)) { MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) req; CommonsMultipartFile uploadedFile = (CommonsMultipartFile) multipartRequest.getFile("sourceFile"); Profile profile = new Profile(); profile.setProfile(new String(uploadedFile.getBytes())); profile.setDescription("Imported profile"); profile.setImported(true); String importAgency = req.getParameter("importAgency"); String importType = req.getParameter("importType"); String importName = req.getParameter("importName"); if (StringUtils.isBlank(importName)) { Date now = new Date(); profile.setName("Profile imported on " + now.toString()); } else { profile.setName(importName); } if (StringUtils.isBlank(importAgency)) { profile.setOwningAgency(AuthUtil.getRemoteUserObject().getAgency()); } else { long agencyOid = Long.parseLong(importAgency); Agency agency = agencyUserManager.getAgencyByOid(agencyOid); profile.setOwningAgency(agency); } if (StringUtils.isBlank(importType)) { profile.setHarvesterType(HarvesterType.DEFAULT.name()); } else { profile.setHarvesterType(importType); } if (profile.getHarvesterType().equals(HarvesterType.HERITRIX3.name())) { HarvestAgent agent = getHarvestAgent(); if (!agent.isValidProfile(profile.getProfile())) { Object[] vals = new Object[]{profile.getProfile()}; log.info("Validation failed for H3 profile"); errors.reject("profile.invalid", vals, "The submitted profile is invalid."); ModelAndView mav = getView(command); mav.addObject(Constants.GBL_ERRORS, errors); return mav; } } try { profileManager.saveOrUpdate(profile); } catch (HibernateOptimisticLockingFailureException e) { Object[] vals = new Object[]{profile.getName(), profile.getOwningAgency().getName()}; errors.reject("profile.modified", vals, "profile has been modified by another user."); ModelAndView mav = getView(command); mav.addObject(Constants.GBL_ERRORS, errors); return mav; } return new ModelAndView("redirect:/curator/profiles/list.html"); } ModelAndView mav = getView(command); mav.addObject(Constants.GBL_CMD_DATA, command); return mav; }
ProfileListController extends AbstractCommandController { @Override protected ModelAndView handle(HttpServletRequest req, HttpServletResponse res, Object comm, BindException errors) throws Exception { ProfileListCommand command = (ProfileListCommand) comm; req.getSession().setAttribute(ProfileListController.SESSION_KEY_SHOW_INACTIVE, command.isShowInactive()); if (command.getActionCommand().equals(ProfileListCommand.ACTION_LIST)) { String defaultAgency = (String) req.getSession().getAttribute(ProfileListController.SESSION_AGENCY_FILTER); if (defaultAgency == null) { defaultAgency = AuthUtil.getRemoteUserObject().getAgency().getName(); } command.setDefaultAgency(defaultAgency); String harvesterType = (String) req.getSession().getAttribute(ProfileListController.SESSION_HARVESTER_TYPE_FILTER); if (harvesterType != null) { command.setHarvesterType(harvesterType); } } else if (command.getActionCommand().equals(ProfileListCommand.ACTION_FILTER)) { req.getSession().setAttribute(ProfileListController.SESSION_AGENCY_FILTER, command.getDefaultAgency()); req.getSession().setAttribute(ProfileListController.SESSION_HARVESTER_TYPE_FILTER, command.getHarvesterType()); } else if (command.getActionCommand().equals(ProfileListCommand.ACTION_IMPORT)) { MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) req; CommonsMultipartFile uploadedFile = (CommonsMultipartFile) multipartRequest.getFile("sourceFile"); Profile profile = new Profile(); profile.setProfile(new String(uploadedFile.getBytes())); profile.setDescription("Imported profile"); profile.setImported(true); String importAgency = req.getParameter("importAgency"); String importType = req.getParameter("importType"); String importName = req.getParameter("importName"); if (StringUtils.isBlank(importName)) { Date now = new Date(); profile.setName("Profile imported on " + now.toString()); } else { profile.setName(importName); } if (StringUtils.isBlank(importAgency)) { profile.setOwningAgency(AuthUtil.getRemoteUserObject().getAgency()); } else { long agencyOid = Long.parseLong(importAgency); Agency agency = agencyUserManager.getAgencyByOid(agencyOid); profile.setOwningAgency(agency); } if (StringUtils.isBlank(importType)) { profile.setHarvesterType(HarvesterType.DEFAULT.name()); } else { profile.setHarvesterType(importType); } if (profile.getHarvesterType().equals(HarvesterType.HERITRIX3.name())) { HarvestAgent agent = getHarvestAgent(); if (!agent.isValidProfile(profile.getProfile())) { Object[] vals = new Object[]{profile.getProfile()}; log.info("Validation failed for H3 profile"); errors.reject("profile.invalid", vals, "The submitted profile is invalid."); ModelAndView mav = getView(command); mav.addObject(Constants.GBL_ERRORS, errors); return mav; } } try { profileManager.saveOrUpdate(profile); } catch (HibernateOptimisticLockingFailureException e) { Object[] vals = new Object[]{profile.getName(), profile.getOwningAgency().getName()}; errors.reject("profile.modified", vals, "profile has been modified by another user."); ModelAndView mav = getView(command); mav.addObject(Constants.GBL_ERRORS, errors); return mav; } return new ModelAndView("redirect:/curator/profiles/list.html"); } ModelAndView mav = getView(command); mav.addObject(Constants.GBL_CMD_DATA, command); return mav; } }
ProfileListController extends AbstractCommandController { @Override protected ModelAndView handle(HttpServletRequest req, HttpServletResponse res, Object comm, BindException errors) throws Exception { ProfileListCommand command = (ProfileListCommand) comm; req.getSession().setAttribute(ProfileListController.SESSION_KEY_SHOW_INACTIVE, command.isShowInactive()); if (command.getActionCommand().equals(ProfileListCommand.ACTION_LIST)) { String defaultAgency = (String) req.getSession().getAttribute(ProfileListController.SESSION_AGENCY_FILTER); if (defaultAgency == null) { defaultAgency = AuthUtil.getRemoteUserObject().getAgency().getName(); } command.setDefaultAgency(defaultAgency); String harvesterType = (String) req.getSession().getAttribute(ProfileListController.SESSION_HARVESTER_TYPE_FILTER); if (harvesterType != null) { command.setHarvesterType(harvesterType); } } else if (command.getActionCommand().equals(ProfileListCommand.ACTION_FILTER)) { req.getSession().setAttribute(ProfileListController.SESSION_AGENCY_FILTER, command.getDefaultAgency()); req.getSession().setAttribute(ProfileListController.SESSION_HARVESTER_TYPE_FILTER, command.getHarvesterType()); } else if (command.getActionCommand().equals(ProfileListCommand.ACTION_IMPORT)) { MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) req; CommonsMultipartFile uploadedFile = (CommonsMultipartFile) multipartRequest.getFile("sourceFile"); Profile profile = new Profile(); profile.setProfile(new String(uploadedFile.getBytes())); profile.setDescription("Imported profile"); profile.setImported(true); String importAgency = req.getParameter("importAgency"); String importType = req.getParameter("importType"); String importName = req.getParameter("importName"); if (StringUtils.isBlank(importName)) { Date now = new Date(); profile.setName("Profile imported on " + now.toString()); } else { profile.setName(importName); } if (StringUtils.isBlank(importAgency)) { profile.setOwningAgency(AuthUtil.getRemoteUserObject().getAgency()); } else { long agencyOid = Long.parseLong(importAgency); Agency agency = agencyUserManager.getAgencyByOid(agencyOid); profile.setOwningAgency(agency); } if (StringUtils.isBlank(importType)) { profile.setHarvesterType(HarvesterType.DEFAULT.name()); } else { profile.setHarvesterType(importType); } if (profile.getHarvesterType().equals(HarvesterType.HERITRIX3.name())) { HarvestAgent agent = getHarvestAgent(); if (!agent.isValidProfile(profile.getProfile())) { Object[] vals = new Object[]{profile.getProfile()}; log.info("Validation failed for H3 profile"); errors.reject("profile.invalid", vals, "The submitted profile is invalid."); ModelAndView mav = getView(command); mav.addObject(Constants.GBL_ERRORS, errors); return mav; } } try { profileManager.saveOrUpdate(profile); } catch (HibernateOptimisticLockingFailureException e) { Object[] vals = new Object[]{profile.getName(), profile.getOwningAgency().getName()}; errors.reject("profile.modified", vals, "profile has been modified by another user."); ModelAndView mav = getView(command); mav.addObject(Constants.GBL_ERRORS, errors); return mav; } return new ModelAndView("redirect:/curator/profiles/list.html"); } ModelAndView mav = getView(command); mav.addObject(Constants.GBL_CMD_DATA, command); return mav; } ProfileListController(); }
ProfileListController extends AbstractCommandController { @Override protected ModelAndView handle(HttpServletRequest req, HttpServletResponse res, Object comm, BindException errors) throws Exception { ProfileListCommand command = (ProfileListCommand) comm; req.getSession().setAttribute(ProfileListController.SESSION_KEY_SHOW_INACTIVE, command.isShowInactive()); if (command.getActionCommand().equals(ProfileListCommand.ACTION_LIST)) { String defaultAgency = (String) req.getSession().getAttribute(ProfileListController.SESSION_AGENCY_FILTER); if (defaultAgency == null) { defaultAgency = AuthUtil.getRemoteUserObject().getAgency().getName(); } command.setDefaultAgency(defaultAgency); String harvesterType = (String) req.getSession().getAttribute(ProfileListController.SESSION_HARVESTER_TYPE_FILTER); if (harvesterType != null) { command.setHarvesterType(harvesterType); } } else if (command.getActionCommand().equals(ProfileListCommand.ACTION_FILTER)) { req.getSession().setAttribute(ProfileListController.SESSION_AGENCY_FILTER, command.getDefaultAgency()); req.getSession().setAttribute(ProfileListController.SESSION_HARVESTER_TYPE_FILTER, command.getHarvesterType()); } else if (command.getActionCommand().equals(ProfileListCommand.ACTION_IMPORT)) { MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) req; CommonsMultipartFile uploadedFile = (CommonsMultipartFile) multipartRequest.getFile("sourceFile"); Profile profile = new Profile(); profile.setProfile(new String(uploadedFile.getBytes())); profile.setDescription("Imported profile"); profile.setImported(true); String importAgency = req.getParameter("importAgency"); String importType = req.getParameter("importType"); String importName = req.getParameter("importName"); if (StringUtils.isBlank(importName)) { Date now = new Date(); profile.setName("Profile imported on " + now.toString()); } else { profile.setName(importName); } if (StringUtils.isBlank(importAgency)) { profile.setOwningAgency(AuthUtil.getRemoteUserObject().getAgency()); } else { long agencyOid = Long.parseLong(importAgency); Agency agency = agencyUserManager.getAgencyByOid(agencyOid); profile.setOwningAgency(agency); } if (StringUtils.isBlank(importType)) { profile.setHarvesterType(HarvesterType.DEFAULT.name()); } else { profile.setHarvesterType(importType); } if (profile.getHarvesterType().equals(HarvesterType.HERITRIX3.name())) { HarvestAgent agent = getHarvestAgent(); if (!agent.isValidProfile(profile.getProfile())) { Object[] vals = new Object[]{profile.getProfile()}; log.info("Validation failed for H3 profile"); errors.reject("profile.invalid", vals, "The submitted profile is invalid."); ModelAndView mav = getView(command); mav.addObject(Constants.GBL_ERRORS, errors); return mav; } } try { profileManager.saveOrUpdate(profile); } catch (HibernateOptimisticLockingFailureException e) { Object[] vals = new Object[]{profile.getName(), profile.getOwningAgency().getName()}; errors.reject("profile.modified", vals, "profile has been modified by another user."); ModelAndView mav = getView(command); mav.addObject(Constants.GBL_ERRORS, errors); return mav; } return new ModelAndView("redirect:/curator/profiles/list.html"); } ModelAndView mav = getView(command); mav.addObject(Constants.GBL_CMD_DATA, command); return mav; } ProfileListController(); void setProfileManager(ProfileManager profileManager); void setAgencyUserManager(AgencyUserManager agencyUserManager); void setAuthorityManager(AuthorityManager authorityManager); }
ProfileListController extends AbstractCommandController { @Override protected ModelAndView handle(HttpServletRequest req, HttpServletResponse res, Object comm, BindException errors) throws Exception { ProfileListCommand command = (ProfileListCommand) comm; req.getSession().setAttribute(ProfileListController.SESSION_KEY_SHOW_INACTIVE, command.isShowInactive()); if (command.getActionCommand().equals(ProfileListCommand.ACTION_LIST)) { String defaultAgency = (String) req.getSession().getAttribute(ProfileListController.SESSION_AGENCY_FILTER); if (defaultAgency == null) { defaultAgency = AuthUtil.getRemoteUserObject().getAgency().getName(); } command.setDefaultAgency(defaultAgency); String harvesterType = (String) req.getSession().getAttribute(ProfileListController.SESSION_HARVESTER_TYPE_FILTER); if (harvesterType != null) { command.setHarvesterType(harvesterType); } } else if (command.getActionCommand().equals(ProfileListCommand.ACTION_FILTER)) { req.getSession().setAttribute(ProfileListController.SESSION_AGENCY_FILTER, command.getDefaultAgency()); req.getSession().setAttribute(ProfileListController.SESSION_HARVESTER_TYPE_FILTER, command.getHarvesterType()); } else if (command.getActionCommand().equals(ProfileListCommand.ACTION_IMPORT)) { MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) req; CommonsMultipartFile uploadedFile = (CommonsMultipartFile) multipartRequest.getFile("sourceFile"); Profile profile = new Profile(); profile.setProfile(new String(uploadedFile.getBytes())); profile.setDescription("Imported profile"); profile.setImported(true); String importAgency = req.getParameter("importAgency"); String importType = req.getParameter("importType"); String importName = req.getParameter("importName"); if (StringUtils.isBlank(importName)) { Date now = new Date(); profile.setName("Profile imported on " + now.toString()); } else { profile.setName(importName); } if (StringUtils.isBlank(importAgency)) { profile.setOwningAgency(AuthUtil.getRemoteUserObject().getAgency()); } else { long agencyOid = Long.parseLong(importAgency); Agency agency = agencyUserManager.getAgencyByOid(agencyOid); profile.setOwningAgency(agency); } if (StringUtils.isBlank(importType)) { profile.setHarvesterType(HarvesterType.DEFAULT.name()); } else { profile.setHarvesterType(importType); } if (profile.getHarvesterType().equals(HarvesterType.HERITRIX3.name())) { HarvestAgent agent = getHarvestAgent(); if (!agent.isValidProfile(profile.getProfile())) { Object[] vals = new Object[]{profile.getProfile()}; log.info("Validation failed for H3 profile"); errors.reject("profile.invalid", vals, "The submitted profile is invalid."); ModelAndView mav = getView(command); mav.addObject(Constants.GBL_ERRORS, errors); return mav; } } try { profileManager.saveOrUpdate(profile); } catch (HibernateOptimisticLockingFailureException e) { Object[] vals = new Object[]{profile.getName(), profile.getOwningAgency().getName()}; errors.reject("profile.modified", vals, "profile has been modified by another user."); ModelAndView mav = getView(command); mav.addObject(Constants.GBL_ERRORS, errors); return mav; } return new ModelAndView("redirect:/curator/profiles/list.html"); } ModelAndView mav = getView(command); mav.addObject(Constants.GBL_CMD_DATA, command); return mav; } ProfileListController(); void setProfileManager(ProfileManager profileManager); void setAgencyUserManager(AgencyUserManager agencyUserManager); void setAuthorityManager(AuthorityManager authorityManager); static final String SESSION_KEY_SHOW_INACTIVE; static final String SESSION_AGENCY_FILTER; static final String SESSION_HARVESTER_TYPE_FILTER; }