labs-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ber...@apache.org
Subject svn commit: r657604 - in /labs/vysper/src: main/java/org/apache/vysper/xmpp/addressing/ main/java/org/apache/vysper/xmpp/modules/core/base/handler/ main/java/org/apache/vysper/xmpp/protocol/ main/java/org/apache/vysper/xmpp/server/response/ main/java/o...
Date Sun, 18 May 2008 17:54:43 GMT
Author: berndf
Date: Sun May 18 10:54:43 2008
New Revision: 657604

URL: http://svn.apache.org/viewvc?rev=657604&view=rev
Log:
[vysper] track proper 'from' addresses in ProtocolWorker, else return stanza error
cover new stanza errors from RFC3920bis

Modified:
    labs/vysper/src/main/java/org/apache/vysper/xmpp/addressing/Entity.java
    labs/vysper/src/main/java/org/apache/vysper/xmpp/addressing/EntityImpl.java
    labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/core/base/handler/PresenceHandler.java
    labs/vysper/src/main/java/org/apache/vysper/xmpp/protocol/ProtocolWorker.java
    labs/vysper/src/main/java/org/apache/vysper/xmpp/protocol/ResponseWriter.java
    labs/vysper/src/main/java/org/apache/vysper/xmpp/server/response/ServerErrorResponses.java
    labs/vysper/src/main/java/org/apache/vysper/xmpp/stanza/StanzaErrorCondition.java
    labs/vysper/src/test/java/org/apache/vysper/xmpp/protocol/ProtocolWorkerProcessTestCase.java

Modified: labs/vysper/src/main/java/org/apache/vysper/xmpp/addressing/Entity.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/java/org/apache/vysper/xmpp/addressing/Entity.java?rev=657604&r1=657603&r2=657604&view=diff
==============================================================================
--- labs/vysper/src/main/java/org/apache/vysper/xmpp/addressing/Entity.java (original)
+++ labs/vysper/src/main/java/org/apache/vysper/xmpp/addressing/Entity.java Sun May 18 10:54:43
2008
@@ -52,7 +52,7 @@
     /**
      * @return string like "node@domain"
      */
-    String getBareJID();
+    Entity getBareJID();
     
     String getCanonicalizedName();
     

Modified: labs/vysper/src/main/java/org/apache/vysper/xmpp/addressing/EntityImpl.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/java/org/apache/vysper/xmpp/addressing/EntityImpl.java?rev=657604&r1=657603&r2=657604&view=diff
==============================================================================
--- labs/vysper/src/main/java/org/apache/vysper/xmpp/addressing/EntityImpl.java (original)
+++ labs/vysper/src/main/java/org/apache/vysper/xmpp/addressing/EntityImpl.java Sun May 18
10:54:43 2008
@@ -93,8 +93,8 @@
         return buffer.toString();
     }
 
