Search in sources :

Example 1 with InvalidWebSocketException

use of org.eclipse.jetty.websocket.api.InvalidWebSocketException in project jetty.project by eclipse.

the class JsrCallable method init.

public void init(JsrSession session) {
    // Default for the session.
    // Session is an optional parameter (always)
    idxSession = findIndexForRole(Param.Role.SESSION);
    if (idxSession >= 0) {
        args[idxSession] = session;
    }
    // Optional EndpointConfig
    idxConfig = findIndexForRole(Param.Role.ENDPOINT_CONFIG);
    if (idxConfig >= 0) {
        args[idxConfig] = session.getEndpointConfig();
    }
    // Default for the path parameters
    // PathParam's are optional parameters (always)
    Map<String, String> pathParams = session.getPathParameters();
    if ((pathParams != null) && (pathParams.size() > 0)) {
        for (Param param : params) {
            if (param.role == Role.PATH_PARAM) {
                int idx = param.index;
                String rawvalue = pathParams.get(param.getPathParamName());
                Decoder decoder = session.getDecoderFactory().getDecoderFor(param.type);
                if (decoder instanceof Decoder.Text<?>) {
                    Decoder.Text<?> textDecoder = (Decoder.Text<?>) decoder;
                    try {
                        args[idx] = textDecoder.decode(rawvalue);
                    } catch (DecodeException e) {
                        session.notifyError(e);
                    }
                } else {
                    throw new InvalidWebSocketException("PathParam decoders must use Decoder.Text");
                }
            }
        }
    }
}
Also used : InvalidWebSocketException(org.eclipse.jetty.websocket.api.InvalidWebSocketException) Decoder(javax.websocket.Decoder) DecodeException(javax.websocket.DecodeException)

Example 2 with InvalidWebSocketException

use of org.eclipse.jetty.websocket.api.InvalidWebSocketException in project jetty.project by eclipse.

the class ClientContainer method getClientEndpointMetadata.

public EndpointMetadata getClientEndpointMetadata(Class<?> endpoint, EndpointConfig config) {
    synchronized (endpointClientMetadataCache) {
        EndpointMetadata metadata = endpointClientMetadataCache.get(endpoint);
        if (metadata != null) {
            return metadata;
        }
        ClientEndpoint anno = endpoint.getAnnotation(ClientEndpoint.class);
        if (anno != null) {
            // Annotated takes precedence here
            AnnotatedClientEndpointMetadata annoMetadata = new AnnotatedClientEndpointMetadata(this, endpoint);
            AnnotatedEndpointScanner<ClientEndpoint, ClientEndpointConfig> scanner = new AnnotatedEndpointScanner<>(annoMetadata);
            scanner.scan();
            metadata = annoMetadata;
        } else if (Endpoint.class.isAssignableFrom(endpoint)) {
            // extends Endpoint
            @SuppressWarnings("unchecked") Class<? extends Endpoint> eendpoint = (Class<? extends Endpoint>) endpoint;
            metadata = new SimpleEndpointMetadata(eendpoint, config);
        } else {
            StringBuilder err = new StringBuilder();
            err.append("Not a recognized websocket [");
            err.append(endpoint.getName());
            err.append("] does not extend @").append(ClientEndpoint.class.getName());
            err.append(" or extend from ").append(Endpoint.class.getName());
            throw new InvalidWebSocketException(err.toString());
        }
        endpointClientMetadataCache.put(endpoint, metadata);
        return metadata;
    }
}
Also used : SimpleEndpointMetadata(org.eclipse.jetty.websocket.jsr356.client.SimpleEndpointMetadata) InvalidWebSocketException(org.eclipse.jetty.websocket.api.InvalidWebSocketException) Endpoint(javax.websocket.Endpoint) ClientEndpoint(javax.websocket.ClientEndpoint) AnnotatedEndpointScanner(org.eclipse.jetty.websocket.jsr356.annotations.AnnotatedEndpointScanner) EmptyClientEndpointConfig(org.eclipse.jetty.websocket.jsr356.client.EmptyClientEndpointConfig) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) ClientEndpoint(javax.websocket.ClientEndpoint) AnnotatedClientEndpointMetadata(org.eclipse.jetty.websocket.jsr356.client.AnnotatedClientEndpointMetadata) SimpleEndpointMetadata(org.eclipse.jetty.websocket.jsr356.client.SimpleEndpointMetadata) AnnotatedClientEndpointMetadata(org.eclipse.jetty.websocket.jsr356.client.AnnotatedClientEndpointMetadata) EndpointMetadata(org.eclipse.jetty.websocket.jsr356.metadata.EndpointMetadata)

