You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@labs.apache.org by be...@apache.org on 2008/10/23 22:44:33 UTC

svn commit: r707470 - in /labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/roster: RosterBadRequestException.java RosterConfiguration.java RosterException.java RosterGroup.java RosterNotAcceptableException.java handler/RosterIQHandler.java

Author: berndf
Date: Thu Oct 23 13:44:33 2008
New Revision: 707470

URL: http://svn.apache.org/viewvc?rev=707470&view=rev
Log:
[vysper] improve roster set

Added:
    labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/roster/RosterBadRequestException.java
    labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/roster/RosterConfiguration.java
    labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/roster/RosterNotAcceptableException.java
Modified:
    labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/roster/RosterException.java
    labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/roster/RosterGroup.java
    labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/roster/handler/RosterIQHandler.java

Added: labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/roster/RosterBadRequestException.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/roster/RosterBadRequestException.java?rev=707470&view=auto
==============================================================================
--- labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/roster/RosterBadRequestException.java (added)
+++ labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/roster/RosterBadRequestException.java Thu Oct 23 13:44:33 2008
@@ -0,0 +1,38 @@
+/***********************************************************************
+ * Copyright (c) 2006-2007 The Apache Software Foundation.             *
+ * All rights reserved.                                                *
+ * ------------------------------------------------------------------- *
+ * Licensed 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.roster;
+
+/**
+ */
+public class RosterBadRequestException extends RosterException {
+
+    public RosterBadRequestException() {
+        super();
+    }
+
+    public RosterBadRequestException(String s) {
+        super(s);
+    }
+
+    public RosterBadRequestException(String s, Throwable throwable) {
+        super(s, throwable);
+    }
+
+    public RosterBadRequestException(Throwable throwable) {
+        super(throwable);
+    }
+}

Added: labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/roster/RosterConfiguration.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/roster/RosterConfiguration.java?rev=707470&view=auto
==============================================================================
--- labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/roster/RosterConfiguration.java (added)
+++ labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/roster/RosterConfiguration.java Thu Oct 23 13:44:33 2008
@@ -0,0 +1,25 @@
+/***********************************************************************
+ * Copyright (c) 2006-2007 The Apache Software Foundation.             *
+ * All rights reserved.                                                *
+ * ------------------------------------------------------------------- *
+ * Licensed 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.roster;
+
+public class RosterConfiguration {
+
+    public static int ROSTER_ITEM_NAME_MAX_LENGTH = 4096;
+    public static int ROSTER_GROUP_NAME_MAX_LENGTH = 4096;
+    public static boolean ROSTER_ITEM_GROUP_ALLOW_DUPLICATES = true;
+
+}

Modified: labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/roster/RosterException.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/roster/RosterException.java?rev=707470&r1=707469&r2=707470&view=diff
==============================================================================
--- labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/roster/RosterException.java (original)
+++ labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/roster/RosterException.java Thu Oct 23 13:44:33 2008
@@ -1,3 +1,19 @@
+/***********************************************************************
+ * Copyright (c) 2006-2007 The Apache Software Foundation.             *
+ * All rights reserved.                                                *
+ * ------------------------------------------------------------------- *
+ * Licensed 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.roster;
 
 /**

Modified: labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/roster/RosterGroup.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/roster/RosterGroup.java?rev=707470&r1=707469&r2=707470&view=diff
==============================================================================
--- labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/roster/RosterGroup.java (original)
+++ labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/roster/RosterGroup.java Thu Oct 23 13:44:33 2008
@@ -29,4 +29,21 @@
     public String getName() {
         return name;
     }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) return true;
+        if (o == null || getClass() != o.getClass()) return false;
+
+        RosterGroup that = (RosterGroup) o;
+
+        if (name != null ? !name.equals(that.name) : that.name != null) return false;
+
+        return true;
+    }
+
+    @Override
+    public int hashCode() {
+        return name != null ? name.hashCode() : 0;
+    }
 }

Added: labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/roster/RosterNotAcceptableException.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/roster/RosterNotAcceptableException.java?rev=707470&view=auto
==============================================================================
--- labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/roster/RosterNotAcceptableException.java (added)
+++ labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/roster/RosterNotAcceptableException.java Thu Oct 23 13:44:33 2008
@@ -0,0 +1,38 @@
+/***********************************************************************
+ * Copyright (c) 2006-2007 The Apache Software Foundation.             *
+ * All rights reserved.                                                *
+ * ------------------------------------------------------------------- *
+ * Licensed 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.roster;
+
+/**
+ */
+public class RosterNotAcceptableException extends RosterException {
+
+    public RosterNotAcceptableException() {
+        super();
+    }
+
+    public RosterNotAcceptableException(String s) {
+        super(s);
+    }
+
+    public RosterNotAcceptableException(String s, Throwable throwable) {
+        super(s, throwable);
+    }
+
+    public RosterNotAcceptableException(Throwable throwable) {
+        super(throwable);
+    }
+}

