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

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

Introduction

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

Prototype

@Nullable
public View getView() 

Source Link

Document

Return the View object, or null if we are using a view name to be resolved by the DispatcherServlet via a ViewResolver.

Usage

From source file:fr.univrouen.poste.utils.ConfigInterceptor.java

@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
        ModelAndView modelAndView) throws Exception {

    // we want to add usual model in modelAndView only when needed, ie with
    // direct html view :
    // not for download response (for example) because we don't need it
    // not for redirect view because we don't need it and we don't want that
    // they appears in the url

    if (modelAndView != null && modelAndView.hasView()) {

        boolean isViewObject = modelAndView.getView() == null;

        boolean isRedirectView = !isViewObject && modelAndView.getView() instanceof RedirectView;

        boolean viewNameStartsWithRedirect = isViewObject
                && modelAndView.getViewName().startsWith(UrlBasedViewResolver.REDIRECT_URL_PREFIX);

        if (!isRedirectView && !viewNameStartsWithRedirect) {

            String title = AppliConfig.getCacheTitre();
            modelAndView.addObject("title", title);

            String piedPage = AppliConfig.getCachePiedPage();
            modelAndView.addObject("piedPage", piedPage);

            String imageUrl = AppliConfig.getCacheImageUrl();
            modelAndView.addObject("imageUrl", imageUrl);

            String path = request.getServletPath();
            String subTitle = subTitles.get(path);
            String activeMenu = path.replaceAll("/", "");

            if (subTitle == null) {
                List<String> keys = new Vector<String>(subTitles.keySet());
                Collections.reverse(keys);
                for (String key : keys) {
                    if (path.startsWith(key)) {
                        subTitle = subTitles.get(key);
                        ;//from  w  ww.  j  a  va  2  s. com
                        activeMenu = key.replaceAll("/", "");
                        break;
                    }
                }
            }

            modelAndView.addObject("subTitle", subTitle);
            modelAndView.addObject("activeMenu", activeMenu);

            modelAndView.addObject("candidatCanSignup", AppliConfig.getCacheCandidatCanSignup());

            modelAndView.addObject("versionEsupDematEC", AppliVersion.getCacheVersion());

        }

        if (request.getParameter("size") != null) {
            Integer size = Integer.valueOf(request.getParameter("size"));
            request.getSession().setAttribute("size_in_session", size);
        } else if (request.getSession(false) != null
                && request.getSession().getAttribute("size_in_session") == null) {
            request.getSession().setAttribute("size_in_session", new Integer(40));
        }

    }
}

From source file:org.jnap.core.mvc.async.AsyncRequestInterceptor.java

@Override
public void onStateChange(AtmosphereResourceEvent<HttpServletRequest, HttpServletResponse> event)
        throws IOException {
    System.out.println("AsyncRequestInterceptor.onStateChange()");
    System.out.println(event.getMessage());
    HttpServletRequest req = event.getResource().getRequest();
    HttpServletResponse res = event.getResource().getResponse();
    try {/*w w  w .  ja v  a  2 s  .  c o  m*/
        ModelAndView mv = (ModelAndView) event.getMessage();
        View view = null;
        if (mv.isReference()) {
            view = this.viewResolver.resolveViewName(mv.getViewName(), this.localeResolver.resolveLocale(req));
        } else {
            view = mv.getView();
            if (view == null) {

            }
        }
        view.render(mv.getModelMap(), req, res);
    } catch (Exception e) {
        throw new IOException(e.getMessage(), e);
    }
}

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

