Skip to content

Commit

Permalink
Merge pull request #881 from jansupol/20.m1
Browse files Browse the repository at this point in the history
Merge 2.0.x into 2.1.x
  • Loading branch information
jansupol authored Jan 8, 2024
2 parents 7759196 + e02b481 commit 64b74b7
Show file tree
Hide file tree
Showing 22 changed files with 472 additions and 117 deletions.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ for easy development of WebSocket applications.Eclipse Tyrus is also
a Jakarta WebSocket 2.0 compatible implementation.

WebSocket protocol defined by IETF
provides bi-directional communication between the server and the remote host. The
provides bidirectional communication between the server and the remote host. The
pros are mainly the ability to communicate both ways, low latency and small
communication overhead. Therefore Tyrus and WebSocket in general are suitable for web
communication overhead. Therefore, Tyrus and WebSocket in general are suitable for web
applications that require sending a huge volume of relatively small messages like
online games or market ticker broadcasting.

Expand All @@ -20,6 +20,15 @@ online games or market ticker broadcasting.
Building Tyrus can be done using `mvn clean install`, but sometimes (such as for building 2.x from a tag)
`mvn clean install -Pstaging` would be required.

## Tyrus Git Branches

| branch | Jakarta Version | Tyrus Version |
|--------|---------------------------------|---------------|
| master | Java EE 8 / Jakarta EE 8 branch | Tyrus 1.x |
| 2.0.x | Jakarta EE 9 branch | Tyrus 2.0.x |
| 2.1.x | Jakarta EE 10 branch | Tyrus 2.1.x |
| 2.x | Jakarta EE 11 branch | Tyrus 2.2.x |

## Licensing

- [Eclipse Public License 2.0](https://projects.eclipse.org/license/epl-2.0)
Expand Down
3 changes: 2 additions & 1 deletion client/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -26,4 +26,5 @@

exports org.glassfish.tyrus.client;
exports org.glassfish.tyrus.client.auth;
exports org.glassfish.tyrus.client.exception;
}
83 changes: 22 additions & 61 deletions client/src/main/java/org/glassfish/tyrus/client/ClientManager.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -42,6 +42,7 @@
import jakarta.websocket.Session;
import jakarta.websocket.WebSocketContainer;

import org.glassfish.tyrus.client.exception.Exceptions;
import org.glassfish.tyrus.core.AnnotatedEndpoint;
import org.glassfish.tyrus.core.BaseContainer;
import org.glassfish.tyrus.core.ComponentProviderService;
Expand All @@ -52,6 +53,7 @@
import org.glassfish.tyrus.core.TyrusFuture;
import org.glassfish.tyrus.core.TyrusSession;
import org.glassfish.tyrus.core.Utils;
import org.glassfish.tyrus.core.collection.SupplierWithEx;
import org.glassfish.tyrus.core.monitoring.EndpointEventListener;
import org.glassfish.tyrus.spi.ClientContainer;
import org.glassfish.tyrus.spi.ClientEngine;
Expand Down Expand Up @@ -288,81 +290,46 @@ public boolean evaluate() {
}

