Skip to content

Commit d7db2b7

Browse files
author
Stephane Landelle
committed
minor clean up
1 parent f822b0a commit d7db2b7

File tree

2 files changed

+24
-70
lines changed

2 files changed

+24
-70
lines changed

api/src/main/java/org/asynchttpclient/websocket/WebSocketUpgradeHandler.java

Lines changed: 22 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,13 @@
2727
public class WebSocketUpgradeHandler implements UpgradeHandler<WebSocket>, AsyncHandler<WebSocket> {
2828

2929
private WebSocket webSocket;
30-
private final ConcurrentLinkedQueue<WebSocketListener> l;
31-
@SuppressWarnings("unused")
32-
private final String protocol;
33-
@SuppressWarnings("unused")
34-
private final long maxByteSize;
35-
@SuppressWarnings("unused")
36-
private final long maxTextSize;
30+
private final ConcurrentLinkedQueue<WebSocketListener> listeners;
3731
private final AtomicBoolean ok = new AtomicBoolean(false);
3832
private final AtomicBoolean onSuccessCalled = new AtomicBoolean(false);
3933
private int status;
4034

41-
private WebSocketUpgradeHandler(Builder b) {
42-
l = b.l;
43-
protocol = b.protocol;
44-
maxByteSize = b.maxByteSize;
45-
maxTextSize = b.maxTextSize;
35+
public WebSocketUpgradeHandler(ConcurrentLinkedQueue<WebSocketListener> listeners) {
36+
this.listeners = listeners;
4637
}
4738

4839
/**
@@ -93,8 +84,9 @@ public final STATE onHeadersReceived(HttpResponseHeaders headers) throws Excepti
9384
public final WebSocket onCompleted() throws Exception {
9485

9586
if (status != 101) {
96-
for (WebSocketListener w : l) {
97-
w.onError(new IllegalStateException(String.format("Invalid Status Code %d", status)));
87+
IllegalStateException e = new IllegalStateException("Invalid Status Code " + status);
88+
for (WebSocketListener listener : listeners) {
89+
listener.onError(e);
9890
}
9991
return null;
10092
}
@@ -111,9 +103,9 @@ public final WebSocket onCompleted() throws Exception {
111103
@Override
112104
public final void onSuccess(WebSocket webSocket) {
113105
this.webSocket = webSocket;
114-
for (WebSocketListener w : l) {
115-
webSocket.addWebSocketListener(w);
116-
w.onOpen(webSocket);
106+
for (WebSocketListener listener : listeners) {
107+
webSocket.addWebSocketListener(listener);
108+
listener.onOpen(webSocket);
117109
}
118110
ok.set(true);
119111
}
@@ -123,11 +115,11 @@ public final void onSuccess(WebSocket webSocket) {
123115
*/
124116
@Override
125117
public final void onFailure(Throwable t) {
126-
for (WebSocketListener w : l) {
118+
for (WebSocketListener listener : listeners) {
127119
if (!ok.get() && webSocket != null) {
128-
webSocket.addWebSocketListener(w);
120+
webSocket.addWebSocketListener(listener);
129121
}
130-
w.onError(t);
122+
listener.onError(t);
131123
}
132124
}
133125

@@ -136,13 +128,13 @@ public final void onClose(WebSocket webSocket, int status, String reasonPhrase)
136128
if (this.webSocket == null)
137129
this.webSocket = webSocket;
138130

139-
for (WebSocketListener w : l) {
131+
for (WebSocketListener listener : listeners) {
140132
if (webSocket != null) {
141-
webSocket.addWebSocketListener(w);
133+
webSocket.addWebSocketListener(listener);
142134
}
143-
w.onClose(webSocket);
144-
if (w instanceof WebSocketCloseCodeReasonListener) {
145-
WebSocketCloseCodeReasonListener.class.cast(w).onClose(webSocket, status, reasonPhrase);
135+
listener.onClose(webSocket);
136+
if (listener instanceof WebSocketCloseCodeReasonListener) {
137+
WebSocketCloseCodeReasonListener.class.cast(listener).onClose(webSocket, status, reasonPhrase);
146138
}
147139
}
148140
}
@@ -151,10 +143,8 @@ public final void onClose(WebSocket webSocket, int status, String reasonPhrase)
151143
* Build a {@link WebSocketUpgradeHandler}
152144
*/
153145
public final static class Builder {
154-
private ConcurrentLinkedQueue<WebSocketListener> l = new ConcurrentLinkedQueue<WebSocketListener>();
155-
private String protocol = "";
156-
private long maxByteSize = 8192;
157-
private long maxTextSize = 8192;
146+
147+
private ConcurrentLinkedQueue<WebSocketListener> listeners = new ConcurrentLinkedQueue<WebSocketListener>();
158148

159149
/**
160150
* Add a {@link WebSocketListener} that will be added to the {@link WebSocket}
@@ -163,7 +153,7 @@ public final static class Builder {
163153
* @return this
164154
*/
165155
public Builder addWebSocketListener(WebSocketListener listener) {
166-
l.add(listener);
156+
listeners.add(listener);
167157
return this;
168158
}
169159

@@ -174,40 +164,7 @@ public Builder addWebSocketListener(WebSocketListener listener) {
174164
* @return this
175165
*/
176166
public Builder removeWebSocketListener(WebSocketListener listener) {
177-
l.remove(listener);
178-
return this;
179-
}
180-
181-
/**
182-
* Set the WebSocket protocol.
183-
*
184-
* @param protocol the WebSocket protocol.
185-
* @return this
186-
*/
187-
public Builder setProtocol(String protocol) {
188-
this.protocol = protocol;
189-
return this;
190-
}
191-
192-
/**
193-
* Set the max size of the WebSocket byte message that will be sent.
194-
*
195-
* @param maxByteSize max size of the WebSocket byte message
196-
* @return this
197-
*/
198-
public Builder setMaxByteSize(long maxByteSize) {
199-
this.maxByteSize = maxByteSize;
200-
return this;
201-
}
202-
203-
/**
204-
* Set the max size of the WebSocket text message that will be sent.
205-
*
206-
* @param maxTextSize max size of the WebSocket byte message
207-
* @return this
208-
*/
209-
public Builder setMaxTextSize(long maxTextSize) {
210-
this.maxTextSize = maxTextSize;
167+
listeners.remove(listener);
211168
return this;
212169
}
213170

@@ -217,7 +174,7 @@ public Builder setMaxTextSize(long maxTextSize) {
217174
* @return a {@link WebSocketUpgradeHandler}
218175
*/
219176
public WebSocketUpgradeHandler build() {
220-
return new WebSocketUpgradeHandler(this);
177+
return new WebSocketUpgradeHandler(listeners);
221178
}
222179
}
223180
}

providers/netty/src/main/java/org/asynchttpclient/providers/netty/ws/NettyWebSocket.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,8 @@ public int getMaxBufferSize() {
103103
return maxBufferSize;
104104
}
105105

106-
public void setMaxBufferSize(int bufferSize) {
107-
maxBufferSize = bufferSize;
108-
109-
if (maxBufferSize < 8192)
110-
maxBufferSize = 8192;
106+
public void setMaxBufferSize(int maxBufferSize) {
107+
this.maxBufferSize = Math.max(maxBufferSize, 8192);
111108
}
112109

113110
@Override

0 commit comments

Comments
 (0)