Author: mir
Date: Mon Mar  8 17:54:28 2010
New Revision: 920420

URL: http://svn.apache.org/viewvc?rev=920420&view=rev
Log:
CLEREZZA-155: UnknownRootException is thrown when getHierarchy/CollectionNode() 
is called whose URIs base URI is not known

Added:
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/UnknownRootExcetpion.java
Modified:
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/AbstractDiscobitsHandler.java
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/CollectionNode.java
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/HierarchyManager.java
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/HierarchyNode.java
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/HierarchyService.java
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/test/java/org/apache/clerezza/platform/content/hierarchy/HierarchyTest.java

Modified: 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/AbstractDiscobitsHandler.java
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/AbstractDiscobitsHandler.java?rev=920420&r1=920419&r2=920420&view=diff
==============================================================================
--- 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/AbstractDiscobitsHandler.java
 (original)
+++ 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/AbstractDiscobitsHandler.java
 Mon Mar  8 17:54:28 2010
@@ -27,6 +27,7 @@
 import org.apache.clerezza.platform.content.hierarchy.HierarchyService;
 import 
org.apache.clerezza.platform.content.hierarchy.NodeAlreadyExistsException;
 import 
org.apache.clerezza.platform.content.hierarchy.NodeDoesNotExistException;
+import org.apache.clerezza.platform.content.hierarchy.UnknownRootExcetpion;
 
 import org.apache.clerezza.rdf.core.LiteralFactory;
 import org.apache.clerezza.rdf.core.MGraph;
@@ -131,13 +132,15 @@
                try {
                        HierarchyService hierarchyService = 
getHierarchyService();
                        if (hierarchyService == null) {
-                               graphNode = new GraphNode(node, mGraph);        
                        
+                               graphNode = new GraphNode(node, mGraph);
                        } else {
                                graphNode = 
hierarchyService.getHierarchyNode((UriRef) node);
                                ((HierarchyNode) graphNode).delete();
                        }
                } catch (NodeDoesNotExistException ex) {
                        graphNode = new GraphNode(node, mGraph);
+               } catch (UnknownRootExcetpion ex) {
+                       graphNode = new GraphNode(node, mGraph);
                }
                graphNode.deleteNodeContext();
        }

Modified: 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/CollectionNode.java
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/CollectionNode.java?rev=920420&r1=920419&r2=920420&view=diff
==============================================================================
--- 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/CollectionNode.java
 (original)
+++ 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/CollectionNode.java
 Mon Mar  8 17:54:28 2010
@@ -89,6 +89,8 @@
                                
nodes.add(hierarchyService.getHierarchyNode(uri));
                        } catch (NodeDoesNotExistException ex) {
                                throw new RuntimeException(ex);
+                       } catch (UnknownRootExcetpion ex) {
+                               throw new RuntimeException(ex);
                        }
                }
                return Collections.unmodifiableList(nodes);

Modified: 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/HierarchyManager.java
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/HierarchyManager.java?rev=920420&r1=920419&r2=920420&view=diff
==============================================================================
--- 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/HierarchyManager.java
 (original)
+++ 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/HierarchyManager.java
 Mon Mar  8 17:54:28 2010
@@ -157,6 +157,9 @@
                        hierarchyNode = 
hierarchyService.getHierarchyNode(nodeUri);
                } catch (NodeDoesNotExistException ex) {
                        return 
Response.status(Response.Status.NOT_FOUND).build();
+               } catch (UnknownRootExcetpion ex) {
+                       return 
Response.status(Response.Status.BAD_REQUEST).entity(ex.toString()).
+                                       type(MediaType.TEXT_PLAIN_TYPE).build();
                }
 
                hierarchyNode.delete();
@@ -194,6 +197,9 @@
                        return Response.status(Response.Status.CONFLICT).entity(
                                        targetCollection.getUnicodeString() + " 
is not a Collection.").
                                        type(MediaType.TEXT_PLAIN_TYPE).build();
+               } catch (UnknownRootExcetpion ex) {
+                       return 
Response.status(Response.Status.BAD_REQUEST).entity(ex.toString()).
+                                       type(MediaType.TEXT_PLAIN_TYPE).build();
                }
                try {
                        hierarchyNode = 
hierarchyNode.move(targetCollectionNode, newName, newPos);
@@ -232,6 +238,9 @@
                        return move(nodeUri, parent.getNode(), pos, newName);
                } catch (NodeDoesNotExistException ex) {
                        return 
Response.status(Response.Status.NOT_FOUND).build();
+               } catch (UnknownRootExcetpion ex) {
+                       return 
Response.status(Response.Status.BAD_REQUEST).entity(ex.toString()).
+                                       type(MediaType.TEXT_PLAIN_TYPE).build();
                }
                
        }