Example 3 with InvalidWebSocketException

use of org.eclipse.jetty.websocket.api.InvalidWebSocketException in project jetty.project by eclipse.

the class EventDriverFactory method wrap.

/**
     * Wrap the given WebSocket object instance in a suitable EventDriver
     * 
     * @param websocket
     *            the websocket instance to wrap. Must either implement {@link WebSocketListener} or be annotated with {@link WebSocket &#064;WebSocket}
     * @return appropriate EventDriver for this websocket instance.
     */
public EventDriver wrap(Object websocket) {
    if (websocket == null) {
        throw new InvalidWebSocketException("null websocket object");
    }
    for (EventDriverImpl impl : implementations) {
        if (impl.supports(websocket)) {
            try {
                return impl.create(websocket, containerScope.getPolicy().clonePolicy());
            } catch (Throwable e) {
                throw new InvalidWebSocketException("Unable to create websocket", e);
            }
        }
    }
    // Create a clear error message for the developer
    StringBuilder err = new StringBuilder();
    err.append(getClassName(websocket));
    err.append(" is not a valid WebSocket object.");
    err.append("  Object must obey one of the following rules: ");
    int len = implementations.size();
    for (int i = 0; i < len; i++) {
        EventDriverImpl impl = implementations.get(i);
        if (i > 0) {
            err.append(" or ");
        }
        err.append("\n(").append(i + 1).append(") ");
        err.append(impl.describeRule());
    }
    throw new InvalidWebSocketException(err.toString());
}
Also used : InvalidWebSocketException(org.eclipse.jetty.websocket.api.InvalidWebSocketException)

Example 4 with InvalidWebSocketException

use of org.eclipse.jetty.websocket.api.InvalidWebSocketException in project jetty.project by eclipse.

the class EventDriverFactoryTest method testBadNotASocket.

/**
     * Test Case for bad declaration (duplicate OnWebSocketBinary declarations)
     */
@Test
public void testBadNotASocket() {
    EventDriverFactory factory = new EventDriverFactory(new SimpleContainerScope(WebSocketPolicy.newClientPolicy()));
    try {
        NotASocket bad = new NotASocket();
        // Should toss exception
        factory.wrap(bad);
    } catch (InvalidWebSocketException e) {
        // Validate that we have clear error message to the developer
        Assert.assertThat(e.getMessage(), allOf(containsString(WebSocketListener.class.getSimpleName()), containsString(WebSocket.class.getSimpleName())));
    }
}
Also used : InvalidWebSocketException(org.eclipse.jetty.websocket.api.InvalidWebSocketException) NotASocket(org.eclipse.jetty.websocket.common.annotations.NotASocket) SimpleContainerScope(org.eclipse.jetty.websocket.common.scopes.SimpleContainerScope) Test(org.junit.Test)

Aggregations

InvalidWebSocketException (org.eclipse.jetty.websocket.api.InvalidWebSocketException)4 ClientEndpoint (javax.websocket.ClientEndpoint)1 ClientEndpointConfig (javax.websocket.ClientEndpointConfig)1 DecodeException (javax.websocket.DecodeException)1 Decoder (javax.websocket.Decoder)1 Endpoint (javax.websocket.Endpoint)1 NotASocket (org.eclipse.jetty.websocket.common.annotations.NotASocket)1 SimpleContainerScope (org.eclipse.jetty.websocket.common.scopes.SimpleContainerScope)1 AnnotatedEndpointScanner (org.eclipse.jetty.websocket.jsr356.annotations.AnnotatedEndpointScanner)1 AnnotatedClientEndpointMetadata (org.eclipse.jetty.websocket.jsr356.client.AnnotatedClientEndpointMetadata)1 EmptyClientEndpointConfig (org.eclipse.jetty.websocket.jsr356.client.EmptyClientEndpointConfig)1 SimpleEndpointMetadata (org.eclipse.jetty.websocket.jsr356.client.SimpleEndpointMetadata)1 EndpointMetadata (org.eclipse.jetty.websocket.jsr356.metadata.EndpointMetadata)1 Test (org.junit.Test)1