Skip to content

Commit 0f0c9a9

Browse files
authored
Merge pull request #1072 from marci4/ImproveCodeQuality
2 parents cfde6e0 + 08be869 commit 0f0c9a9

File tree

15 files changed

+134
-85
lines changed

15 files changed

+134
-85
lines changed

src/main/java/org/java_websocket/AbstractWebSocket.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public abstract class AbstractWebSocket extends WebSocketAdapter {
7474
*
7575
* @since 1.4.1
7676
*/
77-
private ScheduledFuture connectionLostCheckerFuture;
77+
private ScheduledFuture<?> connectionLostCheckerFuture;
7878

7979
/**
8080
* Attribute for the lost connection check interval in nanoseconds
@@ -126,7 +126,7 @@ public void setConnectionLostTimeout(int connectionLostTimeout) {
126126
log.trace("Connection lost timer restarted");
127127
//Reset all the pings
128128
try {
129-
ArrayList<WebSocket> connections = new ArrayList<WebSocket>(getConnections());
129+
ArrayList<WebSocket> connections = new ArrayList<>(getConnections());
130130
WebSocketImpl webSocketImpl;
131131
for (WebSocket conn : connections) {
132132
if (conn instanceof WebSocketImpl) {
@@ -188,14 +188,17 @@ private void restartConnectionLostTimer() {
188188
/**
189189
* Keep the connections in a separate list to not cause deadlocks
190190
*/
191-
private ArrayList<WebSocket> connections = new ArrayList<WebSocket>();
191+
private ArrayList<WebSocket> connections = new ArrayList<>();
192192

193193
@Override
194194
public void run() {
195195
connections.clear();
196196
try {
197197
connections.addAll(getConnections());
198-
long minimumPongTime = (long) (System.nanoTime() - (connectionLostTimeout * 1.5));
198+
long minimumPongTime;
199+
synchronized (syncConnectionLost) {
200+
minimumPongTime = (long) (System.nanoTime() - (connectionLostTimeout * 1.5));
201+
}
199202
for (WebSocket conn : connections) {
200203
executeConnectionLostDetection(conn, minimumPongTime);
201204
}

src/main/java/org/java_websocket/WebSocketImpl.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ public WebSocketImpl(WebSocketListener listener, List<Draft> drafts) {
190190
this.role = Role.SERVER;
191191
// draft.copyInstance will be called when the draft is first needed
192192
if (drafts == null || drafts.isEmpty()) {
193-
knownDrafts = new ArrayList<Draft>();
193+
knownDrafts = new ArrayList<>();
194194
knownDrafts.add(new Draft_6455());
195195
} else {
196196
knownDrafts = drafts;
@@ -208,8 +208,8 @@ public WebSocketImpl(WebSocketListener listener, Draft draft) {
208208
if (listener == null || (draft == null && role == Role.SERVER)) {
209209
throw new IllegalArgumentException("parameters must not be null");
210210
}
211-
this.outQueue = new LinkedBlockingQueue<ByteBuffer>();
212-
inQueue = new LinkedBlockingQueue<ByteBuffer>();
211+
this.outQueue = new LinkedBlockingQueue<>();
212+
inQueue = new LinkedBlockingQueue<>();
213213
this.wsl = listener;
214214
this.role = Role.CLIENT;
215215
if (draft != null) {
@@ -666,7 +666,7 @@ private void send(Collection<Framedata> frames) {
666666
if (frames == null) {
667667
throw new IllegalArgumentException();
668668
}
669-
ArrayList<ByteBuffer> outgoingFrames = new ArrayList<ByteBuffer>();
669+
ArrayList<ByteBuffer> outgoingFrames = new ArrayList<>();
670670
for (Framedata f : frames) {
671671
log.trace("send frame: {}", f);
672672
outgoingFrames.add(draft.createBinaryFrame(f));

src/main/java/org/java_websocket/client/WebSocketClient.java

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
import java.net.URI;
3737
import java.net.UnknownHostException;
3838
import java.nio.ByteBuffer;
39+
import java.security.KeyManagementException;
40+
import java.security.NoSuchAlgorithmException;
3941
import java.util.Collection;
4042
import java.util.Collections;
4143
import java.util.Map;
@@ -223,7 +225,7 @@ public InetAddress resolve(URI uri) throws UnknownHostException {
223225
}
224226
};
225227
if (httpHeaders != null) {
226-
headers = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
228+
headers = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
227229
headers.putAll(httpHeaders);
228230
}
229231
this.connectTimeout = connectTimeout;
@@ -269,7 +271,7 @@ public Socket getSocket() {
269271
*/
270272
public void addHeader(String key, String value) {
271273
if (headers == null) {
272-
headers = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
274+
headers = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
273275
}
274276
headers.put(key, value);
275277
}
@@ -461,19 +463,7 @@ public void sendPing() {
461463
public void run() {
462464
InputStream istream;
463465
try {
464-
boolean upgradeSocketToSSLSocket = false;
465-
// Prioritise a proxy over a socket factory and apply the socketfactory later
466-
if (proxy != Proxy.NO_PROXY) {
467-
socket = new Socket(proxy);
468-
upgradeSocketToSSLSocket = true;
469-
} else if (socketFactory != null) {
470-
socket = socketFactory.createSocket();
471-
} else if (socket == null) {
472-
socket = new Socket(proxy);
473-
upgradeSocketToSSLSocket = true;
474-
} else if (socket.isClosed()) {
475-
throw new IOException();
476-
}
466+
boolean upgradeSocketToSSLSocket = prepareSocket();
477467

478468
socket.setTcpNoDelay(isTcpNoDelay());
479469
socket.setReuseAddress(isReuseAddr());
@@ -485,17 +475,7 @@ public void run() {
485475

486476
// if the socket is set by others we don't apply any TLS wrapper
487477
if (upgradeSocketToSSLSocket && "wss".equals(uri.getScheme())) {
488-
SSLSocketFactory factory;
489-
// Prioritise the provided socketfactory
490-
// Helps when using web debuggers like Fiddler Classic
491-
if (socketFactory != null && (socketFactory instanceof SSLSocketFactory)) {
492-
factory = (SSLSocketFactory) socketFactory;
493-
} else {
494-
SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
495-
sslContext.init(null, null, null);
496-
factory = sslContext.getSocketFactory();
497-
}
498-
socket = factory.createSocket(socket, uri.getHost(), getPort(), true);
478+
upgradeSocketToSSL();
499479
}
500480

501481
if (socket instanceof SSLSocket) {
@@ -546,6 +526,38 @@ public void run() {
546526
connectReadThread = null;
547527
}
548528

529+
private void upgradeSocketToSSL()
530+
throws NoSuchAlgorithmException, KeyManagementException, IOException {
531+
SSLSocketFactory factory;
532+
// Prioritise the provided socketfactory
533+
// Helps when using web debuggers like Fiddler Classic
534+
if (socketFactory instanceof SSLSocketFactory) {
535+
factory = (SSLSocketFactory) socketFactory;
536+
} else {
537+
SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
538+
sslContext.init(null, null, null);
539+
factory = sslContext.getSocketFactory();
540+
}
541+
socket = factory.createSocket(socket, uri.getHost(), getPort(), true);
542+
}
543+
544+
private boolean prepareSocket() throws IOException {
545+
boolean upgradeSocketToSSLSocket = false;
546+
// Prioritise a proxy over a socket factory and apply the socketfactory later
547+
if (proxy != Proxy.NO_PROXY) {
548+
socket = new Socket(proxy);
549+
upgradeSocketToSSLSocket = true;
550+
} else if (socketFactory != null) {
551+
socket = socketFactory.createSocket();
552+
} else if (socket == null) {
553+
socket = new Socket(proxy);
554+
upgradeSocketToSSLSocket = true;
555+
} else if (socket.isClosed()) {
556+
throw new IOException();
557+
}
558+
return upgradeSocketToSSLSocket;
559+
}
560+
549561
/**
550562
* Apply specific SSLParameters If you override this method make sure to always call
551563
* super.onSetSSLParameters() to ensure the hostname validation is active

src/main/java/org/java_websocket/drafts/Draft_6455.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,10 @@ public Draft_6455(List<IExtension> inputExtensions, List<IProtocol> inputProtoco
229229
if (inputExtensions == null || inputProtocols == null || inputMaxFrameSize < 1) {
230230
throw new IllegalArgumentException();
231231
}
232-
knownExtensions = new ArrayList<IExtension>(inputExtensions.size());
233-
knownProtocols = new ArrayList<IProtocol>(inputProtocols.size());
232+
knownExtensions = new ArrayList<>(inputExtensions.size());
233+
knownProtocols = new ArrayList<>(inputProtocols.size());
234234
boolean hasDefault = false;
235-
byteBufferList = new ArrayList<ByteBuffer>();
235+
byteBufferList = new ArrayList<>();
236236
for (IExtension inputExtension : inputExtensions) {
237237
if (inputExtension.getClass().equals(DefaultExtension.class)) {
238238
hasDefault = true;
@@ -442,13 +442,13 @@ public HandshakeBuilder postProcessHandshakeResponseAsServer(ClientHandshake req
442442

443443
@Override
444444
public Draft copyInstance() {
445-
ArrayList<IExtension> newExtensions = new ArrayList<IExtension>();
446-
for (IExtension extension : getKnownExtensions()) {
447-
newExtensions.add(extension.copyInstance());
445+
ArrayList<IExtension> newExtensions = new ArrayList<>();
446+
for (IExtension knownExtension : getKnownExtensions()) {
447+
newExtensions.add(knownExtension.copyInstance());
448448
}
449-
ArrayList<IProtocol> newProtocols = new ArrayList<IProtocol>();
450-
for (IProtocol protocol : getKnownProtocols()) {
451-
newProtocols.add(protocol.copyInstance());
449+
ArrayList<IProtocol> newProtocols = new ArrayList<>();
450+
for (IProtocol knownProtocol : getKnownProtocols()) {
451+
newProtocols.add(knownProtocol.copyInstance());
452452
}
453453
return new Draft_6455(newExtensions, newProtocols, maxFrameSize);
454454
}
@@ -700,7 +700,7 @@ private int getSizeBytes(ByteBuffer mes) {
700700
@Override
701701
public List<Framedata> translateFrame(ByteBuffer buffer) throws InvalidDataException {
702702
while (true) {
703-
List<Framedata> frames = new LinkedList<Framedata>();
703+
List<Framedata> frames = new LinkedList<>();
704704
Framedata cur;
705705
if (incompleteframe != null) {
706706
// complete an incomplete frame

src/main/java/org/java_websocket/exceptions/LimitExceededException.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public LimitExceededException() {
5555
* constructor for a LimitExceededException
5656
* <p>
5757
* calling InvalidDataException with closecode TOOBIG
58+
* @param limit the allowed size which was not enough
5859
*/
5960
public LimitExceededException(int limit) {
6061
super(CloseFrame.TOOBIG);
@@ -65,6 +66,8 @@ public LimitExceededException(int limit) {
6566
* constructor for a LimitExceededException
6667
* <p>
6768
* calling InvalidDataException with closecode TOOBIG
69+
* @param s the detail message.
70+
* @param limit the allowed size which was not enough
6871
*/
6972
public LimitExceededException(String s, int limit) {
7073
super(CloseFrame.TOOBIG, s);

src/main/java/org/java_websocket/exceptions/WrappedIOException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class WrappedIOException extends Exception {
4040
/**
4141
* The websocket where the IOException happened
4242
*/
43-
private final WebSocket connection;
43+
private final transient WebSocket connection;
4444

4545
/**
4646
* The IOException

src/main/java/org/java_websocket/extensions/ExtensionRequestData.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55

66
public class ExtensionRequestData {
77

8-
public static String EMPTY_VALUE = "";
8+
public static final String EMPTY_VALUE = "";
99

1010
private Map<String, String> extensionParameters;
1111
private String extensionName;
1212

1313
private ExtensionRequestData() {
14-
extensionParameters = new LinkedHashMap<String, String>();
14+
extensionParameters = new LinkedHashMap<>();
1515
}
1616

1717
public static ExtensionRequestData parseExtensionRequest(String extensionRequest) {

src/main/java/org/java_websocket/extensions/permessage_deflate/PerMessageDeflateExtension.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class PerMessageDeflateExtension extends CompressionExtension {
4949

5050
// For WebSocketServers, this variable holds the extension parameters that the peer client has requested.
5151
// For WebSocketClients, this variable holds the extension parameters that client himself has requested.
52-
private Map<String, String> requestedParameters = new LinkedHashMap<String, String>();
52+
private Map<String, String> requestedParameters = new LinkedHashMap<>();
5353

5454
private Inflater inflater = new Inflater(true);
5555
private Deflater deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, true);
@@ -71,28 +71,38 @@ public void setDeflater(Deflater deflater) {
7171
}
7272

7373
/**
74-
* @return serverNoContextTakeover
74+
* Access the "server_no_context_takeover" extension parameter
75+
*
76+
* @see <a href="https://tools.ietf.org/html/rfc7692#section-7.1.1.1">The "server_no_context_takeover" Extension Parameter</a>
77+
* @return serverNoContextTakeover is the server no context parameter active
7578
*/
7679
public boolean isServerNoContextTakeover() {
7780
return serverNoContextTakeover;
7881
}
7982

8083
/**
81-
* @param serverNoContextTakeover
84+
* Setter for the "server_no_context_takeover" extension parameter
85+
* @see <a href="https://tools.ietf.org/html/rfc7692#section-7.1.1.1">The "server_no_context_takeover" Extension Parameter</a>
86+
* @param serverNoContextTakeover set the server no context parameter
8287
*/
8388
public void setServerNoContextTakeover(boolean serverNoContextTakeover) {
8489
this.serverNoContextTakeover = serverNoContextTakeover;
8590
}
8691

8792
/**
88-
* @return clientNoContextTakeover
93+
* Access the "client_no_context_takeover" extension parameter
94+
*
95+
* @see <a href="https://tools.ietf.org/html/rfc7692#section-7.1.1.2">The "client_no_context_takeover" Extension Parameter</a>
96+
* @return clientNoContextTakeover is the client no context parameter active
8997
*/
9098
public boolean isClientNoContextTakeover() {
9199
return clientNoContextTakeover;
92100
}
93101

94102
/**
95-
* @param clientNoContextTakeover
103+
* Setter for the "client_no_context_takeover" extension parameter
104+
* @see <a href="https://tools.ietf.org/html/rfc7692#section-7.1.1.2">The "client_no_context_takeover" Extension Parameter</a>
105+
* @param clientNoContextTakeover set the client no context parameter
96106
*/
97107
public void setClientNoContextTakeover(boolean clientNoContextTakeover) {
98108
this.clientNoContextTakeover = clientNoContextTakeover;
@@ -224,7 +234,7 @@ public void encodeFrame(Framedata inputFrame) {
224234
* @param data the bytes of data
225235
* @return true if the data is OK
226236
*/
227-
private boolean endsWithTail(byte[] data) {
237+
private static boolean endsWithTail(byte[] data) {
228238
if (data.length < 4) {
229239
return false;
230240
}

src/main/java/org/java_websocket/handshake/HandshakedataImpl1.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public class HandshakedataImpl1 implements HandshakeBuilder {
4848
* Constructor for handshake implementation
4949
*/
5050
public HandshakedataImpl1() {
51-
map = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
51+
map = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
5252
}
5353

5454
@Override

src/main/java/org/java_websocket/server/DefaultSSLWebSocketServerFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public ByteChannel wrapChannel(SocketChannel channel, SelectionKey key) throws I
6868
* We remove TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 from the enabled ciphers since it is just available when you patch your java installation directly.
6969
* E.g. firefox requests this cipher and this causes some dcs/instable connections
7070
*/
71-
List<String> ciphers = new ArrayList<String>(Arrays.asList(e.getEnabledCipherSuites()));
71+
List<String> ciphers = new ArrayList<>(Arrays.asList(e.getEnabledCipherSuites()));
7272
ciphers.remove("TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256");
7373
e.setEnabledCipherSuites(ciphers.toArray(new String[ciphers.size()]));
7474
e.setUseClientMode(false);

0 commit comments

Comments
 (0)