Polish method and field declaration order

This commit is contained in:
Rossen Stoyanchev
2016-12-12 17:02:18 -05:00
parent f32a41933e
commit 5829e1c141
7 changed files with 98 additions and 95 deletions

View File

@@ -48,8 +48,6 @@ public abstract class AbstractListenerWebSocketSession<T> extends WebSocketSessi
private static final int RECEIVE_BUFFER_SIZE = 8192;
private final AtomicBoolean sendCalled = new AtomicBoolean();
private final String id;
private final URI uri;
@@ -58,6 +56,8 @@ public abstract class AbstractListenerWebSocketSession<T> extends WebSocketSessi
private volatile WebSocketSendProcessor sendProcessor;
private final AtomicBoolean sendCalled = new AtomicBoolean();
public AbstractListenerWebSocketSession(T delegate, String id, URI uri) {
super(delegate);
@@ -104,13 +104,10 @@ public abstract class AbstractListenerWebSocketSession<T> extends WebSocketSessi
}
/**
* Resume receiving new message(s) after demand is generated by the
* downstream Subscriber.
* <p><strong>Note:</strong> if the underlying WebSocket API does not provide
* flow control for receiving messages, and this method should be a no-op
* and {@link #canSuspendReceiving()} should return {@code false}.
* Whether the underlying WebSocket API has flow control and can suspend and
* resume the receiving of messages.
*/
protected abstract void resumeReceiving();
protected abstract boolean canSuspendReceiving();
/**
* Suspend receiving until received message(s) are processed and more demand
@@ -122,16 +119,22 @@ public abstract class AbstractListenerWebSocketSession<T> extends WebSocketSessi
protected abstract void suspendReceiving();
/**
* Whether the underlying WebSocket API has flow control and can suspend and
* resume the receiving of messages.
* Resume receiving new message(s) after demand is generated by the
* downstream Subscriber.
* <p><strong>Note:</strong> if the underlying WebSocket API does not provide
* flow control for receiving messages, and this method should be a no-op
* and {@link #canSuspendReceiving()} should return {@code false}.
*/
protected abstract boolean canSuspendReceiving();
protected abstract void resumeReceiving();
/**
* Send the given WebSocket message.
*/
protected abstract boolean sendMessage(WebSocketMessage message) throws IOException;
// WebSocketHandler adapter delegate methods
/** Handle a message callback from the WebSocketHandler adapter */
void handleMessage(Type type, WebSocketMessage message) {
this.receivePublisher.handleMessage(message);

View File

@@ -53,12 +53,12 @@ public class JettyWebSocketHandlerAdapter {
private static final ByteBuffer EMPTY_PAYLOAD = ByteBuffer.wrap(new byte[0]);
private final DataBufferFactory bufferFactory = new DefaultDataBufferFactory(false);
private final WebSocketHandler delegate;
private JettyWebSocketSession session;
private final DataBufferFactory bufferFactory = new DefaultDataBufferFactory(false);
public JettyWebSocketHandlerAdapter(WebSocketHandler delegate) {
Assert.notNull("WebSocketHandler is required");
@@ -102,20 +102,6 @@ public class JettyWebSocketHandlerAdapter {
}
}
@OnWebSocketClose
public void onWebSocketClose(int statusCode, String reason) {
if (this.session != null) {
this.session.handleClose(new CloseStatus(statusCode, reason));
}
}
@OnWebSocketError
public void onWebSocketError(Throwable cause) {
if (this.session != null) {
this.session.handleError(cause);
}
}
private <T> WebSocketMessage toMessage(Type type, T message) {
if (Type.TEXT.equals(type)) {
byte[] bytes = ((String) message).getBytes(StandardCharsets.UTF_8);
@@ -135,6 +121,20 @@ public class JettyWebSocketHandlerAdapter {
}
}
@OnWebSocketClose
public void onWebSocketClose(int statusCode, String reason) {
if (this.session != null) {
this.session.handleClose(new CloseStatus(statusCode, reason));
}
}
@OnWebSocketError
public void onWebSocketError(Throwable cause) {
if (this.session != null) {
this.session.handleError(cause);
}
}
private final class HandlerResultSubscriber implements Subscriber<Void> {

View File

@@ -46,9 +46,18 @@ public class JettyWebSocketSession extends AbstractListenerWebSocketSession<Sess
@Override
protected Mono<Void> closeInternal(CloseStatus status) {
getDelegate().close(status.getCode(), status.getReason());
return Mono.empty();
protected boolean canSuspendReceiving() {
return false;
}
@Override
protected void suspendReceiving() {
// No-op
}
@Override
protected void resumeReceiving() {
// No-op
}
@Override
@@ -76,18 +85,9 @@ public class JettyWebSocketSession extends AbstractListenerWebSocketSession<Sess
}
@Override
protected void resumeReceiving() {
// No-op
}
@Override
protected void suspendReceiving() {
// No-op
}
@Override
protected boolean canSuspendReceiving() {
return false;
protected Mono<Void> closeInternal(CloseStatus status) {
getDelegate().close(status.getCode(), status.getReason());
return Mono.empty();
}

View File

@@ -45,12 +45,12 @@ import org.springframework.web.reactive.socket.WebSocketMessage.Type;
*/
public class TomcatWebSocketHandlerAdapter extends Endpoint {
private final DataBufferFactory bufferFactory = new DefaultDataBufferFactory(false);
private final WebSocketHandler delegate;
private TomcatWebSocketSession session;
private final DataBufferFactory bufferFactory = new DefaultDataBufferFactory(false);
public TomcatWebSocketHandlerAdapter(WebSocketHandler delegate) {
Assert.notNull("WebSocketHandler is required");
@@ -79,21 +79,6 @@ public class TomcatWebSocketHandlerAdapter extends Endpoint {
this.delegate.handle(this.session).subscribe(resultSubscriber);
}
@Override
public void onClose(Session session, CloseReason reason) {
if (this.session != null) {
int code = reason.getCloseCode().getCode();
this.session.handleClose(new CloseStatus(code, reason.getReasonPhrase()));
}
}
@Override
public void onError(Session session, Throwable exception) {
if (this.session != null) {
this.session.handleError(exception);
}
}
private <T> WebSocketMessage toMessage(T message) {
if (message instanceof String) {
byte[] bytes = ((String) message).getBytes(StandardCharsets.UTF_8);
@@ -112,6 +97,21 @@ public class TomcatWebSocketHandlerAdapter extends Endpoint {
}
}
@Override
public void onClose(Session session, CloseReason reason) {
if (this.session != null) {
int code = reason.getCloseCode().getCode();
this.session.handleClose(new CloseStatus(code, reason.getReasonPhrase()));
}
}
@Override
public void onError(Session session, Throwable exception) {
if (this.session != null) {
this.session.handleError(exception);
}
}
private final class HandlerResultSubscriber implements Subscriber<Void> {

View File

@@ -47,15 +47,18 @@ public class TomcatWebSocketSession extends AbstractListenerWebSocketSession<Ses
@Override
protected Mono<Void> closeInternal(CloseStatus status) {
try {
getDelegate().close(
new CloseReason(CloseCodes.getCloseCode(status.getCode()), status.getReason()));
}
catch (IOException e) {
return Mono.error(e);
}
return Mono.empty();
protected boolean canSuspendReceiving() {
return false;
}
@Override
protected void suspendReceiving() {
// No-op
}
@Override
protected void resumeReceiving() {
// No-op
}
@Override
@@ -83,18 +86,15 @@ public class TomcatWebSocketSession extends AbstractListenerWebSocketSession<Ses
}
@Override
protected void resumeReceiving() {
// No-op
}
@Override
protected void suspendReceiving() {
// No-op
}
@Override
protected boolean canSuspendReceiving() {
return false;
protected Mono<Void> closeInternal(CloseStatus status) {
try {
CloseReason.CloseCode code = CloseCodes.getCloseCode(status.getCode());
getDelegate().close(new CloseReason(code, status.getReason()));
}
catch (IOException e) {
return Mono.error(e);
}
return Mono.empty();
}

View File

@@ -49,12 +49,12 @@ import io.undertow.websockets.spi.WebSocketHttpExchange;
*/
public class UndertowWebSocketHandlerAdapter implements WebSocketConnectionCallback {
private final DataBufferFactory bufferFactory = new DefaultDataBufferFactory(false);
private final WebSocketHandler delegate;
private UndertowWebSocketSession session;
private final DataBufferFactory bufferFactory = new DefaultDataBufferFactory(false);
public UndertowWebSocketHandlerAdapter(WebSocketHandler delegate) {
Assert.notNull("WebSocketHandler is required");

View File

@@ -49,25 +49,16 @@ public class UndertowWebSocketSession extends AbstractListenerWebSocketSession<W
@Override
protected Mono<Void> closeInternal(CloseStatus status) {
CloseMessage cm = new CloseMessage(status.getCode(), status.getReason());
if (!getDelegate().isCloseFrameSent()) {
WebSockets.sendClose(cm, getDelegate(), null);
}
return Mono.empty();
}
protected void resumeReceiving() {
getDelegate().resumeReceives();
protected boolean canSuspendReceiving() {
return true;
}
protected void suspendReceiving() {
getDelegate().suspendReceives();
}
@Override
protected boolean canSuspendReceiving() {
return true;
protected void resumeReceiving() {
getDelegate().resumeReceives();
}
@Override
@@ -96,6 +87,15 @@ public class UndertowWebSocketSession extends AbstractListenerWebSocketSession<W
return true;
}
@Override
protected Mono<Void> closeInternal(CloseStatus status) {
CloseMessage cm = new CloseMessage(status.getCode(), status.getReason());
if (!getDelegate().isCloseFrameSent()) {
WebSockets.sendClose(cm, getDelegate(), null);
}
return Mono.empty();
}
private final class SendProcessorCallback implements WebSocketCallback<Void> {