use of javax.websocket.server.ServerEndpointConfig in project tomcat by apache.
the class TestWsServerContainer method testDuplicatePaths_01.
@Test(expected = javax.websocket.DeploymentException.class)
public void testDuplicatePaths_01() throws Exception {
WsServerContainer sc = new WsServerContainer(new TesterServletContext());
ServerEndpointConfig configA = ServerEndpointConfig.Builder.create(Object.class, "/a/b/c").build();
ServerEndpointConfig configB = ServerEndpointConfig.Builder.create(Object.class, "/a/b/c").build();
sc.addEndpoint(configA);
sc.addEndpoint(configB);
}
use of javax.websocket.server.ServerEndpointConfig in project jetty.project by eclipse.
the class ServerContainer method getServerEndpointMetadata.
public ServerEndpointMetadata getServerEndpointMetadata(final Class<?> endpoint, final ServerEndpointConfig config) throws DeploymentException {
ServerEndpointMetadata metadata = null;
ServerEndpoint anno = endpoint.getAnnotation(ServerEndpoint.class);
if (anno != null) {
// Annotated takes precedence here
AnnotatedServerEndpointMetadata ametadata = new AnnotatedServerEndpointMetadata(this, endpoint, config);
AnnotatedEndpointScanner<ServerEndpoint, ServerEndpointConfig> scanner = new AnnotatedEndpointScanner<>(ametadata);
metadata = ametadata;
scanner.scan();
} else if (Endpoint.class.isAssignableFrom(endpoint)) {
// extends Endpoint
@SuppressWarnings("unchecked") Class<? extends Endpoint> eendpoint = (Class<? extends Endpoint>) endpoint;
metadata = new SimpleServerEndpointMetadata(eendpoint, config);
} else {
StringBuilder err = new StringBuilder();
err.append("Not a recognized websocket [");
err.append(endpoint.getName());
err.append("] does not extend @").append(ServerEndpoint.class.getName());
err.append(" or extend from ").append(Endpoint.class.getName());
throw new DeploymentException("Unable to identify as valid Endpoint: " + endpoint);
}
return metadata;
}
use of javax.websocket.server.ServerEndpointConfig in project jetty.project by eclipse.
the class ServerContainer method newClientEndpointInstance.
public EndpointInstance newClientEndpointInstance(Object endpoint, ServerEndpointConfig config, String path) {
EndpointMetadata metadata = getClientEndpointMetadata(endpoint.getClass(), config);
ServerEndpointConfig cec = config;
if (config == null) {
if (metadata instanceof AnnotatedServerEndpointMetadata) {
cec = ((AnnotatedServerEndpointMetadata) metadata).getConfig();
} else {
cec = new BasicServerEndpointConfig(this, endpoint.getClass(), path);
}
}
return new EndpointInstance(endpoint, cec, metadata);
}
use of javax.websocket.server.ServerEndpointConfig in project jetty.project by eclipse.
the class WebSocketServerContainerInitializer method onStartup.
@Override
public void onStartup(Set<Class<?>> c, ServletContext context) throws ServletException {
if (!isEnabledViaContext(context, ENABLE_KEY, true)) {
LOG.info("JSR-356 is disabled by configuration");
return;
}
ContextHandler handler = ContextHandler.getContextHandler(context);
if (handler == null) {
throw new ServletException("Not running on Jetty, JSR-356 support unavailable");
}
if (!(handler instanceof ServletContextHandler)) {
throw new ServletException("Not running in Jetty ServletContextHandler, JSR-356 support unavailable");
}
ServletContextHandler jettyContext = (ServletContextHandler) handler;
ClassLoader old = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(context.getClassLoader());
// Create the Jetty ServerContainer implementation
ServerContainer jettyContainer = configureContext(jettyContext);
// make sure context is cleaned up when the context stops
context.addListener(new ContextDestroyListener());
if (c.isEmpty()) {
if (LOG.isDebugEnabled()) {
LOG.debug("No JSR-356 annotations or interfaces discovered");
}
return;
}
if (LOG.isDebugEnabled()) {
LOG.debug("Found {} classes", c.size());
}
// Now process the incoming classes
Set<Class<? extends Endpoint>> discoveredExtendedEndpoints = new HashSet<>();
Set<Class<?>> discoveredAnnotatedEndpoints = new HashSet<>();
Set<Class<? extends ServerApplicationConfig>> serverAppConfigs = new HashSet<>();
filterClasses(c, discoveredExtendedEndpoints, discoveredAnnotatedEndpoints, serverAppConfigs);
if (LOG.isDebugEnabled()) {
LOG.debug("Discovered {} extends Endpoint classes", discoveredExtendedEndpoints.size());
LOG.debug("Discovered {} @ServerEndpoint classes", discoveredAnnotatedEndpoints.size());
LOG.debug("Discovered {} ServerApplicationConfig classes", serverAppConfigs.size());
}
// Process the server app configs to determine endpoint filtering
boolean wasFiltered = false;
Set<ServerEndpointConfig> deployableExtendedEndpointConfigs = new HashSet<>();
Set<Class<?>> deployableAnnotatedEndpoints = new HashSet<>();
for (Class<? extends ServerApplicationConfig> clazz : serverAppConfigs) {
if (LOG.isDebugEnabled()) {
LOG.debug("Found ServerApplicationConfig: {}", clazz);
}
try {
ServerApplicationConfig config = clazz.newInstance();
Set<ServerEndpointConfig> seconfigs = config.getEndpointConfigs(discoveredExtendedEndpoints);
if (seconfigs != null) {
wasFiltered = true;
deployableExtendedEndpointConfigs.addAll(seconfigs);
}
Set<Class<?>> annotatedClasses = config.getAnnotatedEndpointClasses(discoveredAnnotatedEndpoints);
if (annotatedClasses != null) {
wasFiltered = true;
deployableAnnotatedEndpoints.addAll(annotatedClasses);
}
} catch (InstantiationException | IllegalAccessException e) {
throw new ServletException("Unable to instantiate: " + clazz.getName(), e);
}
}
// Default behavior if nothing filtered
if (!wasFiltered) {
deployableAnnotatedEndpoints.addAll(discoveredAnnotatedEndpoints);
// Note: it is impossible to determine path of "extends Endpoint" discovered classes
deployableExtendedEndpointConfigs = new HashSet<>();
}
if (LOG.isDebugEnabled()) {
LOG.debug("Deploying {} ServerEndpointConfig(s)", deployableExtendedEndpointConfigs.size());
}
// Deploy what should be deployed.
for (ServerEndpointConfig config : deployableExtendedEndpointConfigs) {
try {
jettyContainer.addEndpoint(config);
} catch (DeploymentException e) {
throw new ServletException(e);
}
}
if (LOG.isDebugEnabled()) {
LOG.debug("Deploying {} @ServerEndpoint(s)", deployableAnnotatedEndpoints.size());
}
for (Class<?> annotatedClass : deployableAnnotatedEndpoints) {
try {
jettyContainer.addEndpoint(annotatedClass);
} catch (DeploymentException e) {
throw new ServletException(e);
}
}
} finally {
Thread.currentThread().setContextClassLoader(old);
}
}
use of javax.websocket.server.ServerEndpointConfig in project jetty.project by eclipse.
the class JsrCreator method createWebSocket.
@Override
public Object createWebSocket(ServletUpgradeRequest req, ServletUpgradeResponse resp) {
JsrHandshakeRequest jsrHandshakeRequest = new JsrHandshakeRequest(req);
JsrHandshakeResponse jsrHandshakeResponse = new JsrHandshakeResponse(resp);
// Get raw config, as defined when the endpoint was added to the container
ServerEndpointConfig config = metadata.getConfig();
// Establish a copy of the config, so that the UserProperties are unique
// per upgrade request.
config = new BasicServerEndpointConfig(containerScope, config);
// Bug 444617 - Expose localAddress and remoteAddress for jsr modify handshake to use
// This is being implemented as an optional set of userProperties so that
// it is not JSR api breaking. A few users on #jetty and a few from cometd
// have asked for access to this information.
Map<String, Object> userProperties = config.getUserProperties();
userProperties.put(PROP_LOCAL_ADDRESS, req.getLocalSocketAddress());
userProperties.put(PROP_REMOTE_ADDRESS, req.getRemoteSocketAddress());
userProperties.put(PROP_LOCALES, Collections.list(req.getLocales()));
// Get Configurator from config object (not guaranteed to be unique per endpoint upgrade)
ServerEndpointConfig.Configurator configurator = config.getConfigurator();
// [JSR] Step 1: check origin
if (!configurator.checkOrigin(req.getOrigin())) {
try {
resp.sendForbidden("Origin mismatch");
} catch (IOException e) {
if (LOG.isDebugEnabled())
LOG.debug("Unable to send error response", e);
}
return null;
}
// [JSR] Step 2: deal with sub protocols
List<String> supported = config.getSubprotocols();
List<String> requested = req.getSubProtocols();
String subprotocol = configurator.getNegotiatedSubprotocol(supported, requested);
if (StringUtil.isNotBlank(subprotocol)) {
resp.setAcceptedSubProtocol(subprotocol);
}
// [JSR] Step 3: deal with extensions
List<Extension> installedExtensions = new ArrayList<>();
for (String extName : extensionFactory.getAvailableExtensions().keySet()) {
installedExtensions.add(new JsrExtension(extName));
}
List<Extension> requestedExts = new ArrayList<>();
for (ExtensionConfig reqCfg : req.getExtensions()) {
requestedExts.add(new JsrExtension(reqCfg));
}
List<Extension> usedExtensions = configurator.getNegotiatedExtensions(installedExtensions, requestedExts);
List<ExtensionConfig> configs = new ArrayList<>();
if (usedExtensions != null) {
for (Extension used : usedExtensions) {
ExtensionConfig ecfg = new ExtensionConfig(used.getName());
for (Parameter param : used.getParameters()) {
ecfg.setParameter(param.getName(), param.getValue());
}
configs.add(ecfg);
}
}
resp.setExtensions(configs);
// [JSR] Step 4: build out new ServerEndpointConfig
PathSpec pathSpec = jsrHandshakeRequest.getRequestPathSpec();
if (pathSpec instanceof UriTemplatePathSpec) {
// We have a PathParam path spec
UriTemplatePathSpec wspathSpec = (UriTemplatePathSpec) pathSpec;
String requestPath = req.getRequestPath();
// Wrap the config with the path spec information
config = new PathParamServerEndpointConfig(containerScope, config, wspathSpec, requestPath);
}
// [JSR] Step 5: Call modifyHandshake
configurator.modifyHandshake(config, jsrHandshakeRequest, jsrHandshakeResponse);
try {
// [JSR] Step 6: create endpoint class
Class<?> endpointClass = config.getEndpointClass();
Object endpoint = config.getConfigurator().getEndpointInstance(endpointClass);
// This will allow CDI to see Session for injection into Endpoint classes.
return new EndpointInstance(endpoint, config, metadata);
} catch (InstantiationException e) {
if (LOG.isDebugEnabled())
LOG.debug("Unable to create websocket: " + config.getEndpointClass().getName(), e);
return null;
}
}
Aggregations