Search in sources :

Example 6 with DeploymentException

use of jakarta.websocket.DeploymentException in project tomcat by apache.

the class WsServerContainer method findMapping.

public WsMappingResult findMapping(String path) {
    // been made to use one
    if (addAllowed) {
        addAllowed = false;
    }
    // Check an exact match. Simple case as there are no templates.
    ExactPathMatch match = configExactMatchMap.get(path);
    if (match != null) {
        return new WsMappingResult(match.getConfig(), Collections.<String, String>emptyMap());
    }
    // No exact match. Need to look for template matches.
    UriTemplate pathUriTemplate = null;
    try {
        pathUriTemplate = new UriTemplate(path);
    } catch (DeploymentException e) {
        // Path is not valid so can't be matched to a WebSocketEndpoint
        return null;
    }
    // Number of segments has to match
    Integer key = Integer.valueOf(pathUriTemplate.getSegmentCount());
    ConcurrentSkipListMap<String, TemplatePathMatch> templateMatches = configTemplateMatchMap.get(key);
    if (templateMatches == null) {
        // no matches
        return null;
    }
    // List is in alphabetical order of normalised templates.
    // Correct match is the first one that matches.
    ServerEndpointConfig sec = null;
    Map<String, String> pathParams = null;
    for (TemplatePathMatch templateMatch : templateMatches.values()) {
        pathParams = templateMatch.getUriTemplate().match(pathUriTemplate);
        if (pathParams != null) {
            sec = templateMatch.getConfig();
            break;
        }
    }
    if (sec == null) {
        // No match
        return null;
    }
    return new WsMappingResult(sec, pathParams);
}
Also used : ServerEndpointConfig(jakarta.websocket.server.ServerEndpointConfig) DeploymentException(jakarta.websocket.DeploymentException)

Example 7 with DeploymentException

use of jakarta.websocket.DeploymentException in project tomcat by apache.

the class WsServerContainer method addEndpoint.

void addEndpoint(ServerEndpointConfig sec, boolean fromAnnotatedPojo) throws DeploymentException {
    if (enforceNoAddAfterHandshake && !addAllowed) {
        throw new DeploymentException(sm.getString("serverContainer.addNotAllowed"));
    }
    if (servletContext == null) {
        throw new DeploymentException(sm.getString("serverContainer.servletContextMissing"));
    }
    if (deploymentFailed) {
        throw new DeploymentException(sm.getString("serverContainer.failedDeployment", servletContext.getContextPath(), servletContext.getVirtualServerName()));
    }
    try {
        String path = sec.getPath();
        // Add method mapping to user properties
        PojoMethodMapping methodMapping = new PojoMethodMapping(sec.getEndpointClass(), sec.getDecoders(), path, getInstanceManager(Thread.currentThread().getContextClassLoader()));
        if (methodMapping.getOnClose() != null || methodMapping.getOnOpen() != null || methodMapping.getOnError() != null || methodMapping.hasMessageHandlers()) {
            sec.getUserProperties().put(org.apache.tomcat.websocket.pojo.Constants.POJO_METHOD_MAPPING_KEY, methodMapping);
        }
        UriTemplate uriTemplate = new UriTemplate(path);
        if (uriTemplate.hasParameters()) {
            Integer key = Integer.valueOf(uriTemplate.getSegmentCount());
            ConcurrentSkipListMap<String, TemplatePathMatch> templateMatches = configTemplateMatchMap.get(key);
            if (templateMatches == null) {
                // Ensure that if concurrent threads execute this block they
                // all end up using the same ConcurrentSkipListMap instance
                templateMatches = new ConcurrentSkipListMap<>();
                configTemplateMatchMap.putIfAbsent(key, templateMatches);
                templateMatches = configTemplateMatchMap.get(key);
            }
            TemplatePathMatch newMatch = new TemplatePathMatch(sec, uriTemplate, fromAnnotatedPojo);
            TemplatePathMatch oldMatch = templateMatches.putIfAbsent(uriTemplate.getNormalizedPath(), newMatch);
            if (oldMatch != null) {
                // before POJOs in WsSci#onStartup()
                if (oldMatch.isFromAnnotatedPojo() && !newMatch.isFromAnnotatedPojo() && oldMatch.getConfig().getEndpointClass() == newMatch.getConfig().getEndpointClass()) {
                    // The WebSocket spec says to ignore the new match in this case
                    templateMatches.put(path, oldMatch);
                } else {
                    // Duplicate uriTemplate;
                    throw new DeploymentException(sm.getString("serverContainer.duplicatePaths", path, sec.getEndpointClass(), sec.getEndpointClass()));
                }
            }
        } else {
            // Exact match
            ExactPathMatch newMatch = new ExactPathMatch(sec, fromAnnotatedPojo);
            ExactPathMatch oldMatch = configExactMatchMap.put(path, newMatch);
            if (oldMatch != null) {
                // before POJOs in WsSci#onStartup()
                if (oldMatch.isFromAnnotatedPojo() && !newMatch.isFromAnnotatedPojo() && oldMatch.getConfig().getEndpointClass() == newMatch.getConfig().getEndpointClass()) {
                    // The WebSocket spec says to ignore the new match in this case
                    configExactMatchMap.put(path, oldMatch);
                } else {
                    // Duplicate path mappings
                    throw new DeploymentException(sm.getString("serverContainer.duplicatePaths", path, oldMatch.getConfig().getEndpointClass(), sec.getEndpointClass()));
                }
            }
        }
        endpointsRegistered = true;
    } catch (DeploymentException de) {
        failDeployment();
        throw de;
    }
}
Also used : DeploymentException(jakarta.websocket.DeploymentException) PojoMethodMapping(org.apache.tomcat.websocket.pojo.PojoMethodMapping)

