Example usage for org.springframework.web.servlet ModelAndView getModel

List of usage examples for org.springframework.web.servlet ModelAndView getModel

Introduction

In this page you can find the example usage for org.springframework.web.servlet ModelAndView getModel.

Prototype

public Map<String, Object> getModel() 

Source Link

Document

Return the model map.

Usage

From source file:org.openmrs.web.controller.OptionsFormControllerTest.java

@Test
public void shouldRejectInvalidNotificationAddress() throws Exception {
    final String incorrectAddress = "gayan@gmail";
    MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
    request.setParameter("notification", "email");
    request.setParameter("notificationAddress", incorrectAddress);

    HttpServletResponse response = new MockHttpServletResponse();
    ModelAndView modelAndView = controller.handleRequest(request, response);

    OptionsForm optionsForm = (OptionsForm) controller.formBackingObject(request);
    assertEquals(incorrectAddress, optionsForm.getNotificationAddress());

    BeanPropertyBindingResult bindingResult = (BeanPropertyBindingResult) modelAndView.getModel()
            .get("org.springframework.validation.BindingResult.opts");
    Assert.assertTrue(bindingResult.hasErrors());
}

From source file:com.lc.storefront.interceptors.beforeview.CartRestorationBeforeViewHandler.java

@Override
public void beforeView(final HttpServletRequest request, final HttpServletResponse response,
        final ModelAndView modelAndView) throws Exception {

    if (getSessionService().getAttribute(WebConstants.CART_RESTORATION) != null
            && getSessionService().getAttribute(WebConstants.CART_RESTORATION_SHOW_MESSAGE) != null
            && ((Boolean) getSessionService().getAttribute(WebConstants.CART_RESTORATION_SHOW_MESSAGE))
                    .booleanValue()) {//  w  ww. ja  v  a 2s. c o  m

        if (getSessionService().getAttribute(WebConstants.CART_RESTORATION_ERROR_STATUS) != null) {
            modelAndView.getModel().put("restorationErrorMsg",
                    getSessionService().getAttribute(WebConstants.CART_RESTORATION_ERROR_STATUS));
        } else {
            modelAndView.getModel().put("restorationData",
                    getSessionService().getAttribute(WebConstants.CART_RESTORATION));
        }

        modelAndView.getModel().put("showModifications", showModifications(request));
    }

    getSessionService().setAttribute(WebConstants.CART_RESTORATION_SHOW_MESSAGE, Boolean.FALSE);
}

From source file:org.tangram.components.spring.SpringViewUtilities.java

public void render(Writer writer, Map<String, Object> model, String view) throws IOException {
    ServletRequest request = (ServletRequest) model.get(Constants.ATTRIBUTE_REQUEST);
    ServletResponse response = (ServletResponse) model.get(Constants.ATTRIBUTE_RESPONSE);

    ViewContext vc = viewContextFactory.createViewContext(model, view);
    ModelAndView mav = SpringViewUtilities.createModelAndView(vc);
    View effectiveView = mav.getView();
    LOG.debug("render() effectiveView={}", effectiveView);
    try {//from   ww  w.  j  av  a 2  s  . c o  m
        if (effectiveView == null) {
            String viewName = mav.getViewName();
            if (viewName == null) {
                viewName = Constants.DEFAULT_VIEW;
            } // if

            effectiveView = viewHandler.resolveView(viewName, mav.getModel(), Locale.getDefault(), request);
        } // if

        if (writer != null) {
            writer.flush();
        } // if
        LOG.debug("render() model={}", mav.getModel());
        LOG.debug("render({}) effectiveView={}", mav.getViewName(), effectiveView);
        effectiveView.render(mav.getModel(), (HttpServletRequest) request, (HttpServletResponse) response);
    } catch (Exception e) {
        LOG.error("render() #" + view, e);
        if (writer != null) {
            writer.write(e.getLocalizedMessage());
        } // if
    } // try/catch
}

From source file:net.nan21.dnet.core.web.controller.session.SessionController.java

/**
 * Process login action//from  w w w .  j a v a  2s  .c o m
 * 
 * @param username
 * @param password
 * @param clientCode
 * @param language
 * @param request
 * @param response
 * @return
 * @throws Exception
 */
