Search in sources :

Example 66 with ServletRequest

use of javax.servlet.ServletRequest in project wildfly by wildfly.

the class UndertowContextTestCase method addRequestListener.

@Test
public void addRequestListener() throws ServletException {
    ServletRequestListener listener = mock(ServletRequestListener.class);
    ServletContext context = mock(ServletContext.class);
    ServletRequest request = mock(ServletRequest.class);
    ApplicationListeners listeners = new ApplicationListeners(Collections.<ManagedListener>emptyList(), context);
    ArgumentCaptor<ServletRequestEvent> event = ArgumentCaptor.forClass(ServletRequestEvent.class);
    when(this.deployment.getApplicationListeners()).thenReturn(listeners);
    this.context.addRequestListener(listener);
    listeners.start();
    listeners.requestInitialized(request);
    verify(listener).requestInitialized(event.capture());
    assertSame(request, event.getValue().getServletRequest());
    assertSame(context, event.getValue().getServletContext());
    event = ArgumentCaptor.forClass(ServletRequestEvent.class);
    listeners.requestDestroyed(request);
    verify(listener).requestDestroyed(event.capture());
    assertSame(request, event.getValue().getServletRequest());
    assertSame(context, event.getValue().getServletContext());
}
Also used : ServletRequest(javax.servlet.ServletRequest) ServletRequestListener(javax.servlet.ServletRequestListener) ApplicationListeners(io.undertow.servlet.core.ApplicationListeners) ServletContext(javax.servlet.ServletContext) ServletRequestEvent(javax.servlet.ServletRequestEvent) Test(org.junit.Test)

Example 67 with ServletRequest

use of javax.servlet.ServletRequest in project wildfly by wildfly.

the class JASPICSecurityContext method buildAppContext.

/**
     * <p>
     * Builds the JASPIC application context.
     * </p>
     *
     * @return a {@code String} representing the application context.
     */
private String buildAppContext() {
    final ServletRequestContext requestContext = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
    ServletRequest servletRequest = requestContext.getServletRequest();
    return servletRequest.getServletContext().getVirtualServerName() + " " + servletRequest.getServletContext().getContextPath();
}
Also used : ServletRequest(javax.servlet.ServletRequest) ServletRequestContext(io.undertow.servlet.handlers.ServletRequestContext)

Example 68 with ServletRequest

use of javax.servlet.ServletRequest in project wildfly by wildfly.

the class AuditNotificationReceiver method handleNotification.