Example 8 with DeploymentException

use of jakarta.websocket.DeploymentException in project tomcat by apache.

the class PojoMethodMapping method getPathParams.

private static PojoPathParam[] getPathParams(Method m, MethodType methodType) throws DeploymentException {
    if (m == null) {
        return new PojoPathParam[0];
    }
    boolean foundThrowable = false;
    Class<?>[] types = m.getParameterTypes();
    Annotation[][] paramsAnnotations = m.getParameterAnnotations();
    PojoPathParam[] result = new PojoPathParam[types.length];
    for (int i = 0; i < types.length; i++) {
        Class<?> type = types[i];
        if (type.equals(Session.class)) {
            result[i] = new PojoPathParam(type, null);
        } else if (methodType == MethodType.ON_OPEN && type.equals(EndpointConfig.class)) {
            result[i] = new PojoPathParam(type, null);
        } else if (methodType == MethodType.ON_ERROR && type.equals(Throwable.class)) {
            foundThrowable = true;
            result[i] = new PojoPathParam(type, null);
        } else if (methodType == MethodType.ON_CLOSE && type.equals(CloseReason.class)) {
            result[i] = new PojoPathParam(type, null);
        } else {
            Annotation[] paramAnnotations = paramsAnnotations[i];
            for (Annotation paramAnnotation : paramAnnotations) {
                if (paramAnnotation.annotationType().equals(PathParam.class)) {
                    result[i] = new PojoPathParam(type, ((PathParam) paramAnnotation).value());
                    break;
                }
            }
            // Parameters without annotations are not permitted
            if (result[i] == null) {
                throw new DeploymentException(sm.getString("pojoMethodMapping.paramWithoutAnnotation", type, m.getName(), m.getClass().getName()));
            }
        }
    }
    if (methodType == MethodType.ON_ERROR && !foundThrowable) {
        throw new DeploymentException(sm.getString("pojoMethodMapping.onErrorNoThrowable", m.getName(), m.getDeclaringClass().getName()));
    }
    return result;
}
Also used : DeploymentException(jakarta.websocket.DeploymentException) PathParam(jakarta.websocket.server.PathParam) Annotation(java.lang.annotation.Annotation)

Example 9 with DeploymentException

use of jakarta.websocket.DeploymentException in project spring-framework by spring-projects.

the class AbstractTyrusRequestUpgradeStrategy method createEndpoint.

private Object createEndpoint(ServerEndpointRegistration registration, ComponentProviderService provider, WebSocketContainer container, TyrusWebSocketEngine engine) throws DeploymentException {
    DirectFieldAccessor accessor = new DirectFieldAccessor(engine);
    Object sessionListener = accessor.getPropertyValue("sessionListener");
    Object clusterContext = accessor.getPropertyValue("clusterContext");
    try {
        if (constructorWithBooleanArgument) {
            // Tyrus 1.11+
            return constructor.newInstance(registration.getEndpoint(), registration, provider, container, "/", registration.getConfigurator(), sessionListener, clusterContext, null, Boolean.TRUE);
        } else {
            return constructor.newInstance(registration.getEndpoint(), registration, provider, container, "/", registration.getConfigurator(), sessionListener, clusterContext, null);
        }
    } catch (Exception ex) {
        throw new HandshakeFailureException("Failed to register " + registration, ex);
    }
}
Also used : DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) HandshakeFailureException(org.springframework.web.socket.server.HandshakeFailureException) ServletException(jakarta.servlet.ServletException) IOException(java.io.IOException) HandshakeFailureException(org.springframework.web.socket.server.HandshakeFailureException) DeploymentException(jakarta.websocket.DeploymentException)

Example 10 with DeploymentException

use of jakarta.websocket.DeploymentException in project spring-framework by spring-projects.

the class ServerEndpointExporter method registerEndpoint.

private void registerEndpoint(Class<?> endpointClass) {
    ServerContainer serverContainer = getServerContainer();
    Assert.state(serverContainer != null, "No ServerContainer set. Most likely the server's own WebSocket ServletContainerInitializer " + "has not run yet. Was the Spring ApplicationContext refreshed through a " + "org.springframework.web.context.ContextLoaderListener, " + "i.e. after the ServletContext has been fully initialized?");
    try {
        if (logger.isDebugEnabled()) {
            logger.debug("Registering @ServerEndpoint class: " + endpointClass);
        }
        serverContainer.addEndpoint(endpointClass);
    } catch (DeploymentException ex) {
        throw new IllegalStateException("Failed to register @ServerEndpoint class: " + endpointClass, ex);
    }
}
Also used : DeploymentException(jakarta.websocket.DeploymentException) ServerContainer(jakarta.websocket.server.ServerContainer)

Aggregations

DeploymentException (jakarta.websocket.DeploymentException)17 ServerEndpointConfig (jakarta.websocket.server.ServerEndpointConfig)4 ServletException (jakarta.servlet.ServletException)3 ServerContainer (jakarta.websocket.server.ServerContainer)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 NamingException (javax.naming.NamingException)3 ClientEndpoint (jakarta.websocket.ClientEndpoint)2 Encoder (jakarta.websocket.Encoder)2 Endpoint (jakarta.websocket.Endpoint)2 Extension (jakarta.websocket.Extension)2 ServerEndpoint (jakarta.websocket.server.ServerEndpoint)2 EOFException (java.io.EOFException)2 URISyntaxException (java.net.URISyntaxException)2 List (java.util.List)2 ExecutionException (java.util.concurrent.ExecutionException)2 TimeoutException (java.util.concurrent.TimeoutException)2 SSLEngine (javax.net.ssl.SSLEngine)2 SSLException (javax.net.ssl.SSLException)2 PojoMethodMapping (org.apache.tomcat.websocket.pojo.PojoMethodMapping)2