Skip to content
Browse files

+ reduce created objects, move helper methods from BoshHandler to sta…

…tic utilities

git-svn-id: https://svn.apache.org/repos/asf/mina/vysper/trunk@1329113 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information...
1 parent ecfe145 commit 430e781f2b341c73d4b372cb0ac2494d14c510cd Bernd Fondermann committed Apr 23, 2012
View
10 ...bosh/src/main/java/org/apache/vysper/xmpp/extension/xep0124/BoshBackedSessionContext.java
@@ -39,6 +39,7 @@
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 @@ private void error(BoshRequest br, String condition) {
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 @@ protected void respondToPause(int pause) {
}
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) {
View
19 ...0124-xep0206-bosh/src/main/java/org/apache/vysper/xmpp/extension/xep0124/BoshHandler.java
@@ -100,7 +100,24 @@ public void process(HttpServletRequest httpRequest, Stanza body) {
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 {
View
20 ...-xep0206-bosh/src/main/java/org/apache/vysper/xmpp/extension/xep0124/BoshStanzaUtils.java
@@ -22,7 +22,7 @@
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 static Stanza mergeResponses(Collection<Stanza> mergeCandidates) {
/**
* 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

0 comments on commit 430e781

Please sign in to comment.
Something went wrong with that request. Please try again.