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.web.socket.BinaryMessage; 020import org.springframework.web.socket.CloseStatus; 021import org.springframework.web.socket.PongMessage; 022import org.springframework.web.socket.TextMessage; 023import org.springframework.web.socket.WebSocketHandler; 024import org.springframework.web.socket.WebSocketMessage; 025import org.springframework.web.socket.WebSocketSession; 026 027/** 028 * A convenient base class for {@link WebSocketHandler} implementation with empty methods. 029 * 030 * @author Rossen Stoyanchev 031 * @author Phillip Webb 032 * @since 4.0 033 */ 034public abstract class AbstractWebSocketHandler implements WebSocketHandler { 035 036 @Override 037 public void afterConnectionEstablished(WebSocketSession session) throws Exception { 038 } 039 040 @Override 041 public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception { 042 if (message instanceof TextMessage) { 043 handleTextMessage(session, (TextMessage) message); 044 } 045 else if (message instanceof BinaryMessage) { 046 handleBinaryMessage(session, (BinaryMessage) message); 047 } 048 else if (message instanceof PongMessage) { 049 handlePongMessage(session, (PongMessage) message); 050 } 051 else { 052 throw new IllegalStateException("Unexpected WebSocket message type: " + message); 053 } 054 } 055 056 protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception { 057 } 058 059 protected void handleBinaryMessage(WebSocketSession session, BinaryMessage message) throws Exception { 060 } 061 062 protected void handlePongMessage(WebSocketSession session, PongMessage message) throws Exception { 063 } 064 065 @Override 066 public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception { 067 } 068 069 @Override 070 public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception { 071 } 072 073 @Override 074 public boolean supportsPartialMessages() { 075 return false; 076 } 077 078}