@Test
public void testOKWithState() throws Exception {
    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET",
            CONTEXT + OAuthConstants.AUTHORIZE_URL);
    mockRequest.setParameter(OAuthConstants.CLIENT_ID, CLIENT_ID);
    mockRequest.setParameter(OAuthConstants.REDIRECT_URI, REDIRECT_URI);
    mockRequest.setParameter(OAuthConstants.STATE, STATE);
    mockRequest.setServerName(CAS_SERVER);
    mockRequest.setServerPort(CAS_PORT);
    mockRequest.setScheme(CAS_SCHEME);//  www .jav  a2  s.  c o  m
    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();
    final ServicesManager servicesManager = mock(ServicesManager.class);
    final List<RegisteredService> services = new ArrayList<RegisteredService>();
    services.add(getRegisteredService(REDIRECT_URI, SERVICE_NAME));
    when(servicesManager.getAllServices()).thenReturn(services);
    final OAuth20WrapperController oauth20WrapperController = new OAuth20WrapperController();
    oauth20WrapperController.setLoginUrl(CAS_URL);
    oauth20WrapperController.setServicesManager(servicesManager);
    oauth20WrapperController.afterPropertiesSet();
    final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse);
    final HttpSession session = mockRequest.getSession();
    assertEquals(REDIRECT_URI, session.getAttribute(OAuthConstants.OAUTH20_CALLBACKURL));
    assertEquals(SERVICE_NAME, session.getAttribute(OAuthConstants.OAUTH20_SERVICE_NAME));
    assertEquals(STATE, session.getAttribute(OAuthConstants.OAUTH20_STATE));
    final View view = modelAndView.getView();
    assertTrue(view instanceof RedirectView);
    final RedirectView redirectView = (RedirectView) view;
    assertEquals(OAuthUtils.addParameter(CAS_URL, "service",
            CAS_URL + CONTEXT + OAuthConstants.CALLBACK_AUTHORIZE_URL), redirectView.getUrl());
}

From source file:de.appsolve.padelcampus.spring.RedirectInterceptor.java

@Override
public final void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
        ModelAndView modelAndView) throws SystemException {
    String redirectHeader = (String) request.getSession().getAttribute(REDIRECT_HEADER);
    if (!StringUtils.isEmpty(redirectHeader)) {
        response.addHeader(REDIRECT_HEADER, redirectHeader);
        request.getSession().removeAttribute(REDIRECT_HEADER);
    } else if (modelAndView != null) {
        String viewName = null;/*from  w ww.  ja  v  a 2s  .c  o m*/
        if (modelAndView.getViewName() != null && modelAndView.getViewName().startsWith(REDIRECT_PREFIX)) {
            viewName = modelAndView.getViewName().substring(REDIRECT_PREFIX.length());
        } else if (modelAndView.getView() != null && modelAndView.getView() instanceof RedirectView) {
            viewName = modelAndView.getViewName();
        }
        if (!StringUtils.isEmpty(viewName)) {
            request.getSession().setAttribute(REDIRECT_HEADER, viewName);
        }
    }
}

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

@Test
public void verifySetupOK() throws Exception {
    final TicketGrantingTicket ticketGrantingTicket = mock(TicketGrantingTicket.class);
    when(ticketGrantingTicket.getId()).thenReturn(TICKET_GRANTING_TICKET_ID);

    final ServiceTicket serviceTicket = mock(ServiceTicket.class);
    when(serviceTicket.getGrantingTicket()).thenReturn(ticketGrantingTicket);

    final TicketRegistry ticketRegistry = mock(TicketRegistry.class);
    when(ticketRegistry.getTicket(SERVICE_TICKET_ID)).thenReturn(serviceTicket);

    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET",
            CONTEXT + OAuthConstants.CALLBACK_AUTHORIZE_URL);
    mockRequest.setParameter(OAuthConstants.TICKET, SERVICE_TICKET_ID);
    final MockHttpSession mockSession = new MockHttpSession();
    mockRequest.setSession(mockSession);

    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();

    final OAuth20WrapperController oauth20WrapperController = new OAuth20WrapperController();
    oauth20WrapperController.setTicketRegistry(ticketRegistry);
    oauth20WrapperController.afterPropertiesSet();

    final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse);
    assertTrue(modelAndView.getView() instanceof RedirectView);
    final RedirectView redirectView = (RedirectView) modelAndView.getView();
    assertTrue(redirectView.getUrl().endsWith(CONTEXT + OAuthConstants.CALLBACK_AUTHORIZE_URL));
}

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

