List of usage examples for org.springframework.web.servlet ModelAndView getModel
public Map<String, Object> getModel()
From source file:org.jasig.cas.support.oauth.web.OAuth20ServiceValidateControllerTests.java
@Test public void verifyBypassCASWithNoServiceTicketWrongSuccessView() throws Exception { final MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET", URL); final WebApplicationService webApplicationService = mock(WebApplicationService.class); when(webApplicationService.getArtifactId()).thenReturn(SERVICE_TICKET_ID); final ArgumentExtractor argumentExtractor = mock(ArgumentExtractor.class); when(argumentExtractor.extractService(mockRequest)).thenReturn(webApplicationService); final TicketGrantingTicket ticketGrantingTicket = mock(TicketGrantingTicket.class); final ServiceTicket serviceTicket = mock(ServiceTicket.class); when(serviceTicket.getGrantingTicket()).thenReturn(ticketGrantingTicket); when(serviceTicket.getService()).thenReturn(webApplicationService); final Assertion assertion = mock(Assertion.class); final CentralAuthenticationService centralAuthenticationService = mock(CentralAuthenticationService.class); when(centralAuthenticationService.getTicket(SERVICE_TICKET_ID, Ticket.class)).thenReturn(null); when(centralAuthenticationService.validateServiceTicket(SERVICE_TICKET_ID, webApplicationService)) .thenReturn(assertion);// ww w . j ava2 s .c o m final Set<String> scopes = new HashSet<>(); scopes.add(SCOPE1); scopes.add(SCOPE2); final AccessToken accessToken = mock(AccessToken.class); when(accessToken.getId()).thenReturn(AT_ID); when(accessToken.getScopes()).thenReturn(scopes); final CentralOAuthService centralOAuthService = mock(CentralOAuthService.class); when(centralOAuthService.grantCASAccessToken(ticketGrantingTicket, webApplicationService)) .thenReturn(accessToken); final WebApplicationContext webApplicationContext = mock(WebApplicationContext.class); final OAuth20ServiceValidateController oauth20ServiceValidateController = new OAuth20ServiceValidateController(); oauth20ServiceValidateController.initApplicationContext(webApplicationContext); oauth20ServiceValidateController.setArgumentExtractor(argumentExtractor); oauth20ServiceValidateController.setCentralAuthenticationService(centralAuthenticationService); oauth20ServiceValidateController.setCentralOAuthService(centralOAuthService); oauth20ServiceValidateController.setSuccessView(SUCCESS_VIEW); final MockHttpServletResponse mockResponse = new MockHttpServletResponse(); final ModelAndView modelAndView = oauth20ServiceValidateController.handleRequest(mockRequest, mockResponse); assertEquals(HttpStatus.SC_OK, mockResponse.getStatus()); assertEquals("", mockResponse.getContentAsString()); final Map<String, Object> model = modelAndView.getModel(); assertTrue(!model.containsKey(OAuthConstants.CAS_PROTOCOL_ACCESS_TOKEN)); assertTrue(!model.containsKey(OAuthConstants.CAS_PROTOCOL_ACCESS_TOKEN_SCOPE)); }
From source file:org.jasig.cas.support.oauth.web.OAuth20ServiceValidateControllerTests.java
@Test public void verifyBypassCASWithWrongSuccessView() throws Exception { final MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET", URL); final WebApplicationService webApplicationService = mock(WebApplicationService.class); when(webApplicationService.getArtifactId()).thenReturn(SERVICE_TICKET_ID); final ArgumentExtractor argumentExtractor = mock(ArgumentExtractor.class); when(argumentExtractor.extractService(mockRequest)).thenReturn(webApplicationService); final TicketGrantingTicket ticketGrantingTicket = mock(TicketGrantingTicket.class); final ServiceTicket serviceTicket = mock(ServiceTicket.class); when(serviceTicket.getGrantingTicket()).thenReturn(ticketGrantingTicket); when(serviceTicket.getService()).thenReturn(webApplicationService); final Assertion assertion = mock(Assertion.class); final CentralAuthenticationService centralAuthenticationService = mock(CentralAuthenticationService.class); when(centralAuthenticationService.getTicket(SERVICE_TICKET_ID, Ticket.class)).thenReturn(serviceTicket); when(centralAuthenticationService.validateServiceTicket(SERVICE_TICKET_ID, webApplicationService)) .thenThrow(new TicketCreationException()); final Set<String> scopes = new HashSet<>(); scopes.add(SCOPE1);/* w ww. ja v a2 s . c o m*/ scopes.add(SCOPE2); final AccessToken accessToken = mock(AccessToken.class); when(accessToken.getId()).thenReturn(AT_ID); when(accessToken.getScopes()).thenReturn(scopes); final CentralOAuthService centralOAuthService = mock(CentralOAuthService.class); when(centralOAuthService.grantCASAccessToken(ticketGrantingTicket, webApplicationService)) .thenReturn(accessToken); final WebApplicationContext webApplicationContext = mock(WebApplicationContext.class); final OAuth20ServiceValidateController oauth20ServiceValidateController = new OAuth20ServiceValidateController(); oauth20ServiceValidateController.initApplicationContext(webApplicationContext); oauth20ServiceValidateController.setArgumentExtractor(argumentExtractor); oauth20ServiceValidateController.setCentralAuthenticationService(centralAuthenticationService); oauth20ServiceValidateController.setCentralOAuthService(centralOAuthService); oauth20ServiceValidateController.setSuccessView(SUCCESS_VIEW); final MockHttpServletResponse mockResponse = new MockHttpServletResponse(); final ModelAndView modelAndView = oauth20ServiceValidateController.handleRequest(mockRequest, mockResponse); assertEquals(HttpStatus.SC_OK, mockResponse.getStatus()); assertEquals("", mockResponse.getContentAsString()); final Map<String, Object> model = modelAndView.getModel(); assertTrue(!model.containsKey(OAuthConstants.CAS_PROTOCOL_ACCESS_TOKEN)); assertTrue(!model.containsKey(OAuthConstants.CAS_PROTOCOL_ACCESS_TOKEN_SCOPE)); }
From source file:com.zb.app.web.controller.manage.ManageCmsController.java
@RequestMapping(value = "/site/tag.htm") public ModelAndView tag(ModelAndView mav, TravelLabelCategoryQuery query, Integer page) { query.setPageSize(1000);/* w w w. j a v a 2 s .com*/ query.setNowPageIndex(Argument.isNotPositive(page) ? 0 : page - 1); PaginationList<TravelLabelCategoryDO> list = cmsService.listPagination(query, new DefaultIpageUrl()); mav.getModel().put(CustomVelocityLayoutView.USE_LAYOUT, "false"); mav.addObject("labelList", list); mav.addObject("pagination", list.getQuery()); mav.setViewName("/manage/site/tag"); return mav; }
From source file:com.zb.app.web.controller.login.LoginController.java
/*** * ?// ww w . j a v a2s . c o m * * @return */ @RequestMapping(value = "/userlogin.htm") public ModelAndView userlogin(ModelAndView model, String type) { if (WebUserTools.hasLogin()) { return new ModelAndView("redirect:" + WebUserTools.getCompanyType().getIndexUrl()); } model.getModel().put(CustomVelocityLayoutView.USE_LAYOUT, "false"); model.addObject("type", CompanyTypeEnum.getEnum(type == null ? "tour" : type).getValue()); model.setViewName("/login/userlogin"); return model; }
From source file:ServerConfigurations.server.AdminConfiguration.Controller.java
@Override public ModelAndView doHandle(HttpServletRequest request, HttpServletResponse response) { try {//from w w w .j a va 2s. c om SUser user = SessionUser.getUser(request); if (this.isAvailable(user)) { String action = request.getParameter("action"); if (action == null || action.trim().length() == 0) action = "list"; else action = action.trim(); boolean isPost = this.isPost(request); if (action.equals("list")) { // List configurations and templates loadConfiguration(); //TODO: remove(DEBUG!!!) ModelAndView modelAndView = new ModelAndView(this.listPagePath); modelAndView.getModel().put("pluginName", Util.PLUGIN_NAME); modelAndView.getModel().put("controllerPath", CONTROLLER_PATH); modelAndView.getModel().put(CONFIGURATIONS_TAG, Controller.configurations.getConfigurationsList()); modelAndView.getModel().put(TEMPLATES_TAG, Controller.configurations.getTemplatesList()); return modelAndView; } else if (action.equals("edit")) { // Edit configuration form String configurationName = request.getParameter("name"); if (configurationName != null) { ServerConfiguration configuration = Controller.configurations .getConfigurationByName(configurationName); if (configuration != null) { configuration.setTemplateName(configuration.getTemplateName()); this.saveConfiguration(); ModelAndView modelAndView = new ModelAndView(this.editPagePath); modelAndView.getModel().put("pluginName", Util.PLUGIN_NAME); modelAndView.getModel().put("controllerPath", CONTROLLER_PATH); modelAndView.getModel().put(TEMPLATES_TAG, Controller.configurations.getTemplatesList()); for (Map.Entry<String, Object> entry : configuration.getConfigurationMap().entrySet()) { modelAndView.getModel().put(entry.getKey(), entry.getValue()); } return modelAndView; } else { getOrCreateMessages(request).addMessage("serverConfigurationMessage", "Cannot find server configuration with name '" + configurationName + "'"); } } return new ModelAndView(this.errorPagePath); } else if (action.equals("new")) { // Create new configuration form ModelAndView modelAndView = new ModelAndView(this.editPagePath); modelAndView.getModel().put("pluginName", Util.PLUGIN_NAME); modelAndView.getModel().put("controllerPath", CONTROLLER_PATH); modelAndView.getModel().put(TEMPLATES_TAG, Controller.configurations.getTemplatesList()); return modelAndView; } else if (action.equals("save") && isPost) { // Save configuration // Check required fields if (!this.hasRequestParameter(request, "name") && !this.hasRequestParameter(request, "initialName")) { return this.sendError(request, response, "Name field cannot be empty"); } if (!this.hasRequestParameter(request, "templateName")) { return this.sendError(request, response, "Template field cannot be empty"); } if (this.hasRequestParameter(request, "name") && this.hasRequestParameter(request, "initialName")) { return this.sendError(request, response, "Cannot change name for configuration"); } // Find/create configuration ServerConfiguration serverConfiguration = null; if (!this.hasRequestParameter(request, "initialName")) { if (Controller.configurations .getConfigurationByName(request.getParameter("name")) != null) { return this.sendError(request, response, "There is another configuration with such name, please choose another"); } else { serverConfiguration = new ServerConfiguration(); serverConfiguration.setName(request.getParameter("name")); Controller.configurations.setConfiguration(serverConfiguration); } } else { serverConfiguration = Controller.configurations .getConfigurationByName(request.getParameter("initialName")); } // Set configuration data serverConfiguration.setTemplateName(request.getParameter("templateName")); String[] propKeys = request.getParameterMap().get("key"); String[] propValues = request.getParameterMap().get("value"); String[] propChanged = request.getParameterMap().get("changed"); if (propKeys != null && propValues != null && propChanged != null) { Map<String, String> properties = new LinkedHashMap<String, String>(); for (Integer i = 0; i < propKeys.length; i++) { if (propValues.length > i && propChanged.length > i && propChanged[i].equals("true")) { properties.put(propKeys[i], propValues[i]); } } serverConfiguration.setProperties(properties); } else { serverConfiguration.clearProperties(); } this.saveConfiguration(); return this.sendMessage(request, response, "Server configuration saved successfully"); } else if (action.equals("delete")) { // Delete configuration String configurationName = request.getParameter("name"); for (String projectId : this.myProjectManager.getProjectIds()) { ProjectConfigurations projectSettings = (ProjectConfigurations) this.projectSettingsManager .getSettings(projectId, Util.PLUGIN_NAME); if (projectSettings.getConfigurationByName(configurationName) != null) { getOrCreateMessages(request).addMessage("serverConfigurationMessage", "You cannot delete this server configuration, it is used in projects"); return new ModelAndView( new RedirectView("/admin/admin.html?item=" + Util.PLUGIN_NAME, true)); } } ServerConfiguration serverConfiguration = Controller.configurations .getConfigurationByName(configurationName); Controller.configurations.deleteConfiguration(serverConfiguration); this.saveConfiguration(); return new ModelAndView(new RedirectView("/admin/admin.html?item=" + Util.PLUGIN_NAME, true)); } else if (action.equals("newTemplate")) { // New template form ModelAndView modelAndView = new ModelAndView(this.editTemplPagePath); modelAndView.getModel().put("pluginName", Util.PLUGIN_NAME); modelAndView.getModel().put("controllerPath", CONTROLLER_PATH); modelAndView.getModel().put("types", Arrays.asList(PropertiesType.values())); return modelAndView; } else if (action.equals("editTemplate")) { // Edit template form String templateName = request.getParameter("name"); if (templateName != null) { Template template = Controller.configurations.getTemplateByName(templateName); if (template != null) { ModelAndView modelAndView = new ModelAndView(this.editTemplPagePath); modelAndView.getModel().put("pluginName", Util.PLUGIN_NAME); modelAndView.getModel().put("controllerPath", CONTROLLER_PATH); modelAndView.getModel().put("types", Arrays.asList(PropertiesType.values())); for (Map.Entry<String, Object> entry : template.getTemplateMap().entrySet()) { modelAndView.getModel().put(entry.getKey(), entry.getValue()); } return modelAndView; } else { getOrCreateMessages(request).addMessage("serverConfigurationMessage", "Cannot find template with name '" + templateName + "'"); } } return new ModelAndView(this.errorPagePath); } else if (action.equals("saveTemplate") && isPost) { // Save template // Check required fields if (!this.hasRequestParameter(request, "name") && !this.hasRequestParameter(request, "initialName")) { return this.sendError(request, response, "Name field cannot be empty"); } if (this.hasRequestParameter(request, "name") && this.hasRequestParameter(request, "initialName")) { return this.sendError(request, response, "Cannot change name for template"); } String[] propNames = request.getParameterMap().get("key"); String[] propTypes = request.getParameterMap().get("type"); if (propNames != null) { for (Integer i = 0; i < propNames.length; i++) { if (propNames[i].trim().equals("")) { return this.sendError(request, response, "Property name field cannot be empty"); } } } // Find/create template Template template = null; if (this.hasRequestParameter(request, "initialName")) { template = Controller.configurations.getTemplateByName(request.getParameter("initialName")); } else { template = new Template(); template.setName(request.getParameter("name")); Controller.configurations.setTemplate(template); } // Set template data template.clearProperties(); if (propNames != null && propTypes != null) { for (Integer i = 0; i < propNames.length; i++) { if (propTypes.length > i) template.setProperty(propNames[i], PropertiesType.valueOf(propTypes[i])); } } this.saveConfiguration(); return this.sendMessage(request, response, "Template saved successfully"); } else if (action.equals("deleteTemplate")) { // Delete template String templateName = request.getParameter("name"); for (ServerConfiguration configuration : configurations.getConfigurations()) { if (configuration.getTemplateName().equals(templateName)) { getOrCreateMessages(request).addMessage("serverConfigurationMessage", "You cannot delete this template, it is used in server configurations"); return new ModelAndView( new RedirectView("/admin/admin.html?item=" + Util.PLUGIN_NAME, true)); } } Template template = Controller.configurations.getTemplateByName(templateName); Controller.configurations.deleteTemplate(template); this.saveConfiguration(); return new ModelAndView(new RedirectView("/admin/admin.html?item=" + Util.PLUGIN_NAME, true)); } } else { WebAuthUtil.addAccessDeniedMessage(request, new AccessDeniedException(user, "You must be an System Administrator or Global ProjectConfiguration Administrator to manage server configurations.")); return null; } } catch (Exception e) { Loggers.SERVER.error(Util.PLUGIN_NAME + ": Could not handle request", e); } return null; }
From source file:com.mmj.app.common.component.ComponentController.java
@ExceptionHandler(Throwable.class) public ModelAndView handleIOException(Throwable e) throws Throwable { if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class) != null) { throw e;// w w w . j a v a2 s .c o m } if (request == null && response == null) { throw e; } if (request == null && response != null) { response.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=UTF-8"); OutputStream out = response.getOutputStream(); PrintWriter pw = new PrintWriter(new OutputStreamWriter(out, "utf-8")); pw.println("{\"code\":-1,\"message\":\",?!\",\"data\":\"\"}"); pw.flush(); pw.close(); } ModelAndView mav = new ModelAndView(); if (InvokeTypeTools.isAjax(request)) { return createJsonMav(",?!", ResultCode.ERROR, e.getMessage()); } mav.addObject("exception", e.getCause() == null ? StringUtils.EMPTY : e.getCause().toString()); mav.addObject("msg", e.getMessage()); mav.addObject("stackTrace", e.getStackTrace().toString()); if (request.getRequestURI() != null) { mav.addObject("url", request.getRequestURI().toString()); } mav.getModel().put(CustomVelocityLayoutView.USE_LAYOUT, "false"); mav.setViewName("error"); return mav; }
From source file:alpha.portal.webapp.controller.CaseFormControllerTest.java
/** * Test last.//w ww. ja v a 2 s.c o m * * @throws Exception * the exception */ @Test public void testLast() throws Exception { MockHttpServletRequest request = this.newGet("/caseform"); request.setParameter("caseId", "550e4713-e22b-11d4-a716-446655440002"); request.setRemoteUser("admin"); ModelAndView mv = this.form.showForm(this.filters, request, new MockHttpServletResponse()); request = this.newGet("/caseform"); request.setParameter("caseId", "last"); request.setRemoteUser("admin"); final AlphaCase aCase = this.caseManager.get("550e4713-e22b-11d4-a716-446655440002"); mv = this.form.showForm(this.filters, request, new MockHttpServletResponse()); Assert.assertEquals("caseform", mv.getViewName()); Assert.assertEquals(aCase, mv.getModel().get("case")); }
From source file:net.nicholaswilliams.java.teamcity.plugin.buildNumber.SharedBuildNumberController.java
protected ModelAndView listBuildNumbers(HttpServletRequest request) { ModelAndView modelAndView = new ModelAndView(this.listJspPagePath); boolean descending = "desc".equalsIgnoreCase(request.getParameter("direction")); SortedSet<SharedBuildNumber> set; if ("name".equalsIgnoreCase(request.getParameter("sort"))) { set = this.configurationService.getAllSharedBuildNumbersSortedByName(descending); modelAndView.getModel().put("sortedBy", "name"); } else {/* ww w.j a v a2 s.co m*/ set = this.configurationService.getAllSharedBuildNumbersSortedById(descending); modelAndView.getModel().put("sortedBy", "id"); } modelAndView.getModel().put(SharedBuildNumberController.PREFIX, BuildNumberPropertiesProvider.PARAMETER_PREFIX); modelAndView.getModel().put("numResults", set.size()); modelAndView.getModel().put("buildNumbers", set); modelAndView.getModel().put("sortClass", descending ? "sortedDesc" : "sortedAsc"); modelAndView.getModel().put("sortChange", descending ? "asc" : "desc"); return modelAndView; }
From source file:alfio.util.TemplateManager.java
private String render(AbstractResource resource, Map<String, Object> model, Locale locale, TemplateOutput templateOutput) { try {/*from w w w.j a va2s .c o m*/ ModelAndView mv = new ModelAndView((String) null, model); mv.addObject("format-date", MustacheCustomTagInterceptor.FORMAT_DATE); mv.addObject(MustacheLocalizationMessageInterceptor.DEFAULT_MODEL_KEY, new CustomLocalizationMessageInterceptor(locale, messageSource).createTranslator()); return compile(resource, templateOutput).execute(mv.getModel()); } catch (Exception e) { throw new IllegalStateException(e); } }
From source file:gov.nih.nci.cabig.caaers.web.participant.SubjectMedHistoryTab.java
/** * Remove an item from the collection through AJAX * */// w w w. ja va 2s . c o m public ModelAndView removePriorTherapy(HttpServletRequest request, Object cmd, Errors errors) { ParticipantInputCommand command = (ParticipantInputCommand) cmd; List<StudyParticipantPriorTherapy> priorTherapies = command.getAssignment().getPriorTherapies(); priorTherapies.remove(priorTherapies.get(command.getIndex())); //remove the element command.getPriorTherapyAgents().remove(command.getIndex()); //decrement the size of priortherapy agents by 1. //create the indexes in reverse order int size = priorTherapies.size(); Integer[] indexes = new Integer[size]; for (int i = 0; i < size; i++) { indexes[i] = size - (i + 1); } ModelAndView modelAndView = new ModelAndView("par/ajax/priorTherapyFormSection"); modelAndView.getModel().put("priorTherapies", priorTherapies); modelAndView.getModel().put("indexes", indexes); return modelAndView; }