Skip to content

Commit

Permalink
Merge branch 'Issue-3' into github-master
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean Deruelle committed Sep 11, 2015
2 parents ab879bd + faf4c00 commit 7ba995b
Show file tree
Hide file tree
Showing 13 changed files with 237 additions and 121 deletions.
22 changes: 6 additions & 16 deletions containers/sip-servlets-as8-drop-in/jboss-as-mobicents/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,46 +106,36 @@
<dependency>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-undertow</artifactId>
<version>8.2.0.Final</version>
<version>8.2.1.Final</version>
<scope>provided</scope>
<!-- exclusions>
<exclusion>
<groupId>io.undertow</groupId>
<artifactId>undertow-core</artifactId>
</exclusion>
<exclusion>
<groupId>io.undertow</groupId>
<artifactId>undertow-servlet</artifactId>
</exclusion>
</exclusions -->
</dependency>
<dependency>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-naming</artifactId>
<version>8.2.0.Final</version>
<version>8.2.1.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-web</artifactId>
<version>8.2.0.Final</version>
<version>8.2.1.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-web-common</artifactId>
<version>8.2.0.Final</version>
<version>8.2.1.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.undertow</groupId>
<artifactId>undertow-core</artifactId>
<version>1.1.0.Final</version>
<version>1.1.8.Final</version>
</dependency>
<dependency>
<groupId>io.undertow</groupId>
<artifactId>undertow-servlet</artifactId>
<version>1.1.0.Final</version>
<version>1.1.8.Final</version>
</dependency>

<dependency>
Expand Down
20 changes: 5 additions & 15 deletions containers/sip-servlets-as8/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,40 +21,30 @@
<dependency>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-undertow</artifactId>
<version>8.2.0.Final</version>
<version>8.2.1.Final</version>
<scope>provided</scope>
<!-- exclusions>
<exclusion>
<groupId>io.undertow</groupId>
<artifactId>undertow-core</artifactId>
</exclusion>
<exclusion>
<groupId>io.undertow</groupId>
<artifactId>undertow-servlet</artifactId>
</exclusion>
</exclusions -->
</dependency>
<dependency>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-web</artifactId>
<version>8.2.0.Final</version>
<version>8.2.1.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-web-common</artifactId>
<version>8.2.0.Final</version>
<version>8.2.1.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.undertow</groupId>
<artifactId>undertow-core</artifactId>
<version>1.1.0.Final</version>
<version>1.1.8.Final</version>
</dependency>
<dependency>
<groupId>io.undertow</groupId>
<artifactId>undertow-servlet</artifactId>
<version>1.1.0.Final</version>
<version>1.1.8.Final</version>
</dependency>
<!--dependency>
<groupId>org.jboss.security</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public void stop() {
}