@Override
public Session connectToServer(Class annotatedEndpointClass, URI path) throws DeploymentException, IOException {
public Session connectToServer(final Class annotatedEndpointClass, final URI path) throws DeploymentException, IOException {
if (annotatedEndpointClass.getAnnotation(ClientEndpoint.class) == null) {
throw new DeploymentException(
String.format(
"Class argument in connectToServer(Class, URI) is to be annotated endpoint class. Class "
+ "%s does not have @ClientEndpoint", annotatedEndpointClass.getName()));
}
try {
return connectToServer(annotatedEndpointClass, null, path.toString(), true).get();
} catch (InterruptedException e) {
throw new DeploymentException(e.getMessage(), e);
} catch (ExecutionException e) {
final Throwable cause = e.getCause();
if (cause instanceof DeploymentException) {
throw (DeploymentException) cause;
} else if (cause instanceof IOException) {
throw (IOException) cause;
} else {
throw new DeploymentException(cause.getMessage(), cause);
}
}
return tryCatchInterruptedExecutionEx(() -> connectToServer(annotatedEndpointClass, null, path.toString(), true));
}

@Override
public Session connectToServer(Class<? extends Endpoint> endpointClass, ClientEndpointConfig cec, URI path) throws
DeploymentException, IOException {
try {
return connectToServer(endpointClass, cec, path.toString(), true).get();
} catch (InterruptedException e) {
throw new DeploymentException(e.getMessage(), e);
} catch (ExecutionException e) {
final Throwable cause = e.getCause();
if (cause instanceof DeploymentException) {
throw (DeploymentException) cause;
} else if (cause instanceof IOException) {
throw (IOException) cause;
} else {
throw new DeploymentException(cause.getMessage(), cause);
}
}
public Session connectToServer(final Class<? extends Endpoint> endpointClass,
final ClientEndpointConfig cec,
final URI path) throws DeploymentException, IOException {
return tryCatchInterruptedExecutionEx(() -> connectToServer(endpointClass, cec, path.toString(), true));
}

@Override
public Session connectToServer(Endpoint endpointInstance, ClientEndpointConfig cec, URI path) throws
public Session connectToServer(final Endpoint endpointInstance, final ClientEndpointConfig cec, final URI path) throws
DeploymentException, IOException {
try {
return connectToServer(endpointInstance, cec, path.toString(), true).get();
} catch (InterruptedException e) {
throw new DeploymentException(e.getMessage(), e);
} catch (ExecutionException e) {
final Throwable cause = e.getCause();
if (cause instanceof DeploymentException) {
throw (DeploymentException) cause;
} else if (cause instanceof IOException) {
throw (IOException) cause;
} else {
throw new DeploymentException(cause.getMessage(), cause);
}
}
return tryCatchInterruptedExecutionEx(() -> connectToServer(endpointInstance, cec, path.toString(), true));
}

@Override
public Session connectToServer(Object obj, URI path) throws DeploymentException, IOException {
public Session connectToServer(final Object obj, final URI path) throws DeploymentException, IOException {
return tryCatchInterruptedExecutionEx(() -> connectToServer(obj, null, path.toString(), true));
}

private Session tryCatchInterruptedExecutionEx(SupplierWithEx<Future<Session>, DeploymentException> supplier)
throws DeploymentException, IOException {
try {
return connectToServer(obj, null, path.toString(), true).get();
return supplier.get().get();
} catch (InterruptedException e) {
throw new DeploymentException(e.getMessage(), e);
} catch (ExecutionException e) {
final Throwable cause = e.getCause();
if (cause instanceof DeploymentException) {
throw (DeploymentException) cause;
} else if (cause instanceof IOException) {
if (cause instanceof IOException) {
throw (IOException) cause;
} else {
throw new DeploymentException(cause.getMessage(), cause);
throw Exceptions.deploymentException(cause.getMessage(), cause);
}
}
}
Expand Down Expand Up @@ -652,11 +619,7 @@ public void onClose(TyrusSession session,
if (countedDown) {
final Throwable exception = listener.getThrowable();
if (exception != null) {
if (exception instanceof DeploymentException) {
throw (DeploymentException) exception;
} else {
throw new DeploymentException("Handshake error.", exception);
}
throw Exceptions.deploymentException("Handshake error.", exception);
}

future.setResult(listener.getSession());
Expand All @@ -669,10 +632,8 @@ public void onClose(TyrusSession session,
timeoutHandler.handleTimeout();
}
}
} catch (DeploymentException e) {
throw e;
} catch (Exception e) {
throw new DeploymentException("Handshake response not received.", e);
throw Exceptions.deploymentException("Handshake response not received.", e);
}

throw new DeploymentException("Handshake response not received.");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2023 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -65,6 +65,7 @@
import org.glassfish.tyrus.spi.ClientEngine;
import org.glassfish.tyrus.spi.Connection;
import org.glassfish.tyrus.spi.ReadHandler;
import org.glassfish.tyrus.spi.TyrusClientEndpointConfigurator;
import org.glassfish.tyrus.spi.UpgradeRequest;
import org.glassfish.tyrus.spi.UpgradeResponse;
import org.glassfish.tyrus.spi.Writer;
Expand Down Expand Up @@ -169,6 +170,9 @@ public UpgradeRequest createUpgradeRequest(TimeoutHandler timeoutHandler) {
clientHandShake.prepareRequest();

UpgradeRequest upgradeRequest = clientHandShake.getRequest();
if (TyrusClientEndpointConfigurator.class.isInstance(config.getConfigurator())) {
((TyrusClientEndpointConfigurator) config.getConfigurator()).beforeRequest(upgradeRequest);
}
config.getConfigurator().beforeRequest(upgradeRequest.getHeaders());

clientEngineState = TyrusClientEngineState.UPGRADE_REQUEST_CREATED;
Expand Down Expand Up @@ -251,6 +255,7 @@ public ClientUpgradeInfo processResponse(final UpgradeResponse upgradeResponse,
throw new IllegalArgumentException(LocalizationMessages.ARGUMENT_NOT_NULL("upgradeResponse"));
}

final ClientEngine.ClientUpgradeInfo upgradeInfo;
switch (upgradeResponse.getStatus()) {
case 101:
return handleSwitchProtocol(upgradeResponse, writer, closeListener);
Expand All @@ -262,7 +267,8 @@ public ClientUpgradeInfo processResponse(final UpgradeResponse upgradeResponse,
case 308:
return handleRedirect(upgradeResponse);
case 401:
return handleAuth(upgradeResponse);
upgradeInfo = handleAuth(upgradeResponse);
break;
case 503:

// get Retry-After header
Expand Down Expand Up @@ -293,19 +299,21 @@ public ClientUpgradeInfo processResponse(final UpgradeResponse upgradeResponse,

listener.onError(new RetryAfterException(
LocalizationMessages.HANDSHAKE_HTTP_RETRY_AFTER_MESSAGE(), delay));
return UPGRADE_INFO_FAILED;
upgradeInfo = UPGRADE_INFO_FAILED;
break;
default:
((ClientEndpointConfig) endpointWrapper.getEndpointConfig()).getConfigurator().afterResponse(upgradeResponse);

clientEngineState = TyrusClientEngineState.FAILED;
HandshakeException e = new HandshakeException(
upgradeResponse.getStatus(),
LocalizationMessages.INVALID_RESPONSE_CODE(101, upgradeResponse.getStatus()));
listener.onError(e);
redirectUriHistory.clear();
return UPGRADE_INFO_FAILED;
upgradeInfo = UPGRADE_INFO_FAILED;
break;
}

((ClientEndpointConfig) endpointWrapper.getEndpointConfig()).getConfigurator().afterResponse(upgradeResponse);
return upgradeInfo;
}

redirectUriHistory.clear();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2023, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.tyrus.client.exception;

import org.glassfish.tyrus.core.HandshakeException;

import jakarta.websocket.DeploymentException;

/**
* The {@link DeploymentException} wrapping the {@link HandshakeException} and makes the HTTP status code accessible directly.
*/
public class DeploymentHandshakeException extends DeploymentException {

public DeploymentHandshakeException(String message) {
super(message);
}

public DeploymentHandshakeException(String message, HandshakeException cause) {
super(message, cause);
}

/**
* Get the error code.
*
* @return the error code.
*/
public int getHttpStatusCode() {
return ((HandshakeException) getCause()).getHttpStatusCode();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (c) 2023, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.tyrus.client.exception;

import org.glassfish.tyrus.core.HandshakeException;

import jakarta.websocket.DeploymentException;

/**
* Converts the exceptions into more specific ones.
*/
public class Exceptions {

/**
* Get the Deployment Exception, or return the exception if of the type.
* @param message The Exception message
* @param cause The Cause Exception
* @return a Deployment exception.
*/
public static DeploymentException deploymentException(String message, Throwable cause) {
if (DeploymentException.class.isInstance(cause)) {
return (DeploymentException) cause;
} else if (HandshakeException.class.isInstance(cause)) {
return new DeploymentHandshakeException(message, (HandshakeException) cause);
} else {
return new DeploymentException(message, cause);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (c) 2023, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

/**
* Common Client Exceptions
*/
package org.glassfish.tyrus.client.exception;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -443,6 +443,11 @@ public void setReasonPhrase(String reason) {

}

@Override
public String getReasonPhrase() {
return null;
}

@Override
public Map<String, List<String>> getHeaders() {
headers.put(HandshakeResponse.SEC_WEBSOCKET_ACCEPT, Collections.singletonList(serverKey));
Expand All @@ -468,6 +473,11 @@ public void setReasonPhrase(String reason) {

}

@Override
public String getReasonPhrase() {
return null;
}

@Override
public Map<String, List<String>> getHeaders() {
return headers;
Expand Down
Loading

0 comments on commit 64b74b7

Please sign in to comment.