1 /*
2 * Copyright 2013 The Netty Project
3 *
4 * The Netty Project licenses this file to you under the Apache License,
5 * version 2.0 (the "License"); you may not use this file except in compliance
6 * with the License. You may obtain a copy of the License at:
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations
14 * under the License.
15 */
16 package io.netty5.channel;
17
18 import io.netty5.util.Resource;
19 import io.netty5.util.internal.TypeParameterMatcher;
20
21 /**
22 * {@link ChannelHandler} which allows to explicit only handle a specific type of messages.
23 *
24 * For example here is an implementation which only handle {@link String} messages.
25 *
26 * <pre>
27 * public class StringHandler extends
28 * {@link SimpleChannelInboundHandler}<{@link String}> {
29 *
30 * {@code @Override}
31 * protected void messageReceived({@link ChannelHandlerContext} ctx, {@link String} message)
32 * throws {@link Exception} {
33 * System.out.println(message);
34 * }
35 * }
36 * </pre>
37 *
38 * Be aware that depending of the constructor parameters it will release all handled messages by passing them to
39 * {@link Resource#dispose(Object)}.
40 */
41 public abstract class SimpleChannelInboundHandler<I> implements ChannelHandler {
42
43 private final TypeParameterMatcher matcher;
44 private final boolean autoRelease;
45
46 /**
47 * see {@link #SimpleChannelInboundHandler(boolean)} with {@code true} as boolean parameter.
48 */
49 protected SimpleChannelInboundHandler() {
50 this(true);
51 }
52
53 /**
54 * Create a new instance which will try to detect the types to match out of the type parameter of the class.
55 *
56 * @param autoRelease {@code true} if handled messages should be released automatically by passing them to
57 * {@link Resource#dispose(Object)}.
58 */
59 protected SimpleChannelInboundHandler(boolean autoRelease) {
60 matcher = TypeParameterMatcher.find(this, SimpleChannelInboundHandler.class, "I");
61 this.autoRelease = autoRelease;
62 }
63
64 /**
65 * see {@link #SimpleChannelInboundHandler(Class, boolean)} with {@code true} as boolean value.
66 */
67 protected SimpleChannelInboundHandler(Class<? extends I> inboundMessageType) {
68 this(inboundMessageType, true);
69 }
70
71 /**
72 * Create a new instance
73 *
74 * @param inboundMessageType The type of messages to match
75 * @param autoRelease {@code true} if handled messages should be released automatically by passing them to
76 * {@link Resource#dispose(Object)}.
77 */
78 protected SimpleChannelInboundHandler(Class<? extends I> inboundMessageType, boolean autoRelease) {
79 matcher = TypeParameterMatcher.get(inboundMessageType);
80 this.autoRelease = autoRelease;
81 }
82
83 /**
84 * Returns {@code true} if the given message should be handled. If {@code false} it will be passed to the next
85 * {@link ChannelHandler} in the {@link ChannelPipeline}.
86 */
87 public boolean acceptInboundMessage(Object msg) throws Exception {
88 return matcher.match(msg);
89 }
90
91 @Override
92 public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
93 boolean release = true;
94 try {
95 if (acceptInboundMessage(msg)) {
96 @SuppressWarnings("unchecked")
97 I imsg = (I) msg;
98 messageReceived(ctx, imsg);
99 } else {
100 release = false;
101 ctx.fireChannelRead(msg);
102 }
103 } catch (Throwable throwable) {
104 if (autoRelease && release) {
105 try {
106 Resource.dispose(msg);
107 } catch (Exception e) {
108 throwable.addSuppressed(e);
109 }
110 }
111 throw throwable;
112 }
113 if (autoRelease && release) {
114 Resource.dispose(msg);
115 }
116 }
117
118 /**
119 * Is called for each message of type {@link I}.
120 *
121 * @param ctx the {@link ChannelHandlerContext} which this {@link SimpleChannelInboundHandler}
122 * belongs to
123 * @param msg the message to handle
124 * @throws Exception is thrown if an error occurred
125 */
126 protected abstract void messageReceived(ChannelHandlerContext ctx, I msg) throws Exception;
127 }