JSR-356 Adding echo tests on primitives

This commit is contained in:
Joakim Erdfelt 2013-07-16 14:05:59 -07:00
parent fb00eb55cd
commit ba50e6b053
15 changed files with 686 additions and 11 deletions

View File

@ -19,6 +19,7 @@
package org.eclipse.jetty.websocket.jsr356.server;
import javax.websocket.server.ServerEndpoint;
import javax.websocket.server.ServerEndpointConfig;
import org.eclipse.jetty.websocket.api.WebSocketPolicy;
import org.eclipse.jetty.websocket.common.events.EventDriver;
@ -42,7 +43,7 @@ public class JsrServerEndpointImpl implements EventDriverImpl
EndpointInstance ei = (EndpointInstance)websocket;
AnnotatedServerEndpointMetadata metadata = (AnnotatedServerEndpointMetadata)ei.getMetadata();
JsrEvents events = new JsrEvents(metadata);
JsrEvents<ServerEndpoint,ServerEndpointConfig> events = new JsrEvents<>(metadata);
return new JsrAnnotatedEventDriver(policy,ei,events);
}

View File

@ -0,0 +1,81 @@
//
// ========================================================================
// 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.server;
import java.util.ArrayList;
import java.util.List;
import javax.websocket.server.ServerEndpoint;
public class EchoCase
{
public static EchoCase add(List<EchoCase[]> data, Class<?> serverPojo, String path)
{
EchoCase ecase = new EchoCase();
ecase.serverPojo = serverPojo;
ecase.path = path;
data.add(new EchoCase[]
{ ecase });
return ecase;
}
public static EchoCase add(List<EchoCase[]> data, Class<?> serverPojo)
{
EchoCase ecase = new EchoCase();
ecase.serverPojo = serverPojo;
data.add(new EchoCase[]
{ ecase });
ServerEndpoint endpoint = serverPojo.getAnnotation(ServerEndpoint.class);
ecase.path = endpoint.value();
return ecase;
}
// The websocket server pojo to test against
public Class<?> serverPojo;
// The (relative) URL path to hit
public String path;
// The messages to transmit
public List<Object> messages = new ArrayList<>();
// The expected Strings (that are echoed back)
public List<String> expectedStrings = new ArrayList<>();
public EchoCase addMessage(Object msg)
{
messages.add(msg);
return this;
}
public EchoCase expect(String message)
{
expectedStrings.add(message);
return this;
}
public EchoCase requestPath(String path)
{
this.path = path;
return this;
}
@Override
public String toString()
{
return String.format("EchoCase['%s',%s,messages=%d]",path,serverPojo.getName(),messages.size());
}
}

View File

@ -0,0 +1,73 @@
//
// ========================================================================
// 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.server;
import java.io.IOException;
import javax.websocket.ClientEndpoint;
import javax.websocket.CloseReason;
import javax.websocket.EncodeException;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
@ClientEndpoint
public class EchoClientSocket extends TrackingSocket
{
private Session session;
@OnOpen
public void onOpen(Session session)
{
this.session = session;
openLatch.countDown();
}
@OnClose
public void onClose(CloseReason close)
{
this.session = null;
super.closeReason = close;
super.closeLatch.countDown();
}
public void sendObject(Object obj) throws IOException, EncodeException
{
session.getBasicRemote().sendObject(obj);
}
@OnError
public void onError(Throwable t)
{
addError(t);
}
@OnMessage
public void onText(String text)
{
addEvent(text);
}
public void close() throws IOException
{
this.session.close(new CloseReason(CloseReason.CloseCodes.NORMAL_CLOSURE,"Test Complete"));
}
}

View File

