Skip to content

Commit

Permalink
Fix handling of empty payload Pong message on Jetty
Browse files Browse the repository at this point in the history
Issue: SPR-12727
  • Loading branch information
rstoyanchev committed Feb 18, 2015
1 parent c8a4d16 commit f1e406c
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2015 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.
Expand All @@ -16,6 +16,8 @@

package org.springframework.web.socket.adapter.jetty;

import java.nio.ByteBuffer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.jetty.websocket.api.Session;
Expand All @@ -36,6 +38,7 @@
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.handler.ExceptionWebSocketHandlerDecorator;


/**
* Adapts {@link WebSocketHandler} to the Jetty 9 WebSocket API.
*
Expand All @@ -45,8 +48,11 @@
@WebSocket
public class JettyWebSocketHandlerAdapter {

private static final ByteBuffer EMPTY_PAYLOAD = ByteBuffer.wrap(new byte[0]);

private static final Log logger = LogFactory.getLog(JettyWebSocketHandlerAdapter.class);


private final WebSocketHandler webSocketHandler;

private final JettyWebSocketSession wsSession;
Expand Down Expand Up @@ -96,7 +102,8 @@ public void onWebSocketBinary(byte[] payload, int offset, int length) {
@OnWebSocketFrame
public void onWebSocketFrame(Frame frame) {
if (OpCode.PONG == frame.getOpCode()) {
PongMessage message = new PongMessage(frame.getPayload());
ByteBuffer payload = frame.getPayload() != null ? frame.getPayload() : EMPTY_PAYLOAD;
PongMessage message = new PongMessage(payload);
try {
this.webSocketHandler.handleMessage(this.wsSession, message);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 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.
Expand All @@ -17,8 +17,14 @@
package org.springframework.web.socket;


import static org.junit.Assert.*;

import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -32,10 +38,10 @@
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.handler.AbstractWebSocketHandler;
import org.springframework.web.socket.handler.TextWebSocketHandler;
import org.springframework.web.socket.server.support.DefaultHandshakeHandler;

import static org.junit.Assert.*;

/**
* Client and server-side WebSocket integration tests.
Expand Down Expand Up @@ -67,6 +73,24 @@ public void subProtocolNegotiation() throws Exception {
URI url = new URI(getWsBaseUrl() + "/ws");
WebSocketSession session = this.webSocketClient.doHandshake(new TextWebSocketHandler(), headers, url).get();
assertEquals("foo", session.getAcceptedProtocol());
session.close();
}

// SPR-12727

@Test
public void unsolicitedPongWithEmptyPayload() throws Exception {
TestWebSocketHandler serverHandler = this.wac.getBean(TestWebSocketHandler.class);
serverHandler.setWaitMessageCount(1);

String url = getWsBaseUrl() + "/ws";
WebSocketSession session = this.webSocketClient.doHandshake(new AbstractWebSocketHandler() {}, url).get();
session.sendMessage(new PongMessage());

serverHandler.await();
assertNull(serverHandler.getTransportError());
assertEquals(1, serverHandler.getReceivedMessages().size());
assertEquals(PongMessage.class, serverHandler.getReceivedMessages().get(0).getClass());
}


Expand All @@ -84,8 +108,51 @@ public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
}

@Bean
public TextWebSocketHandler handler() {
return new TextWebSocketHandler();
public TestWebSocketHandler handler() {
return new TestWebSocketHandler();
}

}

private static class TestWebSocketHandler extends AbstractWebSocketHandler {

private List<WebSocketMessage> receivedMessages = new ArrayList<>();

private int waitMessageCount;

private final CountDownLatch latch = new CountDownLatch(1);

private Throwable transportError;


public void setWaitMessageCount(int waitMessageCount) {
this.waitMessageCount = waitMessageCount;
}

public List<WebSocketMessage> getReceivedMessages() {
return this.receivedMessages;
}

public Throwable getTransportError() {
return this.transportError;
}

@Override
public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {
this.receivedMessages.add(message);
if (this.receivedMessages.size() >= this.waitMessageCount) {
this.latch.countDown();
}
}

@Override
public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
this.transportError = exception;
this.latch.countDown();
}

public void await() throws InterruptedException {
this.latch.await(5, TimeUnit.SECONDS);
}
}

Expand Down

0 comments on commit f1e406c

Please sign in to comment.