Author: ngn
Date: Sun Aug 15 20:55:35 2010
New Revision: 985754
URL: http://svn.apache.org/viewvc?rev=985754&view=rev
Log:
Implement detection of over activity (VYSPER-234, by Bogdan Pistol)
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/test/java/org/apache/vysper/xmpp/extension/xep0124/BoshBackedSessionContextTest.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=985754&r1=985753&r2=985754&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
Sun Aug 15 20:55:35 2010
@@ -30,6 +30,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.eclipse.jetty.continuation.Continuation;
import org.eclipse.jetty.continuation.ContinuationListener;
@@ -149,11 +150,31 @@ public class BoshBackedSessionContext ex
continuation.setAttribute("response", boshResponse);
continuation.resume();
}
+
+ /**
+ * Writes an error to the client and closes the connection
+ * @param condition the error condition
+ */
+ synchronized public void error(String condition) {
+ if (!requestsWindow.isEmpty()) {
+ BoshRequest req = requestsWindow.remove(requestsWindow.firstKey());
+ Stanza stanza = boshHandler.getTerminateResponse();
+ stanza = boshHandler.addAttribute(stanza, "condition", condition);
+ BoshResponse boshResponse = getBoshResponse(stanza, null);
+ if (LOGGER.isDebugEnabled()) {
+ LOGGER.debug("BOSH writing response: {}", new
String(boshResponse.getContent()));
+ }
+ Continuation continuation =
ContinuationSupport.getContinuation(req.getHttpServletRequest());
+ continuation.setAttribute("response", boshResponse);
+ continuation.resume();
+ }
+ close();
+ }
/*
* Terminates the BOSH session
*/
- public void close() {
+ synchronized public void close() {
// respond to all the queued HTTP requests with empty responses
while (!requestsWindow.isEmpty()) {
write0(boshHandler.getEmptyResponse());
@@ -305,6 +326,12 @@ public class BoshBackedSessionContext ex
// TODO: return the old response
return;
}
+ if (requestsWindow.size() + 1 > requests &&
!"terminate".equals(br.getBody().getAttributeValue("type"))
+ && br.getBody().getAttributeValue("pause") == null) {
+ // overactivity
+ error("policy-violation");
+ return;
+ }
Continuation continuation =
ContinuationSupport.getContinuation(br.getHttpServletRequest());
continuation.setTimeout(wait * 1000);
continuation.suspend();
@@ -357,7 +384,7 @@ public class BoshBackedSessionContext ex
private BoshResponse getBoshResponse(Stanza stanza, Long ack) {
if (ack != null) {
- stanza = boshHandler.addAck(stanza, ack);
+ stanza = boshHandler.addAttribute(stanza, "ack", ack.toString());
}
byte[] content = new Renderer(stanza).getComplete().getBytes();
return new BoshResponse(contentType, content);
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=985754&r1=985753&r2=985754&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
Sun Aug 15 20:55:35 2010
@@ -275,18 +275,30 @@ public class BoshHandler {
return wrapStanza(features);
}
- private Stanza getTerminateResponse() {
+ /**
+ * Creates a session termination BOSH response
+ * @return the termination BOSH body
+ */
+ public Stanza getTerminateResponse() {
StanzaBuilder stanzaBuilder = new StanzaBuilder("body",
NamespaceURIs.XEP0124_BOSH);
stanzaBuilder.addAttribute("type", "terminate");
return stanzaBuilder.build();
}
- public Stanza addAck(Stanza stanza, Long ack) {
+ /**
+ * Adds a custom attribute to a BOSH body.
+ *
+ * @param stanza the BOSH body
+ * @param attributeName the name of the attribute
+ * @param attributeValue the value of the attribute
+ * @return a new BOSH body identical with the one provided except it also
has the newly added attribute
+ */
+ public Stanza addAttribute(Stanza stanza, String attributeName, String
attributeValue) {
StanzaBuilder stanzaBuilder = new StanzaBuilder("body",
NamespaceURIs.XEP0124_BOSH);
for (Attribute attr : stanza.getAttributes()) {
stanzaBuilder.addAttribute(attr);
}
- stanzaBuilder.addAttribute("ack", ack.toString());
+ stanzaBuilder.addAttribute(attributeName, attributeValue);
for (XMLElement element : stanza.getInnerElements()) {
stanzaBuilder.addPreparedElement(element);
}
Modified:
mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/src/test/java/org/apache/vysper/xmpp/extension/xep0124/BoshBackedSessionContextTest.java
URL:
http://svn.apache.org/viewvc/mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/src/test/java/org/apache/vysper/xmpp/extension/xep0124/BoshBackedSessionContextTest.java?rev=985754&r1=985753&r2=985754&view=diff
==============================================================================
---
mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/src/test/java/org/apache/vysper/xmpp/extension/xep0124/BoshBackedSessionContextTest.java
(original)
+++
mina/vysper/trunk/server/extensions/xep0124-xep0206-bosh/src/test/java/org/apache/vysper/xmpp/extension/xep0124/BoshBackedSessionContextTest.java
Sun Aug 15 20:55:35 2010
@@ -172,7 +172,7 @@ public class BoshBackedSessionContextTes
continuation2.addContinuationListener(EasyMock.<ContinuationListener>
anyObject());
Stanza body = new StanzaBuilder("body",
NamespaceURIs.XEP0124_BOSH).build();
- expect(boshHandler.addAck(eq(body),
EasyMock.anyLong())).andReturn(body);
+ expect(boshHandler.addAttribute(eq(body), eq("ack"),
Long.toString(EasyMock.anyLong()))).andReturn(body);
// write0
Capture<BoshResponse> captured = new Capture<BoshResponse>();