You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by be...@apache.org on 2012/04/23 10:15:52 UTC

svn commit: r1329113 - in /mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/src/main/java/org/apache/vysper/xmpp/extension/xep0124: BoshBackedSessionContext.java BoshHandler.java BoshStanzaUtils.java

Author: berndf
Date: Mon Apr 23 08:15:51 2012
New Revision: 1329113

URL: http://svn.apache.org/viewvc?rev=1329113&view=rev
Log:
+ reduce created objects, move helper methods from BoshHandler to static utilities

Modified:
    mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/src/main/java/org/apache/vysper/xmpp/extension/xep0124/BoshBackedSessionContext.java
    mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/src/main/java/org/apache/vysper/xmpp/extension/xep0124/BoshHandler.java
    mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/src/main/java/org/apache/vysper/xmpp/extension/xep0124/BoshStanzaUtils.java

Modified: mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/src/main/java/org/apache/vysper/xmpp/extension/xep0124/BoshBackedSessionContext.java
URL: http://svn.apache.org/viewvc/mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/src/main/java/org/apache/vysper/xmpp/extension/xep0124/BoshBackedSessionContext.java?rev=1329113&r1=1329112&r2=1329113&view=diff
==============================================================================
--- mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/src/main/java/org/apache/vysper/xmpp/extension/xep0124/BoshBackedSessionContext.java (original)
+++ mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/src/main/java/org/apache/vysper/xmpp/extension/xep0124/BoshBackedSessionContext.java Mon Apr 23 08:15:51 2012
@@ -39,6 +39,7 @@ import org.apache.vysper.xmpp.server.Abs
 import org.apache.vysper.xmpp.server.ServerRuntimeContext;
 import org.apache.vysper.xmpp.server.SessionState;
 import org.apache.vysper.xmpp.stanza.Stanza;
+import org.apache.vysper.xmpp.stanza.StanzaBuilder;
 import org.apache.vysper.xmpp.writer.StanzaWriter;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -284,8 +285,7 @@ public class BoshBackedSessionContext ex
         final Long rid = br.getRid();
         requestsWindow.put(rid, br);
         BoshRequest req = requestsWindow.remove(requestsWindow.firstKey());
-        Stanza body = BoshStanzaUtils.TERMINATE_BOSH_RESPONSE;
-        body = BoshStanzaUtils.addAttribute(body, "condition", condition);
+        Stanza body = BoshStanzaUtils.createTerminateResponse(condition).build();
         BoshResponse boshResponse = getBoshResponse(body, null);
         if (LOGGER.isDebugEnabled()) {
             LOGGER.debug("rid = {} - BOSH writing response: {}", rid, new String(boshResponse.getContent()));
@@ -641,10 +641,8 @@ public class BoshBackedSessionContext ex
     }
     
     protected void sendBrokenConnectionReport(long report, long delta) {
-        Stanza body = BoshStanzaUtils.TERMINATE_BOSH_RESPONSE;
-        body = BoshStanzaUtils.addAttribute(body, "report", Long.toString(report));
-        body = BoshStanzaUtils.addAttribute(body, "time", Long.toString(delta));
-        writeBoshResponse(body);
+        StanzaBuilder stanzaBuilder = BoshStanzaUtils.createBrokenSessionReport(report, delta);
+        writeBoshResponse(stanzaBuilder.build());
     }
     
     protected void addContinuationExpirationListener(final AsyncContext context) {

Modified: mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/src/main/java/org/apache/vysper/xmpp/extension/xep0124/BoshHandler.java
URL: http://svn.apache.org/viewvc/mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/src/main/java/org/apache/vysper/xmpp/extension/xep0124/BoshHandler.java?rev=1329113&r1=1329112&r2=1329113&view=diff
==============================================================================
--- mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/src/main/java/org/apache/vysper/xmpp/extension/xep0124/BoshHandler.java (original)
+++ mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/src/main/java/org/apache/vysper/xmpp/extension/xep0124/BoshHandler.java Mon Apr 23 08:15:51 2012
@@ -100,7 +100,24 @@ public class BoshHandler {
             LOGGER.error("Invalid request that does not have a request identifier (rid) attribute!");
             return;
         }
-        BoshRequest br = new BoshRequest(httpRequest, body, Long.parseLong(body.getAttributeValue("rid")));
+        final long rid;
+        try {
+            rid = Long.parseLong(body.getAttributeValue("rid"));
+        } catch (NumberFormatException e) {
+            LOGGER.error("not a valid RID: " + body.getAttributeValue("rid"));
+            // TODO handle properly by returning an error response
+            throw new RuntimeException(e);
+        }
+        if (rid <= 0L) {
+            LOGGER.warn("rid is not positive: " + rid);
+            // TODO handle properly by returning an error response
+            throw new RuntimeException("BOSH rid must be a positive, large number, not " + rid);
+        }
+        if (rid > 9007199254740991L) {
+            LOGGER.warn("rid too large: " + rid);
+            // continue anyway, this is not a problem with this implementation
+        }
+        BoshRequest br = new BoshRequest(httpRequest, body, rid);
         if (body.getAttribute("sid") == null) {
             // the session creation request (first request) does not have a "sid" attribute
             try {

Modified: mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/src/main/java/org/apache/vysper/xmpp/extension/xep0124/BoshStanzaUtils.java
URL: http://svn.apache.org/viewvc/mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/src/main/java/org/apache/vysper/xmpp/extension/xep0124/BoshStanzaUtils.java?rev=1329113&r1=1329112&r2=1329113&view=diff
==============================================================================
--- mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/src/main/java/org/apache/vysper/xmpp/extension/xep0124/BoshStanzaUtils.java (original)
+++ mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/src/main/java/org/apache/vysper/xmpp/extension/xep0124/BoshStanzaUtils.java Mon Apr 23 08:15:51 2012
@@ -22,7 +22,7 @@ public class BoshStanzaUtils {
     
     protected static final Stanza RESTART_BOSH_RESPONSE = wrapStanza(new ServerResponses().getFeaturesForSession());
     
-    protected static final Stanza TERMINATE_BOSH_RESPONSE = createTerminateResponse();
+    protected static final Stanza TERMINATE_BOSH_RESPONSE = createTerminateResponse(null).build();
 
     /**
      * Creates a new BOSH response builder
@@ -65,16 +65,26 @@ public class BoshStanzaUtils {
 
     /**
      * Creates a session termination BOSH response
-     * @return the termination BOSH body
     */
-    private static Stanza createTerminateResponse() {
+    public static StanzaBuilder createTerminateResponse(String condition) {
         StanzaBuilder stanzaBuilder = createBoshStanzaBuilder();
         stanzaBuilder.addAttribute("type", "terminate");
-        return stanzaBuilder.build();
+        if (condition != null) stanzaBuilder.addAttribute("condition", condition);
+        return stanzaBuilder;
+    }
+
+    /**
+     * Creates a session termination BOSH response signalling a broken session
+    */
+    public static StanzaBuilder createBrokenSessionReport(long report, long delta) {
+        StanzaBuilder stanzaBuilder = createTerminateResponse(null);
+        stanzaBuilder = stanzaBuilder.addAttribute("report", Long.toString(report));
+        stanzaBuilder = stanzaBuilder.addAttribute("time", Long.toString(delta));
+        return stanzaBuilder;
     }
 
     /**
-     * Adds a custom attribute to a BOSH body.
+     * Adds a top-level custom attribute to a BOSH body after the stanza is already built.
      * 
      * @param stanza the BOSH body
      * @param attributeName the name of the attribute