use of javax.jcr.ItemExistsException in project jackrabbit by apache.
the class BatchedItemOperations method createNodeState.
/**
* Creates a new node based on the given definition.
* <p>
* Note that access rights are <b><i>not</i></b> enforced!
* <p>
* <b>Precondition:</b> the state manager needs to be in edit mode.
*
* @param parent
* @param nodeName
* @param nodeTypeName
* @param mixinNames
* @param id
* @param def
* @return
* @throws ItemExistsException
* @throws ConstraintViolationException
* @throws RepositoryException
* @throws IllegalStateException
*/
public NodeState createNodeState(NodeState parent, Name nodeName, Name nodeTypeName, Name[] mixinNames, NodeId id, QNodeDefinition def) throws ItemExistsException, ConstraintViolationException, RepositoryException, IllegalStateException {
// check for name collisions with existing nodes
if (!def.allowsSameNameSiblings() && parent.hasChildNodeEntry(nodeName)) {
NodeId errorId = parent.getChildNodeEntry(nodeName, 1).getId();
throw new ItemExistsException(safeGetJCRPath(errorId));
}
if (nodeTypeName == null) {
// no primary node type specified,
// try default primary type from definition
nodeTypeName = def.getDefaultPrimaryType();
if (nodeTypeName == null) {
String msg = "an applicable node type could not be determined for " + nodeName;
log.debug(msg);
throw new ConstraintViolationException(msg);
}
}
NodeState node = stateMgr.createNew(id, nodeTypeName, parent.getNodeId());
if (mixinNames != null && mixinNames.length > 0) {
node.setMixinTypeNames(new HashSet<Name>(Arrays.asList(mixinNames)));
}
// now add new child node entry to parent
parent.addChildNodeEntry(nodeName, node.getNodeId());
EffectiveNodeType ent = getEffectiveNodeType(node);
// check shareable
if (ent.includesNodeType(NameConstants.MIX_SHAREABLE)) {
node.addShare(parent.getNodeId());
}
if (!node.getMixinTypeNames().isEmpty()) {
// create jcr:mixinTypes property
QPropertyDefinition pd = ent.getApplicablePropertyDef(NameConstants.JCR_MIXINTYPES, PropertyType.NAME, true);
createPropertyState(node, pd.getName(), pd.getRequiredType(), pd);
}
// add 'auto-create' properties defined in node type
for (QPropertyDefinition pd : ent.getAutoCreatePropDefs()) {
createPropertyState(node, pd.getName(), pd.getRequiredType(), pd);
}
// recursively add 'auto-create' child nodes defined in node type
for (QNodeDefinition nd : ent.getAutoCreateNodeDefs()) {
createNodeState(node, nd.getName(), nd.getDefaultPrimaryType(), null, null, nd);
}
// store node
stateMgr.store(node);
// store parent
stateMgr.store(parent);
return node;
}
use of javax.jcr.ItemExistsException in project jackrabbit by apache.
the class NodeImplTest method testAddNodeUuidCollision.
public void testAddNodeUuidCollision() throws RepositoryException, NotExecutableException {
String uuid = "f81d4fae-7dec-11d0-a765-00a0c91e6bf6";
Node n = testRootNode.addNode(nodeName1);
Node testNode1 = ((NodeImpl) n).addNodeWithUuid(nodeName2, uuid);
testNode1.addMixin(NodeType.MIX_REFERENCEABLE);
testRootNode.getSession().save();
try {
((NodeImpl) n).addNodeWithUuid(nodeName2, uuid);
fail("UUID collision not detected by addNodeWithUuid");
} catch (ItemExistsException e) {
}
}
use of javax.jcr.ItemExistsException in project jackrabbit by apache.
the class DefaultItemCollection method addMember.
/**
* If the specified resource represents a collection, a new node is {@link Node#addNode(String)
* added} to the item represented by this resource. If an input stream is specified
* together with a collection resource {@link Session#importXML(String, java.io.InputStream, int)}
* is called instead and this resource path is used as <code>parentAbsPath</code> argument.
* <p>
* However, if the specified resource is not of resource type collection a
* new {@link Property} is set or an existing one is changed by modifying its
* value.<br>
* NOTE: with the current implementation it is not possible to create or
* modify multivalue JCR properties.<br>
* NOTE: if the JCR property represented by the specified resource has an
* {@link PropertyType#UNDEFINED undefined} resource type, its value will be
* changed/set to type {@link PropertyType#BINARY binary}.
*
* @param resource
* @param inputContext
* @throws org.apache.jackrabbit.webdav.DavException
* @see org.apache.jackrabbit.webdav.DavResource#addMember(org.apache.jackrabbit.webdav.DavResource, InputContext)
* @see Node#addNode(String)
* @see Node#setProperty(String, java.io.InputStream)
*/
@Override
public void addMember(DavResource resource, InputContext inputContext) throws DavException {
/* RFC 2815 states that all 'parents' must exist in order all addition of members */
if (!exists()) {
throw new DavException(DavServletResponse.SC_CONFLICT);
}
File tmpFile = null;
try {
Node n = (Node) item;
InputStream in = (inputContext != null) ? inputContext.getInputStream() : null;
String itemPath = getLocator().getRepositoryPath();
String memberName = getItemName(resource.getLocator().getRepositoryPath());
if (resource.isCollection()) {
if (in == null) {
// MKCOL without a request body, try if a default-primary-type is defined.
n.addNode(memberName);
} else {
// MKCOL, which is not allowed for existing resources
int uuidBehavior = ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW;
String str = inputContext.getProperty(IMPORT_UUID_BEHAVIOR);
if (str != null) {
try {
uuidBehavior = Integer.parseInt(str);
} catch (NumberFormatException e) {
throw new DavException(DavServletResponse.SC_BAD_REQUEST);
}
}
if (getTransactionId() == null) {
// if not part of a transaction directly import on workspace
// since changes would be explicitly saved in the
// complete-call.
getRepositorySession().getWorkspace().importXML(itemPath, in, uuidBehavior);
} else {
// changes will not be persisted unless the tx is completed.
getRepositorySession().importXML(itemPath, in, uuidBehavior);
}
}
} else {
if (in == null) {
// PUT: not possible without request body
throw new DavException(DavServletResponse.SC_BAD_REQUEST, "Cannot create a new non-collection resource without request body.");
}
// PUT : create new or overwrite existing property.
String ct = inputContext.getContentType();
int type = JcrValueType.typeFromContentType(ct);
if (type != PropertyType.UNDEFINED) {
// no need to create value/values property. instead
// prop-value can be retrieved directly:
int pos = ct.indexOf(';');
String charSet = (pos > -1) ? ct.substring(pos) : "UTF-8";
if (type == PropertyType.BINARY) {
n.setProperty(memberName, inputContext.getInputStream());
} else {
BufferedReader r = new BufferedReader(new InputStreamReader(inputContext.getInputStream(), charSet));
String line;
StringBuffer value = new StringBuffer();
while ((line = r.readLine()) != null) {
value.append(line);
}
n.setProperty(memberName, value.toString(), type);
}
} else {
// try to parse the request body into a 'values' property.
tmpFile = File.createTempFile(TMP_PREFIX + Text.escape(memberName), null, null);
FileOutputStream out = new FileOutputStream(tmpFile);
IOUtil.spool(in, out);
out.close();
// try to parse the request body into a 'values' property.
ValuesProperty vp = buildValuesProperty(new FileInputStream(tmpFile));
if (vp != null) {
if (JCR_VALUE.equals(vp.getName())) {
n.setProperty(memberName, vp.getJcrValue());
} else {
n.setProperty(memberName, vp.getJcrValues());
}
} else {
// request body cannot be parsed into a 'values' property.
// fallback: try to import as single value from stream.
n.setProperty(memberName, new FileInputStream(tmpFile));
}
}
}
if (resource.exists() && resource instanceof AbstractItemResource) {
// PUT may modify value of existing jcr property. thus, this
// node is not modified by the 'addMember' call.
((AbstractItemResource) resource).complete();
} else {
complete();
}
} catch (ItemExistsException e) {
// according to RFC 2518: MKCOL only possible on non-existing/deleted resource
throw new JcrDavException(e, DavServletResponse.SC_METHOD_NOT_ALLOWED);
} catch (RepositoryException e) {
throw new JcrDavException(e);
} catch (IOException e) {
throw new DavException(DavServletResponse.SC_UNPROCESSABLE_ENTITY, e.getMessage());
} finally {
if (tmpFile != null) {
tmpFile.delete();
}
}
}
use of javax.jcr.ItemExistsException in project jackrabbit by apache.
the class WorkspaceCloneSameNameSibsTest method testCloneNodesNodeExistsAtDestPath.
/**
* An ItemExistsException is thrown if a node or property already exists at
* destAbsPath.
* <ul>
* <li>{@code sameNameSibsFalseNodeType} name of a node type that does not
* allows same name siblings.
* <li>{@code nodeName3} name of a child node that does not allow same name
* siblings..
* </ul>
*/
public void testCloneNodesNodeExistsAtDestPath() throws RepositoryException {
// create a parent node where allowSameNameSiblings are set to false
Node snsfNode = testRootNodeW2.addNode(nodeName3, sameNameSibsFalseNodeType.getName());
testRootNodeW2.save();
String dstAbsPath = snsfNode.getPath() + "/" + node1W2.getName();
workspaceW2.copy(node1W2.getPath(), dstAbsPath);
// property already exist
try {
workspaceW2.clone(workspace.getName(), node1.getPath(), dstAbsPath, true);
fail("Node exists below '" + dstAbsPath + "'. Test should fail.");
} catch (ItemExistsException e) {
// successful
}
}
use of javax.jcr.ItemExistsException in project jackrabbit by apache.
the class LockTest method setUpSameNameSiblings.
/**
* Create three child nodes with identical names
*/
private Node setUpSameNameSiblings() throws RepositoryException, NotExecutableException {
// create three lockable nodes with same name
try {
Node testNode = testRootNode.addNode(nodeName1, testNodeType);
ensureMixinType(testNode, mixLockable);
testNode = testRootNode.addNode(nodeName1, testNodeType);
ensureMixinType(testNode, mixLockable);
testNode = testRootNode.addNode(nodeName1, testNodeType);
ensureMixinType(testNode, mixLockable);
testRootNode.getSession().save();
return testNode;
} catch (ItemExistsException ex) {
// repository does not seem to support same name siblings on this node type
throw new NotExecutableException("Node type " + testNodeType + " does not support same-name-siblings");
}
}
Aggregations