Skip to content

Commit fbf25c5

Browse files
committed
ChannelInterceptor default methods + deprecate adapter
1 parent eed663f commit fbf25c5

File tree

9 files changed

+42
-41
lines changed

9 files changed

+42
-41
lines changed

Diff for: spring-messaging/src/main/java/org/springframework/messaging/simp/broker/AbstractBrokerMessageHandler.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
3535
import org.springframework.messaging.simp.SimpMessageType;
3636
import org.springframework.messaging.support.ChannelInterceptor;
37-
import org.springframework.messaging.support.ChannelInterceptorAdapter;
3837
import org.springframework.messaging.support.InterceptableChannel;
3938
import org.springframework.util.Assert;
4039
import org.springframework.util.CollectionUtils;
@@ -274,7 +273,7 @@ protected void publishBrokerUnavailableEvent() {
274273
/**
275274
* Detect unsent DISCONNECT messages and process them anyway.
276275
*/
277-
private class UnsentDisconnectChannelInterceptor extends ChannelInterceptorAdapter {
276+
private class UnsentDisconnectChannelInterceptor implements ChannelInterceptor {
278277

279278
@Override
280279
public void afterSendCompletion(

Diff for: spring-messaging/src/main/java/org/springframework/messaging/support/ChannelInterceptor.java

+17-6
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,16 @@ public interface ChannelInterceptor {
3838
* send invocation will not occur.
3939
*/
4040
@Nullable
41-
Message<?> preSend(Message<?> message, MessageChannel channel);
41+
default Message<?> preSend(Message<?> message, MessageChannel channel) {
42+
return message;
43+
}
4244

4345
/**
4446
* Invoked immediately after the send invocation. The boolean
4547
* value argument represents the return value of that invocation.
4648
*/
47-
void postSend(Message<?> message, MessageChannel channel, boolean sent);
49+
default void postSend(Message<?> message, MessageChannel channel, boolean sent) {
50+
}
4851

4952
/**
5053
* Invoked after the completion of a send regardless of any exception that
@@ -53,14 +56,18 @@ public interface ChannelInterceptor {
5356
* completed and returned a Message, i.e. it did not return {@code null}.
5457
* @since 4.1
5558
*/
56-
void afterSendCompletion(Message<?> message, MessageChannel channel, boolean sent, @Nullable Exception ex);
59+
default void afterSendCompletion(Message<?> message, MessageChannel channel, boolean sent,
60+
@Nullable Exception ex) {
61+
}
5762

5863
/**
5964
* Invoked as soon as receive is called and before a Message is
6065
* actually retrieved. If the return value is 'false', then no
6166
* Message will be retrieved. This only applies to PollableChannels.
6267
*/
63-
boolean preReceive(MessageChannel channel);
68+
default boolean preReceive(MessageChannel channel) {
69+
return true;
70+
}
6471

6572
/**
6673
* Invoked immediately after a Message has been retrieved but before
@@ -69,7 +76,9 @@ public interface ChannelInterceptor {
6976
* This only applies to PollableChannels.
7077
*/
7178
@Nullable
72-
Message<?> postReceive(Message<?> message, MessageChannel channel);
79+
default Message<?> postReceive(Message<?> message, MessageChannel channel) {
80+
return message;
81+
}
7382

7483
/**
7584
* Invoked after the completion of a receive regardless of any exception that
@@ -78,6 +87,8 @@ public interface ChannelInterceptor {
7887
* completed and returned {@code true}.
7988
* @since 4.1
8089
*/
81-
void afterReceiveCompletion(@Nullable Message<?> message, MessageChannel channel, @Nullable Exception ex);
90+
default void afterReceiveCompletion(@Nullable Message<?> message, MessageChannel channel,
91+
@Nullable Exception ex) {
92+
}
8293

8394
}

Diff for: spring-messaging/src/main/java/org/springframework/messaging/support/ChannelInterceptorAdapter.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,7 +27,11 @@
2727
* @author Mark Fisher
2828
* @author Rossen Stoyanchev
2929
* @since 4.0
30+
* @deprecated as of 5.0.7 {@link ChannelInterceptor} has default methods (made
31+
* possible by a Java 8 baseline) and can be implemented directly without the
32+
* need for this no-op adapter
3033
*/
34+
@Deprecated
3135
public abstract class ChannelInterceptorAdapter implements ChannelInterceptor {
3236

3337
@Override

Diff for: spring-messaging/src/main/java/org/springframework/messaging/support/ImmutableMessageChannelInterceptor.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,7 +30,7 @@
3030
* @author Rossen Stoyanchev
3131
* @since 4.1.2
3232
*/
33-
public class ImmutableMessageChannelInterceptor extends ChannelInterceptorAdapter {
33+
public class ImmutableMessageChannelInterceptor implements ChannelInterceptor {
3434

3535
@Override
3636
public Message<?> preSend(Message<?> message, MessageChannel channel) {

Diff for: spring-messaging/src/test/java/org/springframework/messaging/simp/config/MessageBrokerConfigurationTests.java

+1-4
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@
1717
package org.springframework.messaging.simp.config;
1818

1919
import java.util.ArrayList;
20-
import java.util.Collections;
2120
import java.util.Iterator;
2221
import java.util.List;
23-
import java.util.Map;
2422
import java.util.Set;
2523
import java.util.concurrent.ConcurrentHashMap;
2624

@@ -64,7 +62,6 @@
6462
import org.springframework.messaging.simp.user.UserRegistryMessageHandler;
6563
import org.springframework.messaging.support.AbstractSubscribableChannel;
6664
import org.springframework.messaging.support.ChannelInterceptor;
67-
import org.springframework.messaging.support.ChannelInterceptorAdapter;
6865
import org.springframework.messaging.support.ExecutorSubscribableChannel;
6966
import org.springframework.messaging.support.MessageBuilder;
7067
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
@@ -607,7 +604,7 @@ static class DefaultConfig extends BaseTestMessageBrokerConfig {
607604
@Configuration
608605
static class CustomConfig extends BaseTestMessageBrokerConfig {
609606

610-
private ChannelInterceptor interceptor = new ChannelInterceptorAdapter() {};
607+
private ChannelInterceptor interceptor = new ChannelInterceptor() {};
611608

612609
@Override
613610
protected void configureClientInboundChannel(ChannelRegistration registration) {

Diff for: spring-messaging/src/test/java/org/springframework/messaging/support/ChannelInterceptorTests.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public void preSendInterceptorReturningNull() {
8888
public void postSendInterceptorMessageWasSent() {
8989
final AtomicBoolean preSendInvoked = new AtomicBoolean(false);
9090
final AtomicBoolean completionInvoked = new AtomicBoolean(false);
91-
this.channel.addInterceptor(new ChannelInterceptorAdapter() {
91+
this.channel.addInterceptor(new ChannelInterceptor() {
9292
@Override
9393
public void postSend(Message<?> message, MessageChannel channel, boolean sent) {
9494
assertInput(message, channel, sent);
@@ -121,7 +121,7 @@ protected boolean sendInternal(Message<?> message, long timeout) {
121121
};
122122
final AtomicBoolean preSendInvoked = new AtomicBoolean(false);
123123
final AtomicBoolean completionInvoked = new AtomicBoolean(false);
124-
testChannel.addInterceptor(new ChannelInterceptorAdapter() {
124+
testChannel.addInterceptor(new ChannelInterceptor() {
125125
@Override
126126
public void postSend(Message<?> message, MessageChannel channel, boolean sent) {
127127
assertInput(message, channel, sent);
@@ -199,7 +199,7 @@ public void handleMessage(Message<?> message) throws MessagingException {
199199
}
200200

201201

202-
private abstract static class AbstractTestInterceptor extends ChannelInterceptorAdapter {
202+
private abstract static class AbstractTestInterceptor implements ChannelInterceptor {
203203

204204
private AtomicInteger counter = new AtomicInteger();
205205

Diff for: spring-messaging/src/test/java/org/springframework/messaging/support/ExecutorSubscribableChannelTests.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,7 @@ public void interceptorWithException() {
186186
}
187187

188188

189-
private abstract static class AbstractTestInterceptor extends ChannelInterceptorAdapter
190-
implements ExecutorChannelInterceptor {
189+
private abstract static class AbstractTestInterceptor implements ChannelInterceptor, ExecutorChannelInterceptor {
191190

192191
private AtomicInteger counter = new AtomicInteger();
193192

@@ -209,7 +208,9 @@ public Message<?> beforeHandle(Message<?> message, MessageChannel channel, Messa
209208
}
210209

211210
@Override
212-
public void afterMessageHandled(Message<?> message, MessageChannel channel, MessageHandler handler, Exception ex) {
211+
public void afterMessageHandled(Message<?> message, MessageChannel channel, MessageHandler handler,
212+
Exception ex) {
213+
213214
this.afterHandledInvoked = true;
214215
}
215216
}

Diff for: spring-websocket/src/test/java/org/springframework/web/socket/config/HandlersBeanDefinitionParserTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import org.springframework.core.io.ClassPathResource;
3030
import org.springframework.http.server.ServerHttpRequest;
3131
import org.springframework.http.server.ServerHttpResponse;
32-
import org.springframework.messaging.support.ChannelInterceptorAdapter;
32+
import org.springframework.messaging.support.ChannelInterceptor;
3333
import org.springframework.scheduling.TaskScheduler;
3434
import org.springframework.scheduling.Trigger;
3535
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
@@ -289,7 +289,7 @@ public boolean doHandshake(ServerHttpRequest request, ServerHttpResponse respons
289289
}
290290

291291

292-
class TestChannelInterceptor extends ChannelInterceptorAdapter {
292+
class TestChannelInterceptor implements ChannelInterceptor {
293293
}
294294

295295

Diff for: spring-websocket/src/test/java/org/springframework/web/socket/messaging/StompSubProtocolHandlerTests.java

+7-18
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
import org.springframework.messaging.simp.stomp.StompEncoder;
4747
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
4848
import org.springframework.messaging.simp.user.DestinationUserNameProvider;
49-
import org.springframework.messaging.support.ChannelInterceptorAdapter;
49+
import org.springframework.messaging.support.ChannelInterceptor;
5050
import org.springframework.messaging.support.ExecutorSubscribableChannel;
5151
import org.springframework.messaging.support.ImmutableMessageChannelInterceptor;
5252
import org.springframework.messaging.support.MessageBuilder;
@@ -59,21 +59,10 @@
5959
import org.springframework.web.socket.handler.TestWebSocketSession;
6060
import org.springframework.web.socket.sockjs.transport.SockJsSession;
6161

62-
import static org.hamcrest.Matchers.is;
63-
import static org.junit.Assert.assertArrayEquals;
64-
import static org.junit.Assert.assertEquals;
65-
import static org.junit.Assert.assertFalse;
66-
import static org.junit.Assert.assertNotNull;
67-
import static org.junit.Assert.assertThat;
68-
import static org.junit.Assert.assertTrue;
62+
import static org.hamcrest.Matchers.*;
63+
import static org.junit.Assert.*;
6964
import static org.mockito.Mockito.any;
70-
import static org.mockito.Mockito.mock;
71-
import static org.mockito.Mockito.reset;
72-
import static org.mockito.Mockito.times;
73-
import static org.mockito.Mockito.verify;
74-
import static org.mockito.Mockito.verifyNoMoreInteractions;
75-
import static org.mockito.Mockito.verifyZeroInteractions;
76-
import static org.mockito.Mockito.when;
65+
import static org.mockito.Mockito.*;
7766

7867
/**
7968
* Test fixture for {@link StompSubProtocolHandler} tests.
@@ -330,7 +319,7 @@ public void handleMessageFromClient() {
330319
public void handleMessageFromClientWithImmutableMessageInterceptor() {
331320
AtomicReference<Boolean> mutable = new AtomicReference<>();
332321
ExecutorSubscribableChannel channel = new ExecutorSubscribableChannel();
333-
channel.addInterceptor(new ChannelInterceptorAdapter() {
322+
channel.addInterceptor(new ChannelInterceptor() {
334323
@Override
335324
public Message<?> preSend(Message<?> message, MessageChannel channel) {
336325
mutable.set(MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.class).isMutable());
@@ -352,7 +341,7 @@ public Message<?> preSend(Message<?> message, MessageChannel channel) {
352341
public void handleMessageFromClientWithoutImmutableMessageInterceptor() {
353342
AtomicReference<Boolean> mutable = new AtomicReference<>();
354343
ExecutorSubscribableChannel channel = new ExecutorSubscribableChannel();
355-
channel.addInterceptor(new ChannelInterceptorAdapter() {
344+
channel.addInterceptor(new ChannelInterceptor() {
356345
@Override
357346
public Message<?> preSend(Message<?> message, MessageChannel channel) {
358347
mutable.set(MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.class).isMutable());
@@ -557,7 +546,7 @@ public void handleMessage(Message<?> message) throws MessagingException {
557546
}
558547
}
559548

560-
private static class AuthenticationInterceptor extends ChannelInterceptorAdapter {
549+
private static class AuthenticationInterceptor implements ChannelInterceptor {
561550

562551
private final String name;
563552

0 commit comments

Comments
 (0)