001/*
002 * Copyright 2002-2017 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.lang.Nullable;
029import org.springframework.web.socket.WebSocketHandler;
030import org.springframework.web.socket.server.HandshakeInterceptor;
031
032/**
033 * A helper class that assists with invoking a list of handshake interceptors.
034 *
035 * @author Rossen Stoyanchev
036 * @since 4.0
037 */
038public class HandshakeInterceptorChain {
039
040        private static final Log logger = LogFactory.getLog(HandshakeInterceptorChain.class);
041
042        private final List<HandshakeInterceptor> interceptors;
043
044        private final WebSocketHandler wsHandler;
045
046        private int interceptorIndex = -1;
047
048
049        public HandshakeInterceptorChain(@Nullable List<HandshakeInterceptor> interceptors, WebSocketHandler wsHandler) {
050                this.interceptors = (interceptors != null ? interceptors : Collections.emptyList());
051                this.wsHandler = wsHandler;
052        }
053
054
055        public boolean applyBeforeHandshake(ServerHttpRequest request, ServerHttpResponse response,
056                        Map<String, Object> attributes) throws Exception {
057
058                for (int i = 0; i < this.interceptors.size(); i++) {
059                        HandshakeInterceptor interceptor = this.interceptors.get(i);
060                        if (!interceptor.beforeHandshake(request, response, this.wsHandler, attributes)) {
061                                if (logger.isDebugEnabled()) {
062                                        logger.debug(interceptor + " returns false from beforeHandshake - precluding handshake");
063                                }
064                                applyAfterHandshake(request, response, null);
065                                return false;
066                        }
067                        this.interceptorIndex = i;
068                }
069                return true;
070        }
071
072        public void applyAfterHandshake(
073                        ServerHttpRequest request, ServerHttpResponse response, @Nullable Exception failure) {
074
075                for (int i = this.interceptorIndex; i >= 0; i--) {
076                        HandshakeInterceptor interceptor = this.interceptors.get(i);
077                        try {
078                                interceptor.afterHandshake(request, response, this.wsHandler, failure);
079                        }
080                        catch (Exception ex) {
081                                if (logger.isWarnEnabled()) {
082                                        logger.warn(interceptor + " threw exception in afterHandshake: " + ex);
083                                }
084                        }
085                }
086        }
087
088}