Search in sources :

Example 6 with Deployment

use of io.undertow.servlet.api.Deployment in project undertow by undertow-io.

the class AsyncContextImpl method dispatch.

@Override
public void dispatch(final ServletContext context, final String path) {
    if (dispatched) {
        throw UndertowServletMessages.MESSAGES.asyncRequestAlreadyDispatched();
    }
    HttpServletRequestImpl requestImpl = servletRequestContext.getOriginalRequest();
    HttpServletResponseImpl responseImpl = servletRequestContext.getOriginalResponse();
    final HttpServerExchange exchange = requestImpl.getExchange();
    exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY).setDispatcherType(DispatcherType.ASYNC);
    requestImpl.setAttribute(ASYNC_REQUEST_URI, requestImpl.getOriginalRequestURI());
    requestImpl.setAttribute(ASYNC_CONTEXT_PATH, requestImpl.getOriginalContextPath());
    requestImpl.setAttribute(ASYNC_SERVLET_PATH, requestImpl.getOriginalServletPath());
    requestImpl.setAttribute(ASYNC_QUERY_STRING, requestImpl.getOriginalQueryString());
    String newQueryString = "";
    int qsPos = path.indexOf("?");
    String newServletPath = path;
    if (qsPos != -1) {
        newQueryString = newServletPath.substring(qsPos + 1);
        newServletPath = newServletPath.substring(0, qsPos);
    }
    String newRequestUri = context.getContextPath() + newServletPath;
    //todo: a more efficient impl
    Map<String, Deque<String>> newQueryParameters = new HashMap<>();
    for (String part : newQueryString.split("&")) {
        String name = part;
        String value = "";
        int equals = part.indexOf('=');
        if (equals != -1) {
            name = part.substring(0, equals);
            value = part.substring(equals + 1);
        }
        Deque<String> queue = newQueryParameters.get(name);
        if (queue == null) {
            newQueryParameters.put(name, queue = new ArrayDeque<>(1));
        }
        queue.add(value);
    }
    requestImpl.setQueryParameters(newQueryParameters);
    requestImpl.getExchange().setRelativePath(newServletPath);
    requestImpl.getExchange().setQueryString(newQueryString);
    requestImpl.getExchange().setRequestPath(newRequestUri);
    requestImpl.getExchange().setRequestURI(newRequestUri);
    requestImpl.setServletContext((ServletContextImpl) context);
    responseImpl.setServletContext((ServletContextImpl) context);
    Deployment deployment = requestImpl.getServletContext().getDeployment();
    ServletPathMatch info = deployment.getServletPaths().getServletHandlerByPath(newServletPath);
    requestImpl.getExchange().getAttachment(ServletRequestContext.ATTACHMENT_KEY).setServletPathMatch(info);
    dispatchAsyncRequest(deployment.getServletDispatcher(), info, exchange);
}
Also used : HttpServerExchange(io.undertow.server.HttpServerExchange) HashMap(java.util.HashMap) Deployment(io.undertow.servlet.api.Deployment) ServletPathMatch(io.undertow.servlet.handlers.ServletPathMatch) Deque(java.util.Deque) ArrayDeque(java.util.ArrayDeque) ArrayDeque(java.util.ArrayDeque)

Example 7 with Deployment

use of io.undertow.servlet.api.Deployment in project undertow by undertow-io.

the class AsyncContextImpl method dispatch.

