Issue #207 - Support javax.websocket version 1.1

This commit is contained in:
Joakim Erdfelt 2016-08-12 06:35:11 -07:00
parent 31174ba434
commit 53c9435183
10 changed files with 118 additions and 760 deletions

View File

@ -18,6 +18,8 @@
package org.eclipse.jetty.websocket.common.function;
import java.io.InputStream;
import java.io.Reader;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
@ -288,45 +290,147 @@ public class CommonEndpointFunctions<T extends Session> extends AbstractLifeCycl
if (onmethod != null)
{
final Arg SESSION = new Arg(Session.class);
final Arg
setOnError(new OnErrorFunction(session, endpoint, onmethod), onmethod);
final Arg CAUSE = new Arg(Throwable.class).required();
UnorderedSignature sig = new UnorderedSignature(SESSION, CAUSE);
if(sig.test(onmethod))
{
assertSignatureValid(onmethod, OnWebSocketError.class);
BiFunction<Object,Object[],Object> invoker = sig.newFunction(onmethod);
final Object[] args = new Object[2];
setOnError((throwable) -> {
args[0] = getSession();
args[1] = throwable;
invoker.apply(endpoint, args);
return null;
}, onmethod);
}
}
// OnWebSocketFrame [0..1]
onmethod = ReflectUtils.findAnnotatedMethod(endpointClass, OnWebSocketFrame.class);
if (onmethod != null)
{
setOnFrame(new OnFrameFunction(session, endpoint, onmethod), onmethod);
final Arg SESSION = new Arg(Session.class);
final Arg FRAME = new Arg(Frame.class).required();
UnorderedSignature sig = new UnorderedSignature(SESSION, FRAME);
if(sig.test(onmethod))
{
assertSignatureValid(onmethod, OnWebSocketFrame.class);
BiFunction<Object,Object[],Object> invoker = sig.newFunction(onmethod);
final Object[] args = new Object[2];
setOnFrame((frame) -> {
args[0] = getSession();
args[1] = frame;
invoker.apply(endpoint, args);
return null;
}, onmethod);
}
}
// OnWebSocketMessage [0..2]
Method onMessages[] = ReflectUtils.findAnnotatedMethods(endpointClass, OnWebSocketMessage.class);
if (onMessages != null && onMessages.length > 0)
{
Arg SESSION = new Arg(Session.class);
Arg TEXT = new Arg(String.class).required();
UnorderedSignature sigText = new UnorderedSignature(SESSION, TEXT);
Arg BYTE_BUFFER = new Arg(ByteBuffer.class).required();
UnorderedSignature sigBinaryBuffer = new UnorderedSignature(SESSION, BYTE_BUFFER);
Arg BYTE_ARRAY = new Arg(byte[].class).required();
Arg OFFSET = new Arg(int.class);
Arg LENGTH = new Arg(int.class);
UnorderedSignature sigBinaryArray = new UnorderedSignature(SESSION, BYTE_ARRAY, OFFSET, LENGTH);
Arg INPUT_STREAM = new Arg(InputStream.class).required();
UnorderedSignature sigInputStream = new UnorderedSignature(SESSION, INPUT_STREAM);
Arg READER = new Arg(Reader.class).required();
UnorderedSignature sigReader = new UnorderedSignature(SESSION, READER);
for (Method onMsg : onMessages)
{
if (OnTextFunction.hasMatchingSignature(onMsg))
if(sigText.test(onMsg))
{
// Normal Text Message
setOnText(new StringMessageSink(policy, new OnTextFunction(session, endpoint, onMsg)), onMsg);
assertSignatureValid(onMsg, OnWebSocketMessage.class);
BiFunction<Object, Object[], Object> invoker = sigText.newFunction(onMsg);
final Object[] args = new Object[2];
StringMessageSink messageSink = new StringMessageSink(policy,
(msg) ->
{
args[0] = getSession();
args[1] = msg;
invoker.apply(endpoint, args);
return null;
});
setOnText(messageSink, onMsg);
}
else if (OnByteBufferFunction.hasMatchingSignature(onMsg))
else if (sigBinaryBuffer.test(onMsg))
{
// ByteBuffer Binary Message
setOnBinary(new ByteBufferMessageSink(policy, new OnByteBufferFunction(session, endpoint, onMsg)), onMsg);
assertSignatureValid(onMsg, OnWebSocketMessage.class);
BiFunction<Object, Object[], Object> invoker = sigBinaryBuffer.newFunction(onMsg);
final Object[] args = new Object[2];
ByteBufferMessageSink messageSink = new ByteBufferMessageSink(policy,
(buffer) ->
{
args[0] = getSession();
args[1] = buffer;
invoker.apply(endpoint, args);
return null;
});
setOnBinary(messageSink, onMsg);
}
else if (OnByteArrayFunction.hasMatchingSignature(onMsg))
else if (sigBinaryArray.test(onMsg))
{
// byte[] Binary Message
setOnBinary(new ByteArrayMessageSink(policy, new OnByteArrayFunction(session, endpoint, onMsg)), onMsg);
assertSignatureValid(onMsg, OnWebSocketMessage.class);
BiFunction<Object, Object[], Object> invoker = sigBinaryArray.newFunction(onMsg);
final Object[] args = new Object[4];
ByteArrayMessageSink messageSink = new ByteArrayMessageSink(policy,
(buffer) ->
{
args[0] = getSession();
args[1] = buffer;
args[2] = 0;
args[3] = buffer.length;
invoker.apply(endpoint, args);
return null;
});
setOnBinary(messageSink, onMsg);
}
else if (OnInputStreamFunction.hasMatchingSignature(onMsg))
else if (sigInputStream.test(onMsg))
{
// InputStream Binary Message
setOnBinary(new InputStreamMessageSink(executor, new OnInputStreamFunction(session, endpoint, onMsg)), onMsg);
assertSignatureValid(onMsg, OnWebSocketMessage.class);
BiFunction<Object, Object[], Object> invoker = sigInputStream.newFunction(onMsg);
final Object[] args = new Object[2];
InputStreamMessageSink messageSink = new InputStreamMessageSink(executor,
(stream) ->
{
args[0] = getSession();
args[1] = stream;
invoker.apply(endpoint, args);
return null;
});
setOnBinary(messageSink, onMsg);
}
else if (OnReaderFunction.hasMatchingSignature(onMsg))
else if (sigReader.test(onMsg))
{
// Reader Text Message
setOnText(new ReaderMessageSink(executor, new OnReaderFunction(session, endpoint, onMsg)), onMsg);
assertSignatureValid(onMsg, OnWebSocketMessage.class);
BiFunction<Object, Object[], Object> invoker = sigReader.newFunction(onMsg);
final Object[] args = new Object[2];
ReaderMessageSink messageSink = new ReaderMessageSink(executor,
(reader) ->
{
args[0] = getSession();
args[1] = reader;
invoker.apply(endpoint, args);
return null;
});
setOnText(messageSink, onMsg);
}
else
{
@ -355,7 +459,7 @@ public class CommonEndpointFunctions<T extends Session> extends AbstractLifeCycl
{
StringBuilder err = new StringBuilder();
err.append("@").append(annotationClass.getSimpleName());
err.append(" method must NOT be static: ");
err.append(" method must not be static: ");
ReflectUtils.append(err, endpoint.getClass(), method);
throw new InvalidSignatureException(err.toString());
}

View File

@ -1,87 +0,0 @@
//
// ========================================================================
// Copyright (c) 1995-2016 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.common.function;
import java.lang.reflect.Method;
import java.util.function.Function;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
import org.eclipse.jetty.websocket.common.InvalidSignatureException;
import org.eclipse.jetty.websocket.common.reflect.Arg;
import org.eclipse.jetty.websocket.common.reflect.DynamicArgs;
import org.eclipse.jetty.websocket.common.util.ReflectUtils;
/**
* Jetty {@link WebSocket} {@link OnWebSocketMessage} method {@link Function} for BINARY/byte[] types
*/
public class OnByteArrayFunction implements Function<byte[], Void>
{
private static final DynamicArgs.Builder ARGBUILDER;
private static final Arg ARG_SESSION = new Arg(Session.class);
private static final Arg ARG_BUFFER = new Arg(byte[].class).required();
private static final Arg ARG_OFFSET = new Arg(int.class);
private static final Arg ARG_LENGTH = new Arg(int.class);
static
{
ARGBUILDER = new DynamicArgs.Builder();
ARGBUILDER.addSignature(ARG_SESSION, ARG_BUFFER, ARG_OFFSET, ARG_LENGTH);
}
public static DynamicArgs.Builder getDynamicArgsBuilder()
{
return ARGBUILDER;
}
public static boolean hasMatchingSignature(Method method)
{
return ARGBUILDER.hasMatchingSignature(method);
}
private final Session session;
private final Object endpoint;
private final Method method;
private final DynamicArgs callable;
public OnByteArrayFunction(Session session, Object endpoint, Method method)
{
this.session = session;
this.endpoint = endpoint;
this.method = method;
ReflectUtils.assertIsAnnotated(method, OnWebSocketMessage.class);
ReflectUtils.assertIsPublicNonStatic(method);
ReflectUtils.assertIsReturn(method, Void.TYPE);
this.callable = ARGBUILDER.build(method, ARG_SESSION, ARG_BUFFER, ARG_OFFSET, ARG_LENGTH);
if (this.callable == null)
{
throw InvalidSignatureException.build(endpoint.getClass(), OnWebSocketMessage.class, method);
}
}
@Override
public Void apply(byte[] bin)
{
this.callable.invoke(endpoint, this.session, bin, 0, bin.length);
return null;
}
}

View File

@ -1,86 +0,0 @@
//
// ========================================================================
// Copyright (c) 1995-2016 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.common.function;
import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import java.util.function.Function;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
import org.eclipse.jetty.websocket.common.InvalidSignatureException;
import org.eclipse.jetty.websocket.common.reflect.Arg;
import org.eclipse.jetty.websocket.common.reflect.DynamicArgs;
import org.eclipse.jetty.websocket.common.util.ReflectUtils;
/**
* Jetty {@link WebSocket} {@link OnWebSocketMessage} method {@link Function} for BINARY/{@link ByteBuffer} types
*/
public class OnByteBufferFunction implements Function<ByteBuffer, Void>
{
private static final DynamicArgs.Builder ARGBUILDER;
private static final Arg ARG_SESSION = new Arg(1, Session.class);
private static final Arg ARG_BUFFER = new Arg(2, ByteBuffer.class).required();
static
{
ARGBUILDER = new DynamicArgs.Builder();
ARGBUILDER.addSignature(ARG_SESSION, ARG_BUFFER);
}
public static DynamicArgs.Builder getDynamicArgsBuilder()
{
return ARGBUILDER;
}
public static boolean hasMatchingSignature(Method method)
{
return ARGBUILDER.hasMatchingSignature(method);
}
private final Session session;
private final Object endpoint;
private final Method method;
private final DynamicArgs callable;
public OnByteBufferFunction(Session session, Object endpoint, Method method)
{
this.session = session;
this.endpoint = endpoint;
this.method = method;
ReflectUtils.assertIsAnnotated(method, OnWebSocketMessage.class);
ReflectUtils.assertIsPublicNonStatic(method);
ReflectUtils.assertIsReturn(method, Void.TYPE);
this.callable = ARGBUILDER.build(method, ARG_SESSION, ARG_BUFFER);
if (this.callable == null)
{
throw InvalidSignatureException.build(endpoint.getClass(), OnWebSocketMessage.class, method);
}
}
@Override
public Void apply(ByteBuffer bin)
{
this.callable.invoke(endpoint, session, bin);
return null;
}
}

View File

@ -1,77 +0,0 @@
//
// ========================================================================
// Copyright (c) 1995-2016 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.common.function;
import java.lang.reflect.Method;
import java.util.function.Function;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose;
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
import org.eclipse.jetty.websocket.common.CloseInfo;
import org.eclipse.jetty.websocket.common.InvalidSignatureException;
import org.eclipse.jetty.websocket.common.reflect.Arg;
import org.eclipse.jetty.websocket.common.reflect.DynamicArgs;
import org.eclipse.jetty.websocket.common.util.ReflectUtils;
/**
* Jetty {@link WebSocket} {@link OnWebSocketClose} method {@link Function}
*/
public class OnCloseFunction implements Function<CloseInfo, Void>
{
private static final DynamicArgs.Builder ARGBUILDER;
private static final Arg ARG_SESSION = new Arg(1, Session.class);
private static final Arg ARG_STATUS_CODE = new Arg(2, int.class);
private static final Arg ARG_REASON = new Arg(3, String.class);
static
{
ARGBUILDER = new DynamicArgs.Builder();
ARGBUILDER.addSignature(ARG_SESSION, ARG_STATUS_CODE, ARG_REASON);
}
private final Session session;
private final Object endpoint;
private final Method method;
private final DynamicArgs callable;
public OnCloseFunction(Session session, Object endpoint, Method method)
{
this.session = session;
this.endpoint = endpoint;
this.method = method;
ReflectUtils.assertIsAnnotated(method, OnWebSocketClose.class);
ReflectUtils.assertIsPublicNonStatic(method);
ReflectUtils.assertIsReturn(method, Void.TYPE);
this.callable = ARGBUILDER.build(method, ARG_SESSION, ARG_STATUS_CODE, ARG_REASON);
if (this.callable == null)
{
throw InvalidSignatureException.build(endpoint.getClass(), OnWebSocketClose.class, method);
}
}
@Override
public Void apply(CloseInfo closeinfo)
{
this.callable.invoke(endpoint, session, closeinfo.getStatusCode(), closeinfo.getReason());
return null;
}
}

View File

@ -1,75 +0,0 @@
//
// ========================================================================
// Copyright (c) 1995-2016 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.common.function;
import java.lang.reflect.Method;
import java.util.function.Function;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketError;
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
import org.eclipse.jetty.websocket.common.InvalidSignatureException;
import org.eclipse.jetty.websocket.common.reflect.Arg;
import org.eclipse.jetty.websocket.common.reflect.DynamicArgs;
import org.eclipse.jetty.websocket.common.util.ReflectUtils;
/**
* Jetty {@link WebSocket} {@link OnWebSocketError} method {@link Function}
*/
public class OnErrorFunction implements Function<Throwable, Void>
{
private static final DynamicArgs.Builder ARGBUILDER;
private static final Arg ARG_SESSION = new Arg(1, Session.class);
private static final Arg ARG_CAUSE = new Arg(2, Throwable.class).required();
static
{
ARGBUILDER = new DynamicArgs.Builder();
ARGBUILDER.addSignature(ARG_SESSION, ARG_CAUSE);
}
private final Session session;
private final Object endpoint;
private final Method method;
private final DynamicArgs callable;
public OnErrorFunction(Session session, Object endpoint, Method method)
{
this.session = session;
this.endpoint = endpoint;
this.method = method;
ReflectUtils.assertIsAnnotated(method, OnWebSocketError.class);
ReflectUtils.assertIsPublicNonStatic(method);
ReflectUtils.assertIsReturn(method, Void.TYPE);
this.callable = ARGBUILDER.build(method, ARG_SESSION, ARG_CAUSE);
if (this.callable == null)
{
throw InvalidSignatureException.build(endpoint.getClass(), OnWebSocketError.class, method);
}
}
@Override
public Void apply(Throwable cause)
{
this.callable.invoke(endpoint, session, cause);
return null;
}
}

View File

@ -1,78 +0,0 @@
//
// ========================================================================
// Copyright (c) 1995-2016 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.common.function;
import java.lang.reflect.Method;
import java.util.function.Function;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketFrame;
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
import org.eclipse.jetty.websocket.api.extensions.Frame;
import org.eclipse.jetty.websocket.common.InvalidSignatureException;
import org.eclipse.jetty.websocket.common.WebSocketFrame;
import org.eclipse.jetty.websocket.common.reflect.Arg;
import org.eclipse.jetty.websocket.common.reflect.DynamicArgs;
import org.eclipse.jetty.websocket.common.util.ReflectUtils;
/**
* Jetty {@link WebSocket} {@link OnWebSocketFrame} method {@link Function}
*/
public class OnFrameFunction implements Function<Frame, Void>
{
private static final DynamicArgs.Builder ARGBUILDER;
private static final Arg ARG_SESSION = new Arg(1, Session.class);
private static final Arg ARG_FRAME = new Arg(2, Frame.class).required();
static
{
ARGBUILDER = new DynamicArgs.Builder();
ARGBUILDER.addSignature(ARG_SESSION, ARG_FRAME);
}
private final Session session;
private final Object endpoint;
private final Method method;
private final DynamicArgs callable;
public OnFrameFunction(Session session, Object endpoint, Method method)
{
this.session = session;
this.endpoint = endpoint;
this.method = method;
ReflectUtils.assertIsAnnotated(method, OnWebSocketFrame.class);
ReflectUtils.assertIsPublicNonStatic(method);
ReflectUtils.assertIsReturn(method, Void.TYPE);
this.callable = ARGBUILDER.build(method, ARG_SESSION, ARG_FRAME);
if (this.callable == null)
{
throw InvalidSignatureException.build(endpoint.getClass(), OnWebSocketFrame.class, method);
}
}
@Override
public Void apply(Frame frame)
{
WebSocketFrame copy = WebSocketFrame.copy(frame);
this.callable.invoke(endpoint, session, copy);
return null;
}
}

View File

@ -1,87 +0,0 @@
//
// ========================================================================
// Copyright (c) 1995-2016 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.common.function;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.function.Function;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
import org.eclipse.jetty.websocket.common.InvalidSignatureException;
import org.eclipse.jetty.websocket.common.reflect.Arg;
import org.eclipse.jetty.websocket.common.reflect.DynamicArgs;
import org.eclipse.jetty.websocket.common.util.ReflectUtils;
/**
* Jetty {@link WebSocket} {@link OnWebSocketMessage} method {@link Function} for BINARY/{@link InputStream} streaming
* types
*/
public class OnInputStreamFunction implements Function<InputStream, Void>
{
private static final DynamicArgs.Builder ARGBUILDER;
private static final Arg ARG_SESSION = new Arg(1, Session.class);
private static final Arg ARG_STREAM = new Arg(2, InputStream.class).required();
static
{
ARGBUILDER = new DynamicArgs.Builder();
ARGBUILDER.addSignature(ARG_SESSION, ARG_STREAM);
}
public static DynamicArgs.Builder getDynamicArgsBuilder()
{
return ARGBUILDER;
}
public static boolean hasMatchingSignature(Method method)
{
return ARGBUILDER.hasMatchingSignature(method);
}
private final Session session;
private final Object endpoint;
private final Method method;
private final DynamicArgs callable;
public OnInputStreamFunction(Session session, Object endpoint, Method method)
{
this.session = session;
this.endpoint = endpoint;
this.method = method;
ReflectUtils.assertIsAnnotated(method, OnWebSocketMessage.class);
ReflectUtils.assertIsPublicNonStatic(method);
ReflectUtils.assertIsReturn(method, Void.TYPE);
this.callable = ARGBUILDER.build(method, ARG_SESSION, ARG_STREAM);
if (this.callable == null)
{
throw InvalidSignatureException.build(endpoint.getClass(), OnWebSocketMessage.class, method);
}
}
@Override
public Void apply(InputStream stream)
{
this.callable.invoke(endpoint, session, stream);
return null;
}
}

View File

@ -1,72 +0,0 @@
//
// ========================================================================
// Copyright (c) 1995-2016 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.common.function;
import java.lang.reflect.Method;
import java.util.function.Function;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
import org.eclipse.jetty.websocket.common.InvalidSignatureException;
import org.eclipse.jetty.websocket.common.reflect.Arg;
import org.eclipse.jetty.websocket.common.reflect.DynamicArgs;
import org.eclipse.jetty.websocket.common.util.ReflectUtils;
/**
* Jetty {@link WebSocket} {@link OnWebSocketConnect} method {@link Function}
*/
public class OnOpenFunction<T extends Session> implements Function<T, Void>
{
private static final DynamicArgs.Builder ARGBUILDER;
private static final Arg ARG_SESSION = new Arg(1, Session.class).required();
static
{
ARGBUILDER = new DynamicArgs.Builder();
ARGBUILDER.addSignature(ARG_SESSION);
}
private final Object endpoint;
private final Method method;
private final DynamicArgs callable;
public OnOpenFunction(Object endpoint, Method method)
{
this.endpoint = endpoint;
this.method = method;
ReflectUtils.assertIsAnnotated(method, OnWebSocketConnect.class);
ReflectUtils.assertIsPublicNonStatic(method);
ReflectUtils.assertIsReturn(method, Void.TYPE);
this.callable = ARGBUILDER.build(method, ARG_SESSION);
if (this.callable == null)
{
throw InvalidSignatureException.build(endpoint.getClass(), OnWebSocketConnect.class, method);
}
}
@Override
public Void apply(Session session)
{
this.callable.invoke(endpoint, session);
return null;
}
}

View File

@ -1,95 +0,0 @@
//
// ========================================================================
// Copyright (c) 1995-2016 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.common.function;
import java.io.Reader;
import java.lang.reflect.Method;
import java.util.Objects;
import java.util.function.Function;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
import org.eclipse.jetty.websocket.common.InvalidSignatureException;
import org.eclipse.jetty.websocket.common.reflect.Arg;
import org.eclipse.jetty.websocket.common.reflect.DynamicArgs;
import org.eclipse.jetty.websocket.common.util.ReflectUtils;
/**
* Jetty {@link WebSocket} {@link OnWebSocketMessage} method {@link Function} for TEXT/{@link Reader} streaming types
*/
public class OnReaderFunction implements Function<Reader, Void>
{
private static final Logger LOG = Log.getLogger(OnReaderFunction.class);
private static final DynamicArgs.Builder ARGBUILDER;
private static final Arg ARG_SESSION = new Arg(Session.class);
private static final Arg ARG_STREAM = new Arg(Reader.class).required();
static
{
ARGBUILDER = new DynamicArgs.Builder();
ARGBUILDER.addSignature(ARG_SESSION, ARG_STREAM);
}
public static DynamicArgs.Builder getDynamicArgsBuilder()
{
return ARGBUILDER;
}
public static boolean hasMatchingSignature(Method method)
{
return ARGBUILDER.hasMatchingSignature(method);
}
private final Session session;
private final Object endpoint;
private final Method method;
private final DynamicArgs callable;
public OnReaderFunction(Session session, Object endpoint, Method method)
{
this.session = session;
this.endpoint = endpoint;
this.method = method;
ReflectUtils.assertIsAnnotated(method, OnWebSocketMessage.class);
ReflectUtils.assertIsPublicNonStatic(method);
ReflectUtils.assertIsReturn(method, Void.TYPE);
this.callable = ARGBUILDER.build(method, ARG_SESSION, ARG_STREAM);
if (this.callable == null)
{
throw InvalidSignatureException.build(endpoint.getClass(), OnWebSocketMessage.class, method);
}
}
@Override
public Void apply(Reader stream)
{
if (LOG.isDebugEnabled())
LOG.debug("apply({}, {}, {})", endpoint, session, stream);
Objects.requireNonNull(stream, "Reader cannot be null");
this.callable.invoke(endpoint, session, stream);
return null;
}
}

View File

@ -1,89 +0,0 @@
//
// ========================================================================
// Copyright (c) 1995-2016 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.common.function;
import java.lang.reflect.Method;
import java.util.Objects;
import java.util.function.Function;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
import org.eclipse.jetty.websocket.common.InvalidSignatureException;
import org.eclipse.jetty.websocket.common.reflect.Arg;
import org.eclipse.jetty.websocket.common.reflect.DynamicArgs;
import org.eclipse.jetty.websocket.common.util.ReflectUtils;
/**
* Jetty {@link WebSocket} {@link OnWebSocketMessage} method {@link Function} for ARG_TEXT/{@link String} types
*/
public class OnTextFunction implements Function<String, Void>
{
private static final DynamicArgs.Builder ARGBUILDER;
private static final Arg ARG_SESSION = new Arg(1, Session.class);
private static final Arg ARG_TEXT = new Arg(2, String.class).required();
static
{
ARGBUILDER = new DynamicArgs.Builder();
ARGBUILDER.addSignature(ARG_SESSION, ARG_TEXT);
}
public static DynamicArgs.Builder getDynamicArgsBuilder()
{
return ARGBUILDER;
}
public static boolean hasMatchingSignature(Method method)
{
return ARGBUILDER.hasMatchingSignature(method);
}
private final Session session;
private final Object endpoint;
private final Method method;
private final DynamicArgs callable;
public OnTextFunction(Session session, Object endpoint, Method method)
{
Objects.requireNonNull(session);
Objects.requireNonNull(endpoint);
Objects.requireNonNull(method);
this.session = session;
this.endpoint = endpoint;
this.method = method;
ReflectUtils.assertIsAnnotated(method, OnWebSocketMessage.class);
ReflectUtils.assertIsPublicNonStatic(method);
ReflectUtils.assertIsReturn(method, Void.TYPE);
this.callable = ARGBUILDER.build(method, ARG_SESSION, ARG_TEXT);
if (this.callable == null)
{
throw InvalidSignatureException.build(endpoint.getClass(), OnWebSocketMessage.class, method);
}
}
@Override
public Void apply(String text)
{
this.callable.invoke(endpoint, session, text);
return null;
}
}