Skip to content

Commit

Permalink
Merge pull request #1743 from rmsamitha/master
Browse files Browse the repository at this point in the history
Websocket access logging feature
  • Loading branch information
rmsamitha authored Jan 9, 2025
2 parents f4e304c + 6fae13f commit d498481
Show file tree
Hide file tree
Showing 3 changed files with 386 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketFrameAggregator;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.ssl.SslHandler;
import io.netty.handler.timeout.IdleStateHandler;
import org.apache.synapse.config.SynapsePropertiesLoader;
Expand Down Expand Up @@ -109,6 +110,7 @@ protected void initChannel(SocketChannel websocketChannel) throws Exception {
Constants.WEBSOCKET_TRANSPORT_MAX_HTTP_CODEC_CHUNK_SIZE, "8192"));
int maxContentLength = Integer.parseInt(SynapsePropertiesLoader.getPropertyValue(
Constants.WEBSOCKET_TRANSPORT_MAX_HTTP_AGGREGATOR_CONTENT_LENGTH, "65536"));
p.addLast(new WebSocketAccessLoggingHandler(LogLevel.DEBUG));
p.addLast("codec", new HttpServerCodec(maxInitLength, maxHeaderSize, maxChunkSize));
p.addLast("aggregator", new HttpObjectAggregator(maxContentLength));
p.addLast("frameAggregator", new WebSocketFrameAggregator(Integer.MAX_VALUE));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* Copyright (c) 2025, WSO2 LLC. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.wso2.carbon.inbound.endpoint.protocol.websocket;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPromise;
import io.netty.handler.logging.ByteBufFormat;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

import java.net.SocketAddress;

/**
* This class is used for managing logging the access logs of the WebSocket APIs.
*/
public class WebSocketAccessLoggingHandler extends LoggingHandler {

WebsocketAccessLogger websocketAccessLogger;

public WebSocketAccessLoggingHandler(LogLevel level) {
super(level, ByteBufFormat.HEX_DUMP);
// Initialize log properties
websocketAccessLogger = new WebsocketAccessLogger(this.logger);
}

@Override
public void channelRegistered(ChannelHandlerContext ctx) {
if (this.logger.isEnabled(this.internalLevel)) {
websocketAccessLogger.logChannelRegistered(ctx);
}
ctx.fireChannelRegistered();
}

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
if (logger.isEnabled(internalLevel)) {
websocketAccessLogger.logChannelRead(ctx, msg);
}
ctx.fireChannelRead(msg);
}

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
if (logger.isEnabled(internalLevel)) {
websocketAccessLogger.logChannelWrite(ctx, msg);
}
ctx.write(msg, promise);
}

@Override
public void channelUnregistered(ChannelHandlerContext ctx) {
if (logger.isEnabled(internalLevel)) {
websocketAccessLogger.logChannelUnregistered(ctx);
}
ctx.fireChannelUnregistered();
}

@Override
public void channelActive(ChannelHandlerContext ctx) {
ctx.fireChannelActive();
}

@Override
public void channelInactive(ChannelHandlerContext ctx) {
ctx.fireChannelInactive();
}

@Override
public void channelReadComplete(ChannelHandlerContext ctx) {
ctx.fireChannelReadComplete();
}

@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
ctx.fireUserEventTriggered(evt);
}

@Override
public void channelWritabilityChanged(ChannelHandlerContext ctx) {
ctx.fireChannelWritabilityChanged();
}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
ctx.fireExceptionCaught(cause);
}

@Override
public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
ctx.close(promise);
}

@Override
public void flush(ChannelHandlerContext ctx) throws Exception {
ctx.flush();
}

@Override
public void bind(ChannelHandlerContext ctx, SocketAddress localAddress, ChannelPromise promise) throws Exception {
ctx.bind(localAddress, promise);
}

@Override
public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress,
ChannelPromise promise) throws Exception {
ctx.connect(remoteAddress, localAddress, promise);
}

@Override
public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) {
ctx.disconnect(promise);
}

@Override
public void deregister(ChannelHandlerContext ctx, ChannelPromise promise) {
ctx.deregister(promise);
}
}
Loading

0 comments on commit d498481

Please sign in to comment.