Search in sources :

Example 1 with DeploymentException

use of javax.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)) {
                    // valid type
                    try {
                        Util.coerceToType(type, "0");
                    } catch (IllegalArgumentException iae) {
                        throw new DeploymentException(sm.getString("pojoMethodMapping.invalidPathParamType"), iae);
                    }
                    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 : Annotation(java.lang.annotation.Annotation) DeploymentException(javax.websocket.DeploymentException) PathParam(javax.websocket.server.PathParam)

Example 2 with DeploymentException

use of javax.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.
    ServerEndpointConfig sec = configExactMatchMap.get(path);
    if (sec != null) {
        return new WsMappingResult(sec, 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());
    SortedSet<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.
    Map<String, String> pathParams = null;
    for (TemplatePathMatch templateMatch : templateMatches) {
        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(javax.websocket.server.ServerEndpointConfig) DeploymentException(javax.websocket.DeploymentException)

Example 3 with DeploymentException

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

the class WsServerContainer method addEndpoint.

/**
     * Provides the equivalent of {@link #addEndpoint(ServerEndpointConfig)}
     * for publishing plain old java objects (POJOs) that have been annotated as
     * WebSocket endpoints.
     *
     * @param pojo   The annotated POJO
     */
@Override
public void addEndpoint(Class<?> pojo) throws DeploymentException {
    ServerEndpoint annotation = pojo.getAnnotation(ServerEndpoint.class);
    if (annotation == null) {
        throw new DeploymentException(sm.getString("serverContainer.missingAnnotation", pojo.getName()));
    }
    String path = annotation.value();
    // Validate encoders
    validateEncoders(annotation.encoders());
    // ServerEndpointConfig
    ServerEndpointConfig sec;
    Class<? extends Configurator> configuratorClazz = annotation.configurator();
    Configurator configurator = null;
    if (!configuratorClazz.equals(Configurator.class)) {
        try {
            configurator = annotation.configurator().newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            throw new DeploymentException(sm.getString("serverContainer.configuratorFail", annotation.configurator().getName(), pojo.getClass().getName()), e);
        }
    }
    sec = ServerEndpointConfig.Builder.create(pojo, path).decoders(Arrays.asList(annotation.decoders())).encoders(Arrays.asList(annotation.encoders())).subprotocols(Arrays.asList(annotation.subprotocols())).configurator(configurator).build();
    addEndpoint(sec);
}
Also used : ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) Configurator(javax.websocket.server.ServerEndpointConfig.Configurator) DeploymentException(javax.websocket.DeploymentException) ServerEndpoint(javax.websocket.server.ServerEndpoint)

Example 4 with DeploymentException

use of javax.websocket.DeploymentException in project che by eclipse.

the class WSocketEventBusClient method connect.

private void connect(final URI wsUri, final Collection<String> channels) throws IOException, DeploymentException {
    Future<WSClient> clientFuture = connections.get(wsUri);
    if (clientFuture == null) {
        FutureTask<WSClient> newFuture = new FutureTask<>(() -> {
            WSClient wsClient = new WSClient(wsUri, new WSocketListener(wsUri, channels));
            wsClient.connect((int) WS_CONNECTION_TIMEOUT);
            return wsClient;
        });
        clientFuture = connections.putIfAbsent(wsUri, newFuture);
        if (clientFuture == null) {
            clientFuture = newFuture;
            newFuture.run();
        }
    }
    boolean connected = false;
    try {
        // wait for connection
        clientFuture.get();
        connected = true;
    } catch (ExecutionException e) {
        final Throwable cause = e.getCause();
        if (cause instanceof Error) {
            throw (Error) cause;
        } else if (cause instanceof RuntimeException) {
            throw (RuntimeException) cause;
        } else if (cause instanceof IOException) {
            throw (IOException) cause;
        } else if (cause instanceof DeploymentException)
            throw (DeploymentException) cause;
        throw new RuntimeException(e);
    } catch (InterruptedException e) {
        LOG.info("Client interrupted " + e.getLocalizedMessage());
        Thread.currentThread().interrupt();
        throw new RuntimeException(e);
    } finally {
        if (!connected) {
            connections.remove(wsUri);
        }
    }
}
Also used : WSClient(org.everrest.websockets.client.WSClient) IOException(java.io.IOException) FutureTask(java.util.concurrent.FutureTask) DeploymentException(javax.websocket.DeploymentException) ExecutionException(java.util.concurrent.ExecutionException)

Example 5 with DeploymentException

use of javax.websocket.DeploymentException in project jetty.project by eclipse.

the class BasicEchoEndpointConfigContextListener method contextInitialized.

@Override
public void contextInitialized(ServletContextEvent sce) {
    ServerContainer container = (ServerContainer) sce.getServletContext().getAttribute(ServerContainer.class.getName());
    if (container == null)
        throw new IllegalStateException("No Websocket ServerContainer in " + sce.getServletContext());
    // Build up a configuration with a specific path
    String path = "/echo";
    ServerEndpointConfig.Builder builder = ServerEndpointConfig.Builder.create(BasicEchoEndpoint.class, path);
    try {
        container.addEndpoint(builder.build());
    } catch (DeploymentException e) {
        throw new RuntimeException("Unable to add endpoint via config file", e);
    }
}
Also used : ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) DeploymentException(javax.websocket.DeploymentException) ServerContainer(javax.websocket.server.ServerContainer)

Aggregations

DeploymentException (javax.websocket.DeploymentException)36 ServerEndpointConfig (javax.websocket.server.ServerEndpointConfig)16 ServerContainer (javax.websocket.server.ServerContainer)11 ServletException (javax.servlet.ServletException)7 WebSocketContainer (javax.websocket.WebSocketContainer)7 IOException (java.io.IOException)6 PrintWriter (java.io.PrintWriter)6 ArrayList (java.util.ArrayList)6 Endpoint (javax.websocket.Endpoint)5 ServerEndpoint (javax.websocket.server.ServerEndpoint)5 ExecutionException (java.util.concurrent.ExecutionException)3 ClientEndpoint (javax.websocket.ClientEndpoint)3 ClientEndpointConfig (javax.websocket.ClientEndpointConfig)3 Extension (javax.websocket.Extension)3 WebSocketExtension (io.undertow.websockets.WebSocketExtension)2 WebSocketChannel (io.undertow.websockets.core.WebSocketChannel)2 EOFException (java.io.EOFException)2 InetSocketAddress (java.net.InetSocketAddress)2 ClosedChannelException (java.nio.channels.ClosedChannelException)2 HashMap (java.util.HashMap)2