@Test
public void verifyBypassPromptIsTrue() throws Exception {
    final TicketGrantingTicket ticketGrantingTicket = mock(TicketGrantingTicket.class);
    when(ticketGrantingTicket.isExpired()).thenReturn(false);

    final TicketRegistry ticketRegistry = mock(TicketRegistry.class);
    when(ticketRegistry.getTicket(TICKET_GRANTING_TICKET_ID)).thenReturn(ticketGrantingTicket);

    final Map<String, Scope> scopeMap = new HashMap<>();
    scopeMap.put("scope1", new Scope("scope1", "description2"));
    scopeMap.put("scope2", new Scope("scope2", "description2"));

    final CentralOAuthService centralOAuthService = mock(CentralOAuthService.class);
    when(centralOAuthService.getScopes(anySetOf(String.class))).thenReturn(scopeMap);

    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET",
            CONTEXT + OAuthConstants.CALLBACK_AUTHORIZE_URL);
    final MockHttpSession mockSession = new MockHttpSession();
    mockSession.putValue(OAuthConstants.OAUTH20_LOGIN_TICKET_ID, TICKET_GRANTING_TICKET_ID);
    mockSession.putValue(OAuthConstants.OAUTH20_SCOPE, SCOPE);
    mockSession.putValue(OAuthConstants.OAUTH20_SERVICE_NAME, SERVICE_NAME);
    mockSession.putValue(OAuthConstants.OAUTH20_TOKEN_TYPE, TokenType.OFFLINE);
    mockSession.putValue(OAuthConstants.OAUTH20_APPROVAL_PROMPT, OAuthConstants.APPROVAL_PROMPT_FORCE);
    mockSession.putValue(OAuthConstants.BYPASS_APPROVAL_PROMPT, true);
    mockRequest.setSession(mockSession);

    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();

    final OAuth20WrapperController oauth20WrapperController = new OAuth20WrapperController();
    oauth20WrapperController.setTicketRegistry(ticketRegistry);
    oauth20WrapperController.setCentralOAuthService(centralOAuthService);
    oauth20WrapperController.afterPropertiesSet();

    final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse);
    assertTrue(modelAndView.getView() instanceof RedirectView);
    final RedirectView redirectView = (RedirectView) modelAndView.getView();
    assertTrue(redirectView.getUrl()/*  w  ww. j  a v a2s .c  o  m*/
            .endsWith(CONTEXT + OAuthConstants.CALLBACK_AUTHORIZE_ACTION_URL + "?action=allow"));
}

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

