001/* 002 * Copyright 2002-2013 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.handler; 018 019import org.springframework.util.Assert; 020import org.springframework.web.socket.CloseStatus; 021import org.springframework.web.socket.WebSocketHandler; 022import org.springframework.web.socket.WebSocketMessage; 023import org.springframework.web.socket.WebSocketSession; 024 025/** 026 * Wraps another {@link org.springframework.web.socket.WebSocketHandler} 027 * instance and delegates to it. 028 * 029 * <p>Also provides a {@link #getDelegate()} method to return the decorated 030 * handler as well as a {@link #getLastHandler()} method to go through all nested 031 * delegates and return the "last" handler. 032 * 033 * @author Rossen Stoyanchev 034 * @since 4.0 035 */ 036public class WebSocketHandlerDecorator implements WebSocketHandler { 037 038 private final WebSocketHandler delegate; 039 040 041 public WebSocketHandlerDecorator(WebSocketHandler delegate) { 042 Assert.notNull(delegate, "Delegate must not be null"); 043 this.delegate = delegate; 044 } 045 046 047 public WebSocketHandler getDelegate() { 048 return this.delegate; 049 } 050 051 public WebSocketHandler getLastHandler() { 052 WebSocketHandler result = this.delegate; 053 while (result instanceof WebSocketHandlerDecorator) { 054 result = ((WebSocketHandlerDecorator) result).getDelegate(); 055 } 056 return result; 057 } 058 059 public static WebSocketHandler unwrap(WebSocketHandler handler) { 060 if (handler instanceof WebSocketHandlerDecorator) { 061 return ((WebSocketHandlerDecorator) handler).getLastHandler(); 062 } 063 else { 064 return handler; 065 } 066 } 067 068 @Override 069 public void afterConnectionEstablished(WebSocketSession session) throws Exception { 070 this.delegate.afterConnectionEstablished(session); 071 } 072 073 @Override 074 public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception { 075 this.delegate.handleMessage(session, message); 076 } 077 078 @Override 079 public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception { 080 this.delegate.handleTransportError(session, exception); 081 } 082 083 @Override 084 public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception { 085 this.delegate.afterConnectionClosed(session, closeStatus); 086 } 087 088 @Override 089 public boolean supportsPartialMessages() { 090 return this.delegate.supportsPartialMessages(); 091 } 092 093 @Override 094 public String toString() { 095 return getClass().getSimpleName() + " [delegate=" + this.delegate + "]"; 096 } 097 098}