Modified: labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/roster/handler/RosterIQHandler.java
URL: http://svn.apache.org/viewvc/labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/roster/handler/RosterIQHandler.java?rev=707470&r1=707469&r2=707470&view=diff
==============================================================================
--- labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/roster/handler/RosterIQHandler.java (original)
+++ labs/vysper/src/main/java/org/apache/vysper/xmpp/modules/roster/handler/RosterIQHandler.java Thu Oct 23 13:44:33 2008
@@ -25,10 +25,8 @@
 import org.apache.vysper.xmpp.resourcebinding.ResourceState;
 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;
+import org.apache.vysper.xmpp.server.response.ServerErrorResponses;
+import org.apache.vysper.xmpp.stanza.*;
 import org.apache.vysper.xmpp.addressing.Entity;
 import org.apache.vysper.xmpp.addressing.EntityImpl;
 import org.apache.vysper.xmpp.addressing.EntityFormatException;
@@ -152,18 +150,13 @@
 
         Entity user = determineFrom(stanza, sessionContext);
 
-        XMLElement itemElement = null;
-        RosterItem setRosterItem = null;
+        RosterItem setRosterItem;
         try {
-            XMLElement queryElement = stanza.getSingleInnerElementsNamed("query");
-            if (queryElement != null) {
-                itemElement = queryElement.getSingleInnerElementsNamed("item");
-
-                setRosterItem = parseRosterItem(itemElement);
-            }
-        } catch (XMLSemanticError xmlSemanticError) {
-            xmlSemanticError.printStackTrace();
-            throw new RuntimeException("roster set needs proper item node", xmlSemanticError);
+            setRosterItem = parseRosterItem(stanza);
+        } catch (RosterBadRequestException e) {
+            return ServerErrorResponses.getInstance().getStanzaError(StanzaErrorCondition.BAD_REQUEST, stanza, StanzaErrorType.MODIFY, e.getMessage(), null, null);
+        } catch (RosterNotAcceptableException e) {
+            return ServerErrorResponses.getInstance().getStanzaError(StanzaErrorCondition.NOT_ACCEPTABLE, stanza, StanzaErrorType.MODIFY, e.getMessage(), null, null);
         }
 
         RosterItem existingItem;
@@ -175,47 +168,80 @@
 
         if (setRosterItem.getSubscriptionType() == SubscriptionType.REMOVE) {
             throw new RuntimeException("roster set REMOVE not yet implemented");
-        }
+        } /* else: all other subscription types are ignored in a roster set */
 
         if (existingItem == null) {
-            // TODO
-            return null;
+            return ServerErrorResponses.getInstance().getStanzaError(StanzaErrorCondition.BAD_REQUEST, stanza, StanzaErrorType.CANCEL, "roster item contact not (yet) in roster: " + setRosterItem.getJid().getBareJID(), null, null);
         }
 
