mirror of
https://github.com/jetty/jetty.project.git
synced 2025-02-28 19:09:10 +00:00
JSR-356 - allow WebSocketContainer.connectToServer(Class<?>, URI) to work with classes that extend Endpoint (not just annotated classes)
This commit is contained in:
parent
feca821522
commit
9959bbd946
@ -26,6 +26,7 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import javax.websocket.ClientEndpoint;
|
||||
import javax.websocket.ClientEndpointConfig;
|
||||
import javax.websocket.DeploymentException;
|
||||
import javax.websocket.Endpoint;
|
||||
@ -34,6 +35,7 @@ import javax.websocket.Session;
|
||||
|
||||
import org.eclipse.jetty.util.log.Log;
|
||||
import org.eclipse.jetty.util.log.Logger;
|
||||
import org.eclipse.jetty.websocket.api.InvalidWebSocketException;
|
||||
import org.eclipse.jetty.websocket.api.extensions.ExtensionFactory;
|
||||
import org.eclipse.jetty.websocket.client.ClientUpgradeRequest;
|
||||
import org.eclipse.jetty.websocket.client.WebSocketClient;
|
||||
@ -115,10 +117,31 @@ public class ClientContainer implements ContainerService
|
||||
{
|
||||
try
|
||||
{
|
||||
ClientEndpoint anno = annotatedEndpointClass.getAnnotation(ClientEndpoint.class);
|
||||
if (anno != null)
|
||||
{
|
||||
// Annotated takes precedence here
|
||||
JsrClientMetadata metadata = new JsrClientMetadata(this,annotatedEndpointClass);
|
||||
Object websocket = annotatedEndpointClass.newInstance();
|
||||
return connect(websocket,metadata.getEndpointConfigCopy(),path);
|
||||
}
|
||||
else if (Endpoint.class.isAssignableFrom(annotatedEndpointClass))
|
||||
{
|
||||
// Try if extends Endpoint (alternate use)
|
||||
Object websocket = annotatedEndpointClass.newInstance();
|
||||
ClientEndpointConfig cec = new JettyClientEndpointConfig();
|
||||
return connect(websocket,cec,path);
|
||||
}
|
||||
else
|
||||
{
|
||||
StringBuilder err = new StringBuilder();
|
||||
err.append("Not a recognized websocket [");
|
||||
err.append(annotatedEndpointClass.getName());
|
||||
err.append("] does not extend @").append(ClientEndpoint.class.getName());
|
||||
err.append(" or extend from ").append(Endpoint.class.getName());
|
||||
throw new DeploymentException(err.toString());
|
||||
}
|
||||
}
|
||||
catch (InstantiationException | IllegalAccessException e)
|
||||
{
|
||||
throw new DeploymentException("Unable to instantiate websocket: " + annotatedEndpointClass,e);
|
||||
|
@ -48,7 +48,8 @@ public class JsrClientMetadata extends JsrMetadata<ClientEndpoint>
|
||||
ClientEndpoint anno = websocket.getAnnotation(ClientEndpoint.class);
|
||||
if (anno == null)
|
||||
{
|
||||
throw new InvalidWebSocketException("Unsupported WebSocket object, missing @" + ClientEndpoint.class + " annotation");
|
||||
throw new InvalidWebSocketException(String.format("Unsupported WebSocket object [%s], missing @%s annotation",websocket.getName(),
|
||||
ClientEndpoint.class.getName()));
|
||||
}
|
||||
|
||||
this.endpoint = anno;
|
||||
|
@ -32,6 +32,7 @@ import org.eclipse.jetty.server.ServerConnector;
|
||||
import org.eclipse.jetty.server.handler.ContextHandler;
|
||||
import org.eclipse.jetty.util.log.Log;
|
||||
import org.eclipse.jetty.util.log.Logger;
|
||||
import org.eclipse.jetty.websocket.jsr356.samples.EchoStringEndpoint;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Assert;
|
||||
import org.junit.BeforeClass;
|
||||
@ -84,15 +85,56 @@ public class EndpointEchoTest
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEcho() throws Exception
|
||||
public void testBasicEchoInstance() throws Exception
|
||||
{
|
||||
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
|
||||
EndpointEchoClient echoer = new EndpointEchoClient();
|
||||
Assert.assertThat(echoer,instanceOf(javax.websocket.Endpoint.class));
|
||||
// Issue connect using instance of class that extends Endpoint
|
||||
Session session = container.connectToServer(echoer,serverUri);
|
||||
LOG.debug("Client Connected: {}",session);
|
||||
session.getBasicRemote().sendText("Echo");
|
||||
LOG.debug("Client Message Sent");
|
||||
echoer.textCapture.messageQueue.awaitMessages(1,1000,TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBasicEchoClassref() throws Exception
|
||||
{
|
||||
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
|
||||
// Issue connect using class reference (class extends Endpoint)
|
||||
Session session = container.connectToServer(EndpointEchoClient.class,serverUri);
|
||||
LOG.debug("Client Connected: {}",session);
|
||||
session.getBasicRemote().sendText("Echo");
|
||||
LOG.debug("Client Message Sent");
|
||||
// TODO: figure out echo verification.
|
||||
// echoer.textCapture.messageQueue.awaitMessages(1,1000,TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAbstractEchoInstance() throws Exception
|
||||
{
|
||||
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
|
||||
EchoStringEndpoint echoer = new EchoStringEndpoint();
|
||||
Assert.assertThat(echoer,instanceOf(javax.websocket.Endpoint.class));
|
||||
// Issue connect using instance of class that extends abstract that extends Endpoint
|
||||
Session session = container.connectToServer(echoer,serverUri);
|
||||
LOG.debug("Client Connected: {}",session);
|
||||
session.getBasicRemote().sendText("Echo");
|
||||
LOG.debug("Client Message Sent");
|
||||
echoer.messageQueue.awaitMessages(1,1000,TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAbstractEchoClassref() throws Exception
|
||||
{
|
||||
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
|
||||
// Issue connect using class reference (class that extends abstract that extends Endpoint)
|
||||
Session session = container.connectToServer(EchoStringEndpoint.class,serverUri);
|
||||
LOG.debug("Client Connected: {}",session);
|
||||
session.getBasicRemote().sendText("Echo");
|
||||
LOG.debug("Client Message Sent");
|
||||
// TODO: figure out echo verification.
|
||||
// echoer.messageQueue.awaitMessages(1,1000,TimeUnit.MILLISECONDS);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,58 @@
|
||||
//
|
||||
// ========================================================================
|
||||
// Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
|
||||
// ------------------------------------------------------------------------
|
||||
// All rights reserved. This program and the accompanying materials
|
||||
// are made available under the terms of the Eclipse Public License v1.0
|
||||
// and Apache License v2.0 which accompanies this distribution.
|
||||
//
|
||||
// The Eclipse Public License is available at
|
||||
// http://www.eclipse.org/legal/epl-v10.html
|
||||
//
|
||||
// The Apache License v2.0 is available at
|
||||
// http://www.opensource.org/licenses/apache2.0.php
|
||||
//
|
||||
// You may elect to redistribute this code under either of these licenses.
|
||||
// ========================================================================
|
||||
//
|
||||
|
||||
package org.eclipse.jetty.websocket.jsr356.samples;
|
||||
|
||||
import javax.websocket.CloseReason;
|
||||
import javax.websocket.Endpoint;
|
||||
import javax.websocket.EndpointConfig;
|
||||
import javax.websocket.MessageHandler;
|
||||
import javax.websocket.Session;
|
||||
|
||||
import org.eclipse.jetty.util.log.Log;
|
||||
import org.eclipse.jetty.util.log.Logger;
|
||||
|
||||
/**
|
||||
* Base Abstract Class.
|
||||
*/
|
||||
public abstract class AbstractStringEndpoint extends Endpoint implements MessageHandler.Whole<String>
|
||||
{
|
||||
private static final Logger LOG = Log.getLogger(AbstractStringEndpoint.class);
|
||||
protected Session session;
|
||||
protected EndpointConfig config;
|
||||
|
||||
@Override
|
||||
public void onOpen(Session session, EndpointConfig config)
|
||||
{
|
||||
LOG.debug("onOpen({}, {})",session,config);
|
||||
session.addMessageHandler(this);
|
||||
this.session = session;
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
public void onClose(Session session, CloseReason closeReason)
|
||||
{
|
||||
LOG.debug("onClose({}, {})",session,closeReason);
|
||||
this.session = null;
|
||||
}
|
||||
|
||||
public void onError(Session session, Throwable thr)
|
||||
{
|
||||
LOG.warn("onError()",thr);
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
//
|
||||
// ========================================================================
|
||||
// Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
|
||||
// ------------------------------------------------------------------------
|
||||
// All rights reserved. This program and the accompanying materials
|
||||
// are made available under the terms of the Eclipse Public License v1.0
|
||||
// and Apache License v2.0 which accompanies this distribution.
|
||||
//
|
||||
// The Eclipse Public License is available at
|
||||
// http://www.eclipse.org/legal/epl-v10.html
|
||||
//
|
||||
// The Apache License v2.0 is available at
|
||||
// http://www.opensource.org/licenses/apache2.0.php
|
||||
//
|
||||
// You may elect to redistribute this code under either of these licenses.
|
||||
// ========================================================================
|
||||
//
|
||||
|
||||
package org.eclipse.jetty.websocket.jsr356.samples;
|
||||
|
||||
import org.eclipse.jetty.websocket.jsr356.MessageQueue;
|
||||
|
||||
/**
|
||||
* Legitimate structure for an Endpoint
|
||||
*/
|
||||
public class EchoStringEndpoint extends AbstractStringEndpoint
|
||||
{
|
||||
public MessageQueue messageQueue = new MessageQueue();
|
||||
|
||||
@Override
|
||||
public void onMessage(String message)
|
||||
{
|
||||
messageQueue.offer(message);
|
||||
session.getAsyncRemote().sendText(message);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user