Issue #207 - javax.websocket.DeploymentException thrown at addEndpoint now

This commit is contained in:
Joakim Erdfelt 2016-10-12 12:07:08 -07:00
parent 3a37386ae9
commit d10de87e09
2 changed files with 49 additions and 1 deletions

View File

@ -19,6 +19,7 @@
package org.eclipse.jetty.websocket.jsr356.server;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
@ -157,10 +158,56 @@ public class ServerContainer extends ClientContainer implements javax.websocket.
private void addEndpointMapping(ServerEndpointConfig config) throws DeploymentException
{
assertIsValidEndpoint(config);
JsrCreator creator = new JsrCreator(this, config, webSocketServerFactory.getExtensionFactory());
mappedCreator.addMapping(new UriTemplatePathSpec(config.getPath()), creator);
}
private void assertIsValidEndpoint(ServerEndpointConfig config) throws DeploymentException
{
EndpointFunctions endpointFunctions = null;
try
{
// Test that endpoint can be instantiated
Object endpoint = config.getEndpointClass().newInstance();
// Establish an EndpointFunctions to test validity of Endpoint declaration
AvailableEncoders availableEncoders = new AvailableEncoders(config);
AvailableDecoders availableDecoders = new AvailableDecoders(config);
Map<String, String> pathParameters = new HashMap<>();
endpointFunctions = newJsrEndpointFunction(endpoint, availableEncoders, availableDecoders, pathParameters, config);
endpointFunctions.start(); // this should trigger an exception if endpoint is invalid.
}
catch (InstantiationException e)
{
throw new DeploymentException("Unable to instantiate new instance of endpoint: " + config.getEndpointClass().getName(), e);
}
catch (IllegalAccessException e)
{
throw new DeploymentException("Unable access endpoint: " + config.getEndpointClass().getName(), e);
}
catch (Exception e)
{
throw new DeploymentException("Unable add endpoint: " + config.getEndpointClass().getName(), e);
}
finally
{
if (endpointFunctions != null)
{
try
{
// Dispose of EndpointFunctions
endpointFunctions.stop();
}
catch (Exception ignore)
{
// ignore
}
}
}
}
@Override
public EndpointFunctions newJsrEndpointFunction(Object endpoint,
AvailableEncoders availableEncoders,

View File

@ -36,6 +36,7 @@ import org.eclipse.jetty.websocket.jsr356.server.samples.InvalidOpenIntSocket;
import org.eclipse.jetty.websocket.jsr356.server.samples.InvalidOpenSessionIntSocket;
import org.eclipse.jetty.websocket.server.MappedWebSocketCreator;
import org.eclipse.jetty.websocket.server.WebSocketServerFactory;
import org.eclipse.jetty.websocket.server.WebSocketUpgradeHandlerWrapper;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
@ -81,7 +82,7 @@ public class DeploymentExceptionTest
@Test
public void testDeploy_InvalidSignature() throws Exception
{
MappedWebSocketCreator creator = new DummyCreator();
MappedWebSocketCreator creator = new WebSocketUpgradeHandlerWrapper();
WebSocketServerFactory serverFactory = new WebSocketServerFactory();
Executor executor = new QueuedThreadPool();
ServerContainer container = new ServerContainer(creator, serverFactory, executor);