Add WebSocket scope
This change adds support for a custom "websocket" scope. WebSocket-scoped beans may be injected into controllers with message handling methods as well as channel interceptor registered on the "inboundClientChannel". Issue: SPR-11305
This commit is contained in:
@@ -0,0 +1,132 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* 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 org.springframework.messaging.simp;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.support.GenericMessage;
|
||||
import org.springframework.messaging.support.MessageBuilder;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.*;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.hamcrest.Matchers.sameInstance;
|
||||
import static org.hamcrest.Matchers.startsWith;
|
||||
|
||||
/**
|
||||
* Unit tests for
|
||||
* {@link org.springframework.messaging.simp.SimpAttributesContextHolder}.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 4.1
|
||||
*/
|
||||
public class SimpAttributesContextHolderTests {
|
||||
|
||||
private SimpAttributes simpAttributes;
|
||||
|
||||
@Rule
|
||||
public ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
Map<String, Object> map = new ConcurrentHashMap<>();
|
||||
this.simpAttributes = new SimpAttributes("session1", map);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
SimpAttributesContextHolder.resetAttributes();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void resetAttributes() {
|
||||
SimpAttributesContextHolder.setAttributes(this.simpAttributes);
|
||||
assertThat(SimpAttributesContextHolder.getAttributes(), sameInstance(this.simpAttributes));
|
||||
|
||||
SimpAttributesContextHolder.resetAttributes();
|
||||
assertThat(SimpAttributesContextHolder.getAttributes(), nullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAttributes() {
|
||||
assertThat(SimpAttributesContextHolder.getAttributes(), nullValue());
|
||||
|
||||
SimpAttributesContextHolder.setAttributes(this.simpAttributes);
|
||||
assertThat(SimpAttributesContextHolder.getAttributes(), sameInstance(this.simpAttributes));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setAttributes() {
|
||||
SimpAttributesContextHolder.setAttributes(this.simpAttributes);
|
||||
assertThat(SimpAttributesContextHolder.getAttributes(), sameInstance(this.simpAttributes));
|
||||
|
||||
SimpAttributesContextHolder.setAttributes(null);
|
||||
assertThat(SimpAttributesContextHolder.getAttributes(), nullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setAttributesFromMessage() {
|
||||
|
||||
String sessionId = "session1";
|
||||
ConcurrentHashMap<String, Object> map = new ConcurrentHashMap<>();
|
||||
|
||||
SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.create();
|
||||
headerAccessor.setSessionId(sessionId);
|
||||
headerAccessor.setSessionAttributes(map);
|
||||
Message<?> message = MessageBuilder.createMessage("", headerAccessor.getMessageHeaders());
|
||||
|
||||
SimpAttributesContextHolder.setAttributesFromMessage(message);
|
||||
|
||||
SimpAttributes attrs = SimpAttributesContextHolder.getAttributes();
|
||||
assertThat(attrs, notNullValue());
|
||||
assertThat(attrs.getSessionId(), is(sessionId));
|
||||
|
||||
attrs.setAttribute("name1", "value1");
|
||||
assertThat(map.get("name1"), is("value1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setAttributesFromMessageWithMissingHeaders() {
|
||||
this.thrown.expect(IllegalStateException.class);
|
||||
this.thrown.expectMessage(startsWith("Message does not contain SiMP session id or attributes"));
|
||||
SimpAttributesContextHolder.setAttributesFromMessage(new GenericMessage<Object>(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void currentAttributes() {
|
||||
SimpAttributesContextHolder.setAttributes(this.simpAttributes);
|
||||
assertThat(SimpAttributesContextHolder.currentAttributes(), sameInstance(this.simpAttributes));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void currentAttributesNone() {
|
||||
this.thrown.expect(IllegalStateException.class);
|
||||
this.thrown.expectMessage(startsWith("No thread-bound SimpAttributes found"));
|
||||
SimpAttributesContextHolder.currentAttributes();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* 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 org.springframework.messaging.simp;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
|
||||
/**
|
||||
* Unit tests for
|
||||
* {@link org.springframework.messaging.simp.SimpAttributes}.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 4.1
|
||||
*/
|
||||
public class SimpAttributesTests {
|
||||
|
||||
private SimpAttributes simpAttributes;
|
||||
|
||||
private Map<String, Object> map;
|
||||
|
||||
@Rule
|
||||
public ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.map = new ConcurrentHashMap<>();
|
||||
this.simpAttributes = new SimpAttributes("session1", this.map);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void getAttribute() {
|
||||
this.simpAttributes.setAttribute("name1", "value1");
|
||||
|
||||
assertThat(this.simpAttributes.getAttribute("name1"), is("value1"));
|
||||
assertThat(this.simpAttributes.getAttribute("name2"), nullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAttributeNames() {
|
||||
this.simpAttributes.setAttribute("name1", "value1");
|
||||
this.simpAttributes.setAttribute("name2", "value1");
|
||||
this.simpAttributes.setAttribute("name3", "value1");
|
||||
|
||||
assertThat(this.simpAttributes.getAttributeNames(), arrayContainingInAnyOrder("name1", "name2", "name3"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void registerDestructionCallback() {
|
||||
Runnable callback = Mockito.mock(Runnable.class);
|
||||
this.simpAttributes.registerDestructionCallback("name1", callback);
|
||||
|
||||
assertThat(this.simpAttributes.getAttribute(
|
||||
SimpAttributes.DESTRUCTION_CALLBACK_NAME_PREFIX + "name1"), sameInstance(callback));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void registerDestructionCallbackAfterSessionCompleted() {
|
||||
this.simpAttributes.sessionCompleted();
|
||||
this.thrown.expect(IllegalStateException.class);
|
||||
this.thrown.expectMessage(containsString("already completed"));
|
||||
this.simpAttributes.registerDestructionCallback("name1", Mockito.mock(Runnable.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void removeDestructionCallback() {
|
||||
Runnable callback1 = Mockito.mock(Runnable.class);
|
||||
Runnable callback2 = Mockito.mock(Runnable.class);
|
||||
this.simpAttributes.registerDestructionCallback("name1", callback1);
|
||||
this.simpAttributes.registerDestructionCallback("name2", callback2);
|
||||
|
||||
assertThat(this.simpAttributes.getAttributeNames().length, is(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getSessionMutex() {
|
||||
assertThat(this.simpAttributes.getSessionMutex(), sameInstance(this.map));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getSessionMutexExplicit() {
|
||||
Object mutex = new Object();
|
||||
this.simpAttributes.setAttribute(SimpAttributes.SESSION_MUTEX_NAME, mutex);
|
||||
|
||||
assertThat(this.simpAttributes.getSessionMutex(), sameInstance(mutex));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sessionCompleted() {
|
||||
Runnable callback1 = Mockito.mock(Runnable.class);
|
||||
Runnable callback2 = Mockito.mock(Runnable.class);
|
||||
this.simpAttributes.registerDestructionCallback("name1", callback1);
|
||||
this.simpAttributes.registerDestructionCallback("name2", callback2);
|
||||
|
||||
this.simpAttributes.sessionCompleted();
|
||||
|
||||
verify(callback1, times(1)).run();
|
||||
verify(callback2, times(1)).run();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void sessionCompletedIsIdempotent() {
|
||||
Runnable callback1 = Mockito.mock(Runnable.class);
|
||||
this.simpAttributes.registerDestructionCallback("name1", callback1);
|
||||
|
||||
this.simpAttributes.sessionCompleted();
|
||||
this.simpAttributes.sessionCompleted();
|
||||
this.simpAttributes.sessionCompleted();
|
||||
|
||||
verify(callback1, times(1)).run();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* 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 org.springframework.messaging.simp;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.ObjectFactory;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link org.springframework.messaging.simp.SimpSessionScope}.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 4.1
|
||||
*/
|
||||
public class SimpSessionScopeTests {
|
||||
|
||||
private SimpSessionScope scope;
|
||||
|
||||
private ObjectFactory objectFactory;
|
||||
|
||||
private SimpAttributes simpAttributes;
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
this.scope = new SimpSessionScope();
|
||||
this.objectFactory = Mockito.mock(ObjectFactory.class);
|
||||
this.simpAttributes = new SimpAttributes("session1", new ConcurrentHashMap<>());
|
||||
SimpAttributesContextHolder.setAttributes(this.simpAttributes);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
SimpAttributesContextHolder.resetAttributes();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void get() {
|
||||
this.simpAttributes.setAttribute("name", "value");
|
||||
Object actual = this.scope.get("name", this.objectFactory);
|
||||
|
||||
assertThat(actual, is("value"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getWithObjectFactory() {
|
||||
when(this.objectFactory.getObject()).thenReturn("value");
|
||||
Object actual = this.scope.get("name", this.objectFactory);
|
||||
|
||||
assertThat(actual, is("value"));
|
||||
assertThat(this.simpAttributes.getAttribute("name"), is("value"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void remove() {
|
||||
this.simpAttributes.setAttribute("name", "value");
|
||||
|
||||
Object removed = this.scope.remove("name");
|
||||
assertThat(removed, is("value"));
|
||||
assertThat(this.simpAttributes.getAttribute("name"), nullValue());
|
||||
|
||||
removed = this.scope.remove("name");
|
||||
assertThat(removed, nullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void registerDestructionCallback() {
|
||||
Runnable runnable = Mockito.mock(Runnable.class);
|
||||
this.scope.registerDestructionCallback("name", runnable);
|
||||
|
||||
this.simpAttributes.sessionCompleted();
|
||||
verify(runnable, times(1)).run();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getSessionId() {
|
||||
assertThat(this.scope.getConversationId(), is("session1"));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -18,6 +18,7 @@ package org.springframework.messaging.simp.annotation.support;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -28,6 +29,8 @@ import org.springframework.messaging.MessageChannel;
|
||||
import org.springframework.messaging.SubscribableChannel;
|
||||
import org.springframework.messaging.handler.annotation.*;
|
||||
import org.springframework.messaging.handler.annotation.support.MethodArgumentNotValidException;
|
||||
import org.springframework.messaging.simp.SimpAttributes;
|
||||
import org.springframework.messaging.simp.SimpAttributesContextHolder;
|
||||
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
|
||||
import org.springframework.messaging.simp.SimpMessageSendingOperations;
|
||||
import org.springframework.messaging.simp.SimpMessageType;
|
||||
@@ -39,6 +42,7 @@ import org.springframework.validation.Errors;
|
||||
import org.springframework.validation.Validator;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
@@ -69,7 +73,7 @@ public class SimpAnnotationMethodMessageHandlerTests {
|
||||
this.messageHandler.afterPropertiesSet();
|
||||
|
||||
testController = new TestController();
|
||||
this.messageHandler.registerHandler(testController);
|
||||
this.messageHandler.registerHandler(this.testController);
|
||||
}
|
||||
|
||||
|
||||
@@ -77,6 +81,8 @@ public class SimpAnnotationMethodMessageHandlerTests {
|
||||
@Test
|
||||
public void headerArgumentResolution() {
|
||||
SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create();
|
||||
headers.setSessionId("session1");
|
||||
headers.setSessionAttributes(new ConcurrentHashMap<>());
|
||||
headers.setDestination("/pre/headers");
|
||||
headers.setHeader("foo", "bar");
|
||||
Message<?> message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build();
|
||||
@@ -90,6 +96,8 @@ public class SimpAnnotationMethodMessageHandlerTests {
|
||||
@Test
|
||||
public void messageMappingDestinationVariableResolution() {
|
||||
SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create();
|
||||
headers.setSessionId("session1");
|
||||
headers.setSessionAttributes(new ConcurrentHashMap<>());
|
||||
headers.setDestination("/pre/message/bar/value");
|
||||
Message<?> message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build();
|
||||
this.messageHandler.handleMessage(message);
|
||||
@@ -102,6 +110,8 @@ public class SimpAnnotationMethodMessageHandlerTests {
|
||||
@Test
|
||||
public void subscribeEventDestinationVariableResolution() {
|
||||
SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create(SimpMessageType.SUBSCRIBE);
|
||||
headers.setSessionId("session1");
|
||||
headers.setSessionAttributes(new ConcurrentHashMap<>());
|
||||
headers.setDestination("/pre/sub/bar/value");
|
||||
Message<?> message = MessageBuilder.withPayload(new byte[0]).copyHeaders(headers.toMap()).build();
|
||||
this.messageHandler.handleMessage(message);
|
||||
@@ -114,6 +124,8 @@ public class SimpAnnotationMethodMessageHandlerTests {
|
||||
@Test
|
||||
public void simpleBinding() {
|
||||
SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create();
|
||||
headers.setSessionId("session1");
|
||||
headers.setSessionAttributes(new ConcurrentHashMap<>());
|
||||
headers.setDestination("/pre/binding/id/12");
|
||||
Message<?> message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build();
|
||||
this.messageHandler.handleMessage(message);
|
||||
@@ -126,12 +138,28 @@ public class SimpAnnotationMethodMessageHandlerTests {
|
||||
@Test
|
||||
public void validationError() {
|
||||
SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create();
|
||||
headers.setSessionId("session1");
|
||||
headers.setSessionAttributes(new ConcurrentHashMap<>());
|
||||
headers.setDestination("/pre/validation/payload");
|
||||
Message<?> message = MessageBuilder.withPayload(TEST_INVALID_VALUE.getBytes()).setHeaders(headers).build();
|
||||
this.messageHandler.handleMessage(message);
|
||||
assertEquals("handleValidationException", this.testController.method);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void simpScope() {
|
||||
ConcurrentHashMap<String, Object> map = new ConcurrentHashMap<>();
|
||||
map.put("name", "value");
|
||||
SimpMessageHeaderAccessor headers = SimpMessageHeaderAccessor.create();
|
||||
headers.setSessionId("session1");
|
||||
headers.setSessionAttributes(map);
|
||||
headers.setDestination("/pre/scope");
|
||||
Message<?> message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build();
|
||||
this.messageHandler.handleMessage(message);
|
||||
|
||||
assertEquals("scope", this.testController.method);
|
||||
}
|
||||
|
||||
|
||||
private static class TestSimpAnnotationMethodMessageHandler extends SimpAnnotationMethodMessageHandler {
|
||||
|
||||
@@ -195,6 +223,13 @@ public class SimpAnnotationMethodMessageHandlerTests {
|
||||
public void handleValidationException() {
|
||||
this.method = "handleValidationException";
|
||||
}
|
||||
|
||||
@MessageMapping("/scope")
|
||||
public void scope() {
|
||||
SimpAttributes simpAttributes = SimpAttributesContextHolder.currentAttributes();
|
||||
assertThat(simpAttributes.getAttribute("name"), is("value"));
|
||||
this.method = "scope";
|
||||
}
|
||||
}
|
||||
|
||||
private static class StringTestValidator implements Validator {
|
||||
|
||||
@@ -20,6 +20,7 @@ import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import org.hamcrest.Matchers;
|
||||
import org.junit.Before;
|
||||
@@ -141,6 +142,7 @@ public class MessageBrokerConfigurationTests {
|
||||
|
||||
StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.SUBSCRIBE);
|
||||
headers.setSessionId("sess1");
|
||||
headers.setSessionAttributes(new ConcurrentHashMap<>());
|
||||
headers.setSubscriptionId("subs1");
|
||||
headers.setDestination("/foo");
|
||||
Message<?> message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build();
|
||||
@@ -230,6 +232,8 @@ public class MessageBrokerConfigurationTests {
|
||||
this.simpleBrokerContext.getBean(SimpAnnotationMethodMessageHandler.class);
|
||||
|
||||
StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.SEND);
|
||||
headers.setSessionId("sess1");
|
||||
headers.setSessionAttributes(new ConcurrentHashMap<>());
|
||||
headers.setDestination("/foo");
|
||||
Message<?> message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user