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 the Server Address, Server Port, and UserProperties to Session. #776

Merged
merged 6 commits into from
Mar 10, 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) 2012, 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 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 @@ -88,136 +88,149 @@ public ServerContainer createContainer(Map<String, Object> properties) {
localProperties = new HashMap<String, Object>(properties);
}

final Integer incomingBufferSize =
Utils.getProperty(localProperties, TyrusWebSocketEngine.INCOMING_BUFFER_SIZE, Integer.class);
final ClusterContext clusterContext =
Utils.getProperty(localProperties, ClusterContext.CLUSTER_CONTEXT, ClusterContext.class);
final ApplicationEventListener applicationEventListener =
Utils.getProperty(localProperties, ApplicationEventListener.APPLICATION_EVENT_LISTENER,
ApplicationEventListener.class);
final Integer maxSessionsPerApp =
Utils.getProperty(localProperties, TyrusWebSocketEngine.MAX_SESSIONS_PER_APP, Integer.class);
final Integer maxSessionsPerRemoteAddr =
Utils.getProperty(localProperties, TyrusWebSocketEngine.MAX_SESSIONS_PER_REMOTE_ADDR, Integer.class);
final Boolean parallelBroadcastEnabled =
Utils.getProperty(localProperties, TyrusWebSocketEngine.PARALLEL_BROADCAST_ENABLED, Boolean.class);
final DebugContext.TracingType tracingType =
Utils.getProperty(localProperties, TyrusWebSocketEngine.TRACING_TYPE, DebugContext.TracingType.class,
DebugContext.TracingType.OFF);
final DebugContext.TracingThreshold tracingThreshold =
Utils.getProperty(localProperties, TyrusWebSocketEngine.TRACING_THRESHOLD,
DebugContext.TracingThreshold.class, DebugContext.TracingThreshold.TRACE);

return new TyrusServerContainer((Set<Class<?>>) null) {

private final WebSocketEngine engine =
TyrusWebSocketEngine.builder(this)
.incomingBufferSize(incomingBufferSize)
.clusterContext(clusterContext)
.applicationEventListener(applicationEventListener)
.maxSessionsPerApp(maxSessionsPerApp)
.maxSessionsPerRemoteAddr(maxSessionsPerRemoteAddr)
.parallelBroadcastEnabled(parallelBroadcastEnabled)
.tracingType(tracingType)
.tracingThreshold(tracingThreshold)
.build();

private HttpServer server;
private String contextPath;
private volatile NetworkListener listener = null;

@Override
public void register(Class<?> endpointClass) throws DeploymentException {
engine.register(endpointClass, contextPath);
}
return new TyrusGrizzlyServerContainer(localProperties);
}