-        if (setRosterItem.getName() != null) {
-            System.out.println("set name to " + setRosterItem.getName());
+        synchronized (existingItem) {
+            if (setRosterItem.getName() != null) {
+                // TODO set name on item
+                System.out.println("set name to " + setRosterItem.getName());
+            }
+            // TODO set groups on item
         }
 
-        return null;
+        return StanzaBuilder.createIQStanza(null, user, IQStanzaType.RESULT, stanza.getID()).getFinalStanza();
     }
 
-    private RosterItem parseRosterItem(XMLElement itemElement) throws org.apache.vysper.xmpp.xmlfragment.XMLSemanticError {
+    private RosterItem parseRosterItem(IQStanza stanza) throws RosterBadRequestException, RosterNotAcceptableException {
+        XMLElement queryElement;
+        try {
+            queryElement = stanza.getSingleInnerElementsNamed("query");
+            if (queryElement == null) throw new XMLSemanticError("missing query node");
+        } catch (XMLSemanticError xmlSemanticError) {
+            throw new RosterBadRequestException("roster set needs a single query node.");
+        }
 
-        if (itemElement == null) {
-            throw new XMLSemanticError("missing 'item' element");
+        XMLElement itemElement;
+        try {
+            itemElement = queryElement.getSingleInnerElementsNamed("item");
+            if (itemElement == null) throw new XMLSemanticError("missing item node");
+        } catch (XMLSemanticError xmlSemanticError) {
+            throw new RosterBadRequestException("roster set needs a single item node.");
         }
+
         Attribute attributeJID = itemElement.getAttribute("jid");
-        if (attributeJID == null || attributeJID.getValue() == null) throw new XMLSemanticError("missing 'jid' attribute");
+        if (attributeJID == null || attributeJID.getValue() == null) throw new RosterBadRequestException("missing 'jid' attribute on item node");
 
         XMLElementVerifier verifier = itemElement.getVerifier();
         String name = verifier.attributePresent("name") ? itemElement.getAttribute("name").getValue() : null;
-        SubscriptionType subscription = verifier.attributePresent("subscription") ? SubscriptionType.valueOf(itemElement.getAttribute("subscription").getValue()) : null;
+        if (name != null && name.length() > RosterConfiguration.ROSTER_ITEM_NAME_MAX_LENGTH) {
+            throw new RosterNotAcceptableException("roster name too long: " + name.length());
+        }
+        
+        SubscriptionType subscription = verifier.attributePresent("subscription") ? SubscriptionType.valueOf(itemElement.getAttribute("subscription").getValue().toUpperCase()) : null;
 
         String contactJid = attributeJID.getValue();
         Entity contact;
         try {
             contact = EntityImpl.parse(contactJid);
         } catch (EntityFormatException e) {
-            throw new XMLSemanticError("jid cannot be parsed: " + contactJid);
+            throw new RosterNotAcceptableException("jid cannot be parsed: " + contactJid);
         }
 
         List<RosterGroup> groups = new ArrayList<RosterGroup>();
         List<XMLElement> groupElements = itemElement.getInnerElementsNamed("group");
         if (groupElements != null) {
             for (XMLElement groupElement : groupElements) {
-                String groupName = groupElement.getSingleInnerText().getText();
-                if (groupName == null || groupName.length() == 0) continue;
-                groups.add(new RosterGroup(groupName));
+                String groupName = null;
+                try {
+                    groupName = groupElement.getSingleInnerText().getText();
+                } catch (XMLSemanticError xmlSemanticError) {
+                    throw new RosterBadRequestException("roster item group node is malformed");
+                }
+                if (groupName == null || groupName.length() == 0) {
+                    throw new RosterNotAcceptableException("roster item group name of zero length");
+                } else if (groupName.length() > RosterConfiguration.ROSTER_GROUP_NAME_MAX_LENGTH) {
+                    throw new RosterNotAcceptableException("roster item group name too long: " + groupName.length());
+                }
+                RosterGroup group = new RosterGroup(groupName);
+                if (groups.contains(group) && !RosterConfiguration.ROSTER_ITEM_GROUP_ALLOW_DUPLICATES) {
+                    throw new RosterNotAcceptableException("duplicate roster group name: " + groupName);
+                } else {
+                    groups.add(group);
+                }
             }
         }
 



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