Search in sources :

Example 36 with ClientEndpointConfig

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);
}
Also used : Endpoint(javax.websocket.Endpoint) ClientEndpoint(javax.websocket.ClientEndpoint) PojoEndpointClient(org.apache.tomcat.websocket.pojo.PojoEndpointClient) DeploymentException(javax.websocket.DeploymentException) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) ClientEndpoint(javax.websocket.ClientEndpoint)

Example 37 with ClientEndpointConfig

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();
    }
}
Also used : KafkaJSONMessageDecoder(com.linkedin.pinot.core.realtime.impl.kafka.KafkaJSONMessageDecoder) IOException(java.io.IOException) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) Endpoint(javax.websocket.Endpoint) JSONObject(org.json.JSONObject) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) KeyedMessage(kafka.producer.KeyedMessage) EndpointConfig(javax.websocket.EndpointConfig) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) Session(javax.websocket.Session)

Example 38 with ClientEndpointConfig

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());
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) WebSocketContainer(javax.websocket.WebSocketContainer) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) EndpointConfig(javax.websocket.EndpointConfig) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) BasicText(org.apache.tomcat.websocket.TesterMessageCountClient.BasicText) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) Session(javax.websocket.Session) Test(org.junit.Test)

Example 39 with ClientEndpointConfig

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());
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) WebSocketContainer(javax.websocket.WebSocketContainer) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) URI(java.net.URI) Session(javax.websocket.Session) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 40 with ClientEndpointConfig

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();
}
Also used : BasicText(org.apache.tomcat.websocket.TesterMessageCountClient.BasicText) WebSocketContainer(javax.websocket.WebSocketContainer) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) Session(javax.websocket.Session)

Aggregations

ClientEndpointConfig (javax.websocket.ClientEndpointConfig)44 Test (org.junit.Test)26 URI (java.net.URI)25 Session (javax.websocket.Session)19 Endpoint (javax.websocket.Endpoint)13 CountDownLatch (java.util.concurrent.CountDownLatch)12 WebSocketContainer (javax.websocket.WebSocketContainer)12 ClientEndpoint (javax.websocket.ClientEndpoint)8 Context (org.apache.catalina.Context)7 DefaultServlet (org.apache.catalina.servlets.DefaultServlet)7 Tomcat (org.apache.catalina.startup.Tomcat)7 ArrayList (java.util.ArrayList)6 EndpointConfig (javax.websocket.EndpointConfig)6 Extension (javax.websocket.Extension)6 BasicText (org.apache.tomcat.websocket.TesterMessageCountClient.BasicText)6 AnnotatedClientEndpointMetadata (org.eclipse.jetty.websocket.jsr356.client.AnnotatedClientEndpointMetadata)5 List (java.util.List)4 ServerEndpoint (javax.websocket.server.ServerEndpoint)4 AnnotatedEndpointScanner (org.eclipse.jetty.websocket.jsr356.annotations.AnnotatedEndpointScanner)4 EmptyClientEndpointConfig (org.eclipse.jetty.websocket.jsr356.client.EmptyClientEndpointConfig)4