[Red5commits] [2251] "IConnection" objects for Remoting properly store attributes accross multiple re

jbauch luke at codegent.com
Tue Aug 28 15:10:17 PDT 2007


"IConnection" objects for Remoting properly store attributes accross multiple requests by using sessions


Timestamp: 08/28/07 17:07:15 EST (less than one hour ago) 
Change: 2251 
Author: jbauch

Files (see diff or trac for details): 
doc/trunk/changelog.txt
java/server/trunk/src/org/red5/server/net/remoting/message/RemotingPacket.java
java/server/trunk/src/org/red5/server/net/servlet/AMFGatewayServlet.java
java/server/trunk/src/org/red5/server/net/servlet/ServletConnection.java


Trac: http://mirror1.cvsdude.com/trac/osflash/red5/changeset/2251

Index: /java/server/trunk/src/org/red5/server/net/remoting/message/RemotingPacket.java
===================================================================
--- /java/server/trunk/src/org/red5/server/net/remoting/message/RemotingPacket.java (revision 2038)
+++ /java/server/trunk/src/org/red5/server/net/remoting/message/RemotingPacket.java (revision 2251)
@@ -24,4 +24,6 @@
 
 import javax.servlet.http.HttpServletRequest;
+
+import org.red5.server.api.IConnection.Encoding;
 
 /**
@@ -81,3 +83,18 @@
 	}
 
+    /**
+     * Return the encoding of the included calls.
+     * 
+     * @return
+     */
+    public Encoding getEncoding() {
+    	List<RemotingCall> calls = getCalls();
+    	if (calls == null || calls.isEmpty()) {
+    		return Encoding.AMF0;
+    	}
+    	
+    	RemotingCall call = calls.get(0);
+    	return call.isAMF3 ? Encoding.AMF3 : Encoding.AMF0;
+    }
+    
 }
Index: /java/server/trunk/src/org/red5/server/net/servlet/ServletConnection.java
===================================================================
--- /java/server/trunk/src/org/red5/server/net/servlet/ServletConnection.java (revision 1936)
+++ /java/server/trunk/src/org/red5/server/net/servlet/ServletConnection.java (revision 2251)
@@ -21,11 +21,16 @@
 
 import java.util.Collections;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 import javax.servlet.http.HttpServletRequest;
-
-import org.red5.server.AttributeStore;
+import javax.servlet.http.HttpSession;
+
+import org.red5.server.api.IAttributeStore;
 import org.red5.server.api.IBasicScope;
 import org.red5.server.api.IClient;
@@ -33,13 +38,15 @@
 import org.red5.server.api.IScope;
 import org.red5.server.api.event.IEvent;
+import org.red5.server.net.remoting.message.RemotingPacket;
 
 /**
- * Simple connection class so the Red5 object works in methods invoked through
- * remoting.
+ * Connection class so the Red5 object works in methods invoked through
+ * remoting. Attributes are stored in the session of the implementing
+ * servlet container.
  * 
  * @author The Red5 Project (red5 at osflash.org)
  * @author Joachim Bauch (jojo at struktur.de)
  */
