Skip to content

Commit

Permalink
Merge remote-tracking branch 'master' into 2.0.x
Browse files Browse the repository at this point in the history
Signed-off-by: jansupol <[email protected]>
  • Loading branch information
jansupol committed Dec 19, 2023
2 parents e7a2b24 + 5ad705f commit 37d2c7f
Show file tree
Hide file tree
Showing 12 changed files with 374 additions and 73 deletions.
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, 2023 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;
}
72 changes: 16 additions & 56 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, 2023 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 @@ -295,74 +297,38 @@ public Session connectToServer(Class annotatedEndpointClass, URI path) throws De
"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);
}
}
return tryCatchInterruptedExecutionEx(() -> connectToServer(endpointClass, cec, path.toString(), true));
}

@Override
public Session connectToServer(Endpoint endpointInstance, ClientEndpointConfig cec, 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 {
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 +618,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 +631,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
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 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 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 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) 2011, 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2023 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 @@ -48,6 +48,7 @@
import org.glassfish.tyrus.client.ClientManager;
import org.glassfish.tyrus.client.ClientProperties;
import org.glassfish.tyrus.client.SslEngineConfigurator;
import org.glassfish.tyrus.client.exception.Exceptions;
import org.glassfish.tyrus.core.TyrusFuture;
import org.glassfish.tyrus.core.Utils;
import org.glassfish.tyrus.spi.ClientEngine;
Expand Down Expand Up @@ -202,7 +203,7 @@ public class GrizzlyClientSocket {
(sharedTransport && sharedTransportTimeoutProperty != null) ? sharedTransportTimeoutProperty : 30;
this.clientEngine = clientEngine;
} catch (RuntimeException e) {
throw new DeploymentException(e.getMessage(), e);
throw Exceptions.deploymentException(e.getMessage(), e);
}

grizzlyConnector = new Callable<Void>() {
Expand All @@ -224,12 +225,10 @@ public Void call() throws Exception {
public void connect() throws DeploymentException, IOException {
try {
grizzlyConnector.call();
} catch (DeploymentException e) {
throw e;
} catch (IOException e) {
throw e;
} catch (Exception e) {
throw new DeploymentException(e.getMessage(), e);
throw Exceptions.deploymentException(e.getMessage(), e);
}
}

Expand Down Expand Up @@ -380,7 +379,7 @@ public void handleTimeout() {
throw new DeploymentException("SSL handshake has failed", e.getCause());
} catch (Exception e) {
closeTransport(privateTransport);
throw new DeploymentException(String.format("Connection to '%s' failed.", requestURI),
throw Exceptions.deploymentException(String.format("Connection to '%s' failed.", requestURI),
e.getCause());
}
}
Expand Down Expand Up @@ -410,7 +409,7 @@ public void handleTimeout() {
}
}

throw new DeploymentException("Connection failed.", exception);
throw Exceptions.deploymentException("Connection failed.", exception);
}

private static TCPNIOTransport createTransport(ThreadPoolConfig workerThreadPoolConfig,
Expand Down
Loading

0 comments on commit 37d2c7f

Please sign in to comment.