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");
}
}
}
}
}
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;
}
}
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 @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());
}
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())));
}
}
Aggregations