@ -0,0 +1,264 @@
//
// ========================================================================
// 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.server;
import static org.hamcrest.Matchers.*;
import java.io.File;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.websocket.ContainerProvider;
import javax.websocket.WebSocketContainer;
import org.eclipse.jetty.toolchain.test.EventQueue;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.websocket.jsr356.server.samples.primitives.BooleanObjectTextSocket;
import org.eclipse.jetty.websocket.jsr356.server.samples.primitives.BooleanTextSocket;
import org.eclipse.jetty.websocket.jsr356.server.samples.primitives.ByteObjectTextSocket;
import org.eclipse.jetty.websocket.jsr356.server.samples.primitives.ByteTextSocket;
import org.eclipse.jetty.websocket.jsr356.server.samples.primitives.CharTextSocket;
import org.eclipse.jetty.websocket.jsr356.server.samples.primitives.CharacterObjectTextSocket;
import org.eclipse.jetty.websocket.jsr356.server.samples.primitives.DoubleObjectTextSocket;
import org.eclipse.jetty.websocket.jsr356.server.samples.primitives.DoubleTextSocket;
import org.eclipse.jetty.websocket.jsr356.server.samples.primitives.FloatObjectTextSocket;
import org.eclipse.jetty.websocket.jsr356.server.samples.primitives.FloatTextSocket;
import org.eclipse.jetty.websocket.jsr356.server.samples.primitives.IntParamTextSocket;
import org.eclipse.jetty.websocket.jsr356.server.samples.primitives.IntTextSocket;
import org.eclipse.jetty.websocket.jsr356.server.samples.primitives.IntegerObjectTextSocket;
import org.eclipse.jetty.websocket.jsr356.server.samples.primitives.LongObjectTextSocket;
import org.eclipse.jetty.websocket.jsr356.server.samples.primitives.LongTextSocket;
import org.eclipse.jetty.websocket.jsr356.server.samples.primitives.ShortObjectTextSocket;
import org.eclipse.jetty.websocket.jsr356.server.samples.primitives.ShortTextSocket;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
@RunWith(Parameterized.class)
public class PrimitiveEchoTest
{
private static final List<EchoCase[]> TESTCASES = new ArrayList<>();
private static WSServer server;
private static URI serverUri;
private static WebSocketContainer client;
static
{
EchoCase.add(TESTCASES,BooleanTextSocket.class).addMessage(true).expect("true");
EchoCase.add(TESTCASES,BooleanTextSocket.class).addMessage(false).expect("false");
EchoCase.add(TESTCASES,BooleanTextSocket.class).addMessage(Boolean.TRUE).expect("true");
EchoCase.add(TESTCASES,BooleanTextSocket.class).addMessage(Boolean.FALSE).expect("false");
EchoCase.add(TESTCASES,BooleanTextSocket.class).addMessage("true").expect("true");
EchoCase.add(TESTCASES,BooleanTextSocket.class).addMessage("TRUe").expect("true");
EchoCase.add(TESTCASES,BooleanTextSocket.class).addMessage("Apple").expect("false");
EchoCase.add(TESTCASES,BooleanTextSocket.class).addMessage("false").expect("false");
EchoCase.add(TESTCASES,BooleanObjectTextSocket.class).addMessage(true).expect("true");
EchoCase.add(TESTCASES,BooleanObjectTextSocket.class).addMessage(false).expect("false");
EchoCase.add(TESTCASES,BooleanObjectTextSocket.class).addMessage(Boolean.TRUE).expect("true");
EchoCase.add(TESTCASES,BooleanObjectTextSocket.class).addMessage(Boolean.FALSE).expect("false");
EchoCase.add(TESTCASES,BooleanObjectTextSocket.class).addMessage("true").expect("true");
EchoCase.add(TESTCASES,BooleanObjectTextSocket.class).addMessage("false").expect("false");
EchoCase.add(TESTCASES,BooleanObjectTextSocket.class).addMessage("FaLsE").expect("false");
EchoCase.add(TESTCASES,ByteTextSocket.class).addMessage((byte)88).expect("0x58");
EchoCase.add(TESTCASES,ByteTextSocket.class).addMessage((byte)101).expect("0x65");
EchoCase.add(TESTCASES,ByteTextSocket.class).addMessage((byte)202).expect("0xCA");
EchoCase.add(TESTCASES,ByteTextSocket.class).addMessage(Byte.valueOf((byte)33)).expect("0x21");
EchoCase.add(TESTCASES,ByteTextSocket.class).addMessage(Byte.valueOf((byte)131)).expect("0x83");
EchoCase.add(TESTCASES,ByteTextSocket.class).addMessage(Byte.valueOf((byte)232)).expect("0xE8");
EchoCase.add(TESTCASES,ByteObjectTextSocket.class).addMessage((byte)88).expect("0x58");
EchoCase.add(TESTCASES,ByteObjectTextSocket.class).addMessage((byte)101).expect("0x65");
EchoCase.add(TESTCASES,ByteObjectTextSocket.class).addMessage((byte)202).expect("0xCA");
EchoCase.add(TESTCASES,ByteObjectTextSocket.class).addMessage(Byte.valueOf((byte)33)).expect("0x21");
EchoCase.add(TESTCASES,ByteObjectTextSocket.class).addMessage(Byte.valueOf((byte)131)).expect("0x83");
EchoCase.add(TESTCASES,ByteObjectTextSocket.class).addMessage(Byte.valueOf((byte)232)).expect("0xE8");
EchoCase.add(TESTCASES,CharTextSocket.class).addMessage((char)40).expect("(");
EchoCase.add(TESTCASES,CharTextSocket.class).addMessage((char)106).expect("j");
EchoCase.add(TESTCASES,CharTextSocket.class).addMessage((char)126).expect("~");
EchoCase.add(TESTCASES,CharTextSocket.class).addMessage(Character.valueOf((char)41)).expect(")");
EchoCase.add(TESTCASES,CharTextSocket.class).addMessage(Character.valueOf((char)74)).expect("J");
EchoCase.add(TESTCASES,CharTextSocket.class).addMessage(Character.valueOf((char)64)).expect("@");
EchoCase.add(TESTCASES,CharacterObjectTextSocket.class).addMessage((char)40).expect("(");
EchoCase.add(TESTCASES,CharacterObjectTextSocket.class).addMessage((char)106).expect("j");
EchoCase.add(TESTCASES,CharacterObjectTextSocket.class).addMessage((char)126).expect("~");
EchoCase.add(TESTCASES,CharacterObjectTextSocket.class).addMessage("E").expect("E");
EchoCase.add(TESTCASES,CharacterObjectTextSocket.class).addMessage(Character.valueOf((char)41)).expect(")");
EchoCase.add(TESTCASES,CharacterObjectTextSocket.class).addMessage(Character.valueOf((char)74)).expect("J");
EchoCase.add(TESTCASES,CharacterObjectTextSocket.class).addMessage(Character.valueOf((char)64)).expect("@");
EchoCase.add(TESTCASES,DoubleTextSocket.class).addMessage((double)3.1459).expect("3.1459");
EchoCase.add(TESTCASES,DoubleTextSocket.class).addMessage((double)123.456).expect("123.4560");
EchoCase.add(TESTCASES,DoubleTextSocket.class).addMessage(Double.valueOf(55)).expect("55.0000");
EchoCase.add(TESTCASES,DoubleTextSocket.class).addMessage(Double.valueOf(1.0E8)).expect("100000000.0000");
EchoCase.add(TESTCASES,DoubleTextSocket.class).addMessage("42").expect("42.0000");
EchoCase.add(TESTCASES,DoubleTextSocket.class).addMessage(".123").expect("0.1230");
EchoCase.add(TESTCASES,DoubleObjectTextSocket.class).addMessage((double)3.1459).expect("3.1459");
EchoCase.add(TESTCASES,DoubleObjectTextSocket.class).addMessage((double)123.456).expect("123.4560");
EchoCase.add(TESTCASES,DoubleObjectTextSocket.class).addMessage(Double.valueOf(55)).expect("55.0000");
EchoCase.add(TESTCASES,DoubleObjectTextSocket.class).addMessage(Double.valueOf(1.0E8)).expect("100000000.0000");
EchoCase.add(TESTCASES,DoubleObjectTextSocket.class).addMessage("42").expect("42.0000");
EchoCase.add(TESTCASES,DoubleObjectTextSocket.class).addMessage(".123").expect("0.1230");
EchoCase.add(TESTCASES,FloatTextSocket.class).addMessage((float)3.1459).expect("3.1459");
EchoCase.add(TESTCASES,FloatTextSocket.class).addMessage((float)123.456).expect("123.4560");
EchoCase.add(TESTCASES,FloatTextSocket.class).addMessage(Float.valueOf(55)).expect("55.0000");
EchoCase.add(TESTCASES,FloatTextSocket.class).addMessage(Float.valueOf(1.0E8f)).expect("100000000.0000");
EchoCase.add(TESTCASES,FloatTextSocket.class).addMessage("42").expect("42.0000");
EchoCase.add(TESTCASES,FloatTextSocket.class).addMessage(".123").expect("0.1230");
EchoCase.add(TESTCASES,FloatTextSocket.class).addMessage("50505E-6").expect("0.0505");
EchoCase.add(TESTCASES,FloatObjectTextSocket.class).addMessage((float)3.1459).expect("3.1459");
EchoCase.add(TESTCASES,FloatObjectTextSocket.class).addMessage((float)123.456).expect("123.4560");
EchoCase.add(TESTCASES,FloatObjectTextSocket.class).addMessage(Float.valueOf(55)).expect("55.0000");
EchoCase.add(TESTCASES,FloatObjectTextSocket.class).addMessage(Float.valueOf(1.0E8f)).expect("100000000.0000");
EchoCase.add(TESTCASES,FloatObjectTextSocket.class).addMessage("42").expect("42.0000");
EchoCase.add(TESTCASES,FloatObjectTextSocket.class).addMessage(".123").expect("0.1230");
EchoCase.add(TESTCASES,FloatObjectTextSocket.class).addMessage("50505E-6").expect("0.0505");
EchoCase.add(TESTCASES,IntTextSocket.class).addMessage((int)8).expect("8");
EchoCase.add(TESTCASES,IntTextSocket.class).addMessage((int)22).expect("22");
EchoCase.add(TESTCASES,IntTextSocket.class).addMessage("12345678").expect("12345678");
EchoCase.add(TESTCASES,IntegerObjectTextSocket.class).addMessage((int)8).expect("8");
EchoCase.add(TESTCASES,IntegerObjectTextSocket.class).addMessage((int)22).expect("22");
EchoCase.add(TESTCASES,IntegerObjectTextSocket.class).addMessage("12345678").expect("12345678");
EchoCase.add(TESTCASES,LongTextSocket.class).addMessage((int)789).expect("789");
EchoCase.add(TESTCASES,LongTextSocket.class).addMessage((long)123456L).expect("123456");
EchoCase.add(TESTCASES,LongTextSocket.class).addMessage(-456).expect("-456");
EchoCase.add(TESTCASES,LongObjectTextSocket.class).addMessage((int)789).expect("789");
EchoCase.add(TESTCASES,LongObjectTextSocket.class).addMessage((long)123456L).expect("123456");
EchoCase.add(TESTCASES,LongObjectTextSocket.class).addMessage(-234).expect("-234");
EchoCase.add(TESTCASES,ShortTextSocket.class).addMessage((int)4).expect("4");
EchoCase.add(TESTCASES,ShortTextSocket.class).addMessage((long)987).expect("987");
EchoCase.add(TESTCASES,ShortTextSocket.class).addMessage("32001").expect("32001");
EchoCase.add(TESTCASES,ShortObjectTextSocket.class).addMessage((int)4).expect("4");
EchoCase.add(TESTCASES,ShortObjectTextSocket.class).addMessage((int)987).expect("987");
EchoCase.add(TESTCASES,ShortObjectTextSocket.class).addMessage(-32001L).expect("-32001");
// PathParam based
/*
EchoCase.add(TESTCASES,IntParamTextSocket.class).requestPath("/echo/primitives/integer/params/5678")
.addMessage(1234).expect("1234|5678");
*/
}
@BeforeClass
public static void startServer() throws Exception
{
File testdir = MavenTestingUtils.getTargetTestingDir(PrimitiveEchoTest.class.getName());
server = new WSServer(testdir,"app");
server.copyWebInf("empty-web.xml");
for (EchoCase cases[] : TESTCASES)
{
for (EchoCase ecase : cases)
{
server.copyClass(ecase.serverPojo);
}
}
server.start();
serverUri = server.getServerBaseURI();
WebAppContext webapp = server.createWebAppContext();
server.deployWebapp(webapp);
server.dump();
}
@BeforeClass
public static void startClient() throws Exception
{
client = ContainerProvider.getWebSocketContainer();
}
@AfterClass
public static void stopServer()
{
server.stop();
}
@Parameters
public static Collection<EchoCase[]> data() throws Exception
{
return TESTCASES;
}
private EchoCase testcase;
public PrimitiveEchoTest(EchoCase testcase)
{
this.testcase = testcase;
System.err.println(testcase);
}
@Test
public void testEcho() throws Exception
{
EchoClientSocket socket = new EchoClientSocket();
URI toUri = serverUri.resolve(testcase.path.substring(1));
try
{
// Connect
client.connectToServer(socket,toUri);
socket.waitForConnected(2,TimeUnit.SECONDS);
// Send Messages
int messageCount = 0;
for (Object msg : testcase.messages)
{
socket.sendObject(msg);
messageCount++;
}
// Collect Responses
EventQueue<String> received = socket.eventQueue;
received.awaitEventCount(messageCount,3,TimeUnit.SECONDS);
// Validate Responses
for (String expected : testcase.expectedStrings)
{
Assert.assertThat("Received Echo Responses",received,contains(expected));
}
}
finally
{
// Close
socket.close();
}
}
}