@Test
public void verifyNoPromptWithExistingToken() throws Exception {
    final Principal principal = mock(Principal.class);
    when(principal.getId()).thenReturn(PRINCIPAL_ID);

    final Authentication authentication = mock(Authentication.class);
    when(authentication.getPrincipal()).thenReturn(principal);

    final TicketGrantingTicket ticketGrantingTicket = mock(TicketGrantingTicket.class);
    when(ticketGrantingTicket.isExpired()).thenReturn(false);
    when(ticketGrantingTicket.getAuthentication()).thenReturn(authentication);

    final TicketRegistry ticketRegistry = mock(TicketRegistry.class);
    when(ticketRegistry.getTicket(TICKET_GRANTING_TICKET_ID)).thenReturn(ticketGrantingTicket);

    final Map<String, Scope> scopeMap = new HashMap<>();
    scopeMap.put("scope1", new Scope("scope1", "description2"));
    scopeMap.put("scope2", new Scope("scope2", "description2"));

    final CentralOAuthService centralOAuthService = mock(CentralOAuthService.class);
    when(centralOAuthService.getScopes(anySetOf(String.class))).thenReturn(scopeMap);
    when(centralOAuthService.isAccessToken(TokenType.ONLINE, CLIENT_ID, PRINCIPAL_ID, scopeMap.keySet()))
            .thenReturn(true);//from  w w  w .  j a  va  2  s. c  o  m
    when(centralOAuthService.isRefreshToken(CLIENT_ID, PRINCIPAL_ID, scopeMap.keySet())).thenReturn(false);

    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET",
            CONTEXT + OAuthConstants.CALLBACK_AUTHORIZE_URL);
    final MockHttpSession mockSession = new MockHttpSession();
    mockSession.putValue(OAuthConstants.OAUTH20_LOGIN_TICKET_ID, TICKET_GRANTING_TICKET_ID);
    mockSession.putValue(OAuthConstants.OAUTH20_SCOPE, SCOPE);
    mockSession.putValue(OAuthConstants.OAUTH20_SERVICE_NAME, SERVICE_NAME);
    mockSession.putValue(OAuthConstants.OAUTH20_CLIENT_ID, CLIENT_ID);
    mockSession.putValue(OAuthConstants.OAUTH20_TOKEN_TYPE, TokenType.ONLINE);
    mockRequest.setSession(mockSession);

    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();

    final OAuth20WrapperController oauth20WrapperController = new OAuth20WrapperController();
    oauth20WrapperController.setTicketRegistry(ticketRegistry);
    oauth20WrapperController.setCentralOAuthService(centralOAuthService);
    oauth20WrapperController.afterPropertiesSet();

    final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse);
    assertTrue(modelAndView.getView() instanceof RedirectView);
    final RedirectView redirectView = (RedirectView) modelAndView.getView();
    assertTrue(redirectView.getUrl()
            .endsWith(CONTEXT + OAuthConstants.CALLBACK_AUTHORIZE_ACTION_URL + "?action=allow"));

    assertEquals(scopeMap.keySet(), mockSession.getAttribute(OAuthConstants.OAUTH20_SCOPE_SET));
}

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

@Test
public void verifyAutoPromptWithExistingToken() throws Exception {
    final Principal principal = mock(Principal.class);
    when(principal.getId()).thenReturn(PRINCIPAL_ID);

    final Authentication authentication = mock(Authentication.class);
    when(authentication.getPrincipal()).thenReturn(principal);

    final TicketGrantingTicket ticketGrantingTicket = mock(TicketGrantingTicket.class);
    when(ticketGrantingTicket.isExpired()).thenReturn(false);
    when(ticketGrantingTicket.getAuthentication()).thenReturn(authentication);

    final TicketRegistry ticketRegistry = mock(TicketRegistry.class);
    when(ticketRegistry.getTicket(TICKET_GRANTING_TICKET_ID)).thenReturn(ticketGrantingTicket);

    final Map<String, Scope> scopeMap = new HashMap<>();
    scopeMap.put("scope1", new Scope("scope1", "description2"));
    scopeMap.put("scope2", new Scope("scope2", "description2"));

    final CentralOAuthService centralOAuthService = mock(CentralOAuthService.class);
    when(centralOAuthService.getScopes(anySetOf(String.class))).thenReturn(scopeMap);
    when(centralOAuthService.isAccessToken(TokenType.OFFLINE, CLIENT_ID, PRINCIPAL_ID, scopeMap.keySet()))
            .thenReturn(false);/*from ww w.j  av  a  2s. c  om*/
    when(centralOAuthService.isRefreshToken(CLIENT_ID, PRINCIPAL_ID, scopeMap.keySet())).thenReturn(true);

    final MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET",
            CONTEXT + OAuthConstants.CALLBACK_AUTHORIZE_URL);
    final MockHttpSession mockSession = new MockHttpSession();
    mockSession.putValue(OAuthConstants.OAUTH20_LOGIN_TICKET_ID, TICKET_GRANTING_TICKET_ID);
    mockSession.putValue(OAuthConstants.OAUTH20_SCOPE, SCOPE);
    mockSession.putValue(OAuthConstants.OAUTH20_SERVICE_NAME, SERVICE_NAME);
    mockSession.putValue(OAuthConstants.OAUTH20_CLIENT_ID, CLIENT_ID);
    mockSession.putValue(OAuthConstants.OAUTH20_APPROVAL_PROMPT, "auto");
    mockSession.putValue(OAuthConstants.OAUTH20_TOKEN_TYPE, TokenType.OFFLINE);
    mockRequest.setSession(mockSession);

    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();

    final OAuth20WrapperController oauth20WrapperController = new OAuth20WrapperController();
    oauth20WrapperController.setTicketRegistry(ticketRegistry);
    oauth20WrapperController.setCentralOAuthService(centralOAuthService);
    oauth20WrapperController.afterPropertiesSet();

    final ModelAndView modelAndView = oauth20WrapperController.handleRequest(mockRequest, mockResponse);
    assertTrue(modelAndView.getView() instanceof RedirectView);
    final RedirectView redirectView = (RedirectView) modelAndView.getView();
    assertTrue(redirectView.getUrl()
            .endsWith(CONTEXT + OAuthConstants.CALLBACK_AUTHORIZE_ACTION_URL + "?action=allow"));

    assertEquals(scopeMap.keySet(), mockSession.getAttribute(OAuthConstants.OAUTH20_SCOPE_SET));
}

