Search in sources :

Example 6 with ServletRequest

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

the class AsyncContextImpl method dispatch.

@Override
public void dispatch() {
    check();
    String path;
    String cpath;
    ServletRequest servletRequest = getRequest();
    if (servletRequest instanceof HttpServletRequest) {
        HttpServletRequest sr = (HttpServletRequest) servletRequest;
        path = sr.getRequestURI();
        cpath = sr.getContextPath();
    } else {
        path = request.getRequestURI();
        cpath = request.getContextPath();
    }
    if (cpath.length() > 1) {
        path = path.substring(cpath.length());
    }
    if (!context.getDispatchersUseEncodedPaths()) {
        path = UDecoder.URLDecode(path, StandardCharsets.UTF_8);
    }
    dispatch(path);
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) ServletRequest(jakarta.servlet.ServletRequest)

Example 7 with ServletRequest

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

the class AsyncContextImpl method dispatch.

@Override
public void dispatch(ServletContext servletContext, String path) {
    synchronized (asyncContextLock) {
        if (log.isDebugEnabled()) {
            logDebug("dispatch   ");
        }
        check();
        if (dispatch != null) {
            throw new IllegalStateException(sm.getString("asyncContextImpl.dispatchingStarted"));
        }
        if (request.getAttribute(ASYNC_REQUEST_URI) == null) {
            request.setAttribute(ASYNC_REQUEST_URI, request.getRequestURI());
            request.setAttribute(ASYNC_CONTEXT_PATH, request.getContextPath());
            request.setAttribute(ASYNC_SERVLET_PATH, request.getServletPath());
            request.setAttribute(ASYNC_PATH_INFO, request.getPathInfo());
            request.setAttribute(ASYNC_QUERY_STRING, request.getQueryString());
        }
        final RequestDispatcher requestDispatcher = servletContext.getRequestDispatcher(path);
        if (!(requestDispatcher instanceof AsyncDispatcher)) {
            throw new UnsupportedOperationException(sm.getString("asyncContextImpl.noAsyncDispatcher"));
        }
        final AsyncDispatcher applicationDispatcher = (AsyncDispatcher) requestDispatcher;
        final ServletRequest servletRequest = getRequest();
        final ServletResponse servletResponse = getResponse();
        this.dispatch = new AsyncRunnable(request, applicationDispatcher, servletRequest, servletResponse);
        this.request.getCoyoteRequest().action(ActionCode.ASYNC_DISPATCH, null);
        clearServletRequestResponse();
    }
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) ServletRequest(jakarta.servlet.ServletRequest) ServletResponse(jakarta.servlet.ServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) AsyncDispatcher(org.apache.catalina.AsyncDispatcher) RequestDispatcher(jakarta.servlet.RequestDispatcher)

Example 8 with ServletRequest

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

the class TestMimeHeadersIntegration method setupHeadersTest.

private void setupHeadersTest(Tomcat tomcat) {
    Context ctx = tomcat.addContext("", getTemporaryDirectory().getAbsolutePath());
    Tomcat.addServlet(ctx, "servlet", new HttpServlet() {

        private static final long serialVersionUID = 1L;

        @Override
        public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
            res.setContentType("text/plain; charset=ISO-8859-1");
            res.getWriter().write("OK");
        }
    });
    ctx.addServletMappingDecoded("/", "servlet");
    alv = new HeaderCountLogValve();
    tomcat.getHost().getPipeline().addValve(alv);
}
Also used : Context(org.apache.catalina.Context) ServletException(jakarta.servlet.ServletException) ServletRequest(jakarta.servlet.ServletRequest) ServletResponse(jakarta.servlet.ServletResponse) HttpServlet(jakarta.servlet.http.HttpServlet) IOException(java.io.IOException)

Example 9 with ServletRequest

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

the class ResponseBodyEmitterReturnValueHandler 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();
        returnType = returnType.nested();
        if (returnValue == null) {
            mavContainer.setRequestHandled(true);
            outputMessage.flush();
            return;
        }
    }
    ServletRequest request = webRequest.getNativeRequest(ServletRequest.class);
    Assert.state(request != null, "No ServletRequest");
    ResponseBodyEmitter emitter;
    if (returnValue instanceof ResponseBodyEmitter) {
        emitter = (ResponseBodyEmitter) returnValue;
    } else {
        emitter = this.reactiveHandler.handleValue(returnValue, returnType, mavContainer, webRequest);
        if (emitter == null) {
            // Not streaming: write headers without committing response..
            outputMessage.getHeaders().forEach((headerName, headerValues) -> {
                for (String headerValue : headerValues) {
                    response.addHeader(headerName, headerValue);
                }
            });
            return;
        }
    }
    emitter.extendResponse(outputMessage);
    // At this point we know we're streaming..
    ShallowEtagHeaderFilter.disableContentCaching(request);
    // Wrap the response to ignore further header changes
    // Headers will be flushed at the first write
    outputMessage = new StreamingServletServerHttpResponse(outputMessage);
    HttpMessageConvertingHandler handler;
    try {
        DeferredResult<?> deferredResult = new DeferredResult<>(emitter.getTimeout());
        WebAsyncUtils.getAsyncManager(webRequest).startDeferredResultProcessing(deferredResult, mavContainer);
        handler = new HttpMessageConvertingHandler(outputMessage, deferredResult);
    } catch (Throwable ex) {
        emitter.initializeWithError(ex);
        throw ex;
    }
    emitter.initialize(handler);
}
Also used : ServletRequest(jakarta.servlet.ServletRequest) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse) ServerHttpResponse(org.springframework.http.server.ServerHttpResponse) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse) DelegatingServerHttpResponse(org.springframework.http.server.DelegatingServerHttpResponse) ResponseEntity(org.springframework.http.ResponseEntity) DeferredResult(org.springframework.web.context.request.async.DeferredResult)

Example 10 with ServletRequest

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

the class ServletModelAttributeMethodProcessor method bindRequestParameters.

/**
 * This implementation downcasts {@link WebDataBinder} to
 * {@link ServletRequestDataBinder} before binding.
 * @see ServletRequestDataBinderFactory
 */
@Override
protected void bindRequestParameters(WebDataBinder binder, NativeWebRequest request) {
    ServletRequest servletRequest = request.getNativeRequest(ServletRequest.class);
    Assert.state(servletRequest != null, "No ServletRequest");
    ServletRequestDataBinder servletBinder = (ServletRequestDataBinder) binder;
    servletBinder.bind(servletRequest);
}
Also used : ServletRequest(jakarta.servlet.ServletRequest) ServletRequestDataBinder(org.springframework.web.bind.ServletRequestDataBinder)

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