View File

@ -57,8 +57,6 @@ import org.junit.Assert;
public class WSServer
{
private static final Logger LOG = Log.getLogger(WSServer.class);
@SuppressWarnings("unused")
private final TestingDir testdir;
private final File contextDir;
private final String contextPath;
private Server server;
@ -69,8 +67,12 @@ public class WSServer
public WSServer(TestingDir testdir, String contextName)
{
this.testdir = testdir;
this.contextDir = testdir.getFile(contextName);
this(testdir.getDir(),contextName);
}
public WSServer(File testdir, String contextName)
{
this.contextDir = new File(testdir,contextName);
this.contextPath = "/" + contextName;
FS.ensureEmpty(contextDir);
}

View File

@ -0,0 +1,67 @@
//
// ========================================================================
// 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.server.samples.primitives;
import java.io.IOException;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.websocket.jsr356.server.StackUtil;
@ServerEndpoint("/echo/primitives/booleanobject/params/{a}")
public class BooleanObjectTextParamSocket
{
private static final Logger LOG = Log.getLogger(BooleanObjectTextParamSocket.class);
private Session session;
@OnOpen
public void onOpen(Session session)
{
this.session = session;
}
@OnMessage
public void onMessage(Boolean b, @PathParam("a") Boolean param) throws IOException
{
if (b == null)
{
session.getAsyncRemote().sendText("Error: Boolean is null");
}
else
{
String msg = String.format("%b|%b", b, param);
session.getAsyncRemote().sendText(msg);
}
}
@OnError
public void onError(Throwable cause) throws IOException
{
LOG.warn("Error",cause);
session.getBasicRemote().sendText("Exception: " + StackUtil.toString(cause));
}
}

View File

@ -0,0 +1,60 @@
//
// ========================================================================
// 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.server.samples.primitives;
import java.io.IOException;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.websocket.jsr356.server.StackUtil;
@ServerEndpoint("/echo/primitives/boolean/params/{a}")
public class BooleanTextParamSocket
{
private static final Logger LOG = Log.getLogger(BooleanTextParamSocket.class);
private Session session;
@OnOpen
public void onOpen(Session session)
{
this.session = session;
}
@OnMessage
public void onMessage(boolean b, @PathParam("a") boolean param) throws IOException
{
String msg = String.format("%b|%b", b, param);
session.getAsyncRemote().sendText(msg);
}
@OnError
public void onError(Throwable cause) throws IOException
{
LOG.warn("Error",cause);
session.getBasicRemote().sendText("Exception: " + StackUtil.toString(cause));
}
}

View File

@ -52,7 +52,7 @@ public class ByteObjectTextSocket
}
else
{
String msg = String.format("0x%02x",b);
String msg = String.format("0x%02X",b);
session.getAsyncRemote().sendText(msg);
}
}