From source file:org.iwethey.forums.web.HeaderInterceptor.java

/**
 * Load the model with the information needed for the header JSP,
 * including user statistics, the quote-of-the-page, and the
 * navigation bar links. Sets the last URI attribute in the session.
 * <p>/*from   w w  w .  j av a  2  s .c om*/
 * @param request The servlet request object.
 * @param response The servlet response object.
 * @param handler The request handler processing this request.
 * @param mv The ModelAndView created by the controller wrapped by this interceptor.
 */
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
        ModelAndView mv) throws Exception {
    String currentUri = request.getRequestURI();
    if (!(currentUri.indexOf("toggle") >= 0 || currentUri.indexOf("logout") >= 0
            || currentUri.indexOf("mark") >= 0)) {
        String query = request.getQueryString();
        if (query != null) {
            currentUri = currentUri + "?" + query;
        }
        WebUtils.setSessionAttribute(request, LAST_URI_ATTRIBUTE, currentUri);
    }

    View view = mv.getView();
    if (view != null && view instanceof RedirectView) {
        return;
    }

    // Copy the stuff set above on the request into the model.
    // Some view engines (Velocity as an example) use the model differently.
    mv.addObject("now", request.getAttribute("now"));
    mv.addObject("start", request.getAttribute("start"));
    mv.addObject(USER_ATTRIBUTE, request.getAttribute(USER_ATTRIBUTE));
    mv.addObject("username", request.getAttribute("username"));

    mv.addObject("userCount", new Integer(mUserManager.getUserCount()));
    int actives = mUserManager.getActiveUserCount();
    mv.addObject("activeUserCount", new Integer(actives));
    mv.addObject("activeSingle", Boolean.valueOf(actives == 1));

    mv.addObject("lph", new Integer(3));
    mv.addObject("mpl", new Integer(60 / 3));

    mv.addObject("notice", mAdminManager.getNotice());

    mv.addObject("lrpd", mAdminManager.getLRPD());

    List nav = null;
    Map model = mv.getModel();
    if (model != null) {
        nav = (List) model.get("navigation");
    }
    if (nav == null) {
        nav = new ArrayList();
    }

    nav.add(0, new NavigationEntry("home", "/main.iwt"));

    mv.addObject("navigation", nav);

    //mv.addObject("ctx", new ContextTool(request, response, RequestContextUtils.getLocale(request), mMessageSource));
}

From source file:com.thoughtworks.go.server.controller.ArtifactsControllerIntegrationTest.java

private void assertStatus(ModelAndView mav, int responseCode) {
    assertThat(mav.getView(), is(instanceOf(ResponseCodeView.class)));
    assertThat(((ResponseCodeView) mav.getView()).getStatusCode(), is(responseCode));
}