@Override
public void handleNotification(SecurityNotification notification) {
    EventType event = notification.getEventType();
    if (event == EventType.AUTHENTICATED || event == EventType.FAILED_AUTHENTICATION) {
        AuditEvent auditEvent = new AuditEvent(event == EventType.AUTHENTICATED ? AuditLevel.SUCCESS : AuditLevel.FAILURE);
        Map<String, Object> ctxMap = new HashMap<String, Object>();
        Account account = notification.getAccount();
        if (account != null) {
            ctxMap.put("principal", account.getPrincipal().getName());
        }
        ctxMap.put("message", notification.getMessage());
        ServletRequestContext src = notification.getExchange().getAttachment(ServletRequestContext.ATTACHMENT_KEY);
        if (src != null) {
            ServletRequest hsr = src.getServletRequest();
            if (hsr instanceof HttpServletRequest) {
                ctxMap.put("request", WebUtil.deriveUsefulInfo((HttpServletRequest) hsr));
            }
        }
        ctxMap.put("Source", getClass().getCanonicalName());
        auditEvent.setContextMap(ctxMap);
        auditManager.audit(auditEvent);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Account(io.undertow.security.idm.Account) ServletRequest(javax.servlet.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) EventType(io.undertow.security.api.SecurityNotification.EventType) HashMap(java.util.HashMap) ServletRequestContext(io.undertow.servlet.handlers.ServletRequestContext) AuditEvent(org.jboss.security.audit.AuditEvent)

Example 69 with ServletRequest

use of javax.servlet.ServletRequest in project undertow by undertow-io.

the class RequestDispatcherImpl method includeImpl.

private void includeImpl(ServletRequest request, ServletResponse response, ServletRequestContext servletRequestContext, HttpServletRequestImpl requestImpl, HttpServletResponseImpl responseImpl) throws ServletException, IOException {
    if (!servletContext.getDeployment().getDeploymentInfo().isAllowNonStandardWrappers()) {
        if (servletRequestContext.getOriginalRequest() != request) {
            if (!(request instanceof ServletRequestWrapper)) {
                throw UndertowServletMessages.MESSAGES.requestWasNotOriginalOrWrapper(request);
            }
        }
        if (servletRequestContext.getOriginalResponse() != response) {
            if (!(response instanceof ServletResponseWrapper)) {
                throw UndertowServletMessages.MESSAGES.responseWasNotOriginalOrWrapper(response);
            }
        }
    }
    final ServletRequest oldRequest = servletRequestContext.getServletRequest();
    final ServletResponse oldResponse = servletRequestContext.getServletResponse();
    Object requestUri = null;
    Object contextPath = null;
    Object servletPath = null;
    Object pathInfo = null;
    Object queryString = null;
    Map<String, Deque<String>> queryParameters = requestImpl.getQueryParameters();
    if (!named) {
        requestUri = request.getAttribute(INCLUDE_REQUEST_URI);
        contextPath = request.getAttribute(INCLUDE_CONTEXT_PATH);
        servletPath = request.getAttribute(INCLUDE_SERVLET_PATH);
        pathInfo = request.getAttribute(INCLUDE_PATH_INFO);
        queryString = request.getAttribute(INCLUDE_QUERY_STRING);
        int qsPos = path.indexOf("?");
        String newServletPath = path;
        if (qsPos != -1) {
            String newQueryString = newServletPath.substring(qsPos + 1);
            newServletPath = newServletPath.substring(0, qsPos);
            String encoding = QueryParameterUtils.getQueryParamEncoding(servletRequestContext.getExchange());
            Map<String, Deque<String>> newQueryParameters = QueryParameterUtils.mergeQueryParametersWithNewQueryString(queryParameters, newQueryString, encoding);
            requestImpl.setQueryParameters(newQueryParameters);
            requestImpl.setAttribute(INCLUDE_QUERY_STRING, newQueryString);
        } else {
            requestImpl.setAttribute(INCLUDE_QUERY_STRING, "");
        }
        String newRequestUri = servletContext.getContextPath() + newServletPath;
        requestImpl.setAttribute(INCLUDE_REQUEST_URI, newRequestUri);
        requestImpl.setAttribute(INCLUDE_CONTEXT_PATH, servletContext.getContextPath());
        requestImpl.setAttribute(INCLUDE_SERVLET_PATH, pathMatch.getMatched());
        requestImpl.setAttribute(INCLUDE_PATH_INFO, pathMatch.getRemaining());
    }
    boolean inInclude = responseImpl.isInsideInclude();
    responseImpl.setInsideInclude(true);
    DispatcherType oldDispatcherType = servletRequestContext.getDispatcherType();
    ServletContextImpl oldContext = requestImpl.getServletContext();
    try {
        requestImpl.setServletContext(servletContext);
        responseImpl.setServletContext(servletContext);
        try {
            servletRequestContext.setServletRequest(request);
            servletRequestContext.setServletResponse(response);
            servletContext.getDeployment().getServletDispatcher().dispatchToServlet(requestImpl.getExchange(), chain, DispatcherType.INCLUDE);
        } catch (ServletException e) {
            throw e;
        } catch (IOException e) {
            throw e;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    } finally {
        responseImpl.setInsideInclude(inInclude);
        requestImpl.setServletContext(oldContext);
        responseImpl.setServletContext(oldContext);
        servletRequestContext.setServletRequest(oldRequest);
        servletRequestContext.setServletResponse(oldResponse);
        servletRequestContext.setDispatcherType(oldDispatcherType);
        if (!named) {
            requestImpl.setAttribute(INCLUDE_REQUEST_URI, requestUri);
            requestImpl.setAttribute(INCLUDE_CONTEXT_PATH, contextPath);
            requestImpl.setAttribute(INCLUDE_SERVLET_PATH, servletPath);
            requestImpl.setAttribute(INCLUDE_PATH_INFO, pathInfo);
            requestImpl.setAttribute(INCLUDE_QUERY_STRING, queryString);
            requestImpl.setQueryParameters(queryParameters);
        }
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) ServletRequestWrapper(javax.servlet.ServletRequestWrapper) IOException(java.io.IOException) Deque(java.util.Deque) ServletException(javax.servlet.ServletException) PrivilegedActionException(java.security.PrivilegedActionException) IOException(java.io.IOException) ServletException(javax.servlet.ServletException) DispatcherType(javax.servlet.DispatcherType) ServletResponseWrapper(javax.servlet.ServletResponseWrapper)

Example 70 with ServletRequest

use of javax.servlet.ServletRequest in project undertow by undertow-io.

the class RequestDispatcherImpl method forwardImpl.

private void forwardImpl(ServletRequest request, ServletResponse response, ServletRequestContext servletRequestContext) throws ServletException, IOException {
    final HttpServletRequestImpl requestImpl = servletRequestContext.getOriginalRequest();
    final HttpServletResponseImpl responseImpl = servletRequestContext.getOriginalResponse();
    if (!servletContext.getDeployment().getDeploymentInfo().isAllowNonStandardWrappers()) {
        if (servletRequestContext.getOriginalRequest() != request) {
            if (!(request instanceof ServletRequestWrapper)) {
                throw UndertowServletMessages.MESSAGES.requestWasNotOriginalOrWrapper(request);
            }
        }
        if (servletRequestContext.getOriginalResponse() != response) {
            if (!(response instanceof ServletResponseWrapper)) {
                throw UndertowServletMessages.MESSAGES.responseWasNotOriginalOrWrapper(response);
            }
        }
    }
    response.resetBuffer();
    final ServletRequest oldRequest = servletRequestContext.getServletRequest();
    final ServletResponse oldResponse = servletRequestContext.getServletResponse();
    Map<String, Deque<String>> queryParameters = requestImpl.getQueryParameters();
    request.removeAttribute(INCLUDE_REQUEST_URI);
    request.removeAttribute(INCLUDE_CONTEXT_PATH);
    request.removeAttribute(INCLUDE_SERVLET_PATH);
    request.removeAttribute(INCLUDE_PATH_INFO);
    request.removeAttribute(INCLUDE_QUERY_STRING);
    if (!named) {
        //only update if this is the first forward
        if (request.getAttribute(FORWARD_REQUEST_URI) == null) {
            requestImpl.setAttribute(FORWARD_REQUEST_URI, requestImpl.getRequestURI());
            requestImpl.setAttribute(FORWARD_CONTEXT_PATH, requestImpl.getContextPath());
            requestImpl.setAttribute(FORWARD_SERVLET_PATH, requestImpl.getServletPath());
            requestImpl.setAttribute(FORWARD_PATH_INFO, requestImpl.getPathInfo());
            requestImpl.setAttribute(FORWARD_QUERY_STRING, requestImpl.getQueryString());
        }
        int qsPos = path.indexOf("?");
        String newServletPath = path;
        if (qsPos != -1) {
            String newQueryString = newServletPath.substring(qsPos + 1);
            newServletPath = newServletPath.substring(0, qsPos);
            String encoding = QueryParameterUtils.getQueryParamEncoding(servletRequestContext.getExchange());
            Map<String, Deque<String>> newQueryParameters = QueryParameterUtils.mergeQueryParametersWithNewQueryString(queryParameters, newQueryString, encoding);
            requestImpl.getExchange().setQueryString(newQueryString);
            requestImpl.setQueryParameters(newQueryParameters);
        }
        String newRequestUri = servletContext.getContextPath() + newServletPath;
        requestImpl.getExchange().setRelativePath(newServletPath);
        requestImpl.getExchange().setRequestPath(newRequestUri);
        requestImpl.getExchange().setRequestURI(newRequestUri);
        requestImpl.getExchange().getAttachment(ServletRequestContext.ATTACHMENT_KEY).setServletPathMatch(pathMatch);
        requestImpl.setServletContext(servletContext);
        responseImpl.setServletContext(servletContext);
    }
    try {
        try {
            servletRequestContext.setServletRequest(request);
            servletRequestContext.setServletResponse(response);
            if (named) {
                servletContext.getDeployment().getServletDispatcher().dispatchToServlet(requestImpl.getExchange(), chain, DispatcherType.FORWARD);
            } else {
                servletContext.getDeployment().getServletDispatcher().dispatchToPath(requestImpl.getExchange(), pathMatch, DispatcherType.FORWARD);
            }
            //if we are not in an async or error dispatch then we close the response
            if (!request.isAsyncStarted()) {
                if (response instanceof HttpServletResponseImpl) {
                    responseImpl.closeStreamAndWriter();
                } else {
                    try {
                        final PrintWriter writer = response.getWriter();
                        writer.flush();
                        writer.close();
                    } catch (IllegalStateException e) {
                        final ServletOutputStream outputStream = response.getOutputStream();
                        outputStream.flush();
                        outputStream.close();
                    }
                }
            }
        } catch (ServletException e) {
            throw e;
        } catch (IOException e) {
            throw e;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    } finally {
        servletRequestContext.setServletRequest(oldRequest);
        servletRequestContext.setServletResponse(oldResponse);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) ServletOutputStream(javax.servlet.ServletOutputStream) ServletRequestWrapper(javax.servlet.ServletRequestWrapper) IOException(java.io.IOException) Deque(java.util.Deque) ServletException(javax.servlet.ServletException) PrivilegedActionException(java.security.PrivilegedActionException) IOException(java.io.IOException) ServletException(javax.servlet.ServletException) ServletResponseWrapper(javax.servlet.ServletResponseWrapper) PrintWriter(java.io.PrintWriter)

Aggregations

ServletRequest (javax.servlet.ServletRequest)185 ServletResponse (javax.servlet.ServletResponse)129 HttpServletRequest (javax.servlet.http.HttpServletRequest)117 HttpServletResponse (javax.servlet.http.HttpServletResponse)95 FilterChain (javax.servlet.FilterChain)79 Test (org.junit.Test)75 ServletException (javax.servlet.ServletException)59 IOException (java.io.IOException)57 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)35 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)35 MockFilterChain (org.springframework.mock.web.MockFilterChain)32 Filter (javax.servlet.Filter)28 Injector (com.google.inject.Injector)25 HttpServletResponseWrapper (javax.servlet.http.HttpServletResponseWrapper)21 NestedServletException (org.springframework.web.util.NestedServletException)19 ServletTestUtils.newFakeHttpServletRequest (com.google.inject.servlet.ServletTestUtils.newFakeHttpServletRequest)18 ServletTestUtils.newFakeHttpServletResponse (com.google.inject.servlet.ServletTestUtils.newFakeHttpServletResponse)18 HttpServletRequestWrapper (javax.servlet.http.HttpServletRequestWrapper)15 ErrorPage (org.springframework.boot.web.server.ErrorPage)15 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)14