View File

@ -46,7 +46,7 @@ public class ByteTextSocket
@OnMessage
public void onMessage(byte b) throws IOException
{
String msg = String.format("0x%02x",b);
String msg = String.format("0x%02X",b);
session.getAsyncRemote().sendText(msg);
}

View File

@ -52,7 +52,7 @@ public class DoubleObjectTextSocket
}
else
{
String msg = d.toString();
String msg = String.format("%.4f",d);
session.getAsyncRemote().sendText(msg);
}
}

View File

@ -46,7 +46,7 @@ public class DoubleTextSocket
@OnMessage
public void onMessage(double d) throws IOException
{
String msg = Double.toString(d);
String msg = String.format("%.4f",d);
session.getAsyncRemote().sendText(msg);
}

View File

@ -52,7 +52,7 @@ public class FloatObjectTextSocket
}
else
{
String msg = f.toString();
String msg = String.format("%.4f",f);
session.getAsyncRemote().sendText(msg);
}
}

View File

@ -46,7 +46,7 @@ public class FloatTextSocket
@OnMessage
public void onMessage(float f) throws IOException
{
String msg = Float.toString(f);
String msg = String.format("%.4f",f);
session.getAsyncRemote().sendText(msg);
}

View File

@ -0,0 +1,60 @@
//
// ========================================================================
// 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.server.samples.primitives;
import java.io.IOException;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.websocket.jsr356.server.StackUtil;
@ServerEndpoint("/echo/primitives/integer/params/{a}")
public class IntParamTextSocket
{
private static final Logger LOG = Log.getLogger(IntParamTextSocket.class);
private Session session;
@OnOpen
public void onOpen(Session session)
{
this.session = session;
}
@OnMessage
public void onMessage(int i, @PathParam("a") int param) throws IOException
{
String msg = String.format("%d|%d",i,param);
session.getAsyncRemote().sendText(msg);
}
@OnError
public void onError(Throwable cause) throws IOException
{
LOG.warn("Error",cause);
session.getBasicRemote().sendText("Exception: " + StackUtil.toString(cause));
}
}

View File

@ -0,0 +1,67 @@
//
// ========================================================================
// 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.server.samples.primitives;
import java.io.IOException;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.websocket.jsr356.server.StackUtil;
@ServerEndpoint("/echo/primitives/integerobject/params/{a}")
public class IntegerObjectParamTextSocket
{
private static final Logger LOG = Log.getLogger(IntegerObjectParamTextSocket.class);
private Session session;
@OnOpen
public void onOpen(Session session)
{
this.session = session;
}
@OnMessage
public void onMessage(Integer i, @PathParam("a") int param) throws IOException
{
if (i == null)
{
session.getAsyncRemote().sendText("Error: Integer is null");
}
else
{
String msg = String.format("%d|%d",i,param);
session.getAsyncRemote().sendText(msg);
}
}
@OnError
public void onError(Throwable cause) throws IOException
{
LOG.warn("Error",cause);
session.getBasicRemote().sendText("Exception: " + StackUtil.toString(cause));
}
}