From 53c9435183d6094d62afb39ffa59fe7edcccb7bb Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Fri, 12 Aug 2016 06:35:11 -0700 Subject: [PATCH] Issue #207 - Support javax.websocket version 1.1 --- .../function/CommonEndpointFunctions.java | 132 ++++++++++++++++-- .../common/function/OnByteArrayFunction.java | 87 ------------ .../common/function/OnByteBufferFunction.java | 86 ------------ .../common/function/OnCloseFunction.java | 77 ---------- .../common/function/OnErrorFunction.java | 75 ---------- .../common/function/OnFrameFunction.java | 78 ----------- .../function/OnInputStreamFunction.java | 87 ------------ .../common/function/OnOpenFunction.java | 72 ---------- .../common/function/OnReaderFunction.java | 95 ------------- .../common/function/OnTextFunction.java | 89 ------------ 10 files changed, 118 insertions(+), 760 deletions(-) delete mode 100644 jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/function/OnByteArrayFunction.java delete mode 100644 jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/function/OnByteBufferFunction.java delete mode 100644 jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/function/OnCloseFunction.java delete mode 100644 jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/function/OnErrorFunction.java delete mode 100644 jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/function/OnFrameFunction.java delete mode 100644 jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/function/OnInputStreamFunction.java delete mode 100644 jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/function/OnOpenFunction.java delete mode 100644 jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/function/OnReaderFunction.java delete mode 100644 jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/function/OnTextFunction.java diff --git a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/function/CommonEndpointFunctions.java b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/function/CommonEndpointFunctions.java index 8ec701489e3..5e597d1f02d 100644 --- a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/function/CommonEndpointFunctions.java +++ b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/function/CommonEndpointFunctions.java @@ -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 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 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 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 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 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 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 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 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 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()); } diff --git a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/function/OnByteArrayFunction.java b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/function/OnByteArrayFunction.java deleted file mode 100644 index 5b5f540bceb..00000000000 --- a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/function/OnByteArrayFunction.java +++ /dev/null @@ -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 -{ - 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; - } -} diff --git a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/function/OnByteBufferFunction.java b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/function/OnByteBufferFunction.java deleted file mode 100644 index 8dfec18f307..00000000000 --- a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/function/OnByteBufferFunction.java +++ /dev/null @@ -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 -{ - 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; - } -} diff --git a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/function/OnCloseFunction.java b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/function/OnCloseFunction.java deleted file mode 100644 index 9b854536806..00000000000 --- a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/function/OnCloseFunction.java +++ /dev/null @@ -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 -{ - 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; - } -} diff --git a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/function/OnErrorFunction.java b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/function/OnErrorFunction.java deleted file mode 100644 index 6f1da31973d..00000000000 --- a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/function/OnErrorFunction.java +++ /dev/null @@ -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 -{ - 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; - } -} diff --git a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/function/OnFrameFunction.java b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/function/OnFrameFunction.java deleted file mode 100644 index d4bc4fab989..00000000000 --- a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/function/OnFrameFunction.java +++ /dev/null @@ -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 -{ - 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; - } -} diff --git a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/function/OnInputStreamFunction.java b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/function/OnInputStreamFunction.java deleted file mode 100644 index f56a579735d..00000000000 --- a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/function/OnInputStreamFunction.java +++ /dev/null @@ -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 -{ - 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; - } -} diff --git a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/function/OnOpenFunction.java b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/function/OnOpenFunction.java deleted file mode 100644 index 4a8e21a14ce..00000000000 --- a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/function/OnOpenFunction.java +++ /dev/null @@ -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 implements Function -{ - 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; - } -} diff --git a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/function/OnReaderFunction.java b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/function/OnReaderFunction.java deleted file mode 100644 index bb01d4a5b85..00000000000 --- a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/function/OnReaderFunction.java +++ /dev/null @@ -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 -{ - 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; - } -} diff --git a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/function/OnTextFunction.java b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/function/OnTextFunction.java deleted file mode 100644 index 931ae26bcf1..00000000000 --- a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/function/OnTextFunction.java +++ /dev/null @@ -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 -{ - 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; - } -}