use of javax.websocket.ClientEndpoint in project jetty.project by eclipse.
the class OnCloseTest method testOnCloseCall.
@Test
public void testOnCloseCall() throws Exception {
// Scan annotations
AnnotatedClientEndpointMetadata metadata = new AnnotatedClientEndpointMetadata(container, testcase.closeClass);
AnnotatedEndpointScanner<ClientEndpoint, ClientEndpointConfig> scanner = new AnnotatedEndpointScanner<>(metadata);
scanner.scan();
// Build up EventDriver
WebSocketPolicy policy = WebSocketPolicy.newClientPolicy();
ClientEndpointConfig config = metadata.getConfig();
TrackingSocket endpoint = (TrackingSocket) testcase.closeClass.newInstance();
EndpointInstance ei = new EndpointInstance(endpoint, config, metadata);
JsrEvents<ClientEndpoint, ClientEndpointConfig> jsrevents = new JsrEvents<>(metadata);
EventDriver driver = new JsrAnnotatedEventDriver(policy, ei, jsrevents);
// Execute onClose call
driver.onClose(new CloseInfo(StatusCode.NORMAL, "normal"));
// Test captured event
EventQueue<String> events = endpoint.eventQueue;
Assert.assertThat("Number of Events Captured", events.size(), is(1));
String closeEvent = events.poll();
Assert.assertThat("Close Event", closeEvent, is(testcase.expectedCloseEvent));
}
use of javax.websocket.ClientEndpoint in project jetty.project by eclipse.
the class ClientAnnotatedEndpointScanner_InvalidSignaturesTest method testScan_InvalidSignature.
@Test
public void testScan_InvalidSignature() throws DeploymentException {
AnnotatedClientEndpointMetadata metadata = new AnnotatedClientEndpointMetadata(container, pojo);
AnnotatedEndpointScanner<ClientEndpoint, ClientEndpointConfig> scanner = new AnnotatedEndpointScanner<>(metadata);
try {
scanner.scan();
Assert.fail("Expected " + InvalidSignatureException.class + " with message that references " + expectedAnnoClass + " annotation");
} catch (InvalidSignatureException e) {
if (LOG.isDebugEnabled())
LOG.debug("{}:{}", e.getClass(), e.getMessage());
Assert.assertThat("Message", e.getMessage(), containsString(expectedAnnoClass.getSimpleName()));
}
}
use of javax.websocket.ClientEndpoint in project jetty.project by eclipse.
the class JsrClientEndpointImpl method create.
@Override
public EventDriver create(Object websocket, WebSocketPolicy policy) throws DeploymentException {
if (!(websocket instanceof EndpointInstance)) {
throw new IllegalStateException(String.format("Websocket %s must be an %s", websocket.getClass().getName(), EndpointInstance.class.getName()));
}
EndpointInstance ei = (EndpointInstance) websocket;
AnnotatedClientEndpointMetadata metadata = (AnnotatedClientEndpointMetadata) ei.getMetadata();
JsrEvents<ClientEndpoint, ClientEndpointConfig> events = new JsrEvents<>(metadata);
// Handle @OnMessage maxMessageSizes
int maxBinaryMessage = getMaxMessageSize(policy.getMaxBinaryMessageSize(), metadata.maxBinaryMessageSize());
int maxTextMessage = getMaxMessageSize(policy.getMaxTextMessageSize(), metadata.maxTextMessageSize());
policy.setMaxBinaryMessageSize(maxBinaryMessage);
policy.setMaxTextMessageSize(maxTextMessage);
return new JsrAnnotatedEventDriver(policy, ei, events);
}
use of javax.websocket.ClientEndpoint in project jetty.project by eclipse.
the class JsrClientEndpointImpl method supports.
@Override
public boolean supports(Object websocket) {
if (!(websocket instanceof EndpointInstance)) {
return false;
}
EndpointInstance ei = (EndpointInstance) websocket;
Object endpoint = ei.getEndpoint();
ClientEndpoint anno = endpoint.getClass().getAnnotation(ClientEndpoint.class);
return (anno != null);
}
use of javax.websocket.ClientEndpoint in project tomcat70 by apache.
the class WsWebSocketContainer method connectToServer.
@Override
public Session connectToServer(Object pojo, URI path) throws DeploymentException {
ClientEndpoint annotation = pojo.getClass().getAnnotation(ClientEndpoint.class);
if (annotation == null) {
throw new DeploymentException(sm.getString("wsWebSocketContainer.missingAnnotation", pojo.getClass().getName()));
}
Endpoint ep = new PojoEndpointClient(pojo, Arrays.asList(annotation.decoders()));
Class<? extends ClientEndpointConfig.Configurator> configuratorClazz = annotation.configurator();
ClientEndpointConfig.Configurator configurator = null;
if (!ClientEndpointConfig.Configurator.class.equals(configuratorClazz)) {
try {
configurator = configuratorClazz.newInstance();
} catch (InstantiationException e) {
throw new DeploymentException(sm.getString("wsWebSocketContainer.defaultConfiguratorFail"), e);
} catch (IllegalAccessException e) {
throw new DeploymentException(sm.getString("wsWebSocketContainer.defaultConfiguratorFail"), e);
}
}
ClientEndpointConfig.Builder builder = ClientEndpointConfig.Builder.create();
// Avoid NPE when using RI API JAR - see BZ 56343
if (configurator != null) {
builder.configurator(configurator);
}
ClientEndpointConfig config = builder.decoders(Arrays.asList(annotation.decoders())).encoders(Arrays.asList(annotation.encoders())).preferredSubprotocols(Arrays.asList(annotation.subprotocols())).build();
return connectToServer(ep, config, path);
}
Aggregations