Skip to content

Commit

Permalink
Issue #4148 - config for jetty specific settings with javax WebSockets (
Browse files Browse the repository at this point in the history
#4295)

* Issue #4148 - config for jetty specific settings with javax WebSockets

* Make every jetty EndpointConfig implementation a wrapper
* Remove BasicEndpointConfig and use Client or Server specific ones
  • Loading branch information
lachlan-roberts authored Dec 16, 2019
1 parent 6f2ac51 commit ce6438c
Show file tree
Hide file tree
Showing 31 changed files with 648 additions and 524 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,101 +18,41 @@

package org.eclipse.jetty.websocket.javax.client;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.websocket.ClientEndpoint;
import javax.websocket.ClientEndpointConfig;
import javax.websocket.Decoder;
import javax.websocket.Encoder;
import javax.websocket.Extension;

import org.eclipse.jetty.websocket.javax.common.ClientEndpointConfigWrapper;
import org.eclipse.jetty.websocket.javax.common.InvalidWebSocketException;

public class AnnotatedClientEndpointConfig implements ClientEndpointConfig
public class AnnotatedClientEndpointConfig extends ClientEndpointConfigWrapper
{
private final ClientEndpoint annotation;
private final List<Class<? extends Decoder>> decoders;
private final List<Class<? extends Encoder>> encoders;
private final List<Extension> extensions;
private final List<String> preferredSubprotocols;
private final Configurator configurator;
private Map<String, Object> userProperties;

public AnnotatedClientEndpointConfig(ClientEndpoint anno)
{
this.annotation = anno;
this.decoders = Collections.unmodifiableList(Arrays.asList(anno.decoders()));
this.encoders = Collections.unmodifiableList(Arrays.asList(anno.encoders()));
this.preferredSubprotocols = Collections.unmodifiableList(Arrays.asList(anno.subprotocols()));

// no extensions declared in annotation
this.extensions = Collections.emptyList();
// no userProperties in annotation
this.userProperties = new HashMap<>();

if (anno.configurator() == null)
Configurator configurator;
try
{
this.configurator = EmptyConfigurator.INSTANCE;
configurator = anno.configurator().getDeclaredConstructor().newInstance();
}
else
catch (Exception e)
{
try
{
this.configurator = anno.configurator().getDeclaredConstructor().newInstance();
}
catch (Exception e)
{
StringBuilder err = new StringBuilder();
err.append("Unable to instantiate ClientEndpoint.configurator() of ");
err.append(anno.configurator().getName());
err.append(" defined as annotation in ");
err.append(anno.getClass().getName());
throw new InvalidWebSocketException(err.toString(), e);
}
StringBuilder err = new StringBuilder();
err.append("Unable to instantiate ClientEndpoint.configurator() of ");
err.append(anno.configurator().getName());
err.append(" defined as annotation in ");
err.append(anno.getClass().getName());
throw new InvalidWebSocketException(err.toString(), e);
}
}

public ClientEndpoint getAnnotation()
{
return annotation;
}

@Override
public Configurator getConfigurator()
{
return configurator;
}

@Override
public List<Class<? extends Decoder>> getDecoders()
{
return decoders;
}

@Override
public List<Class<? extends Encoder>> getEncoders()
{
return encoders;
}
ClientEndpointConfig build = Builder.create()
.encoders(List.of(anno.encoders()))
.decoders(List.of(anno.decoders()))
.preferredSubprotocols(List.of(anno.subprotocols()))
.extensions(Collections.emptyList())
.configurator(configurator)
.build();

@Override
public List<Extension> getExtensions()
{
return extensions;
}

@Override
public List<String> getPreferredSubprotocols()
{
return preferredSubprotocols;
}

@Override
public Map<String, Object> getUserProperties()
{
return userProperties;
init(build);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// ========================================================================
// Copyright (c) 1995-2019 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//

package org.eclipse.jetty.websocket.javax.client;

import org.eclipse.jetty.websocket.javax.common.ClientEndpointConfigWrapper;

public class BasicClientEndpointConfig extends ClientEndpointConfigWrapper
{
public BasicClientEndpointConfig()
{
init(Builder.create().configurator(EmptyConfigurator.INSTANCE).build());
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -202,38 +202,28 @@ private Session connect(ConfiguredEndpoint configuredEndpoint, URI destURI) thro
@Override
public Session connectToServer(final Class<? extends Endpoint> endpointClass, final ClientEndpointConfig config, URI path) throws IOException
{
ClientEndpointConfig clientEndpointConfig = config;
if (clientEndpointConfig == null)
{
clientEndpointConfig = new EmptyClientEndpointConfig();
}
ConfiguredEndpoint instance = newConfiguredEndpoint(endpointClass, clientEndpointConfig);
ConfiguredEndpoint instance = newConfiguredEndpoint(endpointClass, config);
return connect(instance, path);
}

@Override
public Session connectToServer(final Class<?> annotatedEndpointClass, final URI path) throws IOException
{
ConfiguredEndpoint instance = newConfiguredEndpoint(annotatedEndpointClass, new EmptyClientEndpointConfig());
ConfiguredEndpoint instance = newConfiguredEndpoint(annotatedEndpointClass, new BasicClientEndpointConfig());
return connect(instance, path);
}

@Override
public Session connectToServer(final Endpoint endpoint, final ClientEndpointConfig config, final URI path) throws DeploymentException, IOException
{
ClientEndpointConfig clientEndpointConfig = config;
if (clientEndpointConfig == null)
{
clientEndpointConfig = new EmptyClientEndpointConfig();
}
ConfiguredEndpoint instance = newConfiguredEndpoint(endpoint, clientEndpointConfig);
ConfiguredEndpoint instance = newConfiguredEndpoint(endpoint, config);
return connect(instance, path);
}

@Override
public Session connectToServer(Object endpoint, URI path) throws DeploymentException, IOException
{
ConfiguredEndpoint instance = newConfiguredEndpoint(endpoint, new EmptyClientEndpointConfig());
ConfiguredEndpoint instance = newConfiguredEndpoint(endpoint, new BasicClientEndpointConfig());
return connect(instance, path);
}

Expand Down Expand Up @@ -261,25 +251,13 @@ private ConfiguredEndpoint newConfiguredEndpoint(Class<?> endpointClass, Endpoin
}
}

public ConfiguredEndpoint newConfiguredEndpoint(Object endpoint, EndpointConfig providedConfig) throws DeploymentException
private ConfiguredEndpoint newConfiguredEndpoint(Object endpoint, EndpointConfig providedConfig) throws DeploymentException
{
EndpointConfig config = providedConfig;

if (config == null)
{
config = newEmptyConfig(endpoint);
}

config = readAnnotatedConfig(endpoint, config);

EndpointConfig config = readAnnotatedConfig(endpoint, providedConfig != null
? providedConfig : new BasicClientEndpointConfig());
return new ConfiguredEndpoint(endpoint, config);
}

protected EndpointConfig newEmptyConfig(Object endpoint)
{
return new EmptyClientEndpointConfig();
}

protected EndpointConfig readAnnotatedConfig(Object endpoint, EndpointConfig config) throws DeploymentException
{
ClientEndpoint anno = endpoint.getClass().getAnnotation(ClientEndpoint.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ public JavaxWebSocketClientFrameHandlerFactory(JavaxWebSocketContainer container
super(container, InvokerUtils.PARAM_IDENTITY);
}

@Override
public EndpointConfig newDefaultEndpointConfig(Class<?> endpointClass, String path)
{
return new BasicClientEndpointConfig();
}

@Override
public JavaxWebSocketFrameHandlerMetadata createMetadata(Class<?> endpointClass, EndpointConfig endpointConfig)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//
// ========================================================================
// Copyright (c) 1995-2019 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//

package org.eclipse.jetty.websocket.javax.common;

import java.util.List;
import javax.websocket.ClientEndpointConfig;
import javax.websocket.Extension;

public class ClientEndpointConfigWrapper extends EndpointConfigWrapper implements ClientEndpointConfig
{
private ClientEndpointConfig _endpointConfig;

public ClientEndpointConfigWrapper()
{
}

public ClientEndpointConfigWrapper(ClientEndpointConfig endpointConfig)
{
init(endpointConfig);
}

public void init(ClientEndpointConfig endpointConfig)
{
_endpointConfig = endpointConfig;
super.init(endpointConfig);
}

@Override
public List<String> getPreferredSubprotocols()
{
return _endpointConfig.getPreferredSubprotocols();
}

@Override
public List<Extension> getExtensions()
{
return _endpointConfig.getExtensions();
}

@Override
public Configurator getConfigurator()
{
return _endpointConfig.getConfigurator();
}
}
Loading

0 comments on commit ce6438c

Please sign in to comment.