use of io.undertow.websockets.extensions.ExtensionHandshake in project undertow by undertow-io.
the class ServerWebSocketContainer method handshakes.
static WebSocketHandshakeHolder handshakes(ConfiguredServerEndpoint config, List<ExtensionHandshake> extensions) {
List<Handshake> handshakes = new ArrayList<>();
Handshake jsrHybi13Handshake = new JsrHybi13Handshake(config);
Handshake jsrHybi08Handshake = new JsrHybi08Handshake(config);
Handshake jsrHybi07Handshake = new JsrHybi07Handshake(config);
for (ExtensionHandshake extension : extensions) {
jsrHybi13Handshake.addExtension(extension);
jsrHybi08Handshake.addExtension(extension);
jsrHybi07Handshake.addExtension(extension);
}
handshakes.add(jsrHybi13Handshake);
handshakes.add(jsrHybi08Handshake);
handshakes.add(jsrHybi07Handshake);
return new WebSocketHandshakeHolder(handshakes, config);
}
use of io.undertow.websockets.extensions.ExtensionHandshake in project undertow by undertow-io.
the class Bootstrap method handleDeployment.
@Override
public void handleDeployment(DeploymentInfo deploymentInfo, ServletContext servletContext) {
WebSocketDeploymentInfo info = (WebSocketDeploymentInfo) deploymentInfo.getServletContextAttributes().get(WebSocketDeploymentInfo.ATTRIBUTE_NAME);
if (info == null) {
return;
}
Supplier<XnioWorker> worker = info.getWorker();
ByteBufferPool buffers = info.getBuffers();
if (buffers == null) {
ServerWebSocketContainer defaultContainer = UndertowContainerProvider.getDefaultContainer();
if (defaultContainer == null) {
throw JsrWebSocketLogger.ROOT_LOGGER.bufferPoolWasNullAndNoDefault();
}
JsrWebSocketLogger.ROOT_LOGGER.bufferPoolWasNull();
buffers = defaultContainer.getBufferPool();
}
final List<ThreadSetupHandler> setup = new ArrayList<>();
setup.add(new ContextClassLoaderSetupAction(deploymentInfo.getClassLoader()));
setup.addAll(deploymentInfo.getThreadSetupActions());
InetSocketAddress bind = null;
if (info.getClientBindAddress() != null) {
bind = new InetSocketAddress(info.getClientBindAddress(), 0);
}
List<Extension> extensions = new ArrayList<>();
for (ExtensionHandshake e : info.getExtensions()) {
extensions.add(new ExtensionImpl(e.getName(), Collections.emptyList()));
}
ServerWebSocketContainer container = new ServerWebSocketContainer(deploymentInfo.getClassIntrospecter(), servletContext.getClassLoader(), worker, buffers, setup, info.isDispatchToWorkerThread(), bind, info.getReconnectHandler(), extensions);
try {
for (Class<?> annotation : info.getAnnotatedEndpoints()) {
container.addEndpoint(annotation);
}
for (ServerEndpointConfig programatic : info.getProgramaticEndpoints()) {
container.addEndpoint(programatic);
}
} catch (DeploymentException e) {
throw new RuntimeException(e);
}
servletContext.setAttribute(ServerContainer.class.getName(), container);
info.containerReady(container);
SecurityActions.addContainer(deploymentInfo.getClassLoader(), container);
deploymentInfo.addListener(Servlets.listener(WebSocketListener.class));
deploymentInfo.addDeploymentCompleteListener(new ServletContextListener() {
@Override
public void contextInitialized(ServletContextEvent sce) {
container.validateDeployment();
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
}
});
}
use of io.undertow.websockets.extensions.ExtensionHandshake in project undertow by undertow-io.
the class JsrHybi13Handshake method selectedExtension.
@Override
protected List<WebSocketExtension> selectedExtension(List<WebSocketExtension> extensionList) {
List<Extension> ext = new ArrayList<>();
for (WebSocketExtension i : extensionList) {
ext.add(ExtensionImpl.create(i));
}
List<Extension> selected = HandshakeUtil.selectExtensions(config, ext);
if (selected == null) {
return Collections.emptyList();
}
Map<String, ExtensionHandshake> extensionMap = new HashMap<>();
for (ExtensionHandshake availible : availableExtensions) {
extensionMap.put(availible.getName(), availible);
}
List<WebSocketExtension> ret = new ArrayList<>();
List<ExtensionHandshake> accepted = new ArrayList<>();
for (Extension i : selected) {
ExtensionHandshake handshake = extensionMap.get(i.getName());
if (handshake == null) {
// should not happen
continue;
}
List<WebSocketExtension.Parameter> parameters = new ArrayList<>();
for (Extension.Parameter p : i.getParameters()) {
parameters.add(new WebSocketExtension.Parameter(p.getName(), p.getValue()));
}
if (!handshake.isIncompatible(accepted)) {
WebSocketExtension accept = handshake.accept(new WebSocketExtension(i.getName(), parameters));
if (accept != null) {
ret.add(accept);
accepted.add(handshake);
}
}
}
return ret;
}
use of io.undertow.websockets.extensions.ExtensionHandshake in project undertow by undertow-io.
the class Handshake method selectedExtension.
protected List<WebSocketExtension> selectedExtension(List<WebSocketExtension> extensionList) {
List<WebSocketExtension> selected = new ArrayList<>();
List<ExtensionHandshake> configured = new ArrayList<>();
for (WebSocketExtension ext : extensionList) {
for (ExtensionHandshake extHandshake : availableExtensions) {
WebSocketExtension negotiated = extHandshake.accept(ext);
if (negotiated != null && !extHandshake.isIncompatible(configured)) {
selected.add(negotiated);
configured.add(extHandshake);
}
}
}
return selected;
}
use of io.undertow.websockets.extensions.ExtensionHandshake in project undertow by undertow-io.
the class Handshake method initExtensions.
/**
* Create the {@code ExtensionFunction} list associated with the negotiated extensions defined in the exchange's response.
*
* @param exchange the exchange used to retrieve negotiated extensions
* @return a list of {@code ExtensionFunction} with the implementation of the extensions
*/
protected final List<ExtensionFunction> initExtensions(final WebSocketHttpExchange exchange) {
String extHeader = exchange.getResponseHeaders().get(Headers.SEC_WEB_SOCKET_EXTENSIONS_STRING) != null ? exchange.getResponseHeaders().get(Headers.SEC_WEB_SOCKET_EXTENSIONS_STRING).get(0) : null;
List<ExtensionFunction> negotiated = new ArrayList<>();
if (extHeader != null) {
List<WebSocketExtension> extensions = WebSocketExtension.parse(extHeader);
if (extensions != null && !extensions.isEmpty()) {
for (WebSocketExtension ext : extensions) {
for (ExtensionHandshake extHandshake : availableExtensions) {
if (extHandshake.getName().equals(ext.getName())) {
negotiated.add(extHandshake.create());
}
}
}
}
}
return negotiated;
}
Aggregations