use of io.undertow.websockets.core.WebSocketChannel in project undertow by undertow-io.
the class ServerWebSocketContainer method doUpgrade.
public void doUpgrade(HttpServletRequest request, HttpServletResponse response, final ServerEndpointConfig sec, Map<String, String> pathParams) throws ServletException, IOException {
ServerEndpointConfig.Configurator configurator = sec.getConfigurator();
try {
EncodingFactory encodingFactory = EncodingFactory.createFactory(classIntrospecter, sec.getDecoders(), sec.getEncoders());
PathTemplate pt = PathTemplate.create(sec.getPath());
InstanceFactory<?> instanceFactory = null;
try {
instanceFactory = classIntrospecter.createInstanceFactory(sec.getEndpointClass());
} catch (Exception e) {
//so it is possible that this is still valid if a custom configurator is in use
if (configurator == null || configurator.getClass() == ServerEndpointConfig.Configurator.class) {
throw JsrWebSocketMessages.MESSAGES.couldNotDeploy(e);
} else {
instanceFactory = new InstanceFactory<Object>() {
@Override
public InstanceHandle<Object> createInstance() throws InstantiationException {
throw JsrWebSocketMessages.MESSAGES.endpointDoesNotHaveAppropriateConstructor(sec.getEndpointClass());
}
};
}
}
if (configurator == null) {
configurator = DefaultContainerConfigurator.INSTANCE;
}
ServerEndpointConfig config = ServerEndpointConfig.Builder.create(sec.getEndpointClass(), sec.getPath()).decoders(sec.getDecoders()).encoders(sec.getEncoders()).subprotocols(sec.getSubprotocols()).extensions(sec.getExtensions()).configurator(configurator).build();
AnnotatedEndpointFactory annotatedEndpointFactory = null;
if (!Endpoint.class.isAssignableFrom(sec.getEndpointClass())) {
annotatedEndpointFactory = AnnotatedEndpointFactory.create(sec.getEndpointClass(), encodingFactory, pt.getParameterNames());
}
ConfiguredServerEndpoint confguredServerEndpoint;
if (annotatedEndpointFactory == null) {
confguredServerEndpoint = new ConfiguredServerEndpoint(config, instanceFactory, null, encodingFactory);
} else {
confguredServerEndpoint = new ConfiguredServerEndpoint(config, instanceFactory, null, encodingFactory, annotatedEndpointFactory, installedExtensions);
}
WebSocketHandshakeHolder hand;
WebSocketDeploymentInfo info = (WebSocketDeploymentInfo) request.getServletContext().getAttribute(WebSocketDeploymentInfo.ATTRIBUTE_NAME);
if (info == null || info.getExtensions() == null) {
hand = ServerWebSocketContainer.handshakes(confguredServerEndpoint);
} else {
hand = ServerWebSocketContainer.handshakes(confguredServerEndpoint, info.getExtensions());
}
final ServletWebSocketHttpExchange facade = new ServletWebSocketHttpExchange(request, response, new HashSet<WebSocketChannel>());
Handshake handshaker = null;
for (Handshake method : hand.handshakes) {
if (method.matches(facade)) {
handshaker = method;
break;
}
}
if (handshaker != null) {
if (isClosed()) {
response.sendError(StatusCodes.SERVICE_UNAVAILABLE);
return;
}
facade.putAttachment(HandshakeUtil.PATH_PARAMS, pathParams);
final Handshake selected = handshaker;
facade.upgradeChannel(new HttpUpgradeListener() {
@Override
public void handleUpgrade(StreamConnection streamConnection, HttpServerExchange exchange) {
WebSocketChannel channel = selected.createChannel(facade, streamConnection, facade.getBufferPool());
new EndpointSessionHandler(ServerWebSocketContainer.this).onConnect(facade, channel);
}
});
handshaker.handshake(facade);
return;
}
} catch (Exception e) {
throw new ServletException(e);
}
}
use of io.undertow.websockets.core.WebSocketChannel in project undertow by undertow-io.
the class FrameHandler method onText.
@Override
protected void onText(WebSocketChannel webSocketChannel, StreamSourceFrameChannel messageChannel) throws IOException {
if (session.isSessionClosed()) {
//to bad, the channel has already been closed
//we just ignore messages that are received after we have closed, as the endpoint is no longer in a valid state to deal with them
//this this should only happen if a message was on the wire when we called close()
messageChannel.close();
return;
}
final HandlerWrapper handler = getHandler(FrameType.TEXT);
if (handler != null && handler.isPartialHandler()) {
BufferedTextMessage data = new BufferedTextMessage(false);
data.read(messageChannel, new WebSocketCallback<BufferedTextMessage>() {
@Override
public void complete(WebSocketChannel channel, BufferedTextMessage context) {
invokeTextHandler(context, handler, context.isComplete());
}
@Override
public void onError(WebSocketChannel channel, BufferedTextMessage context, Throwable throwable) {
invokeOnError(throwable);
}
});
} else {
bufferFullMessage(messageChannel);
}
}
use of io.undertow.websockets.core.WebSocketChannel in project undertow by undertow-io.
the class WebSocketServletTest method testText.
@Test
public void testText() throws Exception {
final AtomicBoolean connected = new AtomicBoolean(false);
final ServletContainer container = ServletContainer.Factory.newInstance();
DeploymentUtils.setupServlet(new ServletInfo("websocket", WebSocketServlet.class, new ImmediateInstanceFactory<Servlet>(new WebSocketServlet(new WebSocketConnectionCallback() {
@Override
public void onConnect(final WebSocketHttpExchange exchange, final WebSocketChannel channel) {
connected.set(true);
channel.getReceiveSetter().set(new AbstractReceiveListener() {
@Override
protected void onFullTextMessage(WebSocketChannel channel, BufferedTextMessage message) throws IOException {
final String string = message.getData();
if (string.equals("hello")) {
WebSockets.sendText("world", channel, null);
} else {
WebSockets.sendText(string, channel, null);
}
}
});
channel.resumeReceives();
}
}))).addMapping("/*"));
final FutureResult latch = new FutureResult();
WebSocketTestClient client = new WebSocketTestClient(io.netty.handler.codec.http.websocketx.WebSocketVersion.V13, new URI("ws://" + NetworkUtils.formatPossibleIpv6Address(DefaultServer.getHostAddress("default")) + ":" + DefaultServer.getHostPort("default") + "/servletContext/"));
client.connect();
client.send(new TextWebSocketFrame(Unpooled.copiedBuffer("hello", US_ASCII)), new FrameChecker(TextWebSocketFrame.class, "world".getBytes(US_ASCII), latch));
latch.getIoFuture().get();
client.destroy();
}
Aggregations