From 2e01bd48d8f2bcfd22fdfd0e56fe802ae94548ec Mon Sep 17 00:00:00 2001 From: jansupol Date: Tue, 22 Mar 2022 13:11:48 +0100 Subject: [PATCH] Pass ServerEnpointConfigWrapper to @OnOpen method Signed-off-by: jansupol --- .../tyrus/core/AnnotatedEndpoint.java | 8 +- .../core/ServerEndpointConfigWrapper.java | 8 +- .../tyrus/core/TyrusEndpointWrapper.java | 15 +--- .../userproperties/OnOpenConfigurator.java | 41 ++++++++++ .../userproperties/OnOpenServer.java | 50 ++++++++++++ .../userproperties/OnOpenTest.java | 77 +++++++++++++++++++ .../UserPropertiesApplication.java | 2 +- 7 files changed, 186 insertions(+), 15 deletions(-) create mode 100644 tests/e2e/standard-config/src/test/java/org/glassfish/tyrus/test/standard_config/userproperties/OnOpenConfigurator.java create mode 100644 tests/e2e/standard-config/src/test/java/org/glassfish/tyrus/test/standard_config/userproperties/OnOpenServer.java create mode 100644 tests/e2e/standard-config/src/test/java/org/glassfish/tyrus/test/standard_config/userproperties/OnOpenTest.java diff --git a/core/src/main/java/org/glassfish/tyrus/core/AnnotatedEndpoint.java b/core/src/main/java/org/glassfish/tyrus/core/AnnotatedEndpoint.java index 171961be..14f46598 100755 --- a/core/src/main/java/org/glassfish/tyrus/core/AnnotatedEndpoint.java +++ b/core/src/main/java/org/glassfish/tyrus/core/AnnotatedEndpoint.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011, 2021 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2011, 2022 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 @@ -485,7 +485,11 @@ public Object value(Session session, Object... values) { result[i] = new ParameterExtractor() { @Override public Object value(Session session, Object... values) { - return getEndpointConfig(); + final EndpointConfig endpointConfig = getEndpointConfig(); + return ServerEndpointConfig.class.isInstance(endpointConfig) + ? new ServerEndpointConfigWrapper( + (ServerEndpointConfig) endpointConfig, session.getUserProperties()) + : endpointConfig; } }; } else if (params.contains(type)) { diff --git a/core/src/main/java/org/glassfish/tyrus/core/ServerEndpointConfigWrapper.java b/core/src/main/java/org/glassfish/tyrus/core/ServerEndpointConfigWrapper.java index 73286136..717f6f90 100644 --- a/core/src/main/java/org/glassfish/tyrus/core/ServerEndpointConfigWrapper.java +++ b/core/src/main/java/org/glassfish/tyrus/core/ServerEndpointConfigWrapper.java @@ -30,9 +30,15 @@ public class ServerEndpointConfigWrapper implements ServerEndpointConfig { protected final ServerEndpointConfig wrapped; + private final Map properties; /* package */ ServerEndpointConfigWrapper(ServerEndpointConfig wrapped) { + this(wrapped, wrapped.getUserProperties()); + } + + /* package */ ServerEndpointConfigWrapper(ServerEndpointConfig wrapped, Map properties) { this.wrapped = wrapped; + this.properties = properties; } @Override @@ -72,7 +78,7 @@ public List> getDecoders() { @Override public Map getUserProperties() { - return wrapped.getUserProperties(); + return properties; } /** diff --git a/core/src/main/java/org/glassfish/tyrus/core/TyrusEndpointWrapper.java b/core/src/main/java/org/glassfish/tyrus/core/TyrusEndpointWrapper.java index 5d70bc65..3a0b2d2f 100755 --- a/core/src/main/java/org/glassfish/tyrus/core/TyrusEndpointWrapper.java +++ b/core/src/main/java/org/glassfish/tyrus/core/TyrusEndpointWrapper.java @@ -1633,19 +1633,12 @@ void onHandShakeResponse(UpgradeRequest request, UpgradeResponse response) { final ServerEndpointConfig serverEndpointConfig = (ServerEndpointConfig) configuration; final TyrusConfiguration tyrusConfiguration = ((RequestContext) request).getTyrusConfiguration(); - final ServerEndpointConfig proxiedEndpointConfig = new ServerEndpointConfigWrapper(serverEndpointConfig) { - { - tyrusConfiguration.userProperties().putAll(serverEndpointConfig.getUserProperties()); - } - - @Override - public Map getUserProperties() { - return tyrusConfiguration.userProperties(); - } - }; + tyrusConfiguration.userProperties().putAll(serverEndpointConfig.getUserProperties()); + final ServerEndpointConfig wrappedEndpointConfig = + new ServerEndpointConfigWrapper(serverEndpointConfig, tyrusConfiguration.userProperties()); serverEndpointConfig.getConfigurator() - .modifyHandshake(proxiedEndpointConfig, createHandshakeRequest(request), response); + .modifyHandshake(wrappedEndpointConfig, createHandshakeRequest(request), response); } } diff --git a/tests/e2e/standard-config/src/test/java/org/glassfish/tyrus/test/standard_config/userproperties/OnOpenConfigurator.java b/tests/e2e/standard-config/src/test/java/org/glassfish/tyrus/test/standard_config/userproperties/OnOpenConfigurator.java new file mode 100644 index 00000000..5c2a8c75 --- /dev/null +++ b/tests/e2e/standard-config/src/test/java/org/glassfish/tyrus/test/standard_config/userproperties/OnOpenConfigurator.java @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2022 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.test.standard_config.userproperties; + +import jakarta.websocket.HandshakeResponse; +import jakarta.websocket.server.HandshakeRequest; +import jakarta.websocket.server.ServerEndpointConfig; + +public class OnOpenConfigurator extends ServerEndpointConfig.Configurator { + + private static ServerEndpointConfig config; + + @Override + public void modifyHandshake(ServerEndpointConfig sec, + HandshakeRequest request, HandshakeResponse response) { + OnOpenConfigurator.setConfig(sec); + super.modifyHandshake(sec, request, response); + } + + static ServerEndpointConfig getConfig() { + return config; + } + + private static void setConfig(ServerEndpointConfig config) { + OnOpenConfigurator.config = config; + } +} diff --git a/tests/e2e/standard-config/src/test/java/org/glassfish/tyrus/test/standard_config/userproperties/OnOpenServer.java b/tests/e2e/standard-config/src/test/java/org/glassfish/tyrus/test/standard_config/userproperties/OnOpenServer.java new file mode 100644 index 00000000..800742d9 --- /dev/null +++ b/tests/e2e/standard-config/src/test/java/org/glassfish/tyrus/test/standard_config/userproperties/OnOpenServer.java @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2022 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.test.standard_config.userproperties; + +import jakarta.websocket.EndpointConfig; +import jakarta.websocket.OnError; +import jakarta.websocket.OnMessage; +import jakarta.websocket.OnOpen; +import jakarta.websocket.Session; +import jakarta.websocket.server.ServerEndpoint; +import jakarta.websocket.server.ServerEndpointConfig; + +@ServerEndpoint(value = "/onopenserver", configurator = OnOpenConfigurator.class) +public class OnOpenServer { + ServerEndpointConfig config; + + @OnMessage + public String onMessage(String msg) { + boolean ret = false; + if (msg.equals("config")) { + ret = OnOpenConfigurator.getConfig().getClass().getName() + .equals(config.getClass().getName()); + } + return String.valueOf(ret); + } + + @OnOpen + public void onOpen(EndpointConfig config) { + this.config = (ServerEndpointConfig) config; + } + + @OnError + public void onError(Session session, Throwable thr) { + thr.printStackTrace(); // Write to error log, too + } +} diff --git a/tests/e2e/standard-config/src/test/java/org/glassfish/tyrus/test/standard_config/userproperties/OnOpenTest.java b/tests/e2e/standard-config/src/test/java/org/glassfish/tyrus/test/standard_config/userproperties/OnOpenTest.java new file mode 100644 index 00000000..f12b238e --- /dev/null +++ b/tests/e2e/standard-config/src/test/java/org/glassfish/tyrus/test/standard_config/userproperties/OnOpenTest.java @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2022 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.test.standard_config.userproperties; + +import jakarta.websocket.DeploymentException; +import jakarta.websocket.Endpoint; +import jakarta.websocket.EndpointConfig; +import jakarta.websocket.MessageHandler; +import jakarta.websocket.Session; +import org.glassfish.tyrus.client.ClientManager; +import org.glassfish.tyrus.server.Server; +import org.glassfish.tyrus.test.tools.TestContainer; +import org.junit.Assert; +import org.junit.Test; + +import java.io.IOException; +import java.util.HashMap; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; + +public class OnOpenTest extends TestContainer { + @Test + public void testUserProperties() throws DeploymentException { + setServerProperties(new HashMap<>()); + + Server server = startServer(UserPropertiesApplication.class); + try { + AtomicReference receivedTestMessage = new AtomicReference<>(); + CountDownLatch messageLatch = new CountDownLatch(1); + + ClientManager client = createClient(); + client.connectToServer(new Endpoint() { + @Override + public void onOpen(Session session, EndpointConfig config) { + session.addMessageHandler(new MessageHandler.Whole() { + @Override + public void onMessage(String message) { + receivedTestMessage.set(message); + messageLatch.countDown(); + } + }); + sendMessage(session, "config"); + } + }, getURI(OnOpenServer.class)); + messageLatch.await(5, TimeUnit.SECONDS); + Assert.assertEquals("true", receivedTestMessage.get()); + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException(e.getMessage(), e); + } finally { + stopServer(server); + } + } + + private static void sendMessage(Session session, String message) { + try { + session.getBasicRemote().sendText(message); + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/tests/e2e/standard-config/src/test/java/org/glassfish/tyrus/test/standard_config/userproperties/UserPropertiesApplication.java b/tests/e2e/standard-config/src/test/java/org/glassfish/tyrus/test/standard_config/userproperties/UserPropertiesApplication.java index cbd768d6..aa9a33a2 100644 --- a/tests/e2e/standard-config/src/test/java/org/glassfish/tyrus/test/standard_config/userproperties/UserPropertiesApplication.java +++ b/tests/e2e/standard-config/src/test/java/org/glassfish/tyrus/test/standard_config/userproperties/UserPropertiesApplication.java @@ -36,6 +36,6 @@ public Set getEndpointConfigs(Set> getAnnotatedEndpointClasses(Set> scanned) { - return Collections.emptySet(); + return Collections.singleton(OnOpenServer.class); } }