@Override
public void dispatch() {
    if (dispatched) {
        throw UndertowServletMessages.MESSAGES.asyncRequestAlreadyDispatched();
    }
    final HttpServletRequestImpl requestImpl = this.servletRequestContext.getOriginalRequest();
    Deployment deployment = requestImpl.getServletContext().getDeployment();
    if (requestSupplied && servletRequest instanceof HttpServletRequest) {
        ServletContainer container = deployment.getServletContainer();
        final String requestURI = ((HttpServletRequest) servletRequest).getRequestURI();
        DeploymentManager context = container.getDeploymentByPath(requestURI);
        if (context == null) {
            throw UndertowServletMessages.MESSAGES.couldNotFindContextToDispatchTo(requestImpl.getOriginalContextPath());
        }
        String toDispatch = requestURI.substring(context.getDeployment().getServletContext().getContextPath().length());
        String qs = ((HttpServletRequest) servletRequest).getQueryString();
        if (qs != null && !qs.isEmpty()) {
            toDispatch = toDispatch + "?" + qs;
        }
        dispatch(context.getDeployment().getServletContext(), toDispatch);
    } else {
        //original request
        ServletContainer container = deployment.getServletContainer();
        DeploymentManager context = container.getDeploymentByPath(requestImpl.getOriginalContextPath());
        if (context == null) {
            //this should never happen
            throw UndertowServletMessages.MESSAGES.couldNotFindContextToDispatchTo(requestImpl.getOriginalContextPath());
        }
        String toDispatch = CanonicalPathUtils.canonicalize(requestImpl.getOriginalRequestURI()).substring(requestImpl.getOriginalContextPath().length());
        String qs = requestImpl.getOriginalQueryString();
        if (qs != null && !qs.isEmpty()) {
            toDispatch = toDispatch + "?" + qs;
        }
        dispatch(context.getDeployment().getServletContext(), toDispatch);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) DeploymentManager(io.undertow.servlet.api.DeploymentManager) ServletContainer(io.undertow.servlet.api.ServletContainer) Deployment(io.undertow.servlet.api.Deployment)

Example 8 with Deployment

use of io.undertow.servlet.api.Deployment in project wildfly by wildfly.

the class UndertowSessionExpirationListenerTestCase method sessionExpired.

@Test
public void sessionExpired() {
    Deployment deployment = mock(Deployment.class);
    UndertowSessionManager manager = mock(UndertowSessionManager.class);
    SessionManager<LocalSessionContext, Batch> delegateManager = mock(SessionManager.class);
    Batcher<Batch> batcher = mock(Batcher.class);
    Batch batch = mock(Batch.class);
    SessionListener listener = mock(SessionListener.class);
    ImmutableSession session = mock(ImmutableSession.class);
    ImmutableSessionAttributes attributes = mock(ImmutableSessionAttributes.class);
    ImmutableSessionMetaData metaData = mock(ImmutableSessionMetaData.class);
    ArgumentCaptor<Session> capturedSession = ArgumentCaptor.forClass(Session.class);
    String expectedSessionId = "session";
    SessionListeners listeners = new SessionListeners();
    listeners.addSessionListener(listener);
    SessionExpirationListener expirationListener = new UndertowSessionExpirationListener(deployment, listeners);
    when(deployment.getSessionManager()).thenReturn(manager);
    when(manager.getSessionManager()).thenReturn(delegateManager);
    when(delegateManager.getBatcher()).thenReturn(batcher);
    when(batcher.suspendBatch()).thenReturn(batch);
    when(session.getId()).thenReturn(expectedSessionId);
    when(session.getAttributes()).thenReturn(attributes);
    when(attributes.getAttributeNames()).thenReturn(Collections.emptySet());
    when(session.getMetaData()).thenReturn(metaData);
    when(metaData.getCreationTime()).thenReturn(Instant.now());
    when(metaData.getLastAccessedTime()).thenReturn(Instant.now());
    when(metaData.getMaxInactiveInterval()).thenReturn(Duration.ZERO);
    expirationListener.sessionExpired(session);
    verify(batcher).suspendBatch();
    verify(listener).sessionDestroyed(capturedSession.capture(), isNull(HttpServerExchange.class), same(SessionListener.SessionDestroyedReason.TIMEOUT));
    verify(batcher).resumeBatch(batch);
    assertSame(expectedSessionId, capturedSession.getValue().getId());
    assertSame(manager, capturedSession.getValue().getSessionManager());
}
Also used : ImmutableSessionAttributes(org.wildfly.clustering.web.session.ImmutableSessionAttributes) ImmutableSession(org.wildfly.clustering.web.session.ImmutableSession) Deployment(io.undertow.servlet.api.Deployment) ImmutableSessionMetaData(org.wildfly.clustering.web.session.ImmutableSessionMetaData) HttpServerExchange(io.undertow.server.HttpServerExchange) SessionExpirationListener(org.wildfly.clustering.web.session.SessionExpirationListener) Batch(org.wildfly.clustering.ee.Batch) SessionListeners(io.undertow.server.session.SessionListeners) SessionListener(io.undertow.server.session.SessionListener) Session(io.undertow.server.session.Session) ImmutableSession(org.wildfly.clustering.web.session.ImmutableSession) Test(org.junit.Test)

Aggregations

Deployment (io.undertow.servlet.api.Deployment)8 DeploymentInfo (io.undertow.servlet.api.DeploymentInfo)4 HttpHandler (io.undertow.server.HttpHandler)3 Test (org.junit.Test)3 HttpServerExchange (io.undertow.server.HttpServerExchange)2 Context (org.jboss.modcluster.container.Context)2 SecurityContext (io.undertow.security.api.SecurityContext)1 Account (io.undertow.security.idm.Account)1 Session (io.undertow.server.session.Session)1 SessionListener (io.undertow.server.session.SessionListener)1 SessionListeners (io.undertow.server.session.SessionListeners)1 AuthorizationManager (io.undertow.servlet.api.AuthorizationManager)1 DeploymentManager (io.undertow.servlet.api.DeploymentManager)1 ServletContainer (io.undertow.servlet.api.ServletContainer)1 ServletChain (io.undertow.servlet.handlers.ServletChain)1 ServletPathMatch (io.undertow.servlet.handlers.ServletPathMatch)1 ServletRequestContext (io.undertow.servlet.handlers.ServletRequestContext)1 HttpString (io.undertow.util.HttpString)1 ArrayDeque (java.util.ArrayDeque)1 Deque (java.util.Deque)1