@Override
public void register(ServerEndpointConfig serverEndpointConfig) throws DeploymentException {
engine.register(serverEndpointConfig, contextPath);
}
/* package */ static class TyrusGrizzlyServerContainer extends TyrusServerContainer {
private final Map<String, Object> localProperties;
private final WebSocketEngine engine;
private final ApplicationEventListener applicationEventListener;

TyrusGrizzlyServerContainer(Map<String, Object> properties) {
super((Set<Class<?>>) null);
this.localProperties = properties;

final Integer incomingBufferSize =
Utils.getProperty(localProperties, TyrusWebSocketEngine.INCOMING_BUFFER_SIZE, Integer.class);
final ClusterContext clusterContext =
Utils.getProperty(localProperties, ClusterContext.CLUSTER_CONTEXT, ClusterContext.class);
final Integer maxSessionsPerApp =
Utils.getProperty(localProperties, TyrusWebSocketEngine.MAX_SESSIONS_PER_APP, Integer.class);
final Integer maxSessionsPerRemoteAddr =
Utils.getProperty(localProperties, TyrusWebSocketEngine.MAX_SESSIONS_PER_REMOTE_ADDR, Integer.class);
final Boolean parallelBroadcastEnabled =
Utils.getProperty(localProperties, TyrusWebSocketEngine.PARALLEL_BROADCAST_ENABLED, Boolean.class);
final DebugContext.TracingType tracingType =
Utils.getProperty(localProperties, TyrusWebSocketEngine.TRACING_TYPE, DebugContext.TracingType.class,
DebugContext.TracingType.OFF);
final DebugContext.TracingThreshold tracingThreshold =
Utils.getProperty(localProperties, TyrusWebSocketEngine.TRACING_THRESHOLD,
DebugContext.TracingThreshold.class, DebugContext.TracingThreshold.TRACE);

applicationEventListener = Utils.getProperty(localProperties, ApplicationEventListener.APPLICATION_EVENT_LISTENER,
ApplicationEventListener.class);

engine = TyrusWebSocketEngine.builder(this)
.incomingBufferSize(incomingBufferSize)
.clusterContext(clusterContext)
.applicationEventListener(applicationEventListener)
.maxSessionsPerApp(maxSessionsPerApp)
.maxSessionsPerRemoteAddr(maxSessionsPerRemoteAddr)
.parallelBroadcastEnabled(parallelBroadcastEnabled)
.tracingType(tracingType)
.tracingThreshold(tracingThreshold)
.build();
}

@Override
public WebSocketEngine getWebSocketEngine() {
return engine;
}
private HttpServer server;
private String contextPath;
private volatile NetworkListener listener = null;

@Override
public void start(final String rootPath, int port) throws IOException, DeploymentException {
contextPath = rootPath;
server = new HttpServer();
final ServerConfiguration config = server.getServerConfiguration();

listener = new NetworkListener("grizzly", "0.0.0.0", port);
server.addListener(listener);

// server = HttpServer.createSimpleServer(rootPath, port);
ThreadPoolConfig workerThreadPoolConfig =
Utils.getProperty(localProperties, WORKER_THREAD_POOL_CONFIG, ThreadPoolConfig.class);
ThreadPoolConfig selectorThreadPoolConfig =
Utils.getProperty(localProperties, SELECTOR_THREAD_POOL_CONFIG, ThreadPoolConfig.class);

// TYRUS-287: configurable server thread pools
if (workerThreadPoolConfig != null || selectorThreadPoolConfig != null) {
TCPNIOTransportBuilder transportBuilder = TCPNIOTransportBuilder.newInstance();
if (workerThreadPoolConfig != null) {
transportBuilder.setWorkerThreadPoolConfig(workerThreadPoolConfig);
}
if (selectorThreadPoolConfig != null) {
transportBuilder.setSelectorThreadPoolConfig(selectorThreadPoolConfig);
}
transportBuilder.setIOStrategy(WorkerThreadIOStrategy.getInstance());
server.getListener("grizzly").setTransport(transportBuilder.build());
} else {
// if no configuration is set, just update IO Strategy to worker thread strat.
server.getListener("grizzly").getTransport().setIOStrategy(WorkerThreadIOStrategy.getInstance());
}
@Override
public void register(Class<?> endpointClass) throws DeploymentException {
engine.register(endpointClass, contextPath);
}

// idle timeout set to indefinite.
server.getListener("grizzly").getKeepAlive().setIdleTimeoutInSeconds(-1);
server.getListener("grizzly").registerAddOn(new WebSocketAddOn(this, contextPath));
@Override
public void register(ServerEndpointConfig serverEndpointConfig) throws DeploymentException {
engine.register(serverEndpointConfig, contextPath);
}

final WebSocketEngine webSocketEngine = getWebSocketEngine();
@Override
public WebSocketEngine getWebSocketEngine() {
return engine;
}

final Object staticContentPath = localProperties.get(Server.STATIC_CONTENT_ROOT);
HttpHandler staticHandler = null;
if (staticContentPath != null && !staticContentPath.toString().isEmpty()) {
staticHandler = new StaticHttpHandler(staticContentPath.toString());
@Override
public void start(final String rootPath, int port) throws IOException, DeploymentException {
contextPath = rootPath;
server = new HttpServer();
final ServerConfiguration config = server.getServerConfiguration();

listener = new NetworkListener("grizzly", "0.0.0.0", port);
server.addListener(listener);

// server = HttpServer.createSimpleServer(rootPath, port);
ThreadPoolConfig workerThreadPoolConfig =
Utils.getProperty(localProperties, WORKER_THREAD_POOL_CONFIG, ThreadPoolConfig.class);
ThreadPoolConfig selectorThreadPoolConfig =
Utils.getProperty(localProperties, SELECTOR_THREAD_POOL_CONFIG, ThreadPoolConfig.class);

// TYRUS-287: configurable server thread pools
if (workerThreadPoolConfig != null || selectorThreadPoolConfig != null) {
TCPNIOTransportBuilder transportBuilder = TCPNIOTransportBuilder.newInstance();
if (workerThreadPoolConfig != null) {
transportBuilder.setWorkerThreadPoolConfig(workerThreadPoolConfig);
}
if (selectorThreadPoolConfig != null) {
transportBuilder.setSelectorThreadPoolConfig(selectorThreadPoolConfig);
}
transportBuilder.setIOStrategy(WorkerThreadIOStrategy.getInstance());
server.getListener("grizzly").setTransport(transportBuilder.build());
} else {
// if no configuration is set, just update IO Strategy to worker thread strat.
server.getListener("grizzly").getTransport().setIOStrategy(WorkerThreadIOStrategy.getInstance());
}

final Object wsadl = localProperties.get(TyrusWebSocketEngine.WSADL_SUPPORT);
// idle timeout set to indefinite.
server.getListener("grizzly").getKeepAlive().setIdleTimeoutInSeconds(-1);
server.getListener("grizzly").registerAddOn(new WebSocketAddOn(this, contextPath));

if (wsadl != null && wsadl.toString().equalsIgnoreCase("true")) { // wsadl enabled
config.addHttpHandler(new WsadlHttpHandler((TyrusWebSocketEngine) webSocketEngine, staticHandler));
} else if (staticHandler != null) { // wsadl disabled
config.addHttpHandler(staticHandler);
}
final WebSocketEngine webSocketEngine = getWebSocketEngine();

if (applicationEventListener != null) {
applicationEventListener.onApplicationInitialized(rootPath);
}
final Object staticContentPath = localProperties.get(Server.STATIC_CONTENT_ROOT);
HttpHandler staticHandler = null;
if (staticContentPath != null && !staticContentPath.toString().isEmpty()) {
staticHandler = new StaticHttpHandler(staticContentPath.toString());
}

server.start();
super.start(rootPath, port);
final Object wsadl = localProperties.get(TyrusWebSocketEngine.WSADL_SUPPORT);

if (wsadl != null && wsadl.toString().equalsIgnoreCase("true")) { // wsadl enabled
config.addHttpHandler(new WsadlHttpHandler((TyrusWebSocketEngine) webSocketEngine, staticHandler));
} else if (staticHandler != null) { // wsadl disabled
config.addHttpHandler(staticHandler);
}

@Override
public int getPort() {
if (listener != null && listener.getPort() > 0) {
return listener.getPort();
} else {
return -1;
}
if (applicationEventListener != null) {
applicationEventListener.onApplicationInitialized(rootPath);
}

@Override
public void stop() {
super.stop();
server.shutdownNow();
if (applicationEventListener != null) {
applicationEventListener.onApplicationDestroyed();
}
server.start();
super.start(rootPath, port);
}

@Override
public int getPort() {
if (listener != null && listener.getPort() > 0) {
return listener.getPort();
} else {
return -1;
}
}

@Override
public void stop() {
super.stop();
server.shutdownNow();
if (applicationEventListener != null) {
applicationEventListener.onApplicationDestroyed();
}
};
}

/* package */ Map<String, Object> getProperties() {
return localProperties;
}
}

private static class WsadlHttpHandler extends HttpHandler {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 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 All @@ -19,6 +19,7 @@
import java.io.IOException;
import java.net.URI;
import java.nio.ByteBuffer;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -221,7 +222,11 @@ private TaskProcessor getTaskProcessor(FilterChainContext ctx) {
* @return {@link NextAction} instruction for {@link FilterChain}, how it should continue the execution
*/
private NextAction handleHandshake(final FilterChainContext ctx, HttpContent content) {
final UpgradeRequest upgradeRequest = createWebSocketRequest(content);
final UpgradeRequest upgradeRequest = createWebSocketRequest(content,
GrizzlyServerContainer.TyrusGrizzlyServerContainer.class.isInstance(serverContainer)
? ((GrizzlyServerContainer.TyrusGrizzlyServerContainer) serverContainer).getProperties()
: Collections.EMPTY_MAP
);

if (!upgradeRequest.getRequestURI().getPath().startsWith(contextPath)) {
// the request is not for the deployed application
Expand Down Expand Up @@ -302,7 +307,7 @@ private void writeTraceHeaders(FilterChainContext ctx, UpgradeResponse upgradeRe
}
}

private static UpgradeRequest createWebSocketRequest(final HttpContent requestContent) {
private static UpgradeRequest createWebSocketRequest(final HttpContent requestContent, Map<String, Object> properties) {

final HttpRequestPacket requestPacket = (HttpRequestPacket) requestContent.getHttpHeader();

Expand All @@ -317,14 +322,17 @@ private static UpgradeRequest createWebSocketRequest(final HttpContent requestCo
parameterMap.put(paramName, parameters.getParameterValues(paramName));
}

final RequestContext requestContext = RequestContext.Builder.create()
.requestURI(
URI.create(requestPacket.getRequestURI()))
.queryString(requestPacket.getQueryString())
.parameterMap(parameterMap)
.secure(requestPacket.isSecure())
.remoteAddr(requestPacket.getRemoteAddress())
.build();
final RequestContext requestContext =
RequestContext.Builder.create()
.requestURI(URI.create(requestPacket.getRequestURI()))
.queryString(requestPacket.getQueryString())
.parameterMap(parameterMap)
.secure(requestPacket.isSecure())
.remoteAddr(requestPacket.getRemoteAddress())
.serverAddr(requestPacket.getLocalHost() == null ? requestPacket.getLocalAddress() : requestPacket.getLocalHost())
.serverPort(requestPacket.getLocalPort())
.tyrusProperties(properties)
.build();

for (String name : requestPacket.getHeaders().names()) {
for (String headerValue : requestPacket.getHeaders().values(name)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import jakarta.servlet.http.HttpServletResponse;
import jakarta.websocket.DeploymentException;
import jakarta.websocket.Endpoint;
import jakarta.websocket.server.HandshakeRequest;
import jakarta.websocket.server.ServerApplicationConfig;
import jakarta.websocket.server.ServerContainer;
import jakarta.websocket.server.ServerEndpoint;
Expand Down Expand Up @@ -220,7 +219,7 @@ private static class TyrusServerContainerImpl extends TyrusServerContainer {
private final WebSocketEngine engine;
private final TyrusServletUpgrade tyrusServletUpgrade;

public TyrusServerContainerImpl(Set<Class<?>> set, ApplicationEventListener applicationEventListener,
private TyrusServerContainerImpl(Set<Class<?>> set, ApplicationEventListener applicationEventListener,
Integer incomingBufferSize, Integer maxSessionsPerApp, Integer maxSessionsPerRemoteAddr,
Boolean parallelBroadcastEnabled, DebugContext.TracingType tracingType,
DebugContext.TracingThreshold tracingThreshold, String contextPath, boolean wsadlEnabled) {
Expand Down
Loading