INT-4360: Add ClientStompEncoder support

The `StompSubProtocolHandler` explicitly sets `stompCommand` header
to the `MESSAGE` value ignoring any client inputs.
In this case the message is treated as from the server and ignored on
the STOMP Broker side from the client session.

* Introduce `ClientStompEncoder` for the client side to be injected
into the `StompSubProtocolHandler` for the proper client side messages
encoding/decoding.
Override `stompCommand` header to the `SEND` value if it is `MESSAGE`
before encoding to the `byte[]` to send to the session

JIRA: https://jira.spring.io/browse/INT-4360

**Cherry-pick to 4.3.x**

Fix WebSocket test to rely on the proper client config class
and don't pick up the server config unconditionally in the test context
This commit is contained in:
Artem Bilan
2017-10-30 14:28:51 -04:00
committed by Gary Russell
parent 2712e28074
commit 8766262399
5 changed files with 85 additions and 24 deletions

View File

@@ -0,0 +1,48 @@
/*
* Copyright 2017 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.integration.websocket.support;
import java.util.Map;
import org.springframework.messaging.simp.stomp.StompCommand;
import org.springframework.messaging.simp.stomp.StompEncoder;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
/**
* A {@link StompEncoder} extension to prepare a message for sending/receiving
* before/after encoding/decoding when used from WebSockets client side.
* For example it updates the {@code stompCommand} header from the {@code MESSAGE}
* to {@code SEND} frame, which is the case of
* {@link org.springframework.web.socket.messaging.StompSubProtocolHandler}.
*
* @author Artem Bilan
*
* @since 4.3.13
*/
public class ClientStompEncoder extends StompEncoder {
@Override
public byte[] encode(Map<String, Object> headers, byte[] payload) {
if (StompCommand.MESSAGE.equals(headers.get("stompCommand"))) {
StompHeaderAccessor stompHeaderAccessor = StompHeaderAccessor.create(StompCommand.SEND);
stompHeaderAccessor.copyHeadersIfAbsent(headers);
headers = stompHeaderAccessor.getMessageHeaders();
}
return super.encode(headers, payload);
}
}