diff --git a/netty-websocket-http2/src/main/java/com/jauntsdn/netty/handler/codec/http2/websocketx/Http2WebSocketClientBuilder.java b/netty-websocket-http2/src/main/java/com/jauntsdn/netty/handler/codec/http2/websocketx/Http2WebSocketClientBuilder.java index 8127f88..54a8c1c 100644 --- a/netty-websocket-http2/src/main/java/com/jauntsdn/netty/handler/codec/http2/websocketx/Http2WebSocketClientBuilder.java +++ b/netty-websocket-http2/src/main/java/com/jauntsdn/netty/handler/codec/http2/websocketx/Http2WebSocketClientBuilder.java @@ -18,6 +18,7 @@ import io.netty.handler.codec.http.websocketx.WebSocketDecoderConfig; import io.netty.handler.codec.http.websocketx.extensions.compression.PerMessageDeflateClientExtensionHandshaker; +import java.util.Objects; /** Builder for {@link Http2WebSocketClientHandler} */ public final class Http2WebSocketClientBuilder { @@ -45,7 +46,7 @@ public static Http2WebSocketClientBuilder create() { * @return this {@link Http2WebSocketClientBuilder} instance */ public Http2WebSocketClientBuilder codec(Http1WebSocketCodec webSocketCodec) { - this.webSocketCodec = Preconditions.requireNonNull(webSocketCodec, "webSocketCodec"); + this.webSocketCodec = Objects.requireNonNull(webSocketCodec, "webSocketCodec"); return this; } @@ -55,7 +56,7 @@ public Http2WebSocketClientBuilder codec(Http1WebSocketCodec webSocketCodec) { */ public Http2WebSocketClientBuilder decoderConfig(WebSocketDecoderConfig webSocketDecoderConfig) { this.webSocketDecoderConfig = - Preconditions.requireNonNull(webSocketDecoderConfig, "webSocketDecoderConfig"); + Objects.requireNonNull(webSocketDecoderConfig, "webSocketDecoderConfig"); return this; } @@ -74,7 +75,7 @@ public Http2WebSocketClientBuilder maskPayload(boolean maskPayload) { */ public Http2WebSocketClientBuilder handshakeTimeoutMillis(long handshakeTimeoutMillis) { this.handshakeTimeoutMillis = - Preconditions.requirePositive(handshakeTimeoutMillis, "handshakeTimeoutMillis"); + Http2WebSocketProtocol.requirePositive(handshakeTimeoutMillis, "handshakeTimeoutMillis"); return this; } @@ -87,7 +88,7 @@ public Http2WebSocketClientBuilder handshakeTimeoutMillis(long handshakeTimeoutM public Http2WebSocketClientBuilder closedWebSocketRemoveTimeoutMillis( long closedWebSocketRemoveTimeoutMillis) { this.closedWebSocketRemoveTimeoutMillis = - Preconditions.requirePositive( + Http2WebSocketProtocol.requirePositive( closedWebSocketRemoveTimeoutMillis, "closedWebSocketRemoveTimeoutMillis"); return this; } @@ -144,7 +145,7 @@ public Http2WebSocketClientBuilder compression( * @return this {@link Http2WebSocketClientBuilder} instance */ public Http2WebSocketClientBuilder streamWeight(int weight) { - this.streamWeight = Preconditions.requireRange(weight, 1, 256, "streamWeight"); + this.streamWeight = Http2WebSocketProtocol.requireRange(weight, 1, 256, "streamWeight"); return this; } diff --git a/netty-websocket-http2/src/main/java/com/jauntsdn/netty/handler/codec/http2/websocketx/Http2WebSocketClientHandshaker.java b/netty-websocket-http2/src/main/java/com/jauntsdn/netty/handler/codec/http2/websocketx/Http2WebSocketClientHandshaker.java index 8f8859b..e9aeca2 100644 --- a/netty-websocket-http2/src/main/java/com/jauntsdn/netty/handler/codec/http2/websocketx/Http2WebSocketClientHandshaker.java +++ b/netty-websocket-http2/src/main/java/com/jauntsdn/netty/handler/codec/http2/websocketx/Http2WebSocketClientHandshaker.java @@ -101,7 +101,8 @@ public final class Http2WebSocketClientHandshaker { */ public static Http2WebSocketClientHandshaker create(Channel channel) { Objects.requireNonNull(channel, "channel"); - return Preconditions.requireHandler(channel, Http2WebSocketClientHandler.class).handShaker(); + return Http2WebSocketHandler.requireChannelHandler(channel, Http2WebSocketClientHandler.class) + .handShaker(); } /** @@ -162,10 +163,10 @@ public ChannelFuture handshake( String subprotocol, Http2Headers requestHeaders, ChannelHandler webSocketHandler) { - Preconditions.requireNonEmpty(path, "path"); - Preconditions.requireNonNull(subprotocol, "subprotocol"); - Preconditions.requireNonNull(requestHeaders, "requestHeaders"); - Preconditions.requireNonNull(webSocketHandler, "webSocketHandler"); + requireNonEmpty(path, "path"); + Objects.requireNonNull(subprotocol, "subprotocol"); + Objects.requireNonNull(requestHeaders, "requestHeaders"); + Objects.requireNonNull(webSocketHandler, "webSocketHandler"); long startNanos = System.nanoTime(); ChannelHandlerContext ctx = webSocketsParent.context(); @@ -469,6 +470,13 @@ private static boolean isEqual(String str, @Nullable CharSequence seq) { return str.contentEquals(seq); } + private static String requireNonEmpty(String string, String message) { + if (string == null || string.isEmpty()) { + throw new IllegalArgumentException(message + " must be non empty"); + } + return string; + } + static class Handshake extends Http2WebSocketServerHandshaker.Handshake { private final Http2WebSocketChannel webSocketChannel; private final Http2Headers requestHeaders; diff --git a/netty-websocket-http2/src/main/java/com/jauntsdn/netty/handler/codec/http2/websocketx/Http2WebSocketEvent.java b/netty-websocket-http2/src/main/java/com/jauntsdn/netty/handler/codec/http2/websocketx/Http2WebSocketEvent.java index 9d657f4..65ea5f0 100644 --- a/netty-websocket-http2/src/main/java/com/jauntsdn/netty/handler/codec/http2/websocketx/Http2WebSocketEvent.java +++ b/netty-websocket-http2/src/main/java/com/jauntsdn/netty/handler/codec/http2/websocketx/Http2WebSocketEvent.java @@ -449,7 +449,7 @@ public static final class Http2WebSocketStreamWeightUpdateEvent extends Http2Web Http2WebSocketStreamWeightUpdateEvent(short streamWeight) { super(Type.WEIGHT_UPDATE); - this.streamWeight = Preconditions.requireRange(streamWeight, 1, 256, "streamWeight"); + this.streamWeight = Http2WebSocketProtocol.requireRange(streamWeight, 1, 256, "streamWeight"); } public short streamWeight() { diff --git a/netty-websocket-http2/src/main/java/com/jauntsdn/netty/handler/codec/http2/websocketx/Http2WebSocketHandler.java b/netty-websocket-http2/src/main/java/com/jauntsdn/netty/handler/codec/http2/websocketx/Http2WebSocketHandler.java index b149ece..d8bc13b 100644 --- a/netty-websocket-http2/src/main/java/com/jauntsdn/netty/handler/codec/http2/websocketx/Http2WebSocketHandler.java +++ b/netty-websocket-http2/src/main/java/com/jauntsdn/netty/handler/codec/http2/websocketx/Http2WebSocketHandler.java @@ -17,7 +17,9 @@ package com.jauntsdn.netty.handler.codec.http2.websocketx; import io.netty.buffer.ByteBuf; +import io.netty.channel.Channel; import io.netty.channel.ChannelDuplexHandler; +import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.http2.Http2ConnectionDecoder; import io.netty.handler.codec.http2.Http2ConnectionHandler; @@ -44,8 +46,7 @@ public abstract class Http2WebSocketHandler extends ChannelDuplexHandler @Override public void handlerAdded(ChannelHandlerContext ctx) throws Exception { Http2ConnectionHandler http2Handler = - this.http2Handler = - Preconditions.requireHandler(ctx.channel(), Http2ConnectionHandler.class); + this.http2Handler = requireChannelHandler(ctx.channel(), Http2ConnectionHandler.class); HandlerListener listener = handlerListener; if (listener == null) { Http2ConnectionDecoder decoder = http2Handler.decoder(); @@ -183,6 +184,15 @@ static AsciiString endOfStreamValue(boolean endOfStream) { : HEADER_WEBSOCKET_ENDOFSTREAM_VALUE_FALSE; } + static T requireChannelHandler(Channel channel, Class handler) { + T h = channel.pipeline().get(handler); + if (h == null) { + throw new IllegalArgumentException( + handler.getSimpleName() + " is absent in the channel pipeline"); + } + return h; + } + static final class HandlerListener implements Http2FrameListener { Http2FrameListener cur; Http2FrameListener next; diff --git a/netty-websocket-http2/src/main/java/com/jauntsdn/netty/handler/codec/http2/websocketx/Http2WebSocketProtocol.java b/netty-websocket-http2/src/main/java/com/jauntsdn/netty/handler/codec/http2/websocketx/Http2WebSocketProtocol.java index 54a5140..39c8cd1 100644 --- a/netty-websocket-http2/src/main/java/com/jauntsdn/netty/handler/codec/http2/websocketx/Http2WebSocketProtocol.java +++ b/netty-websocket-http2/src/main/java/com/jauntsdn/netty/handler/codec/http2/websocketx/Http2WebSocketProtocol.java @@ -313,4 +313,21 @@ public static boolean isValid(final Http2Headers requestHeaders, boolean endOfSt } } } + + /*preconditions*/ + + static long requirePositive(long value, String message) { + if (value <= 0) { + throw new IllegalArgumentException(message + " must be positive: " + value); + } + return value; + } + + static short requireRange(int value, int from, int to, String message) { + if (value >= from && value <= to) { + return (short) value; + } + throw new IllegalArgumentException( + String.format("%s must belong to range [%d, %d]: ", message, from, to)); + } } diff --git a/netty-websocket-http2/src/main/java/com/jauntsdn/netty/handler/codec/http2/websocketx/Http2WebSocketServerBuilder.java b/netty-websocket-http2/src/main/java/com/jauntsdn/netty/handler/codec/http2/websocketx/Http2WebSocketServerBuilder.java index 0e260f9..fa280cb 100644 --- a/netty-websocket-http2/src/main/java/com/jauntsdn/netty/handler/codec/http2/websocketx/Http2WebSocketServerBuilder.java +++ b/netty-websocket-http2/src/main/java/com/jauntsdn/netty/handler/codec/http2/websocketx/Http2WebSocketServerBuilder.java @@ -97,7 +97,7 @@ public static Http2ConnectionHandlerBuilder configureHttp2Server( * @return this {@link Http2WebSocketClientBuilder} instance */ public Http2WebSocketServerBuilder codec(Http1WebSocketCodec webSocketCodec) { - this.webSocketCodec = Preconditions.requireNonNull(webSocketCodec, "webSocketCodec"); + this.webSocketCodec = Objects.requireNonNull(webSocketCodec, "webSocketCodec"); return this; } @@ -107,7 +107,7 @@ public Http2WebSocketServerBuilder codec(Http1WebSocketCodec webSocketCodec) { */ public Http2WebSocketServerBuilder decoderConfig(WebSocketDecoderConfig webSocketDecoderConfig) { this.webSocketDecoderConfig = - Preconditions.requireNonNull(webSocketDecoderConfig, "webSocketDecoderConfig"); + Objects.requireNonNull(webSocketDecoderConfig, "webSocketDecoderConfig"); return this; } @@ -120,7 +120,7 @@ public Http2WebSocketServerBuilder decoderConfig(WebSocketDecoderConfig webSocke public Http2WebSocketServerBuilder closedWebSocketRemoveTimeout( long closedWebSocketRemoveTimeoutMillis) { this.closedWebSocketRemoveTimeoutMillis = - Preconditions.requirePositive( + Http2WebSocketProtocol.requirePositive( closedWebSocketRemoveTimeoutMillis, "closedWebSocketRemoveTimeoutMillis"); return this; } diff --git a/netty-websocket-http2/src/main/java/com/jauntsdn/netty/handler/codec/http2/websocketx/Preconditions.java b/netty-websocket-http2/src/main/java/com/jauntsdn/netty/handler/codec/http2/websocketx/Preconditions.java deleted file mode 100644 index 6c61b5c..0000000 --- a/netty-websocket-http2/src/main/java/com/jauntsdn/netty/handler/codec/http2/websocketx/Preconditions.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright 2020 - present Maksym Ostroverkhov. - * - * Licensed 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 com.jauntsdn.netty.handler.codec.http2.websocketx; - -import io.netty.channel.Channel; -import io.netty.channel.ChannelHandler; - -final class Preconditions { - static T requireNonNull(T t, String message) { - if (t == null) { - throw new IllegalArgumentException(message + " must be non null"); - } - return t; - } - - static String requireNonEmpty(String string, String message) { - if (string == null || string.isEmpty()) { - throw new IllegalArgumentException(message + " must be non empty"); - } - return string; - } - - static T requireHandler(Channel channel, Class handler) { - T h = channel.pipeline().get(handler); - if (h == null) { - throw new IllegalArgumentException( - handler.getSimpleName() + " is absent in the channel pipeline"); - } - return h; - } - - static long requirePositive(long value, String message) { - if (value <= 0) { - throw new IllegalArgumentException(message + " must be positive: " + value); - } - return value; - } - - static short requireRange(int value, int from, int to, String message) { - if (value >= from && value <= to) { - return (short) value; - } - throw new IllegalArgumentException( - String.format("%s must belong to range [%d, %d]: ", message, from, to)); - } -}