Search in sources :

Example 11 with ServletRequest

use of jakarta.servlet.ServletRequest in project spring-framework by spring-projects.

the class ServletModelAttributeMethodProcessor method resolveConstructorArgument.

@Override
@Nullable
public Object resolveConstructorArgument(String paramName, Class<?> paramType, NativeWebRequest request) throws Exception {
    Object value = super.resolveConstructorArgument(paramName, paramType, request);
    if (value != null) {
        return value;
    }
    ServletRequest servletRequest = request.getNativeRequest(ServletRequest.class);
    if (servletRequest != null) {
        String attr = HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE;
        @SuppressWarnings("unchecked") Map<String, String> uriVars = (Map<String, String>) servletRequest.getAttribute(attr);
        return uriVars.get(paramName);
    }
    return null;
}
Also used : ServletRequest(jakarta.servlet.ServletRequest) Map(java.util.Map) Nullable(org.springframework.lang.Nullable)

Example 12 with ServletRequest

use of jakarta.servlet.ServletRequest in project spring-framework by spring-projects.

the class StreamingResponseBodyReturnValueHandler method handleReturnValue.

@Override
@SuppressWarnings("resource")
public void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {
    if (returnValue == null) {
        mavContainer.setRequestHandled(true);
        return;
    }
    HttpServletResponse response = webRequest.getNativeResponse(HttpServletResponse.class);
    Assert.state(response != null, "No HttpServletResponse");
    ServerHttpResponse outputMessage = new ServletServerHttpResponse(response);
    if (returnValue instanceof ResponseEntity) {
        ResponseEntity<?> responseEntity = (ResponseEntity<?>) returnValue;
        response.setStatus(responseEntity.getStatusCodeValue());
        outputMessage.getHeaders().putAll(responseEntity.getHeaders());
        returnValue = responseEntity.getBody();
        if (returnValue == null) {
            mavContainer.setRequestHandled(true);
            outputMessage.flush();
            return;
        }
    }
    ServletRequest request = webRequest.getNativeRequest(ServletRequest.class);
    Assert.state(request != null, "No ServletRequest");
    ShallowEtagHeaderFilter.disableContentCaching(request);
    Assert.isInstanceOf(StreamingResponseBody.class, returnValue, "StreamingResponseBody expected");
    StreamingResponseBody streamingBody = (StreamingResponseBody) returnValue;
    Callable<Void> callable = new StreamingResponseBodyTask(outputMessage.getBody(), streamingBody);
    WebAsyncUtils.getAsyncManager(webRequest).startCallableProcessing(callable, mavContainer);
}
Also used : ServletRequest(jakarta.servlet.ServletRequest) ResponseEntity(org.springframework.http.ResponseEntity) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse) ServerHttpResponse(org.springframework.http.server.ServerHttpResponse) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse)

Example 13 with ServletRequest

use of jakarta.servlet.ServletRequest in project spring-framework by spring-projects.

the class FormTag method processAction.

/**
 * Process the action through a {@link RequestDataValueProcessor} instance
 * if one is configured or otherwise returns the action unmodified.
 */
private String processAction(String action) {
    RequestDataValueProcessor processor = getRequestContext().getRequestDataValueProcessor();
    ServletRequest request = this.pageContext.getRequest();
    if (processor != null && request instanceof HttpServletRequest) {
        action = processor.processAction((HttpServletRequest) request, action, getHttpMethod());
    }
    return action;
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) ServletRequest(jakarta.servlet.ServletRequest) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) RequestDataValueProcessor(org.springframework.web.servlet.support.RequestDataValueProcessor)

Example 14 with ServletRequest

use of jakarta.servlet.ServletRequest in project spring-framework by spring-projects.

the class RequestContextFilterTests method testFilterInvocation.

