]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - org.argeo.jcr/src/org/argeo/jcr/fs/JcrFileSystemProvider.java
Improve exception handling
[lgpl/argeo-commons.git] / org.argeo.jcr / src / org / argeo / jcr / fs / JcrFileSystemProvider.java
index add900540a3e2023d7934d2fc0ecdcee3f68e4ff..5e3e8ca213041e7652fdf4c826e7b7c8d9882593 100644 (file)
@@ -40,13 +40,16 @@ public abstract class JcrFileSystemProvider extends FileSystemProvider {
                try {
                        Node node = toNode(path);
                        if (node == null) {
-                               Node parentNode = toNode(path.getParent());
-                               if (parentNode == null)
-                                       throw new JcrFsException("No parent directory for " + path);
-                               if (!(parentNode.getPath().equals("/") || parentNode.isNodeType(NodeType.NT_FOLDER)))
-                                       throw new JcrFsException("Parent of " + path + " is not a directory");
+                               Node parent = toNode(path.getParent());
+                               if (parent == null)
+                                       throw new IOException("No parent directory for " + path);
+                               if (parent.getPrimaryNodeType().isNodeType(NodeType.NT_FILE)
+                                               || parent.getPrimaryNodeType().isNodeType(NodeType.NT_LINKED_FILE))
+                                       throw new IOException(path + " parent is a file");
 
-                               node = parentNode.addNode(path.getFileName().toString(), NodeType.NT_FILE);
+                               String fileName = path.getFileName().toString();
+                               fileName = Text.escapeIllegalJcrChars(fileName);
+                               node = parent.addNode(fileName, NodeType.NT_FILE);
                                node.addMixin(NodeType.MIX_CREATED);
                                node.addMixin(NodeType.MIX_LAST_MODIFIED);
                        }
@@ -54,7 +57,7 @@ public abstract class JcrFileSystemProvider extends FileSystemProvider {
                                throw new UnsupportedOperationException(node + " must be a file");
                        return new BinaryChannel(node);
                } catch (RepositoryException e) {
-                       throw new JcrFsException("Cannot read file", e);
+                       throw new IOException("Cannot read file", e);
                }
        }
 
@@ -64,7 +67,7 @@ public abstract class JcrFileSystemProvider extends FileSystemProvider {
                        Node base = toNode(dir);
                        return new NodeDirectoryStream((JcrFileSystem) dir.getFileSystem(), base.getNodes(), filter);
                } catch (RepositoryException e) {
-                       throw new JcrFsException("Cannot list directory", e);
+                       throw new IOException("Cannot list directory", e);
                }
        }
 
@@ -78,8 +81,10 @@ public abstract class JcrFileSystemProvider extends FileSystemProvider {
                                        throw new IOException("Parent of " + dir + " does not exist");
                                if (parent.getPrimaryNodeType().isNodeType(NodeType.NT_FILE)
                                                || parent.getPrimaryNodeType().isNodeType(NodeType.NT_LINKED_FILE))
-                                       throw new JcrFsException(dir + " parent is a file");
-                               node = parent.addNode(dir.getFileName().toString(), NodeType.NT_FOLDER);
+                                       throw new IOException(dir + " parent is a file");
+                               String fileName = dir.getFileName().toString();
+                               fileName = Text.escapeIllegalJcrChars(fileName);
+                               node = parent.addNode(fileName, NodeType.NT_FOLDER);
                                node.addMixin(NodeType.MIX_CREATED);
                                node.addMixin(NodeType.MIX_LAST_MODIFIED);
                                node.getSession().save();
@@ -88,7 +93,7 @@ public abstract class JcrFileSystemProvider extends FileSystemProvider {
                                        throw new FileExistsException(dir + " exists and is not a directory");
                        }
                } catch (RepositoryException e) {
-                       throw new JcrFsException("Cannot create directory " + dir, e);
+                       throw new IOException("Cannot create directory " + dir, e);
                }
 
        }
@@ -109,7 +114,7 @@ public abstract class JcrFileSystemProvider extends FileSystemProvider {
                        }
                        session.save();
                } catch (RepositoryException e) {
-                       throw new JcrFsException("Cannot delete " + path, e);
+                       throw new IOException("Cannot delete " + path, e);
                }
 
        }
@@ -122,7 +127,7 @@ public abstract class JcrFileSystemProvider extends FileSystemProvider {
                        JcrUtils.copy(sourceNode, targetNode);
                        sourceNode.getSession().save();
                } catch (RepositoryException e) {
-                       throw new JcrFsException("Cannot copy from " + source + " to " + target, e);
+                       throw new IOException("Cannot copy from " + source + " to " + target, e);
                }
        }
 
@@ -134,7 +139,7 @@ public abstract class JcrFileSystemProvider extends FileSystemProvider {
                        session.move(sourceNode.getPath(), target.toString());
                        session.save();
                } catch (RepositoryException e) {
-                       throw new JcrFsException("Cannot move from " + source + " to " + target, e);
+                       throw new IOException("Cannot move from " + source + " to " + target, e);
                }
        }
 
@@ -151,7 +156,7 @@ public abstract class JcrFileSystemProvider extends FileSystemProvider {
                                Node node2 = toNode(path2);
                                return node.isSame(node2);
                        } catch (RepositoryException e) {
-                               throw new JcrFsException("Cannot check whether " + path + " and " + path2 + " are same", e);
+                               throw new IOException("Cannot check whether " + path + " and " + path2 + " are same", e);
                        }
                }
 
@@ -176,7 +181,7 @@ public abstract class JcrFileSystemProvider extends FileSystemProvider {
                                throw new NoSuchFileException(path + " does not exist");
                        // TODO check access via JCR api
                } catch (RepositoryException e) {
-                       throw new JcrFsException("Cannot delete " + path, e);
+                       throw new IOException("Cannot delete " + path, e);
                }
        }
 
@@ -192,9 +197,12 @@ public abstract class JcrFileSystemProvider extends FileSystemProvider {
                try {
                        // TODO check if assignable
                        Node node = toNode(path);
+                       if (node == null) {
+                               throw new IOException("JCR node not found for " + path);
+                       }
                        return (A) new JcrBasicfileAttributes(node);
                } catch (RepositoryException e) {
-                       throw new JcrFsException("Cannot read basic attributes of " + path, e);
+                       throw new IOException("Cannot read basic attributes of " + path, e);
                }
        }
 
@@ -234,7 +242,7 @@ public abstract class JcrFileSystemProvider extends FileSystemProvider {
                        }
                        return res;
                } catch (RepositoryException e) {
-                       throw new JcrFsException("Cannot read attributes of " + path, e);
+                       throw new IOException("Cannot read attributes of " + path, e);
                }
        }
 
@@ -251,7 +259,7 @@ public abstract class JcrFileSystemProvider extends FileSystemProvider {
                        }
                        node.getSession().save();
                } catch (RepositoryException e) {
-                       throw new JcrFsException("Cannot set attribute " + attribute + " on " + path, e);
+                       throw new IOException("Cannot set attribute " + attribute + " on " + path, e);
                }
        }
 
@@ -259,4 +267,13 @@ public abstract class JcrFileSystemProvider extends FileSystemProvider {
                return ((JcrPath) path).getNode();
        }
 
+       /**
+        * To be overriden in order to support the ~ path, with an implementation
+        * specific concept of user home.
+        * 
+        * @return null by default
+        */
+       public Node getUserHome(Session session) {
+               return null;
+       }
 }