-    public String getBareJID() {
-		return buildEntityString(node, domain, null);
+    public Entity getBareJID() {
+		return new EntityImpl(node, domain, null);
 	}
 
     public String getCanonicalizedName() {

Modified: labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/core/base/handler/PresenceHandler.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/core/base/handler/PresenceHandler.java?rev=657604&r1=657603&r2=657604&view=diff
==============================================================================
--- labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/core/base/handler/PresenceHandler.java
(original)
+++ labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/core/base/handler/PresenceHandler.java
Sun May 18 10:54:43 2008
@@ -223,8 +223,7 @@
 		for (String resource : resources) {
 			Entity userResource = new EntityImpl(user, resource);
 			// TODO: determine the right subscription: 'none' or 'to'
-			Stanza push = buildRosterPushStanza(userResource
-					.getFullQualifiedName(),
+			Stanza push = buildRosterPushStanza(userResource,
 					sessionContext.nextSequenceValue(), contact.getBareJID(),
 					"???");
 
@@ -244,8 +243,7 @@
 		for (String resource : resources) {
 			Entity userResource = new EntityImpl(user, resource);
 			// TODO: determine the correct subscription state: 'none' or 'from'
-			Stanza push = buildRosterPushStanza(userResource
-					.getFullQualifiedName(),
+			Stanza push = buildRosterPushStanza(userResource,
 					sessionContext.nextSequenceValue(), contact.getBareJID(),
 					"???");
 			relayStanza(userResource, push, sessionContext);
@@ -266,8 +264,7 @@
 		for (String resource : resources) {
 			Entity userResource = new EntityImpl(user, resource);
 			// TODO: determine the correct subscription state
-			Stanza push = buildRosterPushStanza(userResource
-					.getFullQualifiedName(),
+			Stanza push = buildRosterPushStanza(userResource,
 					sessionContext.nextSequenceValue(), contact.getBareJID(),
 					"???");
 			relayStanza(userResource, push, sessionContext);
@@ -287,8 +284,7 @@
 		for (String resource : resources) {
 			Entity userResource = new EntityImpl(user, resource);
 			// TODO: determine the right subscription: 'to' or 'none'
-			Stanza push = buildRosterPushStanza(userResource
-					.getFullQualifiedName(),
+			Stanza push = buildRosterPushStanza(userResource,
 					sessionContext.nextSequenceValue(), contact.getBareJID(),
 					"???");
 
@@ -309,8 +305,7 @@
 
 		for (String resource : resources) {
 			Entity userResource = new EntityImpl(user, resource);
-			Stanza push = buildRosterPushStanza(userResource
-					.getFullQualifiedName(),
+			Stanza push = buildRosterPushStanza(userResource,
 					sessionContext.nextSequenceValue(), contact.getBareJID(),
 					"from");
 
@@ -322,8 +317,7 @@
 		resources = registry.getAvailableResources(user);
 		for (String resource : resources) {
 			Entity userResource = new EntityImpl(user, resource);
-			Stanza presence = buildPresenceStanza(userResource
-					.getFullQualifiedName(), contact.getBareJID(), null);
+			Stanza presence = buildPresenceStanza(userResource, contact.getBareJID(), null);
 
 			relayStanza(contact, presence, sessionContext);
 		}
@@ -342,8 +336,8 @@
 			List<String> resources = registry.getInterestedResources(user);
 			for (String resource : resources) {
 				Entity userResource = new EntityImpl(user, resource);
-				Stanza push = buildRosterPushStanza(userResource.getFullQualifiedName(), sessionContext
-						.nextSequenceValue(), contact.getBareJID(), "to");
+				Stanza push = buildRosterPushStanza(userResource, sessionContext.nextSequenceValue(),

+                        contact.getBareJID(), "to");
 				relayStanza(userResource, push, sessionContext);
 			}
 		} else {
@@ -414,8 +408,7 @@
 		List<String> resources = registry.getInterestedResources(user);
 		for (String resource : resources) {
 			Entity userResource = new EntityImpl(user, resource);
-			Stanza push = buildRosterPushStanza(userResource
-					.getFullQualifiedName(),
+			Stanza push = buildRosterPushStanza(userResource,
 					sessionContext.nextSequenceValue(), contact.getBareJID(),
 					"none");
 			try {
@@ -444,7 +437,7 @@
         List<Entity> contacts1 = new ArrayList<Entity>();
         List<Entity> contacts = contacts1;
         for (Entity contact : contacts) {
-            Stanza probeStanza = buildPresenceStanza(user.getFullQualifiedName(), contact.getBareJID(),
null);
+            Stanza probeStanza = buildPresenceStanza(user, contact, null);
             relayStanza(contact, probeStanza, sessionContext);
         }
 
@@ -455,7 +448,7 @@
         // above
         contacts = new ArrayList<Entity>();
         for (Entity contact : contacts) {
-            Stanza presenceStanza = buildPresenceStanza(user.getFullQualifiedName(), contact.getBareJID(),
null);
+            Stanza presenceStanza = buildPresenceStanza(user, contact, null);
             relayStanza(contact, presenceStanza, sessionContext);
         }
 
@@ -464,8 +457,7 @@
         List<String> resources = registry.getAvailableResources(user);
         for (String resource : resources) {
             Entity otherResource = new EntityImpl(user, resource);
-            Stanza presenceStanza = buildPresenceStanza(user.getFullQualifiedName(),
-                    otherResource.getFullQualifiedName(), null);
+            Stanza presenceStanza = buildPresenceStanza(user, otherResource, null);
             relayStanza(otherResource, presenceStanza, sessionContext);
         }
 
@@ -483,26 +475,26 @@
 		}
 	}
 
-	private PresenceStanza buildPresenceStanza(String from, String to,
+	private PresenceStanza buildPresenceStanza(Entity from, Entity to,
 			String type) {
 		StanzaBuilder builder = new StanzaBuilder("presence");
-		builder.addAttribute("from", from);
-		builder.addAttribute("to", to);
+		builder.addAttribute("from", from.getFullQualifiedName());
+		builder.addAttribute("to", to.getFullQualifiedName());
 		if (type != null) {
 			builder.addAttribute("type", type);
 		}
 		return (PresenceStanza) XMPPCoreStanza.getWrapper(builder.getFinalStanza());
 	}
 
-	private Stanza buildRosterPushStanza(String to, String id,
-			String bareJidOfRosterItem, String subscription, String ask) {
+	private Stanza buildRosterPushStanza(Entity to, String id,
+			Entity bareJidOfRosterItem, String subscription, String ask) {
 		StanzaBuilder builder = new StanzaBuilder("iq");
-		builder.addAttribute("to", to);
+		builder.addAttribute("to", to.getFullQualifiedName());
 		builder.addAttribute("type", "set");
 		builder.addAttribute("id", id);
 		builder.startInnerElement("query", NamespaceURIs.JABBER_IQ_ROSTER);
 		builder.startInnerElement("item");
-		builder.addAttribute("jid", bareJidOfRosterItem);
+		builder.addAttribute("jid", bareJidOfRosterItem.getBareJID().getFullQualifiedName());
 		builder.addAttribute("subscription", subscription);
 		if (ask != null) {
 			builder.addAttribute("ask", ask);
@@ -513,8 +505,8 @@
 		return builder.getFinalStanza();
 	}
 
-	private Stanza buildRosterPushStanza(String to, String id,
-			String bareJidOfRosterItem, String subscription) {
+	private Stanza buildRosterPushStanza(Entity to, String id,
+			Entity bareJidOfRosterItem, String subscription) {
 		return buildRosterPushStanza(to, id, bareJidOfRosterItem, subscription,
 				null);
 	}

Modified: labs/vysper/src/main/java/org/apache/vysper/xmpp/protocol/ProtocolWorker.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/java/org/apache/vysper/xmpp/protocol/ProtocolWorker.java?rev=657604&r1=657603&r2=657604&view=diff
==============================================================================
--- labs/vysper/src/main/java/org/apache/vysper/xmpp/protocol/ProtocolWorker.java (original)
+++ labs/vysper/src/main/java/org/apache/vysper/xmpp/protocol/ProtocolWorker.java Sun May
18 10:54:43 2008
@@ -31,6 +31,7 @@
 import org.apache.vysper.xmpp.server.SessionState;
 import org.apache.vysper.xmpp.stanza.Stanza;
 import org.apache.vysper.xmpp.stanza.XMPPCoreStanza;
+import org.apache.vysper.xmpp.addressing.Entity;
 
 import java.util.HashMap;
 import java.util.Map;
@@ -112,6 +113,22 @@
                 responseWriter.handleNotAuthorized(sessionContext, stanza);
                 return;
             }
+        } 
+
+        // make sure that 'from' (if present) matches the bare authorized entity
+        // else repond with a stanza error 'unknown-sender'
+        // see rfc3920_draft-saintandre-rfc3920bis-04.txt 8.5.4
+        if (sessionContext.getInitiatingEntity() != null) {
+            XMPPCoreStanza coreStanza = XMPPCoreStanza.getWrapper(stanza);
+            if (coreStanza != null && coreStanza.getFrom() != null) {
+                Entity from = coreStanza.getFrom().getBareJID();
+                Entity initiatingEntity = sessionContext.getInitiatingEntity();
+                if (!initiatingEntity.equals(from)) {
+                    responseWriter.handleWrongFromJID(sessionContext, coreStanza);
+                    return;
+                }
+            }
+            
         }
 
         stateAwareProtocolWorker.processStanza(sessionContext, sessionStateHolder,

Modified: labs/vysper/src/main/java/org/apache/vysper/xmpp/protocol/ResponseWriter.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/java/org/apache/vysper/xmpp/protocol/ResponseWriter.java?rev=657604&r1=657603&r2=657604&view=diff
==============================================================================
--- labs/vysper/src/main/java/org/apache/vysper/xmpp/protocol/ResponseWriter.java (original)
+++ labs/vysper/src/main/java/org/apache/vysper/xmpp/protocol/ResponseWriter.java Sun May
18 10:54:43 2008
@@ -22,6 +22,9 @@
 import org.apache.vysper.xmpp.server.response.ServerErrorResponses;
 import org.apache.vysper.xmpp.server.response.ServerResponses;
 import org.apache.vysper.xmpp.stanza.Stanza;
+import org.apache.vysper.xmpp.stanza.StanzaErrorCondition;
+import org.apache.vysper.xmpp.stanza.StanzaErrorType;
+import org.apache.vysper.xmpp.stanza.XMPPCoreStanza;
 import org.apache.vysper.xmpp.parser.ParsingException;
 import org.apache.vysper.xmpp.parser.ParsingErrorCondition;
 
@@ -89,6 +92,15 @@
         writeErrorAndClose(sessionContext, errorStanza);
     }
 
+    public void handleWrongFromJID(SessionContext sessionContext, XMPPCoreStanza receivedStanza)
{
+        Stanza errorStanza = ServerErrorResponses.getInstance().getStanzaError(StanzaErrorCondition.UNKNOWN_SENDER,
+                                                          receivedStanza,
+                                                          StanzaErrorType.MODIFY,
+                                                          "from attribute does not match
authorized entity",
+                                                          null, null);
+        writeResponse(sessionContext, errorStanza);
+    }
+
     public void handleParsingException(SessionContext sessionContext, ParsingException e)
{
         //TODO write the __right__ error response, not bad-format default only
         if (e.getErrorCondition() != ParsingErrorCondition.BAD_FORMAT) throw new RuntimeException("cannot
handle this error condition yet");

Modified: labs/vysper/src/main/java/org/apache/vysper/xmpp/server/response/ServerErrorResponses.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/java/org/apache/vysper/xmpp/server/response/ServerErrorResponses.java?rev=657604&r1=657603&r2=657604&view=diff
==============================================================================
--- labs/vysper/src/main/java/org/apache/vysper/xmpp/server/response/ServerErrorResponses.java
(original)
+++ labs/vysper/src/main/java/org/apache/vysper/xmpp/server/response/ServerErrorResponses.java
Sun May 18 10:54:43 2008
@@ -82,7 +82,7 @@
     /**
      * TODO move to a more general error handling
      * @param errorCondition - corresponds to one of the defined stanza error conditions
-     * @param stanza
+     * @param stanza - the stanza to which the error stanza is the answer
      * @param type
      * @param errorText - is optional together with errorLang, both together might be NULL
      * @param errorLang - must be present, if errorText is not NULL

Modified: labs/vysper/src/main/java/org/apache/vysper/xmpp/stanza/StanzaErrorCondition.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/java/org/apache/vysper/xmpp/stanza/StanzaErrorCondition.java?rev=657604&r1=657603&r2=657604&view=diff
==============================================================================
--- labs/vysper/src/main/java/org/apache/vysper/xmpp/stanza/StanzaErrorCondition.java (original)
+++ labs/vysper/src/main/java/org/apache/vysper/xmpp/stanza/StanzaErrorCondition.java Sun
May 18 10:54:43 2008
@@ -36,6 +36,7 @@
     NOT_ACCEPTABLE ("not-acceptable"),
     NOT_ALLOWED ("not-allowed"),
     NOT_AUTHORIZED ("not-authorized"),
+    NOT_MODIFIED ("not-modified"),
     PAYMENT_REQUIRED ("payment-required"),
     RECIPIENT_UNAVAILABLE ("recipient-unavailable"),
     REDIRECT ("redirect"),
@@ -46,7 +47,8 @@
     SERVICE_UNAVAILABLE ("service-unavailable"),
     SUBSCRIPTION_REQUIRED ("subscription-required"),
     UNDEFINED_CONDITION ("undefined-condition"),
-    UNEXPECTED_REQUEST ("unexpected-request");
+    UNEXPECTED_REQUEST ("unexpected-request"),
+    UNKNOWN_SENDER ("unknown-sender");
 
     private final String value;
 

Modified: labs/vysper/src/test/java/org/apache/vysper/xmpp/protocol/ProtocolWorkerProcessTestCase.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/test/java/org/apache/vysper/xmpp/protocol/ProtocolWorkerProcessTestCase.java?rev=657604&r1=657603&r2=657604&view=diff
==============================================================================
--- labs/vysper/src/test/java/org/apache/vysper/xmpp/protocol/ProtocolWorkerProcessTestCase.java
(original)
+++ labs/vysper/src/test/java/org/apache/vysper/xmpp/protocol/ProtocolWorkerProcessTestCase.java
Sun May 18 10:54:43 2008
@@ -22,10 +22,14 @@
 import org.apache.vysper.xmpp.server.TestSessionContext;
 import org.apache.vysper.xmpp.stanza.Stanza;
 import org.apache.vysper.xmpp.stanza.StanzaBuilder;
+import org.apache.vysper.xmpp.stanza.IQStanzaType;
+import org.apache.vysper.xmpp.stanza.IQStanza;
+import org.apache.vysper.xmpp.stanza.XMPPCoreStanza;
 import org.apache.vysper.xmpp.addressing.Entity;
 import org.apache.vysper.xmpp.addressing.EntityImpl;
 import org.apache.vysper.xmpp.xmlfragment.XMLElementVerifier;
 import org.apache.vysper.xmpp.xmlfragment.XMLElement;
+import org.apache.vysper.xmpp.xmlfragment.XMLSemanticError;
 import org.apache.vysper.xmpp.delivery.StanzaReceiverRelay;
 import junit.framework.TestCase;
 
@@ -148,4 +152,37 @@
         assertTrue("closed", sessionContext.isClosed());
     }
 
+    public void testTrackProperFromAddress() throws XMLSemanticError {
+
+        Entity server = sessionContext.getServerRuntimeContext().getServerEnitity();
+        sessionContext.setSessionState(SessionState.AUTHENTICATED);
+        sessionContext.setInitiatingEntity(new EntityImpl("mark", server.getDomain(), null));
+
+        CallTestStanzaHandler stanzaHandler = new CallTestStanzaHandler("ProtocolWorkerProcessTestCase");
+        namespaceHandlerDictionary.register(stanzaHandler);
+
+        StanzaBuilder stanzaBuilder = StanzaBuilder.createIQStanza(IQStanzaType.GET, "test");
+        stanzaBuilder.addAttribute("from", new EntityImpl("ernest", server.getDomain(), null).getFullQualifiedName());
+        stanzaBuilder.startInnerElement("query").addNamespaceAttribute("jabber:iq:roster").endInnerElement();
+        
+        protocolWorker.processStanza(sessionContext, stanzaBuilder.getFinalStanza(), sessionStateHolder);
+
+        try {
+            stanzaHandler.assertHandlerCalled();
+            fail("handler called");
+        } catch (Exception e) {
+            // not called, OK
+        }
+        Stanza recordedResponse = sessionContext.getRecordedResponse();
+        XMLElementVerifier verifier = recordedResponse.getVerifier();
+        assertEquals("iq stanza error", "iq", recordedResponse.getName());
+        IQStanza iqStanza = (IQStanza) XMPPCoreStanza.getWrapper(recordedResponse);
+        assertEquals("error", iqStanza.getType());
+        assertTrue("error embedded", verifier.subElementPresent("error"));
+        XMLElement errorInner = recordedResponse.getSingleInnerElementsNamed("error");
+        assertEquals("modify", errorInner.getAttribute("type").getValue());
+        XMLElementVerifier errorVerifier = errorInner.getVerifier();
+        errorVerifier.subElementPresent("unknown-sender");
+    }
+
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@labs.apache.org
For additional commands, e-mail: commits-help@labs.apache.org


Mime
View raw message