Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/main/java/org/java_websocket/AbstractWebSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public abstract class AbstractWebSocket extends WebSocketAdapter {
*
* @since 1.4.1
*/
private ScheduledFuture connectionLostCheckerFuture;
private ScheduledFuture<?> connectionLostCheckerFuture;

/**
* Attribute for the lost connection check interval in nanoseconds
Expand Down Expand Up @@ -126,7 +126,7 @@ public void setConnectionLostTimeout(int connectionLostTimeout) {
log.trace("Connection lost timer restarted");
//Reset all the pings
try {
ArrayList<WebSocket> connections = new ArrayList<WebSocket>(getConnections());
ArrayList<WebSocket> connections = new ArrayList<>(getConnections());
WebSocketImpl webSocketImpl;
for (WebSocket conn : connections) {
if (conn instanceof WebSocketImpl) {
Expand Down Expand Up @@ -188,14 +188,17 @@ private void restartConnectionLostTimer() {
/**
* Keep the connections in a separate list to not cause deadlocks
*/
private ArrayList<WebSocket> connections = new ArrayList<WebSocket>();
private ArrayList<WebSocket> connections = new ArrayList<>();

@Override
public void run() {
connections.clear();
try {
connections.addAll(getConnections());
long minimumPongTime = (long) (System.nanoTime() - (connectionLostTimeout * 1.5));
long minimumPongTime;
synchronized (syncConnectionLost) {
minimumPongTime = (long) (System.nanoTime() - (connectionLostTimeout * 1.5));
}
for (WebSocket conn : connections) {
executeConnectionLostDetection(conn, minimumPongTime);
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/java_websocket/WebSocketImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public WebSocketImpl(WebSocketListener listener, List<Draft> drafts) {
this.role = Role.SERVER;
// draft.copyInstance will be called when the draft is first needed
if (drafts == null || drafts.isEmpty()) {
knownDrafts = new ArrayList<Draft>();
knownDrafts = new ArrayList<>();
knownDrafts.add(new Draft_6455());
} else {
knownDrafts = drafts;
Expand All @@ -208,8 +208,8 @@ public WebSocketImpl(WebSocketListener listener, Draft draft) {
if (listener == null || (draft == null && role == Role.SERVER)) {
throw new IllegalArgumentException("parameters must not be null");
}
this.outQueue = new LinkedBlockingQueue<ByteBuffer>();
inQueue = new LinkedBlockingQueue<ByteBuffer>();
this.outQueue = new LinkedBlockingQueue<>();
inQueue = new LinkedBlockingQueue<>();
this.wsl = listener;
this.role = Role.CLIENT;
if (draft != null) {
Expand Down Expand Up @@ -666,7 +666,7 @@ private void send(Collection<Framedata> frames) {
if (frames == null) {
throw new IllegalArgumentException();
}
ArrayList<ByteBuffer> outgoingFrames = new ArrayList<ByteBuffer>();
ArrayList<ByteBuffer> outgoingFrames = new ArrayList<>();
for (Framedata f : frames) {
log.trace("send frame: {}", f);
outgoingFrames.add(draft.createBinaryFrame(f));
Expand Down
64 changes: 38 additions & 26 deletions src/main/java/org/java_websocket/client/WebSocketClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import java.net.URI;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
Expand Down Expand Up @@ -223,7 +225,7 @@ public InetAddress resolve(URI uri) throws UnknownHostException {
}
};
if (httpHeaders != null) {
headers = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
headers = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
headers.putAll(httpHeaders);
}
this.connectTimeout = connectTimeout;
Expand Down Expand Up @@ -269,7 +271,7 @@ public Socket getSocket() {
*/
public void addHeader(String key, String value) {
if (headers == null) {
headers = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
headers = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
}
headers.put(key, value);
}
Expand Down Expand Up @@ -461,19 +463,7 @@ public void sendPing() {
public void run() {
InputStream istream;
try {
boolean upgradeSocketToSSLSocket = false;
// Prioritise a proxy over a socket factory and apply the socketfactory later
if (proxy != Proxy.NO_PROXY) {
socket = new Socket(proxy);
upgradeSocketToSSLSocket = true;
} else if (socketFactory != null) {
socket = socketFactory.createSocket();
} else if (socket == null) {
socket = new Socket(proxy);
upgradeSocketToSSLSocket = true;
} else if (socket.isClosed()) {
throw new IOException();
}
boolean upgradeSocketToSSLSocket = prepareSocket();

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

// if the socket is set by others we don't apply any TLS wrapper
if (upgradeSocketToSSLSocket && "wss".equals(uri.getScheme())) {
SSLSocketFactory factory;
// Prioritise the provided socketfactory
// Helps when using web debuggers like Fiddler Classic
if (socketFactory != null && (socketFactory instanceof SSLSocketFactory)) {
factory = (SSLSocketFactory) socketFactory;
} else {
SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
sslContext.init(null, null, null);
factory = sslContext.getSocketFactory();
}
socket = factory.createSocket(socket, uri.getHost(), getPort(), true);
upgradeSocketToSSL();
}

if (socket instanceof SSLSocket) {
Expand Down Expand Up @@ -546,6 +526,38 @@ public void run() {
connectReadThread = null;
}

private void upgradeSocketToSSL()
throws NoSuchAlgorithmException, KeyManagementException, IOException {
SSLSocketFactory factory;
// Prioritise the provided socketfactory
// Helps when using web debuggers like Fiddler Classic
if (socketFactory instanceof SSLSocketFactory) {
factory = (SSLSocketFactory) socketFactory;
} else {
SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
sslContext.init(null, null, null);
factory = sslContext.getSocketFactory();
}
socket = factory.createSocket(socket, uri.getHost(), getPort(), true);
}

private boolean prepareSocket() throws IOException {
boolean upgradeSocketToSSLSocket = false;
// Prioritise a proxy over a socket factory and apply the socketfactory later
if (proxy != Proxy.NO_PROXY) {
socket = new Socket(proxy);
upgradeSocketToSSLSocket = true;
} else if (socketFactory != null) {
socket = socketFactory.createSocket();
} else if (socket == null) {
socket = new Socket(proxy);
upgradeSocketToSSLSocket = true;
} else if (socket.isClosed()) {
throw new IOException();
}
return upgradeSocketToSSLSocket;
}

/**
* Apply specific SSLParameters If you override this method make sure to always call
* super.onSetSSLParameters() to ensure the hostname validation is active
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/org/java_websocket/drafts/Draft_6455.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,10 @@ public Draft_6455(List<IExtension> inputExtensions, List<IProtocol> inputProtoco
if (inputExtensions == null || inputProtocols == null || inputMaxFrameSize < 1) {
throw new IllegalArgumentException();
}
knownExtensions = new ArrayList<IExtension>(inputExtensions.size());
knownProtocols = new ArrayList<IProtocol>(inputProtocols.size());
knownExtensions = new ArrayList<>(inputExtensions.size());
knownProtocols = new ArrayList<>(inputProtocols.size());
boolean hasDefault = false;
byteBufferList = new ArrayList<ByteBuffer>();
byteBufferList = new ArrayList<>();
for (IExtension inputExtension : inputExtensions) {
if (inputExtension.getClass().equals(DefaultExtension.class)) {
hasDefault = true;
Expand Down Expand Up @@ -442,13 +442,13 @@ public HandshakeBuilder postProcessHandshakeResponseAsServer(ClientHandshake req

@Override
public Draft copyInstance() {
ArrayList<IExtension> newExtensions = new ArrayList<IExtension>();
for (IExtension extension : getKnownExtensions()) {
newExtensions.add(extension.copyInstance());
ArrayList<IExtension> newExtensions = new ArrayList<>();
for (IExtension knownExtension : getKnownExtensions()) {
newExtensions.add(knownExtension.copyInstance());
}
ArrayList<IProtocol> newProtocols = new ArrayList<IProtocol>();
for (IProtocol protocol : getKnownProtocols()) {
newProtocols.add(protocol.copyInstance());
ArrayList<IProtocol> newProtocols = new ArrayList<>();
for (IProtocol knownProtocol : getKnownProtocols()) {
newProtocols.add(knownProtocol.copyInstance());
}
return new Draft_6455(newExtensions, newProtocols, maxFrameSize);
}
Expand Down Expand Up @@ -700,7 +700,7 @@ private int getSizeBytes(ByteBuffer mes) {
@Override
public List<Framedata> translateFrame(ByteBuffer buffer) throws InvalidDataException {
while (true) {
List<Framedata> frames = new LinkedList<Framedata>();
List<Framedata> frames = new LinkedList<>();
Framedata cur;
if (incompleteframe != null) {
// complete an incomplete frame
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public LimitExceededException() {
* constructor for a LimitExceededException
* <p>
* calling InvalidDataException with closecode TOOBIG
* @param limit the allowed size which was not enough
*/
public LimitExceededException(int limit) {
super(CloseFrame.TOOBIG);
Expand All @@ -65,6 +66,8 @@ public LimitExceededException(int limit) {
* constructor for a LimitExceededException
* <p>
* calling InvalidDataException with closecode TOOBIG
* @param s the detail message.
* @param limit the allowed size which was not enough
*/
public LimitExceededException(String s, int limit) {
super(CloseFrame.TOOBIG, s);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class WrappedIOException extends Exception {
/**
* The websocket where the IOException happened
*/
private final WebSocket connection;
private final transient WebSocket connection;

/**
* The IOException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

public class ExtensionRequestData {

public static String EMPTY_VALUE = "";
public static final String EMPTY_VALUE = "";

private Map<String, String> extensionParameters;
private String extensionName;

private ExtensionRequestData() {
extensionParameters = new LinkedHashMap<String, String>();
extensionParameters = new LinkedHashMap<>();
}

public static ExtensionRequestData parseExtensionRequest(String extensionRequest) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class PerMessageDeflateExtension extends CompressionExtension {

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

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

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

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

/**
* @return clientNoContextTakeover
* Access the "client_no_context_takeover" extension parameter
*
* @see <a href="https://tools.ietf.org/html/rfc7692#section-7.1.1.2">The "client_no_context_takeover" Extension Parameter</a>
* @return clientNoContextTakeover is the client no context parameter active
*/
public boolean isClientNoContextTakeover() {
return clientNoContextTakeover;
}

/**
* @param clientNoContextTakeover
* Setter for the "client_no_context_takeover" extension parameter
* @see <a href="https://tools.ietf.org/html/rfc7692#section-7.1.1.2">The "client_no_context_takeover" Extension Parameter</a>
* @param clientNoContextTakeover set the client no context parameter
*/
public void setClientNoContextTakeover(boolean clientNoContextTakeover) {
this.clientNoContextTakeover = clientNoContextTakeover;
Expand Down Expand Up @@ -224,7 +234,7 @@ public void encodeFrame(Framedata inputFrame) {
* @param data the bytes of data
* @return true if the data is OK
*/
private boolean endsWithTail(byte[] data) {
private static boolean endsWithTail(byte[] data) {
if (data.length < 4) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class HandshakedataImpl1 implements HandshakeBuilder {
* Constructor for handshake implementation
*/
public HandshakedataImpl1() {
map = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
map = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public ByteChannel wrapChannel(SocketChannel channel, SelectionKey key) throws I
* 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.
* E.g. firefox requests this cipher and this causes some dcs/instable connections
*/
List<String> ciphers = new ArrayList<String>(Arrays.asList(e.getEnabledCipherSuites()));
List<String> ciphers = new ArrayList<>(Arrays.asList(e.getEnabledCipherSuites()));
ciphers.remove("TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256");
e.setEnabledCipherSuites(ciphers.toArray(new String[ciphers.size()]));
e.setUseClientMode(false);
Expand Down
Loading