tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
Subject svn commit: r1420647 - in /tomcat/trunk/java/org/apache/tomcat/websocket: PojoMessageHandlerAsync.java PojoMessageHandlerBase.java PojoMessageHandlerBasic.java PojoMethodMapping.java WsEndpointPojo.java
Date Wed, 12 Dec 2012 12:50:53 GMT
Author: markt
Date: Wed Dec 12 12:50:51 2012
New Revision: 1420647

URL: http://svn.apache.org/viewvc?rev=1420647&view=rev
Log:
WebSocket 1.0 implementation part 12 of many
Start to add the plumbing for @WebSocketMessage annotation handling

Added:
    tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerAsync.java   (with props)
    tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerBase.java   (with props)
    tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerBasic.java   (with props)
Modified:
    tomcat/trunk/java/org/apache/tomcat/websocket/PojoMethodMapping.java
    tomcat/trunk/java/org/apache/tomcat/websocket/WsEndpointPojo.java

Added: tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerAsync.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerAsync.java?rev=1420647&view=auto
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerAsync.java (added)
+++ tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerAsync.java Wed Dec 12
12:50:51 2012
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.tomcat.websocket;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+import javax.websocket.MessageHandler;
+
+public class PojoMessageHandlerAsync<T> extends PojoMessageHandlerBase<T>
+        implements MessageHandler.Async<T>{
+
+    public PojoMessageHandlerAsync(Object pojo, Method method,
+            WsSession wsSession) {
+        super(pojo, method, wsSession);
+    }
+
+    @Override
+    public void onMessage(T message, boolean last) {
+
+        Object[] params = null; // TODO insert message, last and session into params
+        Object result;
+        try {
+             result = method.invoke(pojo, params);
+        } catch (IllegalAccessException | InvocationTargetException e) {
+            throw new IllegalArgumentException();
+        }
+
+        processResult(result);
+    }
+}

Propchange: tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerAsync.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerBase.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerBase.java?rev=1420647&view=auto
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerBase.java (added)
+++ tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerBase.java Wed Dec 12 12:50:51
2012
@@ -0,0 +1,56 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.tomcat.websocket;
+
+import java.io.IOException;
+import java.lang.reflect.Method;
+import java.nio.ByteBuffer;
+
+import javax.websocket.EncodeException;
+
+public abstract class PojoMessageHandlerBase<T> {
+
+    protected final Object pojo;
+    protected final Method method;
+    protected final WsSession wsSession;
+
+
+    public PojoMessageHandlerBase(Object pojo, Method method,
+            WsSession wsSession) {
+        this.pojo = pojo;
+        this.method = method;
+        this.wsSession = wsSession;
+    }
+
+
+    protected void processResult(Object result) {
+        try {
+            if (result instanceof String) {
+                wsSession.getRemote().sendString((String)result);
+            } else if (result instanceof ByteBuffer){
+                wsSession.getRemote().sendBytes((ByteBuffer)result);
+            } else if (result instanceof byte[]) {
+                wsSession.getRemote().sendBytes(
+                        ByteBuffer.wrap((byte[])result));
+            } else if (result != null) {
+                wsSession.getRemote().sendObject(result);
+            }
+        } catch (IOException | EncodeException ioe) {
+            throw new IllegalStateException(ioe);
+        }
+    }
+}

Propchange: tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerBase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerBasic.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerBasic.java?rev=1420647&view=auto
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerBasic.java (added)
+++ tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerBasic.java Wed Dec 12
12:50:51 2012
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.tomcat.websocket;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+import javax.websocket.MessageHandler;
+
+public class PojoMessageHandlerBasic<T>  extends PojoMessageHandlerBase<T>
+        implements MessageHandler.Basic<T>{
+
+    public PojoMessageHandlerBasic(Object pojo, Method method,
+            WsSession wsSession) {
+        super(pojo, method, wsSession);
+    }
+
+    @Override
+    public void onMessage(T message) {
+
+        Object[] params = null; // TODO insert message and session into params
+        Object result;
+        try {
+             result = method.invoke(pojo, params);
+        } catch (IllegalAccessException | InvocationTargetException e) {
+            throw new IllegalArgumentException();
+        }
+
+        processResult(result);
+    }
+}

