-/*
- * 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;
import java.util.TreeMap;
import javax.jcr.Binary;
+import javax.jcr.Credentials;
import javax.jcr.NamespaceRegistry;
import javax.jcr.NoSuchWorkspaceException;
import javax.jcr.Node;
* Queries one single node.
*
* @return one single node or null if none was found
- * @throws ArgeoJcrException
- * if more than one node was found
+ * @throws ArgeoJcrException if more than one node was found
*/
public static Node querySingleNode(Query query) {
NodeIterator nodeIterator;
/**
* The provided data as a path ('/' at the end, not the beginning)
*
- * @param cal
- * the date
- * @param addHour
- * whether to add hour as well
+ * @param cal the date
+ * @param addHour whether to add hour as well
*/
public static String dateAsPath(Calendar cal, Boolean addHour) {
StringBuffer buf = new StringBuffer(14);
}
}
+ /** Concisely get the path of the given node. */
+ public static String getPath(Node node) {
+ try {
+ return node.getPath();
+ } catch (RepositoryException e) {
+ throw new ArgeoJcrException("Cannot get path of " + node, e);
+ }
+ }
+
/** Concisely get the boolean value of a property */
public static Boolean check(Node node, String propertyName) {
try {
/**
* Create sub nodes relative to a parent node
*
- * @param nodeType
- * the type of the leaf node
+ * @param nodeType the type of the leaf node
*/
public static Node mkdirs(Node parentNode, String relativePath, String nodeType) {
return mkdirs(parentNode, relativePath, nodeType, null);
/**
* Create sub nodes relative to a parent node
*
- * @param nodeType
- * the type of the leaf node
+ * @param nodeType the type of the leaf node
*/
public static Node mkdirs(Node parentNode, String relativePath, String nodeType, String intermediaryNodeType) {
List<String> tokens = tokenize(relativePath);
}
/**
- * @param type
- * the type of the leaf node
+ * @param type the type of the leaf node
*/
public static Node mkdirs(Session session, String path, String type) {
return mkdirs(session, path, type, null, false);
/** Writes a {@link Binary} from a byte array */
public static void setBinaryAsBytes(Node node, String property, byte[] bytes) {
- // InputStream in = null;
Binary binary = null;
try (InputStream in = new ByteArrayInputStream(bytes)) {
- // in = new ByteArrayInputStream(bytes);
binary = node.getSession().getValueFactory().createBinary(in);
node.setProperty(property, binary);
} catch (Exception e) {
- throw new ArgeoJcrException("Cannot read binary " + property + " as bytes", e);
+ throw new ArgeoJcrException("Cannot set binary " + property + " as bytes", e);
+ } finally {
+ closeQuietly(binary);
+ }
+ }
+
+ /** Writes a {@link Binary} from a byte array */
+ public static void setBinaryAsBytes(Property prop, byte[] bytes) {
+ Binary binary = null;
+ try (InputStream in = new ByteArrayInputStream(bytes)) {
+ binary = prop.getSession().getValueFactory().createBinary(in);
+ prop.setValue(binary);
+ } catch (RepositoryException | IOException e) {
+ throw new ArgeoJcrException("Cannot set binary " + prop + " as bytes", e);
} finally {
- // IOUtils.closeQuietly(in);
closeQuietly(binary);
}
}
*/
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 {
}
}
- /** Logs out the session, not throwing any exception, even if it is null. */
+ /**
+ * Logs out the session, not throwing any exception, even if it is null.
+ * {@link Jcr#logout(Session)} should rather be used.
+ */
public static void logoutQuietly(Session session) {
- try {
- if (session != null)
- if (session.isLive())
- session.logout();
- } catch (Exception e) {
- // silent
- }
+ Jcr.logout(session);
+// try {
+// if (session != null)
+// if (session.isLive())
+// session.logout();
+// } catch (Exception e) {
+// // silent
+// }
}
/**
/**
* Update lastModified recursively until this parent.
*
- * @param node
- * the node
- * @param untilPath
- * the base path, null is equivalent to "/"
+ * @param node the node
+ * @param untilPath the base path, null is equivalent to "/"
*/
public static void updateLastModifiedAndParents(Node node, String untilPath) {
try {
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)
* Copy only nt:folder and nt:file, without their additional types and
* properties.
*
- * @param recursive
- * if true copies folders as well, otherwise only first level files
+ * @param recursive if true copies folders as well, otherwise only first level
+ * files
* @return how many files were copied
*/
public static Long copyFiles(Node fromNode, Node toNode, Boolean recursive, JcrMonitor monitor, boolean onlyAdd) {
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);
}
}
+ /** 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.
*
*/
@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);
}
}