@@ -258,6 +267,9 @@
                } catch (NodeDoesNotExistException ex) {
                        throw new WebApplicationException(
                                        
Response.status(Response.Status.BAD_REQUEST).build());
+               } catch (UnknownRootExcetpion ex) {
+                       throw new 
WebApplicationException(Response.status(Response.Status.BAD_REQUEST)
+                                       
.entity(ex.toString()).type(MediaType.TEXT_PLAIN_TYPE).build());
                }
                MGraph result = new SimpleMGraph();
                result.addAll(collectionNode.getNodeContext());
@@ -307,6 +319,9 @@
                } catch (NodeDoesNotExistException ex) {
                        throw new WebApplicationException(
                                        
Response.status(Response.Status.NOT_FOUND).build());
+               } catch (UnknownRootExcetpion ex) {
+                       throw new 
WebApplicationException(Response.status(Response.Status.BAD_REQUEST)
+                                       
.entity(ex.toString()).type(MediaType.TEXT_PLAIN_TYPE).build());
                }
                return getMembersCount(collection).toString();
        }

Modified: 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/HierarchyNode.java
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/HierarchyNode.java?rev=920420&r1=920419&r2=920420&view=diff
==============================================================================
--- 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/HierarchyNode.java
 (original)
+++ 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/HierarchyNode.java
 Mon Mar  8 17:54:28 2010
@@ -76,6 +76,8 @@
                        UriRef parentCollectionUri = HierarchyUtils
                                        .extractParentCollectionUri(getNode());
                        return 
hierarchyService.getCollectionNode(parentCollectionUri);
+               } catch (UnknownRootExcetpion ex) {
+                       throw new RuntimeException(ex);
                } catch (NodeDoesNotExistException ex) {
                        throw new RuntimeException(ex);
                }
@@ -147,13 +149,17 @@
                if (parentMembers.contains(newUri) || 
parentMembers.contains(alternativeUri)) {
                        HierarchyNode existingNode = null;
                        try {
-                               existingNode = 
hierarchyService.getHierarchyNode(newUri);
-                       } catch (NodeDoesNotExistException ex) {
                                try {
-                                       existingNode = 
hierarchyService.getHierarchyNode(alternativeUri);
-                               } catch (NodeDoesNotExistException e) {
-                                       throw new RuntimeException(ex);
+                                       existingNode = 
hierarchyService.getHierarchyNode(newUri);
+                               } catch (NodeDoesNotExistException ex) {
+                                       try {
+                                               existingNode = 
hierarchyService.getHierarchyNode(alternativeUri);
+                                       } catch (NodeDoesNotExistException e) {
+                                               throw new RuntimeException(ex);
+                                       }
                                }
+                       } catch (UnknownRootExcetpion ex) {
+                               throw new RuntimeException(ex);
                        }
                        throw new NodeAlreadyExistsException(existingNode);
                }

Modified: 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/HierarchyService.java
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/HierarchyService.java?rev=920420&r1=920419&r2=920420&view=diff
==============================================================================
--- 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/HierarchyService.java
 (original)
+++ 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/HierarchyService.java
 Mon Mar  8 17:54:28 2010
@@ -87,10 +87,15 @@
         * specified resource.
         * @throws NodeDoesNotExistException Thrown if the specified node does 
not 
         *              exist.
+        * @throws UnknownRootExcetpion Thrown if the base URI of the specified 
uri
+        *              is not a known root of an hierarchy.
         * @param uri
         */
        public HierarchyNode getHierarchyNode(UriRef uri)
