use of io.undertow.servlet.util.ConstructorInstanceFactory in project undertow by undertow-io.
the class ServerWebSocketContainer method addEndpointInternal.
private synchronized void addEndpointInternal(final Class<?> endpoint, boolean requiresCreation) throws DeploymentException {
ServerEndpoint serverEndpoint = endpoint.getAnnotation(ServerEndpoint.class);
ClientEndpoint clientEndpoint = endpoint.getAnnotation(ClientEndpoint.class);
if (serverEndpoint != null) {
JsrWebSocketLogger.ROOT_LOGGER.addingAnnotatedServerEndpoint(endpoint, serverEndpoint.value());
final PathTemplate template = PathTemplate.create(serverEndpoint.value());
if (seenPaths.contains(template)) {
PathTemplate existing = null;
for (PathTemplate p : seenPaths) {
if (p.compareTo(template) == 0) {
existing = p;
break;
}
}
throw JsrWebSocketMessages.MESSAGES.multipleEndpointsWithOverlappingPaths(template, existing);
}
seenPaths.add(template);
Class<? extends ServerEndpointConfig.Configurator> configuratorClass = serverEndpoint.configurator();
EncodingFactory encodingFactory = EncodingFactory.createFactory(classIntrospecter, serverEndpoint.decoders(), serverEndpoint.encoders());
AnnotatedEndpointFactory annotatedEndpointFactory = AnnotatedEndpointFactory.create(endpoint, encodingFactory, template.getParameterNames());
InstanceFactory<?> instanceFactory = null;
try {
instanceFactory = classIntrospecter.createInstanceFactory(endpoint);
} catch (Exception e) {
//so it is possible that this is still valid if a custom configurator is in use
if (configuratorClass == ServerEndpointConfig.Configurator.class) {
throw JsrWebSocketMessages.MESSAGES.couldNotDeploy(e);
} else {
instanceFactory = new InstanceFactory<Object>() {
@Override
public InstanceHandle<Object> createInstance() throws InstantiationException {
throw JsrWebSocketMessages.MESSAGES.endpointDoesNotHaveAppropriateConstructor(endpoint);
}
};
}
}
ServerEndpointConfig.Configurator configurator;
if (configuratorClass != ServerEndpointConfig.Configurator.class) {
try {
configurator = classIntrospecter.createInstanceFactory(configuratorClass).createInstance().getInstance();
} catch (InstantiationException | NoSuchMethodException e) {
throw JsrWebSocketMessages.MESSAGES.couldNotDeploy(e);
}
} else {
configurator = DefaultContainerConfigurator.INSTANCE;
}
ServerEndpointConfig config = ServerEndpointConfig.Builder.create(endpoint, serverEndpoint.value()).decoders(Arrays.asList(serverEndpoint.decoders())).encoders(Arrays.asList(serverEndpoint.encoders())).subprotocols(Arrays.asList(serverEndpoint.subprotocols())).extensions(Collections.<Extension>emptyList()).configurator(configurator).build();
ConfiguredServerEndpoint confguredServerEndpoint = new ConfiguredServerEndpoint(config, instanceFactory, template, encodingFactory, annotatedEndpointFactory, installedExtensions);
configuredServerEndpoints.add(confguredServerEndpoint);
handleAddingFilterMapping();
} else if (clientEndpoint != null) {
JsrWebSocketLogger.ROOT_LOGGER.addingAnnotatedClientEndpoint(endpoint);
EncodingFactory encodingFactory = EncodingFactory.createFactory(classIntrospecter, clientEndpoint.decoders(), clientEndpoint.encoders());
InstanceFactory<?> instanceFactory;
try {
instanceFactory = classIntrospecter.createInstanceFactory(endpoint);
} catch (Exception e) {
try {
//this endpoint cannot be created by the container, the user will instantiate it
instanceFactory = new ConstructorInstanceFactory<>(endpoint.getConstructor());
} catch (NoSuchMethodException e1) {
if (requiresCreation) {
throw JsrWebSocketMessages.MESSAGES.couldNotDeploy(e);
} else {
instanceFactory = new InstanceFactory<Object>() {
@Override
public InstanceHandle<Object> createInstance() throws InstantiationException {
throw new InstantiationException();
}
};
}
}
}
AnnotatedEndpointFactory factory = AnnotatedEndpointFactory.create(endpoint, encodingFactory, Collections.<String>emptySet());
ClientEndpointConfig.Configurator configurator = null;
try {
configurator = classIntrospecter.createInstanceFactory(clientEndpoint.configurator()).createInstance().getInstance();
} catch (InstantiationException | NoSuchMethodException e) {
throw JsrWebSocketMessages.MESSAGES.couldNotDeploy(e);
}
ClientEndpointConfig config = ClientEndpointConfig.Builder.create().decoders(Arrays.asList(clientEndpoint.decoders())).encoders(Arrays.asList(clientEndpoint.encoders())).preferredSubprotocols(Arrays.asList(clientEndpoint.subprotocols())).configurator(configurator).build();
ConfiguredClientEndpoint configuredClientEndpoint = new ConfiguredClientEndpoint(config, factory, encodingFactory, instanceFactory);
clientEndpoints.put(endpoint, configuredClientEndpoint);
} else {
throw JsrWebSocketMessages.MESSAGES.classWasNotAnnotated(endpoint);
}
}
Aggregations