Author: ngn
Date: Tue Jun 23 09:26:37 2009
New Revision: 787596

URL: http://svn.apache.org/viewvc?rev=787596&view=rev
Log:
Adding support for XEP-0199 client to server pings (VYSPER-12)

Added:
    
mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0119_xmppping/
    
mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0119_xmppping/XmppPingIQHandler.java
    
mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0119_xmppping/XmppPingModule.java
    
mina/sandbox/vysper/trunk/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0199_xmppping/
    
mina/sandbox/vysper/trunk/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0199_xmppping/XmppPingIQHandlerTestCase.java
Modified:
    
mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/protocol/NamespaceURIs.java

Added: 
mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0119_xmppping/XmppPingIQHandler.java
URL: 
http://svn.apache.org/viewvc/mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0119_xmppping/XmppPingIQHandler.java?rev=787596&view=auto
==============================================================================
--- 
mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0119_xmppping/XmppPingIQHandler.java
 (added)
+++ 
mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0119_xmppping/XmppPingIQHandler.java
 Tue Jun 23 09:26:37 2009
@@ -0,0 +1,70 @@
+/*
+ *  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.vysper.xmpp.modules.extension.xep0119_xmppping;
+
+import org.apache.vysper.compliance.SpecCompliant;
+import org.apache.vysper.xmpp.modules.core.base.handler.DefaultIQHandler;
+import org.apache.vysper.xmpp.protocol.NamespaceURIs;
+import org.apache.vysper.xmpp.server.ServerRuntimeContext;
+import org.apache.vysper.xmpp.server.SessionContext;
+import org.apache.vysper.xmpp.stanza.IQStanza;
+import org.apache.vysper.xmpp.stanza.IQStanzaType;
+import org.apache.vysper.xmpp.stanza.Stanza;
+import org.apache.vysper.xmpp.stanza.StanzaBuilder;
+
+/**
+ * Implementation of <a 
href="http://xmpp.org/extensions/xep-0199.html";>XEP-0199 XMPP Ping</a>.
+ * 
+ * The specification defines several modes, where client to server is 
currently the only mode
+ * implemented here. An wire protocol example of a client to server ping: 
+ * <pre>
+ * C: &lt;iq from='capulet.lit' to='[email protected]/balcony' id='s2c1' 
type='get'&gt;
+ *      &lt;ping xmlns='urn:xmpp:ping'/&gt;
+ *    &lt;/iq&gt;
+ *
+ * S: &lt;iq from='[email protected]/balcony' to='capulet.lit' id='s2c1' 
type='result'/&gt;
+ * </pre>
+ *  
+ * @author The Apache MINA Project ([email protected])
+ */
+...@speccompliant(spec="xep-0199", status= 
SpecCompliant.ComplianceStatus.IN_PROGRESS, coverage = 
SpecCompliant.ComplianceCoverage.PARTIAL)
+public class XmppPingIQHandler extends DefaultIQHandler {
+
+    public XmppPingIQHandler() {
+    }
+
+    @Override
+    protected boolean verifyNamespace(Stanza stanza) {
+        return verifyInnerNamespace(stanza, NamespaceURIs.URN_XMPP_PING);
+    }
+
+    @Override
+    protected boolean verifyInnerElement(Stanza stanza) {
+        return verifyInnerElementWorker(stanza, "ping");
+    }
+
+    @Override
+    protected Stanza handleGet(IQStanza stanza, ServerRuntimeContext 
serverRuntimeContext, SessionContext sessionContext) {
+
+        StanzaBuilder stanzaBuilder = 
StanzaBuilder.createIQStanza(stanza.getTo(), stanza.getFrom(), 
IQStanzaType.RESULT, stanza.getID());
+
+        return stanzaBuilder.getFinalStanza();
+    }
+}

Added: 
mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0119_xmppping/XmppPingModule.java
URL: 
http://svn.apache.org/viewvc/mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0119_xmppping/XmppPingModule.java?rev=787596&view=auto
==============================================================================
--- 
mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0119_xmppping/XmppPingModule.java
 (added)
+++ 
mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0119_xmppping/XmppPingModule.java
 Tue Jun 23 09:26:37 2009
@@ -0,0 +1,66 @@
+/*
+ *  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.vysper.xmpp.modules.extension.xep0119_xmppping;
+
+import org.apache.vysper.xmpp.modules.DefaultDiscoAwareModule;
+import org.apache.vysper.xmpp.modules.servicediscovery.management.Feature;
+import org.apache.vysper.xmpp.modules.servicediscovery.management.InfoElement;
+import org.apache.vysper.xmpp.modules.servicediscovery.management.InfoRequest;
+import 
org.apache.vysper.xmpp.modules.servicediscovery.management.ServerInfoRequestListener;
+import org.apache.vysper.xmpp.protocol.HandlerDictionary;
+import org.apache.vysper.xmpp.protocol.NamespaceHandlerDictionary;
+import org.apache.vysper.xmpp.protocol.NamespaceURIs;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * A module for <a href="http://xmpp.org/extensions/xep-0199.html";>XEP-0199 
XMPP Ping</a>.
+ *
+ * @author The Apache MINA Project ([email protected])
+ */
+public class XmppPingModule extends DefaultDiscoAwareModule implements 
ServerInfoRequestListener {
+
+    @Override
+    public String getName() {
+        return "XEP-0199 XMPP Ping";
+    }
+
+    @Override
+    public String getVersion() {
+        return "2.0";
+    }
+
+    @Override
+    protected void 
addServerInfoRequestListeners(List<ServerInfoRequestListener> 
serverInfoRequestListeners) {
+        serverInfoRequestListeners.add(this);
+    }
+
+    public List<InfoElement> getServerInfosFor(InfoRequest request) {
+        List<InfoElement> infoElements = new ArrayList<InfoElement>();
+        infoElements.add(new Feature(NamespaceURIs.URN_XMPP_PING));
+        return infoElements;
+    }
+
+    @Override
+    protected void addHandlerDictionaries(List<HandlerDictionary> dictionary) {
+        dictionary.add(new 
NamespaceHandlerDictionary(NamespaceURIs.URN_XMPP_PING, new 
XmppPingIQHandler()));
+    }
+}