-                       throws NodeDoesNotExistException {
+                       throws NodeDoesNotExistException, UnknownRootExcetpion {
+               if (!hasKnownRoot(uri)) {
+                       throw new UnknownRootExcetpion(extractBaseUri(uri));
+               }
                HierarchyNode hierarchyNode;
                try {
                        CollectionNode collectionNode =
@@ -133,10 +138,15 @@
         *              exist.
         * @throws IllegalArgumentException Thrown if the node at the specified 
uri
         *              is not a CollectionNode.
+        * @throws UnknownRootExcetpion Thrown if the base URI of the specified 
uri
+        *              is not a known root of an hierarchy.
         * @param uri
         */
        public CollectionNode getCollectionNode(UriRef uri)
-                       throws NodeDoesNotExistException{
+                       throws NodeDoesNotExistException, UnknownRootExcetpion{
+               if (!hasKnownRoot(uri)) {
+                       throw new UnknownRootExcetpion(extractBaseUri(uri));
+               }
                CollectionNode collectionNode;
                try {
                        collectionNode =
@@ -226,18 +236,25 @@
         * the base URI of uri as new root.
         * @param uri The Uri to be checked.
         */
-       private void handleRootOfUri(UriRef uri) {
+       private void handleRootOfUri(UriRef uri) {              
+               if (hasKnownRoot(uri)) {
+                       return;
+               }
+               UriRef baseUri = extractBaseUri(uri);
+               config.addBaseUri(baseUri);
+               addRoot(baseUri);
+       }
+
+       private boolean hasKnownRoot(UriRef uri) {
                Iterator<CollectionNode> rootsIter = roots.iterator();
                while (rootsIter.hasNext()) {
                        CollectionNode root = rootsIter.next();
                        UriRef rootUri = root.getNode();
                        if 
(uri.getUnicodeString().startsWith(rootUri.getUnicodeString())) {
-                               return;
+                               return true;
                        }
                }
-               UriRef baseUri = extractBaseUri(uri);
-               config.addBaseUri(baseUri);
-               addRoot(baseUri);
+               return false;
        }
 
        private UriRef extractBaseUri(UriRef uriRef) {
@@ -272,6 +289,8 @@
                        UriRef parentCollectionUri = HierarchyUtils.
                                        
extractParentCollectionUri(node.getNode());
                        parentCollection = (CollectionNode) 
getHierarchyNode(parentCollectionUri);
+               } catch (UnknownRootExcetpion ex) {
+                       throw new RuntimeException(ex);
                } catch (NodeDoesNotExistException ex) {
                        try {
                                parentCollection = 
createCollectionNode(ex.getNodeUri());

Added: 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/UnknownRootExcetpion.java
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/UnknownRootExcetpion.java?rev=920420&view=auto
==============================================================================
--- 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/UnknownRootExcetpion.java
 (added)
+++ 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/main/java/org/apache/clerezza/platform/content/hierarchy/UnknownRootExcetpion.java
 Mon Mar  8 17:54:28 2010
@@ -0,0 +1,36 @@
+/*
+ * 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.clerezza.platform.content.hierarchy;
+
+import org.apache.clerezza.rdf.core.UriRef;
+
+/**
+ * This exception is thrown when someone tries to get a hierarchy node whose
+ * base URI is not a known root of an hierarchy.
+ *
+ * @author mir
+ */
+public class UnknownRootExcetpion extends Exception{
+
+       public UnknownRootExcetpion(UriRef root) {
+               super(root + " is not a known root URI.");
+       }
+
+}

Modified: 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/test/java/org/apache/clerezza/platform/content/hierarchy/HierarchyTest.java
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/test/java/org/apache/clerezza/platform/content/hierarchy/HierarchyTest.java?rev=920420&r1=920419&r2=920420&view=diff
==============================================================================
--- 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/test/java/org/apache/clerezza/platform/content/hierarchy/HierarchyTest.java
 (original)
+++ 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.content/src/test/java/org/apache/clerezza/platform/content/hierarchy/HierarchyTest.java
 Mon Mar  8 17:54:28 2010
@@ -94,6 +94,13 @@
                        exceptionThrown = true;
                }
                Assert.assertTrue(exceptionThrown);
+               try {
+                       hierarchyService.createCollectionNode(fooResource);
+               } catch(IllegalArgumentException e) {
+                       exceptionThrown = true;
+               }
+
+               Assert.assertTrue(exceptionThrown);
                CollectionNode fooNode = 
(CollectionNode)hierarchyService.getHierarchyNode(foo);
                List<HierarchyNode> fooMembers = fooNode.getMembers();
                Assert.assertEquals(1, fooMembers.size());      
@@ -276,6 +283,12 @@
                } catch (NodeAlreadyExistsException ex) {}
        }
 
+       @Test(expected=UnknownRootExcetpion.class)
+       public void missingRootTest() throws Exception{
+               HierarchyService hierarchyService = getHierarchyService();
+               hierarchyService.getHierarchyNode(newRootTest);
+       }
+
        @Test
        public void rootAutoCreationTest() throws Exception{
                HierarchyService hierarchyService = getHierarchyService();


Reply via email to