Search in sources :

Example 21 with ServletRequest

use of jakarta.servlet.ServletRequest in project tomcat by apache.

the class Request method setRequest.

/**
 * Set a wrapped HttpServletRequest to pass to the application. Components
 * wishing to wrap the request should obtain the request via
 * {@link #getRequest()}, wrap it and then call this method with the
 * wrapped request.
 *
 * @param applicationRequest The wrapped request to pass to the application
 */
public void setRequest(HttpServletRequest applicationRequest) {
    // Check the wrapper wraps this request
    ServletRequest r = applicationRequest;
    while (r instanceof HttpServletRequestWrapper) {
        r = ((HttpServletRequestWrapper) r).getRequest();
    }
    if (r != facade) {
        throw new IllegalArgumentException(sm.getString("request.illegalWrap"));
    }
    this.applicationRequest = applicationRequest;
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) ServletRequest(jakarta.servlet.ServletRequest) HttpServletRequestWrapper(jakarta.servlet.http.HttpServletRequestWrapper)

Example 22 with ServletRequest

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

the class JaasApiIntegrationFilterTests method assertJaasSubjectEquals.

private void assertJaasSubjectEquals(final Subject expectedValue) throws Exception {
    MockFilterChain chain = new MockFilterChain() {

        @Override
        public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
            // See if the subject was updated
            Subject currentSubject = Subject.getSubject(AccessController.getContext());
            assertThat(currentSubject).isEqualTo(expectedValue);
            // run so we know the chain was executed
            super.doFilter(request, response);
        }
    };
    this.filter.doFilter(this.request, this.response, chain);
    // ensure that the chain was actually invoked
    assertThat(chain.getRequest()).isNotNull();
}
Also used : ServletRequest(jakarta.servlet.ServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) ServletResponse(jakarta.servlet.ServletResponse) MockFilterChain(org.springframework.mock.web.MockFilterChain) Subject(javax.security.auth.Subject)

Example 23 with ServletRequest

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

the class ViewResolverTests method internalResourceViewResolverWithSpecificContextBeans.

@Test
public void internalResourceViewResolverWithSpecificContextBeans() 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.setExposedContextBeanNames(new String[] { "myBean2" });
    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")).isNull();
                    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)

Example 24 with ServletRequest

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

the class FormTag method doEndTag.

/**
 * Closes the '{@code form}' block tag and removes the form object name
 * from the {@link jakarta.servlet.jsp.PageContext}.
 */
@Override
public int doEndTag() throws JspException {
    RequestDataValueProcessor processor = getRequestContext().getRequestDataValueProcessor();
    ServletRequest request = this.pageContext.getRequest();
    if (processor != null && request instanceof HttpServletRequest) {
        writeHiddenFields(processor.getExtraHiddenFields((HttpServletRequest) request));
    }
    Assert.state(this.tagWriter != null, "No TagWriter set");
    this.tagWriter.endTag();
    return EVAL_PAGE;
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) ServletRequest(jakarta.servlet.ServletRequest) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) RequestDataValueProcessor(org.springframework.web.servlet.support.RequestDataValueProcessor)

Example 25 with ServletRequest

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

the class UrlTag method doEndTag.

@Override
public int doEndTag() throws JspException {
    String url = createUrl();
    RequestDataValueProcessor processor = getRequestContext().getRequestDataValueProcessor();
    ServletRequest request = this.pageContext.getRequest();
    if ((processor != null) && (request instanceof HttpServletRequest)) {
        url = processor.processUrl((HttpServletRequest) request, url);
    }
    if (this.var == null) {
        // print the url to the writer
        try {
            this.pageContext.getOut().print(url);
        } catch (IOException ex) {
            throw new JspException(ex);
        }
    } else {
        // store the url as a variable
        this.pageContext.setAttribute(this.var, url, this.scope);
    }
    return EVAL_PAGE;
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) ServletRequest(jakarta.servlet.ServletRequest) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) JspException(jakarta.servlet.jsp.JspException) RequestDataValueProcessor(org.springframework.web.servlet.support.RequestDataValueProcessor) IOException(java.io.IOException)

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