Modified: 
mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/protocol/NamespaceURIs.java
URL: 
http://svn.apache.org/viewvc/mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/protocol/NamespaceURIs.java?rev=787596&r1=787595&r2=787596&view=diff
==============================================================================
--- 
mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/protocol/NamespaceURIs.java
 (original)
+++ 
mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/protocol/NamespaceURIs.java
 Tue Jun 23 09:26:37 2009
@@ -46,6 +46,7 @@
     public static final String JABBER_X_DATA = "jabber:x:data";
     public static final String PRIVATE_DATA = "jabber:iq:private";
     public static final String URN_XMPP_TIME = "urn:xmpp:time";
+    public static final String URN_XMPP_PING = "urn:xmpp:ping";
     public static final String VCARD_TEMP  = "vcard-temp";
     public static final String XEP0030_SERVICE_DISCOVERY_ITEMS = 
"http://jabber.org/protocol/disco#items";;
     public static final String XEP0030_SERVICE_DISCOVERY_INFO  = 
"http://jabber.org/protocol/disco#info";;

Added: 
mina/sandbox/vysper/trunk/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0199_xmppping/XmppPingIQHandlerTestCase.java
URL: 
http://svn.apache.org/viewvc/mina/sandbox/vysper/trunk/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0199_xmppping/XmppPingIQHandlerTestCase.java?rev=787596&view=auto
==============================================================================
--- 
mina/sandbox/vysper/trunk/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0199_xmppping/XmppPingIQHandlerTestCase.java
 (added)
+++ 
mina/sandbox/vysper/trunk/src/test/java/org/apache/vysper/xmpp/modules/extension/xep0199_xmppping/XmppPingIQHandlerTestCase.java
 Tue Jun 23 09:26:37 2009
@@ -0,0 +1,67 @@
+package org.apache.vysper.xmpp.modules.extension.xep0199_xmppping;
+
+import junit.framework.TestCase;
+
+import org.apache.vysper.xmpp.addressing.Entity;
+import org.apache.vysper.xmpp.addressing.EntityImpl;
+import 
org.apache.vysper.xmpp.modules.extension.xep0119_xmppping.XmppPingIQHandler;
+import org.apache.vysper.xmpp.protocol.NamespaceURIs;
+import org.apache.vysper.xmpp.protocol.ResponseStanzaContainer;
+import org.apache.vysper.xmpp.server.TestSessionContext;
+import org.apache.vysper.xmpp.stanza.IQStanzaType;
+import org.apache.vysper.xmpp.stanza.Stanza;
+import org.apache.vysper.xmpp.stanza.StanzaBuilder;
+
+/**
+ */
+public class XmppPingIQHandlerTestCase extends TestCase {
+    
+    private static final String IQ_ID = "id1";
+
+    private TestSessionContext sessionContext;
+
+    protected Entity client;
+    protected Entity boundClient;
+    protected Entity server;
+    protected XmppPingIQHandler handler;
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        
+        client = EntityImpl.parse("[email protected]");
+        
+        sessionContext = 
TestSessionContext.createWithStanzaReceiverRelayAuthenticated();
+        sessionContext.setInitiatingEntity(client);
+        
+        boundClient = new EntityImpl(client, sessionContext.bindResource());
+        server = sessionContext.getServerJID();
+        
+        handler = new XmppPingIQHandler();
+    }
+
+    public void testClientToServerPing() {
+        // C: <iq from='capulet.lit' to='[email protected]/balcony' id='s2c1' 
type='get'>
+        //      <ping xmlns='urn:xmpp:ping'/>
+        //    </iq>
+        //
+        // S: <iq from='[email protected]/balcony' to='capulet.lit' id='s2c1' 
type='result'/>
+        
+        StanzaBuilder stanzaBuilder = 
StanzaBuilder.createIQStanza(boundClient, server, IQStanzaType.GET, IQ_ID);
+        
stanzaBuilder.startInnerElement("ping").addNamespaceAttribute(NamespaceURIs.URN_XMPP_PING).endInnerElement();
+
+        Stanza requestStanza = stanzaBuilder.getFinalStanza();
+        ResponseStanzaContainer resp = handler.execute(requestStanza, 
sessionContext.getServerRuntimeContext(), true, sessionContext, null);
+
+        // we should always get a response
+        assertTrue(resp.hasResponse());
+        
+        Stanza respStanza = resp.getResponseStanza();
+
+        assertEquals("iq", respStanza.getName());
+        assertEquals(boundClient, respStanza.getTo());
+        assertEquals(server, respStanza.getFrom());
+        assertEquals(IQ_ID, respStanza.getAttributeValue("id"));
+        assertEquals("result", respStanza.getAttributeValue("type"));
+    }
+}


Reply via email to