Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass ServerEnpointConfigWrapper to @OnOpen method #779

Merged
merged 1 commit into from
Mar 23, 2022
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
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, 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
Expand Down Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,15 @@
public class ServerEndpointConfigWrapper implements ServerEndpointConfig {

protected final ServerEndpointConfig wrapped;
private final Map<String, Object> properties;

/* package */ ServerEndpointConfigWrapper(ServerEndpointConfig wrapped) {
this(wrapped, wrapped.getUserProperties());
}

/* package */ ServerEndpointConfigWrapper(ServerEndpointConfig wrapped, Map<String, Object> properties) {
this.wrapped = wrapped;
this.properties = properties;
}

@Override
Expand Down Expand Up @@ -72,7 +78,7 @@ public List<Class<? extends Decoder>> getDecoders() {

@Override
public Map<String, Object> getUserProperties() {
return wrapped.getUserProperties();
return properties;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object> 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);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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<String> 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<String>() {
@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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ public Set<ServerEndpointConfig> getEndpointConfigs(Set<Class<? extends Endpoint

@Override
public Set<Class<?>> getAnnotatedEndpointClasses(Set<Class<?>> scanned) {
return Collections.emptySet();
return Collections.singleton(OnOpenServer.class);
}
}