Search in sources :

Example 16 with ServletRequestAttributes

use of org.springframework.web.context.request.ServletRequestAttributes in project spring-framework by spring-projects.

the class ContentNegotiatingViewResolverTests method createViewResolver.

@Before
public void createViewResolver() {
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(new MockServletContext());
    wac.refresh();
    viewResolver = new ContentNegotiatingViewResolver();
    viewResolver.setApplicationContext(wac);
    request = new MockHttpServletRequest("GET", "/test");
    RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) MockServletContext(org.springframework.mock.web.test.MockServletContext) Before(org.junit.Before)

Example 17 with ServletRequestAttributes

use of org.springframework.web.context.request.ServletRequestAttributes in project spring-framework by spring-projects.

the class ClassPathBeanDefinitionScannerJsr330ScopeIntegrationTests method setUp.

@Before
public void setUp() {
    this.oldRequestAttributes = new ServletRequestAttributes(new MockHttpServletRequest());
    this.newRequestAttributes = new ServletRequestAttributes(new MockHttpServletRequest());
    MockHttpServletRequest oldRequestWithSession = new MockHttpServletRequest();
    oldRequestWithSession.setSession(new MockHttpSession());
    this.oldRequestAttributesWithSession = new ServletRequestAttributes(oldRequestWithSession);
    MockHttpServletRequest newRequestWithSession = new MockHttpServletRequest();
    newRequestWithSession.setSession(new MockHttpSession());
    this.newRequestAttributesWithSession = new ServletRequestAttributes(newRequestWithSession);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) MockHttpSession(org.springframework.mock.web.MockHttpSession) Before(org.junit.Before)

Example 18 with ServletRequestAttributes

use of org.springframework.web.context.request.ServletRequestAttributes in project spring-framework by spring-projects.

the class AopNamespaceHandlerScopeIntegrationTests method testRequestScoping.

@Test
public void testRequestScoping() throws Exception {
    MockHttpServletRequest oldRequest = new MockHttpServletRequest();
    MockHttpServletRequest newRequest = new MockHttpServletRequest();
    RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(oldRequest));
    ITestBean scoped = (ITestBean) this.context.getBean("requestScoped");
    assertTrue("Should be AOP proxy", AopUtils.isAopProxy(scoped));
    assertTrue("Should be target class proxy", scoped instanceof TestBean);
    ITestBean testBean = (ITestBean) this.context.getBean("testBean");
    assertTrue("Should be AOP proxy", AopUtils.isAopProxy(testBean));
    assertFalse("Regular bean should be JDK proxy", testBean instanceof TestBean);
    String rob = "Rob Harrop";
    String bram = "Bram Smeets";
    assertEquals(rob, scoped.getName());
    scoped.setName(bram);
    RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(newRequest));
    assertEquals(rob, scoped.getName());
    RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(oldRequest));
    assertEquals(bram, scoped.getName());
    assertTrue("Should have advisors", ((Advised) scoped).getAdvisors().length > 0);
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) Test(org.junit.Test)

Example 19 with ServletRequestAttributes

use of org.springframework.web.context.request.ServletRequestAttributes in project spring-framework by spring-projects.

the class TilesView method checkResource.

@Override
public boolean checkResource(final Locale locale) throws Exception {
    HttpServletRequest servletRequest = null;
    RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
    if (requestAttributes instanceof ServletRequestAttributes) {
        servletRequest = ((ServletRequestAttributes) requestAttributes).getRequest();
    }
    Request request = new ServletRequest(this.applicationContext, servletRequest, null) {

        @Override
        public Locale getRequestLocale() {
            return locale;
        }
    };
    return this.renderer.isRenderable(getUrl(), request);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(org.apache.tiles.request.servlet.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) AbstractRequest(org.apache.tiles.request.AbstractRequest) ServletRequest(org.apache.tiles.request.servlet.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) Request(org.apache.tiles.request.Request) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) RequestAttributes(org.springframework.web.context.request.RequestAttributes)

Example 20 with ServletRequestAttributes

use of org.springframework.web.context.request.ServletRequestAttributes in project spring-framework by spring-projects.

the class ContentNegotiatingViewResolver method resolveViewName.

@Override
public View resolveViewName(String viewName, Locale locale) throws Exception {
    RequestAttributes attrs = RequestContextHolder.getRequestAttributes();
    Assert.state(attrs instanceof ServletRequestAttributes, "No current ServletRequestAttributes");
    List<MediaType> requestedMediaTypes = getMediaTypes(((ServletRequestAttributes) attrs).getRequest());
    if (requestedMediaTypes != null) {
        List<View> candidateViews = getCandidateViews(viewName, locale, requestedMediaTypes);
        View bestView = getBestView(candidateViews, requestedMediaTypes, attrs);
        if (bestView != null) {
            return bestView;
        }
    }
    if (this.useNotAcceptableStatusCode) {
        if (logger.isDebugEnabled()) {
            logger.debug("No acceptable view found; returning 406 (Not Acceptable) status code");
        }
        return NOT_ACCEPTABLE_VIEW;
    } else {
        logger.debug("No acceptable view found; returning null");
        return null;
    }
}
Also used : ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) MediaType(org.springframework.http.MediaType) RequestAttributes(org.springframework.web.context.request.RequestAttributes) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) SmartView(org.springframework.web.servlet.SmartView) View(org.springframework.web.servlet.View)

Aggregations

ServletRequestAttributes (org.springframework.web.context.request.ServletRequestAttributes)30 RequestAttributes (org.springframework.web.context.request.RequestAttributes)13 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)11 HttpServletRequest (javax.servlet.http.HttpServletRequest)8 Before (org.junit.Before)6 Test (org.junit.Test)3 MockHttpSession (org.springframework.mock.web.MockHttpSession)3 Date (java.util.Date)2 HttpSession (javax.servlet.http.HttpSession)2 BeforeClass (org.junit.BeforeClass)2 RecordNameEntity (org.orcid.persistence.jpa.entities.RecordNameEntity)2 ITestBean (org.springframework.tests.sample.beans.ITestBean)2 TestBean (org.springframework.tests.sample.beans.TestBean)2 IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1 Principal (java.security.Principal)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 PortletRequest (javax.portlet.PortletRequest)1 ServletException (javax.servlet.ServletException)1