Remove old license headers
[lgpl/argeo-commons.git] / org.argeo.jcr / src / org / argeo / jcr / JcrUtils.java
index 3e7c8737403ed969378e9c93972508abea85293b..eb6be528bf25fb4bc610359782802e03bb5f5cfe 100644 (file)
@@ -1,18 +1,3 @@
-/*
- * Copyright (C) 2007-2012 Argeo GmbH
- *
- * 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.argeo.jcr;
 
 import java.io.ByteArrayInputStream;
@@ -38,6 +23,7 @@ import java.util.Map;
 import java.util.TreeMap;
 
 import javax.jcr.Binary;
+import javax.jcr.Credentials;
 import javax.jcr.NamespaceRegistry;
 import javax.jcr.NoSuchWorkspaceException;
 import javax.jcr.Node;
@@ -1067,16 +1053,25 @@ public class JcrUtils {
         */
        public static Session loginOrCreateWorkspace(Repository repository, String workspaceName)
                        throws RepositoryException {
+               return loginOrCreateWorkspace(repository, workspaceName, null);
+       }
+
+       /**
+        * Login to a workspace with implicit credentials, creates the workspace with
+        * these credentials if it does not already exist.
+        */
+       public static Session loginOrCreateWorkspace(Repository repository, String workspaceName, Credentials credentials)
+                       throws RepositoryException {
                Session workspaceSession = null;
                Session defaultSession = null;
                try {
                        try {
-                               workspaceSession = repository.login(workspaceName);
+                               workspaceSession = repository.login(credentials, workspaceName);
                        } catch (NoSuchWorkspaceException e) {
                                // try to create workspace
-                               defaultSession = repository.login();
+                               defaultSession = repository.login(credentials);
                                defaultSession.getWorkspace().createWorkspace(workspaceName);
-                               workspaceSession = repository.login(workspaceName);
+                               workspaceSession = repository.login(credentials, workspaceName);
                        }
                        return workspaceSession;
                } finally {
@@ -1308,23 +1303,30 @@ public class JcrUtils {
                return true;
        }
 
-       /** Gets access control list for this path, throws exception if not found */
+       /**
+        * Gets the first available access control list for this path, throws exception
+        * if not found
+        */
        public synchronized static AccessControlList getAccessControlList(AccessControlManager acm, String path)
                        throws RepositoryException {
                // search for an access control list
                AccessControlList acl = null;
                AccessControlPolicyIterator policyIterator = acm.getApplicablePolicies(path);
-               if (policyIterator.hasNext()) {
+               applicablePolicies: if (policyIterator.hasNext()) {
                        while (policyIterator.hasNext()) {
                                AccessControlPolicy acp = policyIterator.nextAccessControlPolicy();
-                               if (acp instanceof AccessControlList)
+                               if (acp instanceof AccessControlList) {
                                        acl = ((AccessControlList) acp);
+                                       break applicablePolicies;
+                               }
                        }
                } else {
                        AccessControlPolicy[] existingPolicies = acm.getPolicies(path);
-                       for (AccessControlPolicy acp : existingPolicies) {
-                               if (acp instanceof AccessControlList)
+                       existingPolicies: for (AccessControlPolicy acp : existingPolicies) {
+                               if (acp instanceof AccessControlList) {
                                        acl = ((AccessControlList) acp);
+                                       break existingPolicies;
+                               }
                        }
                }
                if (acl != null)
@@ -1497,7 +1499,7 @@ public class JcrUtils {
                                contentNode = fileNode.getNode(Node.JCR_CONTENT);
                        } else {
                                fileNode = folderNode.addNode(fileName, NodeType.NT_FILE);
-                               contentNode = fileNode.addNode(Node.JCR_CONTENT, NodeType.NT_RESOURCE);
+                               contentNode = fileNode.addNode(Node.JCR_CONTENT, NodeType.NT_UNSTRUCTURED);
                        }
                        binary = contentNode.getSession().getValueFactory().createBinary(in);
                        contentNode.setProperty(Property.JCR_DATA, binary);
@@ -1509,6 +1511,11 @@ public class JcrUtils {
                }
        }
 
+       /** Read an an nt:file as an {@link InputStream}. */
+       public static InputStream getFileAsStream(Node fileNode) throws RepositoryException {
+               return fileNode.getNode(Node.JCR_CONTENT).getProperty(Property.JCR_DATA).getBinary().getStream();
+       }
+
        /**
         * Computes the checksum of an nt:file.
         * 
@@ -1516,14 +1523,11 @@ public class JcrUtils {
         */
        @Deprecated
        public static String checksumFile(Node fileNode, String algorithm) {
-               Binary data = null;
                try (InputStream in = fileNode.getNode(Node.JCR_CONTENT).getProperty(Property.JCR_DATA).getBinary()
                                .getStream()) {
                        return digest(algorithm, in);
                } catch (RepositoryException | IOException e) {
                        throw new ArgeoJcrException("Cannot checksum file " + fileNode, e);
-               } finally {
-                       closeQuietly(data);
                }
        }