use of javax.websocket.ClientEndpointConfig in project tomcat 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 | 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);
}
use of javax.websocket.ClientEndpointConfig in project pinot by linkedin.
the class MeetupRsvpStream method run.
public void run() {
try {
final ClientEndpointConfig cec = ClientEndpointConfig.Builder.create().build();
final KafkaJSONMessageDecoder decoder = new KafkaJSONMessageDecoder();
decoder.init(null, schema, null);
client = ClientManager.createClient();
client.connectToServer(new Endpoint() {
@Override
public void onOpen(Session session, EndpointConfig config) {
try {
session.addMessageHandler(new MessageHandler.Whole<String>() {
@Override
public void onMessage(String message) {
try {
JSONObject messageJSON = new JSONObject(message);
JSONObject extracted = new JSONObject();
if (messageJSON.has("venue")) {
JSONObject venue = messageJSON.getJSONObject("venue");
extracted.put("venue_name", venue.getString("venue_name"));
}
if (messageJSON.has("event")) {
JSONObject event = messageJSON.getJSONObject("event");
extracted.put("event_name", event.getString("event_name"));
extracted.put("event_id", event.getString("event_id"));
extracted.put("event_time", event.getLong("time"));
}
if (messageJSON.has("group")) {
JSONObject group = messageJSON.getJSONObject("group");
extracted.put("group_city", group.getString("group_city"));
extracted.put("group_country", group.getString("group_country"));
extracted.put("group_id", group.getLong("group_id"));
extracted.put("group_name", group.getString("group_name"));
}
extracted.put("mtime", messageJSON.getLong("mtime"));
extracted.put("rsvp_count", 1);
if (keepPublishing) {
KeyedMessage<String, byte[]> data = new KeyedMessage<String, byte[]>("meetupRSVPEvents", extracted.toString().getBytes("UTF-8"));
producer.send(data);
}
} catch (Exception e) {
//LOGGER.error("error processing raw event ", e);
}
}
});
session.getBasicRemote().sendText("");
} catch (IOException e) {
//LOGGER.error("found an event where data did not have all the fields, don't care about for quickstart");
}
}
}, cec, new URI("ws://stream.meetup.com/2/rsvps"));
} catch (Exception e) {
//e.printStackTrace();
}
}
use of javax.websocket.ClientEndpointConfig in project tomcat by apache.
the class TestWsWebSocketContainer method testConnectToServerEndpointSSL.
@Test
public void testConnectToServerEndpointSSL() throws Exception {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
ctx.addApplicationListener(TesterEchoServer.Config.class.getName());
Tomcat.addServlet(ctx, "default", new DefaultServlet());
ctx.addServletMappingDecoded("/", "default");
TesterSupport.initSsl(tomcat);
tomcat.start();
WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
ClientEndpointConfig clientEndpointConfig = ClientEndpointConfig.Builder.create().build();
clientEndpointConfig.getUserProperties().put(org.apache.tomcat.websocket.Constants.SSL_TRUSTSTORE_PROPERTY, "test/org/apache/tomcat/util/net/ca.jks");
Session wsSession = wsContainer.connectToServer(TesterProgrammaticEndpoint.class, clientEndpointConfig, new URI("wss://" + getHostName() + ":" + getPort() + TesterEchoServer.Config.PATH_ASYNC));
CountDownLatch latch = new CountDownLatch(1);
BasicText handler = new BasicText(latch);
wsSession.addMessageHandler(handler);
wsSession.getBasicRemote().sendText(MESSAGE_STRING_1);
boolean latchResult = handler.getLatch().await(10, TimeUnit.SECONDS);
Assert.assertTrue(latchResult);
Queue<String> messages = handler.getMessages();
Assert.assertEquals(1, messages.size());
Assert.assertEquals(MESSAGE_STRING_1, messages.peek());
}
use of javax.websocket.ClientEndpointConfig in project tomcat by apache.
the class TestAsyncMessages method testAsyncTiming.
@Test
public void testAsyncTiming() throws Exception {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
ctx.addApplicationListener(TesterAsyncTiming.Config.class.getName());
Tomcat.addServlet(ctx, "default", new DefaultServlet());
ctx.addServletMappingDecoded("/", "default");
tomcat.start();
WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
ClientEndpointConfig clientEndpointConfig = ClientEndpointConfig.Builder.create().build();
Session wsSession = wsContainer.connectToServer(TesterProgrammaticEndpoint.class, clientEndpointConfig, new URI("ws://localhost:" + getPort() + TesterAsyncTiming.Config.PATH));
AsyncTimingClientHandler handler = new AsyncTimingClientHandler();
wsSession.addMessageHandler(ByteBuffer.class, handler);
wsSession.getBasicRemote().sendText("Hello");
System.out.println("Sent Hello message, waiting for data");
handler.waitForLatch();
Assert.assertFalse(handler.hasFailed());
}
use of javax.websocket.ClientEndpointConfig in project tomcat by apache.
the class TestWebSocketFrameClient method echoTester.
public void echoTester(String path) throws Exception {
WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
ClientEndpointConfig clientEndpointConfig = ClientEndpointConfig.Builder.create().build();
Session wsSession = wsContainer.connectToServer(TesterProgrammaticEndpoint.class, clientEndpointConfig, new URI("ws://localhost:" + getPort() + path));
CountDownLatch latch = new CountDownLatch(1);
BasicText handler = new BasicText(latch);
wsSession.addMessageHandler(handler);
wsSession.getBasicRemote().sendText("Hello");
boolean latchResult = handler.getLatch().await(10, TimeUnit.SECONDS);
Assert.assertTrue(latchResult);
Queue<String> messages = handler.getMessages();
Assert.assertEquals(1, messages.size());
for (String message : messages) {
Assert.assertEquals("Hello", message);
}
wsSession.close();
}
Aggregations