use of io.undertow.websockets.jsr.annotated.AnnotatedEndpoint in project minijax by minijax.
the class WebSocketTest method testRun.
@Test
public void testRun() throws Exception {
final Minijax minijax = createMinijax();
minijax.register(WebSocketResource.class);
minijax.start();
final MinijaxApplication application = minijax.getDefaultApplication();
final DeploymentInfo deploymentInfo = new DeploymentInfo();
MinijaxWebSocketUtils.init(deploymentInfo, application);
final WebSocketDeploymentInfo webSocketDeploymentInfo = (WebSocketDeploymentInfo) deploymentInfo.getServletContextAttributes().get(WebSocketDeploymentInfo.ATTRIBUTE_NAME);
final ServerEndpointConfig endpointConfig = webSocketDeploymentInfo.getProgramaticEndpoints().get(0);
final MinijaxWebSocketConfigurator configurator = (MinijaxWebSocketConfigurator) endpointConfig.getConfigurator();
final MockHttpServletRequest request = new MockHttpServletRequest("GET", URI.create("/echo"));
try (MinijaxRequestContext context = new MinijaxRequestContext(application, request, null)) {
configurator.modifyHandshake(endpointConfig, null, null);
final AnnotatedEndpoint endpoint = configurator.getEndpointInstance(AnnotatedEndpoint.class);
assertNotNull(endpoint);
}
}
use of io.undertow.websockets.jsr.annotated.AnnotatedEndpoint in project undertow by undertow-io.
the class EndpointSessionHandler method onConnect.
@Override
public void onConnect(WebSocketHttpExchange exchange, WebSocketChannel channel) {
ConfiguredServerEndpoint config = HandshakeUtil.getConfig(channel);
try {
if (container.isClosed()) {
// if the underlying container is closed we just reject
channel.sendClose();
channel.resumeReceives();
return;
}
InstanceFactory<?> endpointFactory = config.getEndpointFactory();
ServerEndpointConfig.Configurator configurator = config.getEndpointConfiguration().getConfigurator();
final InstanceHandle<?> instance;
DefaultContainerConfigurator.setCurrentInstanceFactory(endpointFactory);
final Object instanceFromConfigurator = configurator.getEndpointInstance(config.getEndpointConfiguration().getEndpointClass());
final InstanceHandle<?> factoryInstance = DefaultContainerConfigurator.clearCurrentInstanceFactory();
if (factoryInstance == null) {
instance = new ImmediateInstanceHandle<>(instanceFromConfigurator);
} else if (factoryInstance.getInstance() == instanceFromConfigurator) {
instance = factoryInstance;
} else {
// the default instance has been wrapped
instance = new InstanceHandle<Object>() {
@Override
public Object getInstance() {
return instanceFromConfigurator;
}
@Override
public void release() {
factoryInstance.release();
}
};
}
ServletRequestContext src = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY);
Principal principal = exchange.getAttachment(HandshakeUtil.PRINCIPAL);
if (principal == null) {
if (src.getServletRequest() instanceof HttpServletRequest) {
principal = ((HttpServletRequest) src.getServletRequest()).getUserPrincipal();
} else {
principal = src.getOriginalRequest().getUserPrincipal();
}
}
final InstanceHandle<Endpoint> endpointInstance;
if (config.getAnnotatedEndpointFactory() != null) {
final AnnotatedEndpoint annotated = config.getAnnotatedEndpointFactory().createInstance(instance);
endpointInstance = new InstanceHandle<Endpoint>() {
@Override
public Endpoint getInstance() {
return annotated;
}
@Override
public void release() {
instance.release();
}
};
} else {
endpointInstance = (InstanceHandle<Endpoint>) instance;
}
UndertowSession session = new UndertowSession(channel, URI.create(exchange.getRequestURI()), exchange.getAttachment(HandshakeUtil.PATH_PARAMS), exchange.getRequestParameters(), this, principal, endpointInstance, config.getEndpointConfiguration(), exchange.getQueryString(), config.getEncodingFactory().createEncoding(config.getEndpointConfiguration()), config, channel.getSubProtocol(), Collections.<Extension>emptyList(), null);
config.addOpenSession(session);
session.setMaxBinaryMessageBufferSize(getContainer().getDefaultMaxBinaryMessageBufferSize());
session.setMaxTextMessageBufferSize(getContainer().getDefaultMaxTextMessageBufferSize());
session.setMaxIdleTimeout(getContainer().getDefaultMaxSessionIdleTimeout());
session.getAsyncRemote().setSendTimeout(getContainer().getDefaultAsyncSendTimeout());
try {
endpointInstance.getInstance().onOpen(session, config.getEndpointConfiguration());
} catch (Exception e) {
endpointInstance.getInstance().onError(session, e);
IoUtils.safeClose(session);
}
channel.resumeReceives();
} catch (Exception e) {
JsrWebSocketLogger.REQUEST_LOGGER.endpointCreationFailed(e);
IoUtils.safeClose(channel);
}
}
Aggregations