JSR-356 - adding more examples of using Primitives for @OnMessage

This commit is contained in:
Joakim Erdfelt 2013-07-10 16:35:50 -07:00
parent 5bb66f028a
commit 022b8d4a12
20 changed files with 1096 additions and 8 deletions

View File

@ -18,6 +18,7 @@
package org.eclipse.jetty.websocket.jsr356.annotations;
import java.io.InputStream;
import java.nio.ByteBuffer;
import javax.websocket.OnMessage;
@ -59,6 +60,15 @@ public class JsrParamIdBinary extends JsrParamIdOnMessage implements IJsrParamId
return true;
}
// Streaming
if (param.type.isAssignableFrom(InputStream.class))
{
assertPartialMessageSupportDisabled(param,callable);
param.bind(Role.MESSAGE_BINARY_STREAM);
// Streaming have no decoder
return true;
}
// Boolean (for indicating partial message support)
if (param.type.isAssignableFrom(Boolean.TYPE))
{

View File

@ -18,6 +18,8 @@
package org.eclipse.jetty.websocket.jsr356.annotations;
import java.io.Reader;
import javax.websocket.OnMessage;
import javax.websocket.Session;
@ -66,49 +68,49 @@ public class JsrParamIdText extends JsrParamIdOnMessage implements IJsrParamId
callable.setDecoderClass(BooleanDecoder.class);
return true;
}
if (param.type.isAssignableFrom(Byte.class))
if (param.type.isAssignableFrom(Byte.class) || (param.type == Byte.TYPE))
{
assertPartialMessageSupportDisabled(param,callable);
param.bind(Role.MESSAGE_TEXT);
callable.setDecoderClass(ByteDecoder.class);
return true;
}
if (param.type.isAssignableFrom(Character.class))
if (param.type.isAssignableFrom(Character.class) || (param.type == Character.TYPE))
{
assertPartialMessageSupportDisabled(param,callable);
param.bind(Role.MESSAGE_TEXT);
callable.setDecoderClass(CharacterDecoder.class);
return true;
}
if (param.type.isAssignableFrom(Double.class))
if (param.type.isAssignableFrom(Double.class) || (param.type == Double.TYPE))
{
assertPartialMessageSupportDisabled(param,callable);
param.bind(Role.MESSAGE_TEXT);
callable.setDecoderClass(DoubleDecoder.class);
return true;
}
if (param.type.isAssignableFrom(Float.class))
if (param.type.isAssignableFrom(Float.class) || (param.type == Float.TYPE))
{
assertPartialMessageSupportDisabled(param,callable);
param.bind(Role.MESSAGE_TEXT);
callable.setDecoderClass(FloatDecoder.class);
return true;
}
if (param.type.isAssignableFrom(Integer.class))
if (param.type.isAssignableFrom(Integer.class) || (param.type == Integer.TYPE))
{
assertPartialMessageSupportDisabled(param,callable);
param.bind(Role.MESSAGE_TEXT);
callable.setDecoderClass(IntegerDecoder.class);
return true;
}
if (param.type.isAssignableFrom(Long.class))
if (param.type.isAssignableFrom(Long.class) || (param.type == Long.TYPE))
{
assertPartialMessageSupportDisabled(param,callable);
param.bind(Role.MESSAGE_TEXT);
callable.setDecoderClass(LongDecoder.class);
return true;
}
if (param.type.isAssignableFrom(Short.class))
if (param.type.isAssignableFrom(Short.class) || (param.type == Short.TYPE))
{
assertPartialMessageSupportDisabled(param,callable);
param.bind(Role.MESSAGE_TEXT);
@ -116,6 +118,15 @@ public class JsrParamIdText extends JsrParamIdOnMessage implements IJsrParamId
return true;
}
// Streaming
if (param.type.isAssignableFrom(Reader.class))
{
assertPartialMessageSupportDisabled(param,callable);
param.bind(Role.MESSAGE_TEXT_STREAM);
// Streaming have no decoder
return true;
}
// Boolean (for indicating partial message support)
if (param.type.isAssignableFrom(Boolean.TYPE))
{

View File

@ -20,6 +20,7 @@ package org.eclipse.jetty.websocket.jsr356.server;
import static org.hamcrest.Matchers.*;
import java.io.Reader;
import java.lang.reflect.Field;
import java.nio.ByteBuffer;
import java.util.ArrayList;
@ -29,6 +30,8 @@ import java.util.List;
import javax.websocket.CloseReason;
import javax.websocket.PongMessage;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import javax.websocket.server.ServerEndpointConfig;
import org.eclipse.jetty.websocket.jsr356.annotations.AnnotatedEndpointScanner;
import org.eclipse.jetty.websocket.jsr356.annotations.JsrCallable;
@ -47,6 +50,18 @@ import org.eclipse.jetty.websocket.jsr356.server.samples.BasicOpenSocket;
import org.eclipse.jetty.websocket.jsr356.server.samples.BasicPongMessageSocket;
import org.eclipse.jetty.websocket.jsr356.server.samples.BasicTextMessageStringSocket;
import org.eclipse.jetty.websocket.jsr356.server.samples.StatelessTextMessageStringSocket;
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.IntTextSocket;
import org.eclipse.jetty.websocket.jsr356.server.samples.primitives.IntegerObjectTextSocket;
import org.eclipse.jetty.websocket.jsr356.server.samples.streaming.ReaderParamSocket;
import org.eclipse.jetty.websocket.jsr356.server.samples.streaming.StringReturnReaderParamSocket;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -113,6 +128,20 @@ public class ServerAnnotatedEndpointScanner_GoodSignaturesTest
// -- Text Events
Case.add(data, BasicTextMessageStringSocket.class, fText, String.class);
Case.add(data, StatelessTextMessageStringSocket.class, fText, Session.class, String.class);
// -- Primitives
Case.add(data, ByteTextSocket.class, fText, Byte.TYPE);
Case.add(data, ByteObjectTextSocket.class, fText, Byte.class);
Case.add(data, CharTextSocket.class, fText, Character.TYPE);
Case.add(data, CharacterObjectTextSocket.class, fText, Character.class);
Case.add(data, DoubleTextSocket.class, fText, Double.TYPE);
Case.add(data, DoubleObjectTextSocket.class, fText, Double.class);
Case.add(data, FloatTextSocket.class, fText, Float.TYPE);
Case.add(data, FloatObjectTextSocket.class, fText, Float.class);
Case.add(data, IntTextSocket.class, fText, Integer.TYPE);
Case.add(data, IntegerObjectTextSocket.class, fText, Integer.class);
// -- Reader Events
Case.add(data, ReaderParamSocket.class, fText, Reader.class, String.class);
Case.add(data, StringReturnReaderParamSocket.class, fText, Reader.class, String.class);
// -- Binary Events
Case.add(data, BasicBinaryMessageByteBufferSocket.class, fBinary, ByteBuffer.class);
// -- Pong Events
@ -140,7 +169,7 @@ public class ServerAnnotatedEndpointScanner_GoodSignaturesTest
public void testScan_Basic() throws Exception
{
AnnotatedServerEndpointMetadata metadata = new AnnotatedServerEndpointMetadata(container,testcase.pojo);
AnnotatedEndpointScanner scanner = new AnnotatedEndpointScanner(metadata);
AnnotatedEndpointScanner<ServerEndpoint,ServerEndpointConfig> scanner = new AnnotatedEndpointScanner<>(metadata);
scanner.scan();
Assert.assertThat("Metadata",metadata,notNullValue());

View File

@ -0,0 +1,42 @@
//
// ========================================================================
// 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 java.io.PrintWriter;
import java.io.StringWriter;
public final class StackUtil
{
public static String toString(Throwable t)
{
try (StringWriter w = new StringWriter())
{
try (PrintWriter out = new PrintWriter(w))
{
t.printStackTrace(out);
return w.toString();
}
}
catch (IOException e)
{
return "Unable to get stacktrace for: " + t;
}
}
}

View File

@ -0,0 +1,66 @@
//
// ========================================================================
// 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.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/byteobject")
public class ByteObjectTextSocket
{
private static final Logger LOG = Log.getLogger(ByteObjectTextSocket.class);
private Session session;
@OnOpen
public void onOpen(Session session)
{
this.session = session;
}
@OnMessage
public void onMessage(Byte b) throws IOException
{
if (b == null)
{
session.getAsyncRemote().sendText("Error: Byte is null");
}
else
{
String msg = String.format("0x%02x",b);
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,59 @@
//
// ========================================================================
// 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.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/byte")
public class ByteTextSocket
{
private static final Logger LOG = Log.getLogger(ByteTextSocket.class);
private Session session;
@OnOpen
public void onOpen(Session session)
{
this.session = session;
}
@OnMessage
public void onMessage(byte b) throws IOException
{
String msg = String.format("0x%02x",b);
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,59 @@
//
// ========================================================================
// 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.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/char")
public class CharTextSocket
{
private static final Logger LOG = Log.getLogger(CharTextSocket.class);
private Session session;
@OnOpen
public void onOpen(Session session)
{
this.session = session;
}
@OnMessage
public void onMessage(char c) throws IOException
{
String msg = Character.toString(c);
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,66 @@
//
// ========================================================================
// 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.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/characterobject")
public class CharacterObjectTextSocket
{
private static final Logger LOG = Log.getLogger(CharacterObjectTextSocket.class);
private Session session;
@OnOpen
public void onOpen(Session session)
{
this.session = session;
}
@OnMessage
public void onMessage(Character c) throws IOException
{
if (c == null)
{
session.getAsyncRemote().sendText("Error: Character is null");
}
else
{
String msg = c.toString();
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,66 @@
//
// ========================================================================
// 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.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/doubleobject")
public class DoubleObjectTextSocket
{
private static final Logger LOG = Log.getLogger(DoubleObjectTextSocket.class);
private Session session;
@OnOpen
public void onOpen(Session session)
{
this.session = session;
}
@OnMessage
public void onMessage(Double d) throws IOException
{
if (d == null)
{
session.getAsyncRemote().sendText("Error: Double is null");
}
else
{
String msg = d.toString();
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,59 @@
//
// ========================================================================
// 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.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/double")
public class DoubleTextSocket
{
private static final Logger LOG = Log.getLogger(DoubleTextSocket.class);
private Session session;
@OnOpen
public void onOpen(Session session)
{
this.session = session;
}
@OnMessage
public void onMessage(double d) throws IOException
{
String msg = Double.toString(d);
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,66 @@
//
// ========================================================================
// 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.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/floatobject")
public class FloatObjectTextSocket
{
private static final Logger LOG = Log.getLogger(FloatObjectTextSocket.class);
private Session session;
@OnOpen
public void onOpen(Session session)
{
this.session = session;
}
@OnMessage
public void onMessage(Float f) throws IOException
{
if (f == null)
{
session.getAsyncRemote().sendText("Error: Float is null");
}
else
{
String msg = f.toString();
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,59 @@
//
// ========================================================================
// 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.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/float")
public class FloatTextSocket
{
private static final Logger LOG = Log.getLogger(FloatTextSocket.class);
private Session session;
@OnOpen
public void onOpen(Session session)
{
this.session = session;
}
@OnMessage
public void onMessage(float f) throws IOException
{
String msg = Float.toString(f);
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,59 @@
//
// ========================================================================
// 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.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")
public class IntTextSocket
{
private static final Logger LOG = Log.getLogger(IntTextSocket.class);
private Session session;
@OnOpen
public void onOpen(Session session)
{
this.session = session;
}
@OnMessage
public void onMessage(int i) throws IOException
{
String msg = Integer.toString(i);
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,66 @@
//
// ========================================================================
// 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.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")
public class IntegerObjectTextSocket
{
private static final Logger LOG = Log.getLogger(IntegerObjectTextSocket.class);
private Session session;
@OnOpen
public void onOpen(Session session)
{
this.session = session;
}
@OnMessage
public void onMessage(Integer i) throws IOException
{
if (i == null)
{
session.getAsyncRemote().sendText("Error: Integer is null");
}
else
{
String msg = i.toString();
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,66 @@
//
// ========================================================================
// 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.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/longobject")
public class LongObjectTextSocket
{
private static final Logger LOG = Log.getLogger(LongObjectTextSocket.class);
private Session session;
@OnOpen
public void onOpen(Session session)
{
this.session = session;
}
@OnMessage
public void onMessage(Long l) throws IOException
{
if (l == null)
{
session.getAsyncRemote().sendText("Error: Long is null");
}
else
{
String msg = l.toString();
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,59 @@
//
// ========================================================================
// 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.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/long")
public class LongTextSocket
{
private static final Logger LOG = Log.getLogger(LongTextSocket.class);
private Session session;
@OnOpen
public void onOpen(Session session)
{
this.session = session;
}
@OnMessage
public void onMessage(long l) throws IOException
{
String msg = Long.toString(l);
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,66 @@
//
// ========================================================================
// 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.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/shortobject")
public class ShortObjectTextSocket
{
private static final Logger LOG = Log.getLogger(ShortObjectTextSocket.class);
private Session session;
@OnOpen
public void onOpen(Session session)
{
this.session = session;
}
@OnMessage
public void onMessage(Short s) throws IOException
{
if (s == null)
{
session.getAsyncRemote().sendText("Error: Short is null");
}
else
{
String msg = s.toString();
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,59 @@
//
// ========================================================================
// 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.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/short")
public class ShortTextSocket
{
private static final Logger LOG = Log.getLogger(ShortTextSocket.class);
private Session session;
@OnOpen
public void onOpen(Session session)
{
this.session = session;
}
@OnMessage
public void onMessage(short s) throws IOException
{
String msg = Short.toString(s);
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,65 @@
//
// ========================================================================
// 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.streaming;
import java.io.IOException;
import java.io.Reader;
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.IO;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.websocket.jsr356.server.StackUtil;
@ServerEndpoint("/echo/streaming/readerparam/{param}")
public class ReaderParamSocket
{
private static final Logger LOG = Log.getLogger(ReaderParamSocket.class);
private Session session;
@OnOpen
public void onOpen(Session session)
{
this.session = session;
}
@OnMessage
public void onReader(Reader reader, @PathParam("param") String param) throws IOException
{
StringBuilder msg = new StringBuilder();
msg.append(IO.toString(reader));
msg.append('|');
msg.append(param);
session.getAsyncRemote().sendText(msg.toString());
}
@OnError
public void onError(Throwable cause) throws IOException
{
LOG.warn("Error",cause);
session.getBasicRemote().sendText("Exception: " + StackUtil.toString(cause));
}
}

View File

@ -0,0 +1,56 @@
//
// ========================================================================
// 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.streaming;
import java.io.IOException;
import java.io.Reader;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.websocket.jsr356.server.StackUtil;
@ServerEndpoint("/stringreaderparam/{param}")
public class StringReturnReaderParamSocket
{
private static final Logger LOG = Log.getLogger(StringReturnReaderParamSocket.class);
@OnMessage
public String onReader(Reader reader, @PathParam("param") String param) throws IOException
{
StringBuilder msg = new StringBuilder();
msg.append(IO.toString(reader));
msg.append('|');
msg.append(param);
return msg.toString();
}
@OnError
public void onError(Session session, Throwable cause) throws IOException
{
LOG.warn("Error",cause);
session.getBasicRemote().sendText("Exception: " + StackUtil.toString(cause));
}
}