@Override
public Session createSession(HttpServerExchange serverExchange, SessionConfig sessionCookieConfig) {
public Session createSession(final HttpServerExchange serverExchange, final SessionConfig config) {
if (evictionQueue != null) {
while (sessions.size() >= maxSize && !evictionQueue.isEmpty()) {
String key = evictionQueue.poll();
Expand All @@ -114,10 +114,10 @@ public Session createSession(HttpServerExchange serverExchange, SessionConfig se
}
}
}
if (sessionCookieConfig == null) {
if (config == null) {
throw UndertowMessages.MESSAGES.couldNotFindSessionCookieConfig();
}
String sessionID = sessionCookieConfig.findSessionId(serverExchange);
String sessionID = config.findSessionId(serverExchange);
int count = 0;
while (sessionID == null) {
sessionID = sessionIdGenerator.createSessionId();
Expand All @@ -136,10 +136,10 @@ public Session createSession(HttpServerExchange serverExchange, SessionConfig se
} else {
evictionToken = null;
}
final ConvergedSessionImpl session = new ConvergedSessionImpl(this, sessionID, sessionCookieConfig, serverExchange.getIoThread(), serverExchange.getConnection().getWorker(), evictionToken);
final ConvergedSessionImpl session = new ConvergedSessionImpl(this, sessionID, config, serverExchange.getIoThread(), serverExchange.getConnection().getWorker(), evictionToken);
ConvergedInMemorySession im = new ConvergedInMemorySession(session, defaultSessionTimeout);
sessions.put(sessionID, im);
sessionCookieConfig.setSessionId(serverExchange, session.getId());
config.setSessionId(serverExchange, session.getId());
im.lastAccessed = System.currentTimeMillis();
session.bumpTimeout();
sessionListeners.sessionCreated(session, serverExchange);
Expand All @@ -158,7 +158,7 @@ public void addConvergedSessionDeletegateToSession(SessionConfig sessionCookieCo
}

@Override
public Session getSession(HttpServerExchange serverExchange, SessionConfig sessionCookieConfig) {
public Session getSession(final HttpServerExchange serverExchange, final SessionConfig sessionCookieConfig) {
String sessionId = sessionCookieConfig.findSessionId(serverExchange);
return getSession(sessionId);
}
Expand All @@ -182,12 +182,12 @@ public void registerSessionListener(SessionListener listener) {
}

@Override
public void removeSessionListener(SessionListener listener) {
public synchronized void removeSessionListener(final SessionListener listener) {
sessionListeners.removeSessionListener(listener);
}

@Override
public void setDefaultSessionTimeout(int timeout) {
public void setDefaultSessionTimeout(final int timeout) {
defaultSessionTimeout = timeout;
}

Expand All @@ -206,6 +206,23 @@ public Set<String> getAllSessions() {
return new HashSet<>(sessions.keySet());
}

@Override
public boolean equals(Object object) {
if (!(object instanceof SessionManager)) return false;
SessionManager manager = (SessionManager) object;
return this.deploymentName.equals(manager.getDeploymentName());
}

@Override
public int hashCode() {
return this.deploymentName.hashCode();
}

@Override
public String toString() {
return this.deploymentName;
}

/**
* session implementation for the in memory session manager
*/
Expand Down Expand Up @@ -235,6 +252,7 @@ private static AtomicReferenceFieldUpdater<ConvergedSessionImpl, Object> createT
private volatile Object evictionToken;
private final SessionConfig sessionCookieConfig;
private volatile long expireTime = -1;
private volatile boolean invalidationStarted = false;

final XnioExecutor executor;
final XnioWorker worker;
Expand Down Expand Up @@ -268,9 +286,13 @@ private ConvergedSessionImpl(ConvergedInMemorySessionManager sessionManager, fin
}

synchronized void bumpTimeout() {
if(invalidationStarted) {
return;
}

final int maxInactiveInterval = getMaxInactiveInterval();
if (maxInactiveInterval > 0) {
long newExpireTime = System.currentTimeMillis() + (maxInactiveInterval * 1000);
long newExpireTime = System.currentTimeMillis() + (maxInactiveInterval * 1000L);
if(timerCancelKey != null && (newExpireTime < expireTime)) {
// We have to re-schedule as the new maxInactiveInterval is lower than the old one
if (!timerCancelKey.remove()) {
Expand All @@ -283,7 +305,7 @@ synchronized void bumpTimeout() {
//+1 second, to make sure that the time has actually expired
//we don't re-schedule every time, as it is expensive
//instead when it expires we check if the timeout has been bumped, and if so we re-schedule
timerCancelKey = executor.executeAfter(cancelTask, (maxInactiveInterval * 1000) + 1, TimeUnit.MILLISECONDS);
timerCancelKey = executor.executeAfter(cancelTask, (maxInactiveInterval * 1000L) + 1, TimeUnit.MILLISECONDS);
}
}
if (evictionToken != null) {
Expand Down Expand Up @@ -405,22 +427,27 @@ public void invalidate(final HttpServerExchange exchange) {
invalidate(exchange, SessionListener.SessionDestroyedReason.INVALIDATED);
}

synchronized void invalidate(final HttpServerExchange exchange, SessionListener.SessionDestroyedReason reason) {
if (timerCancelKey != null) {
timerCancelKey.remove();
}
ConvergedInMemorySession sess = sessionManager.sessions.get(sessionId);
if (sess == null) {
if (reason == SessionListener.SessionDestroyedReason.INVALIDATED) {
throw UndertowMessages.MESSAGES.sessionAlreadyInvalidated();
void invalidate(final HttpServerExchange exchange, SessionListener.SessionDestroyedReason reason) {
synchronized(ConvergedSessionImpl.this) {
if (timerCancelKey != null) {
timerCancelKey.remove();
}
return;
ConvergedInMemorySession sess = sessionManager.sessions.get(sessionId);
if (sess == null) {
if (reason == SessionListener.SessionDestroyedReason.INVALIDATED) {
throw UndertowMessages.MESSAGES.sessionAlreadyInvalidated();
}
return;
}
invalidationStarted = true;
}
sessionManager.sessionListeners.sessionDestroyed(sess.session, exchange, reason);
sessionManager.sessionListeners.sessionDestroyed(this, exchange, reason);

sessionManager.sessions.remove(sessionId);
if (exchange != null) {
sessionCookieConfig.clearSession(exchange, this.getId());
}

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ public void deploy() {
}

final List<ThreadSetupAction> setup = new ArrayList<>();
setup.add(ServletRequestContextThreadSetupAction.INSTANCE);
setup.add(new ContextClassLoaderSetupAction(deploymentInfo.getClassLoader()));
setup.addAll(deploymentInfo.getThreadSetupActions());
final CompositeThreadSetupAction threadSetupAction = new CompositeThreadSetupAction(setup);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public class ConvergedSessionListenerBridge extends SessionListenerBridge{
private final ServletContext servletContext;
private final SessionManager manager;

public ConvergedSessionListenerBridge(ThreadSetupAction threadSetup, ApplicationListeners applicationListeners,
ServletContext servletContext, SessionManager manager) {
public ConvergedSessionListenerBridge(final ThreadSetupAction threadSetup, final ApplicationListeners applicationListeners,
final ServletContext servletContext, final SessionManager manager) {
super(threadSetup, applicationListeners, servletContext);
this.threadSetup = threadSetup;
this.applicationListeners = applicationListeners;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@
*/
class SecurityActions {

static String getSystemProperty(final String prop) {
if (System.getSecurityManager() == null) {
return System.getProperty(prop);
} else {
return (String) AccessController.doPrivileged(new PrivilegedAction<Object>() {
public Object run() {
return System.getProperty(prop);
}
});
}
}

static HttpSession forSession(final Session session, final ServletContext servletContext, final boolean newSession, final SessionManager manager) {
if (System.getSecurityManager() == null) {
return ConvergedHttpSessionFacade.forConvergedSession(session, servletContext, newSession, manager);
Expand All @@ -69,18 +81,34 @@ public ServletRequestContext run() {
}
}

static String getSystemProperty(final String prop) {
static void setCurrentRequestContext(final ServletRequestContext servletRequestContext) {
if (System.getSecurityManager() == null) {
return System.getProperty(prop);
ServletRequestContext.setCurrentRequestContext(servletRequestContext);
} else {
return (String) AccessController.doPrivileged(new PrivilegedAction<Object>() {
AccessController.doPrivileged(new PrivilegedAction<Object>() {
@Override
public Object run() {
return System.getProperty(prop);
ServletRequestContext.setCurrentRequestContext(servletRequestContext);
return null;
}
});
}
}

static void clearCurrentServletAttachments() {
if (System.getSecurityManager() == null) {
ServletRequestContext.clearCurrentServletAttachments();
} else {
AccessController.doPrivileged(new PrivilegedAction<Object>() {
@Override
public Object run() {
ServletRequestContext.clearCurrentServletAttachments();
return null;
}
});
}
}

static ServletInitialHandler createServletInitialHandler(final ServletPathMatches paths, final HttpHandler next, final CompositeThreadSetupAction setupAction, final ServletContext servletContext) {
if (System.getSecurityManager() == null) {
return new ConvergedServletInitialHandler(paths, next, setupAction, /*TODO*/(ConvergedServletContextImpl) servletContext);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* TeleStax, Open Source Cloud Communications
* Copyright 2011-2015, Telestax Inc and individual contributors
* by the @authors tag.
*
* This program is free software: you can redistribute it and/or modify
* under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation; either version 3 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package org.mobicents.io.undertow.servlet.core;

import io.undertow.server.HttpServerExchange;
import io.undertow.servlet.api.ThreadSetupAction;
import io.undertow.servlet.handlers.ServletRequestContext;

/**
* This class is based on protected class io.undertow.servlet.core.ServletRequestContextThreadSetupAction.
*
* @author [email protected]
*/
class ServletRequestContextThreadSetupAction implements ThreadSetupAction {

static final ServletRequestContextThreadSetupAction INSTANCE = new ServletRequestContextThreadSetupAction();

private ServletRequestContextThreadSetupAction() {

}

private static final Handle HANDLE = new Handle() {
@Override
public void tearDown() {
SecurityActions.clearCurrentServletAttachments();
}
};

@Override
public Handle setup(HttpServerExchange exchange) {
if(exchange == null) {
return null;
}
ServletRequestContext servletRequestContext = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
SecurityActions.setCurrentRequestContext(servletRequestContext);
return HANDLE;
}
}
Loading

0 comments on commit 7ba995b

Please sign in to comment.