private void testFilterInvocation(final ServletException sex) throws Exception {
    final MockHttpServletRequest req = new MockHttpServletRequest();
    req.setAttribute("myAttr", "myValue");
    final MockHttpServletResponse resp = new MockHttpServletResponse();
    // Expect one invocation by the filter being tested
    class DummyFilterChain implements FilterChain {

        public int invocations = 0;

        @Override
        public void doFilter(ServletRequest req, ServletResponse resp) throws IOException, ServletException {
            ++invocations;
            if (invocations == 1) {
                assertThat(RequestContextHolder.currentRequestAttributes().getAttribute("myAttr", RequestAttributes.SCOPE_REQUEST)).isSameAs("myValue");
                if (sex != null) {
                    throw sex;
                }
            } else {
                throw new IllegalStateException("Too many invocations");
            }
        }
    }
    DummyFilterChain fc = new DummyFilterChain();
    MockFilterConfig mfc = new MockFilterConfig(new MockServletContext(), "foo");
    RequestContextFilter rbf = new RequestContextFilter();
    rbf.init(mfc);
    try {
        rbf.doFilter(req, resp, fc);
        assertThat(sex).isNull();
    } catch (ServletException ex) {
        assertThat(sex).isNotNull();
    }
    assertThatIllegalStateException().isThrownBy(RequestContextHolder::currentRequestAttributes);
    assertThat(fc.invocations).isEqualTo(1);
}
Also used : ServletException(jakarta.servlet.ServletException) RequestContextHolder(org.springframework.web.context.request.RequestContextHolder) ServletRequest(jakarta.servlet.ServletRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) ServletResponse(jakarta.servlet.ServletResponse) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) FilterChain(jakarta.servlet.FilterChain) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) MockFilterConfig(org.springframework.web.testfixture.servlet.MockFilterConfig)

Example 15 with ServletRequest

use of jakarta.servlet.ServletRequest in project spring-framework by spring-projects.

the class ViewResolverTests method internalResourceViewResolverWithContextBeans.

@Test
public void internalResourceViewResolverWithContextBeans() throws Exception {
    this.wac.registerSingleton("myBean", TestBean.class);
    this.wac.registerSingleton("myBean2", TestBean.class);
    this.wac.refresh();
    InternalResourceViewResolver vr = new InternalResourceViewResolver();
    Properties props = new Properties();
    props.setProperty("key1", "value1");
    vr.setAttributes(props);
    Map<String, Object> map = new HashMap<>();
    map.put("key2", 2);
    vr.setAttributesMap(map);
    vr.setExposeContextBeansAsAttributes(true);
    vr.setApplicationContext(this.wac);
    HttpServletRequest request = new MockHttpServletRequest(this.sc) {

        @Override
        public RequestDispatcher getRequestDispatcher(String path) {
            return new MockRequestDispatcher(path) {

                @Override
                public void forward(ServletRequest forwardRequest, ServletResponse forwardResponse) {
                    assertThat(forwardRequest.getAttribute("rc") == null).as("Correct rc attribute").isTrue();
                    assertThat(forwardRequest.getAttribute("key1")).isEqualTo("value1");
                    assertThat(forwardRequest.getAttribute("key2")).isEqualTo(2);
                    assertThat(forwardRequest.getAttribute("myBean")).isSameAs(wac.getBean("myBean"));
                    assertThat(forwardRequest.getAttribute("myBean2")).isSameAs(wac.getBean("myBean2"));
                }
            };
        }
    };
    request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.wac);
    request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
    View view = vr.resolveViewName("example1", Locale.getDefault());
    view.render(new HashMap<String, Object>(), request, this.response);
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) ServletRequest(jakarta.servlet.ServletRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) ServletResponse(jakarta.servlet.ServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Properties(java.util.Properties) AcceptHeaderLocaleResolver(org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver) View(org.springframework.web.servlet.View) MockRequestDispatcher(org.springframework.web.testfixture.servlet.MockRequestDispatcher) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Test(org.junit.jupiter.api.Test)

Aggregations

ServletRequest (jakarta.servlet.ServletRequest)28 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)17 ServletResponse (jakarta.servlet.ServletResponse)13 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)12 ServletException (jakarta.servlet.ServletException)9 IOException (java.io.IOException)8 Test (org.junit.jupiter.api.Test)5 Request (org.apache.catalina.connector.Request)4 MockFilterChain (org.springframework.mock.web.MockFilterChain)4 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)4 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)4 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)4 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)4 ServletRequestWrapper (jakarta.servlet.ServletRequestWrapper)3 RequestDataValueProcessor (org.springframework.web.servlet.support.RequestDataValueProcessor)3 Principal (java.security.Principal)2 HashMap (java.util.HashMap)2 Properties (java.util.Properties)2 Context (org.apache.catalina.Context)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2