-public class ServletConnection extends AttributeStore implements IConnection {
+public class ServletConnection implements IConnection {
     /**
      * Scope
@@ -51,4 +58,14 @@
 	protected HttpServletRequest request;
 
+	/**
+	 * Remoting packet that triggered the connection.
+	 */
+	protected RemotingPacket packet;
+	
+	/**
+	 * Session used to store properties.
+	 */
+	protected HttpSession session;
+	
     /**
      * Create servlet connection from request and scope
@@ -56,9 +73,31 @@
      * @param scope             Scope
      */
-    public ServletConnection(HttpServletRequest request, IScope scope) {
+    public ServletConnection(HttpServletRequest request, IScope scope, RemotingPacket packet) {
 		this.request = request;
 		this.scope = scope;
-	}
-
+		this.packet = packet;
+		this.session = request.getSession();
+	}
+
+    /**
+     * Return string representation of the connection.
+     * 
+     * @return
+     */
+    public String toString() {
+		return getClass().getSimpleName() + " from " + getRemoteAddress() + ':'
+			+ getRemotePort() + " to " + getHost() + " (session: "
+			+ session.getId() + ')';
+    }
+    
+    /**
+     * Update the current packet.
+     * 
+     * @param packet
+     */
+    protected void setPacket(RemotingPacket packet) {
+    	this.packet = packet;
+    }
+    
     /**
      * Throws Not supported runtime exception
@@ -73,5 +112,5 @@
      */
     public Encoding getEncoding() {
-		return Encoding.AMF0;
+		return packet.getEncoding();
 	}
 
@@ -105,5 +144,5 @@
 	/** {@inheritDoc} */
     public void close() {
-		// Nothing to do.
+		session.invalidate();
 	}
 
@@ -235,3 +274,175 @@
 	}
 
+	/** {@inheritDoc} */
+	public Boolean getBoolAttribute(String name) {
+		return (Boolean) getAttribute(name);
+	}
+
+	/** {@inheritDoc} */
+	public Byte getByteAttribute(String name) {
+		return (Byte) getAttribute(name);
+	}
+
+	/** {@inheritDoc} */
+	public Double getDoubleAttribute(String name) {
+		return (Double) getAttribute(name);
+	}
+
+	/** {@inheritDoc} */
+	public Integer getIntAttribute(String name) {
+		return (Integer) getAttribute(name);
+	}
+
+	/** {@inheritDoc} */
+	public List getListAttribute(String name) {
+		return (List) getAttribute(name);
+	}
+
+	/** {@inheritDoc} */
+	public Long getLongAttribute(String name) {
+		return (Long) getAttribute(name);
+	}
+
+	/** {@inheritDoc} */
+	public Map getMapAttribute(String name) {
+		return (Map) getAttribute(name);
+	}
+
+	/** {@inheritDoc} */
+	public Set getSetAttribute(String name) {
+		return (Set) getAttribute(name);
+	}
+
+	/** {@inheritDoc} */
+	public Short getShortAttribute(String name) {
+		return (Short) getAttribute(name);
+	}
+
+	/** {@inheritDoc} */
+	public String getStringAttribute(String name) {
+		return (String) getAttribute(name);
+	}
+
+	/** {@inheritDoc} */
+	public Object getAttribute(String name) {
+		if (name == null) {
+			return null;
+		}
+		
+		return session.getAttribute(name);
+	}
+
+	/** {@inheritDoc} */
+	public Object getAttribute(String name, Object defaultValue) {
+		if (name == null) {
+			return null;
+		}
+		
+		// Synchronize so default value doesn't override other default value 
+		synchronized (session) {
+			Object result = session.getAttribute(name);
+			if (result == null && defaultValue != null) {
+				session.setAttribute(name, defaultValue);
+				result = defaultValue;
+			}
+			return result;
+		}
+	}
+
+	/** {@inheritDoc} */
+	@SuppressWarnings("unchecked")
+	public Set<String> getAttributeNames() {
+		final Set<String> result = new HashSet<String>();
+		// Synchronize to prevent parallel modifications
+		synchronized (session) {
+			final Enumeration<String> names = session.getAttributeNames();
+			while (names.hasMoreElements()) {
+				result.add(names.nextElement());
+			}
+		}
+		return Collections.unmodifiableSet(result);
+	}
+
+	/** {@inheritDoc} */
+	@SuppressWarnings("unchecked")
+	public Map<String, Object> getAttributes() {
+		final Map<String, Object> result = new HashMap<String, Object>();
+		// Synchronize to prevent parallel modifications
+		synchronized (session) {
+			final Enumeration<String> names = session.getAttributeNames();
+			while (names.hasMoreElements()) {
+				final String name = names.nextElement();
+				result.put(name, session.getAttribute(name));
+			}
+		}
+		return Collections.unmodifiableMap(result);
+	}
+
+	/** {@inheritDoc} */
+	public boolean hasAttribute(String name) {
+		if (name == null) {
+			return false;
+		}
+		
+		return (getAttribute(name) != null);
+	}
+
+	/** {@inheritDoc} */
+	public boolean removeAttribute(String name) {
+		if (name == null) {
+			return false;
+		}
+		
+		// Synchronize to prevent parallel modifications
+		synchronized (session) {
+			if (!hasAttribute(name)) {
+				return false;
+			}
+			session.removeAttribute(name);
+		}
+		return true;
+	}
+
+	/** {@inheritDoc} */
+	@SuppressWarnings("unchecked")
+	public void removeAttributes() {
+		// Synchronize to prevent parallel modifications
+		synchronized (session) {
+			final Enumeration<String> names = session.getAttributeNames();
+			while (names.hasMoreElements()) {
+				session.removeAttribute(names.nextElement());
+			}
+		}
+	}
+
+	/** {@inheritDoc} */
+	public boolean setAttribute(String name, Object value) {
+		if (name == null) {
+			return false;
+		}
+		
+		if (value == null) {
+			session.removeAttribute(name);
+		} else {
+			session.setAttribute(name, value);
+		}
+		return true;
+	}
+
+	/** {@inheritDoc} */
+	public void setAttributes(Map<String, Object> values) {
+		for (Map.Entry<String, Object> entry: values.entrySet()) {
+			final String name = entry.getKey();
+			final Object value = entry.getValue();
+			if (name != null && value != null) {
+				session.setAttribute(name, value);
+			}
+		}
+	}
+
+	/** {@inheritDoc} */
+	public void setAttributes(IAttributeStore values) {
+		setAttributes(values.getAttributes());
+	}
+
 }
Index: /java/server/trunk/src/org/red5/server/net/servlet/AMFGatewayServlet.java
===================================================================
--- /java/server/trunk/src/org/red5/server/net/servlet/AMFGatewayServlet.java (revision 2081)
+++ /java/server/trunk/src/org/red5/server/net/servlet/AMFGatewayServlet.java (revision 2251)
@@ -167,5 +167,5 @@
 		IScope scope = webContext.resolveScope(message.getScopePath());
 		// Provide a valid IConnection in the Red5 object
-		Red5.setConnectionLocal(new ServletConnection(req, scope));
+		Red5.setConnectionLocal(new ServletConnection(req, scope, message));
 
         for (RemotingCall call: message.getCalls()) {
Index: /doc/trunk/changelog.txt
===================================================================
--- /doc/trunk/changelog.txt (revision 2219)
+++ /doc/trunk/changelog.txt (revision 2251)
@@ -21,4 +21,6 @@
 - object attributes are converted if necessary in AMF0/AMF3 codecs
 - "mx.utils.ObjectProxy" objects supported by AMF3 codec (Jira APPSERVER-173)
+- "IConnection" objects for Remoting properly store attributes accross
+  multiple requests by using sessions
 
 Bugfixes:
@@ -33,4 +35,5 @@
 - state is properly reset if exceptions occur in package decoding
   (Jira APPSERVER-137)
+- numbers outside integer range are correctly serialized in AMF3 codec
 
 


Note:
Diffs are chopped if more than 25k.
This is to get past the limit on the mailing list.



More information about the Red5commits mailing list