@RequestMapping(value = "/" + Constants.SESSION_ACTION_LOGIN, method = RequestMethod.POST)
public ModelAndView login(@RequestParam(value = "user", required = true) String username,
        @RequestParam(value = "pswd", required = true) String password,
        @RequestParam(value = "client", required = true) String clientCode,
        @RequestParam(value = "lang", required = false) String language, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    try {

        if (logger.isInfoEnabled()) {
            logger.info("Session request: -> login ");
        }

        if (logger.isDebugEnabled()) {
            logger.debug("  --> request-params: user={}, client={}, pswd=*** ",
                    new String[] { username, clientCode });
        }

        request.getSession().invalidate();
        request.getSession();

        prepareLoginParamsHolder(clientCode, language, request);

        String hashedPass = getMD5Password(password);

        Authentication authRequest = new UsernamePasswordAuthenticationToken(username, hashedPass);
        Authentication authResponse = this.getAuthenticationManager().authenticate(authRequest);
        SecurityContextHolder.getContext().setAuthentication(authResponse);

        response.sendRedirect(Constants.URL_DNET_UI_EXTJS);
        return null;
    } catch (Exception e) {
        ModelAndView err = this.showLogin(request, response);
        String msg = "Authentication failed. ";
        if (e.getMessage() != null && !"".equals(e.getMessage())) {
            msg += e.getMessage();
        }
        err.getModel().put("error", msg);
        return err;
    }
}

From source file:seava.j4e.web.controller.session.SessionController.java

/**
 * Process login action//from w  ww  .j  av a 2  s  . c o m
 * 
 * @param username
 * @param password
 * @param clientCode
 * @param language
 * @param request
 * @param response
 * @return
 * @throws Exception
 */
@RequestMapping(value = "/" + Constants.SESSION_ACTION_LOGIN, method = RequestMethod.POST)
public ModelAndView login(@RequestParam(value = "user", required = true) String username,
        @RequestParam(value = "pswd", required = true) String password,
        @RequestParam(value = "client", required = true) String clientCode,
        @RequestParam(value = "lang", required = false) String language, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    try {

        if (logger.isInfoEnabled()) {
            logger.info("Session request: -> login ");
        }

        if (logger.isDebugEnabled()) {
            logger.debug("  --> request-params: user={}, client={}, pswd=*** ",
                    new Object[] { username, clientCode });
        }

        request.getSession().invalidate();
        request.getSession();

        prepareLoginParamsHolder(clientCode, language, request);

        String hashedPass = getMD5Password(password);

        Authentication authRequest = new UsernamePasswordAuthenticationToken(username, hashedPass);
        Authentication authResponse = this.getAuthenticationManager().authenticate(authRequest);
        SecurityContextHolder.getContext().setAuthentication(authResponse);

        response.sendRedirect(this.getSettings().get(Constants.PROP_CTXPATH) + Constants.URL_UI_EXTJS);
        return null;
    } catch (Exception e) {
        ModelAndView err = this.showLogin(request, response);
        String msg = "Access denied. ";
        if (e.getMessage() != null && !"".equals(e.getMessage())) {
            msg += e.getMessage();
        }
        err.getModel().put("error", msg);
        return err;
    }
}

From source file:org.jasig.cas.support.oauth.web.OAuth20ServiceValidateControllerTests.java

@Test
public void verifyGetTicketException() 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))
            .thenThrow(new InvalidTicketException("weak"));
    when(centralAuthenticationService.validateServiceTicket(SERVICE_TICKET_ID, webApplicationService))
            .thenReturn(assertion);// w w  w.jav  a2  s  . co 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 MockHttpServletResponse mockResponse = new MockHttpServletResponse();
    final OAuth20ServiceValidateController oauth20ServiceValidateController = new OAuth20ServiceValidateController();
    oauth20ServiceValidateController.setArgumentExtractor(argumentExtractor);
    oauth20ServiceValidateController.setCentralAuthenticationService(centralAuthenticationService);
    oauth20ServiceValidateController.setCentralOAuthService(centralOAuthService);
    oauth20ServiceValidateController.setSuccessView(SUCCESS_VIEW);
    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 verifyOK() 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))
            .thenReturn(assertion);/*from   w w w.  j av  a2s. c om*/

    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();
    assertEquals(AT_ID, model.get(OAuthConstants.CAS_PROTOCOL_ACCESS_TOKEN));
    assertEquals(scopes, model.get(OAuthConstants.CAS_PROTOCOL_ACCESS_TOKEN_SCOPE));
}

From source file:org.jasig.cas.support.oauth.web.OAuth20ServiceValidateControllerTests.java

@Test
public void verifyBypassCASWithNoService() 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(null);

    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))
            .thenReturn(assertion);//from   w w  w  .j  av a2s.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));
}