org.frameworkset.web.socket.handler.HandshakeInterceptorChain Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bboss-websocket Show documentation
Show all versions of bboss-websocket Show documentation
bboss is a j2ee framework include aop/ioc,mvc,persistent,taglib,rpc,event ,bean-xml serializable and so on.http://www.bbossgroups.com
The newest version!
package org.frameworkset.web.socket.handler;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.frameworkset.http.ServerHttpRequest;
import org.frameworkset.http.ServerHttpResponse;
import org.frameworkset.web.socket.inf.WebSocketHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HandshakeInterceptorChain {
private static final Logger logger = LoggerFactory.getLogger(HandshakeInterceptorChain.class);
private final List interceptors;
private final WebSocketHandler wsHandler;
private int interceptorIndex = -1;
public HandshakeInterceptorChain(List interceptors, WebSocketHandler wsHandler) {
this.interceptors = (interceptors != null ? interceptors : Collections.emptyList());
this.wsHandler = wsHandler;
}
public boolean applyBeforeHandshake(ServerHttpRequest request, ServerHttpResponse response,
Map attributes) throws Exception {
for (int i = 0; i < this.interceptors.size(); i++) {
HandshakeInterceptor interceptor = this.interceptors.get(i);
if (!interceptor.beforeHandshake(request, response, this.wsHandler, attributes)) {
if (logger.isDebugEnabled()) {
logger.debug(interceptor + " returns false from beforeHandshake - precluding handshake");
}
applyAfterHandshake(request, response, null);
return false;
}
this.interceptorIndex = i;
}
return true;
}
public void applyAfterHandshake(ServerHttpRequest request, ServerHttpResponse response, Exception failure) {
for (int i = this.interceptorIndex; i >= 0; i--) {
HandshakeInterceptor interceptor = this.interceptors.get(i);
try {
interceptor.afterHandshake(request, response, this.wsHandler, failure);
}
catch (Throwable ex) {
if (logger.isWarnEnabled()) {
logger.warn(interceptor + " threw exception in afterHandshake: " + ex);
}
}
}
}
}