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);
}
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);
}
}
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());
}
Aggregations