Propchange: tomcat/trunk/java/org/apache/tomcat/websocket/PojoMessageHandlerBasic.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: tomcat/trunk/java/org/apache/tomcat/websocket/PojoMethodMapping.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/PojoMethodMapping.java?rev=1420647&r1=1420646&r2=1420647&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/websocket/PojoMethodMapping.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/websocket/PojoMethodMapping.java Wed Dec 12 12:50:51
2012
@@ -18,8 +18,11 @@ package org.apache.tomcat.websocket;
 
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Method;
+import java.util.HashSet;
 import java.util.Map;
+import java.util.Set;
 
+import javax.websocket.MessageHandler;
 import javax.websocket.Session;
 import javax.websocket.WebSocketClose;
 import javax.websocket.WebSocketError;
@@ -41,6 +44,8 @@ public class PojoMethodMapping {
     private final PathParam[] onCloseParams;
     private final PathParam[] onErrorParams;
 
+    private final Set<MessageMethod> onMessage = new HashSet<>();
+
     private final UriTemplate template;
 
     public PojoMethodMapping(Class<?> clazzPojo, String path,
@@ -59,7 +64,7 @@ public class PojoMethodMapping {
                     method.getAnnotation(WebSocketError.class) != null) {
                 error = method;
             } else if (method.getAnnotation(WebSocketMessage.class) != null) {
-                // TODO
+                onMessage.add(new MessageMethod(method));
             }
         }
         this.onOpen = open;
@@ -109,6 +114,27 @@ public class PojoMethodMapping {
     }
 
 
+    public Set<MessageHandler> getMessageHandlers(Object pojo, String pathInfo,
+            Session session) {
+
+        Set<MessageHandler> result = new HashSet<>();
+
+        for (MessageMethod messageMethod : onMessage) {
+            result.add(buildMessageHandler(
+                    messageMethod, pojo, pathInfo, session));
+        }
+
+        return result;
+    }
+
+
+    private static MessageHandler buildMessageHandler(
+            MessageMethod messageMethod, Object pojo, String pathInfo,
+            Session session) {
+
+        return null;
+    }
+
     private static PathParam[] getPathParams(Method m, boolean isError) {
         if (m == null) {
             return new PathParam[0];
@@ -203,4 +229,22 @@ public class PojoMethodMapping {
             throw new IllegalArgumentException();
         }
     }
+
+
+    private static class MessageMethod {
+
+        private final Method m;
+
+        public MessageMethod(Method m) {
+            this.m = m;
+        }
+
+        public Method getMethod() {
+            return m;
+        }
+
+        public Object[] getParameters() {
+            return null;
+        }
+    }
 }

Modified: tomcat/trunk/java/org/apache/tomcat/websocket/WsEndpointPojo.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/WsEndpointPojo.java?rev=1420647&r1=1420646&r2=1420647&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/websocket/WsEndpointPojo.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/websocket/WsEndpointPojo.java Wed Dec 12 12:50:51
2012
@@ -20,6 +20,7 @@ import java.lang.reflect.InvocationTarge
 
 import javax.websocket.CloseReason;
 import javax.websocket.Endpoint;
+import javax.websocket.MessageHandler;
 import javax.websocket.Session;
 
 /**
@@ -57,6 +58,11 @@ public class WsEndpointPojo extends Endp
                 e.printStackTrace();
             }
         }
+
+        for (MessageHandler mh :
+                methodMapping.getMessageHandlers(pojo, pathInfo, session)) {
+            session.addMessageHandler(mh);
+        }
     }
 
     @Override



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


Mime
View raw message