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.handler;
018
019import org.apache.commons.logging.Log;
020import org.apache.commons.logging.LogFactory;
021
022import org.springframework.web.socket.CloseStatus;
023import org.springframework.web.socket.WebSocketHandler;
024import org.springframework.web.socket.WebSocketMessage;
025import org.springframework.web.socket.WebSocketSession;
026
027/**
028 * A {@link WebSocketHandlerDecorator} that adds logging to WebSocket lifecycle events.
029 *
030 * @author Rossen Stoyanchev
031 * @since 4.0
032 */
033public class LoggingWebSocketHandlerDecorator extends WebSocketHandlerDecorator {
034
035        private static final Log logger = LogFactory.getLog(LoggingWebSocketHandlerDecorator.class);
036
037
038        public LoggingWebSocketHandlerDecorator(WebSocketHandler delegate) {
039                super(delegate);
040        }
041
042
043        @Override
044        public void afterConnectionEstablished(WebSocketSession session) throws Exception {
045                if (logger.isDebugEnabled()) {
046                        logger.debug("New "     + session);
047                }
048                super.afterConnectionEstablished(session);
049        }
050
051        @Override
052        public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {
053                if (logger.isTraceEnabled()) {
054                        logger.trace("Handling " + message + " in " + session);
055                }
056                super.handleMessage(session, message);
057        }
058
059        @Override
060        public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
061                if (logger.isDebugEnabled()) {
062                        logger.debug("Transport error in " + session, exception);
063                }
064                super.handleTransportError(session, exception);
065        }
066
067        @Override
068        public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception {
069                if (logger.isDebugEnabled()) {
070                        logger.debug(session + " closed with " + closeStatus);
071                }
072                super.afterConnectionClosed(session, closeStatus);
073        }
074
075}