001/*
002 * Copyright 2002-2014 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.web.socket.server.support;
018
019import java.util.Collections;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.commons.logging.Log;
024import org.apache.commons.logging.LogFactory;
025
026import org.springframework.http.server.ServerHttpRequest;
027import org.springframework.http.server.ServerHttpResponse;
028import org.springframework.web.socket.WebSocketHandler;
029import org.springframework.web.socket.server.HandshakeInterceptor;
030
031/**
032 * A helper class that assists with invoking a list of handshake interceptors.
033 *
034 * @author Rossen Stoyanchev
035 * @since 4.0
036 */
037public class HandshakeInterceptorChain {
038
039        private static final Log logger = LogFactory.getLog(HandshakeInterceptorChain.class);
040
041        private final List<HandshakeInterceptor> interceptors;
042
043        private final WebSocketHandler wsHandler;
044
045        private int interceptorIndex = -1;
046
047
048        public HandshakeInterceptorChain(List<HandshakeInterceptor> interceptors, WebSocketHandler wsHandler) {
049                this.interceptors = (interceptors != null ? interceptors : Collections.<HandshakeInterceptor>emptyList());
050                this.wsHandler = wsHandler;
051        }
052
053
054        public boolean applyBeforeHandshake(ServerHttpRequest request, ServerHttpResponse response,
055                        Map<String, Object> attributes) throws Exception {
056
057                for (int i = 0; i < this.interceptors.size(); i++) {
058                        HandshakeInterceptor interceptor = this.interceptors.get(i);
059                        if (!interceptor.beforeHandshake(request, response, this.wsHandler, attributes)) {
060                                if (logger.isDebugEnabled()) {
061                                        logger.debug(interceptor + " returns false from beforeHandshake - precluding handshake");
062                                }
063                                applyAfterHandshake(request, response, null);
064                                return false;
065                        }
066                        this.interceptorIndex = i;
067                }
068                return true;
069        }
070
071
072        public void applyAfterHandshake(ServerHttpRequest request, ServerHttpResponse response, Exception failure) {
073                for (int i = this.interceptorIndex; i >= 0; i--) {
074                        HandshakeInterceptor interceptor = this.interceptors.get(i);
075                        try {
076                                interceptor.afterHandshake(request, response, this.wsHandler, failure);
077                        }
078                        catch (Throwable ex) {
079                                if (logger.isWarnEnabled()) {
080                                        logger.warn(interceptor + " threw exception in afterHandshake: " + ex);
081                                }
082                        }
083                }
084        }
085
086}