use of javax.jcr.Item in project jackrabbit by apache.
the class DavResourceFactoryImpl method createResourceForItem.
/**
* Tries to retrieve the repository item defined by the locator's resource
* path and build the corresponding WebDAV resource. The following distinction
* is made between items: Version nodes, VersionHistory nodes, root node,
* unspecified nodes and finally property items.
*
* @param locator
* @param sessionImpl
* @return DavResource representing a repository item.
* @throws RepositoryException if {@link javax.jcr.Session#getItem(String)} fails.
*/
private DavResource createResourceForItem(DavResourceLocator locator, JcrDavSession sessionImpl) throws RepositoryException, DavException {
DavResource resource;
Item item = getItem(sessionImpl, locator);
if (item.isNode()) {
// create special resources for Version and VersionHistory
if (item instanceof Version) {
resource = new VersionItemCollection(locator, sessionImpl, this, item);
} else if (item instanceof VersionHistory) {
resource = new VersionHistoryItemCollection(locator, sessionImpl, this, item);
} else {
resource = new VersionControlledItemCollection(locator, sessionImpl, this, item);
}
} else {
resource = new DefaultItemResource(locator, sessionImpl, this, item);
}
return resource;
}
use of javax.jcr.Item in project jackrabbit by apache.
the class JsonDiffHandler method setProperty.
/**
* @see DiffHandler#setProperty(String, String)
*/
@Override
public void setProperty(String targetPath, String diffValue) throws DiffException {
try {
String itemPath = getItemPath(targetPath);
Item item = session.getItem(Text.getRelativeParent(itemPath, 1));
if (!item.isNode()) {
throw new DiffException("No such node " + itemPath, new ItemNotFoundException(itemPath));
}
Node parent = (Node) item;
String propName = Text.getName(itemPath);
if (JcrConstants.JCR_MIXINTYPES.equals(propName)) {
setMixins(parent, extractValuesFromRequest(targetPath));
} else if (JcrConstants.JCR_PRIMARYTYPE.equals(propName)) {
setPrimaryType(parent, extractValuesFromRequest(targetPath));
} else {
if (diffValue == null || diffValue.length() == 0) {
// single valued property with value present in multipart.
Value[] vs = extractValuesFromRequest(targetPath);
if (vs.length == 0) {
if (parent.hasProperty(propName)) {
// avoid problems with single vs. multi valued props.
parent.getProperty(propName).remove();
} else {
// property does not exist -> stick to rule that missing
// [] indicates single valued.
parent.setProperty(propName, (Value) null);
}
} else if (vs.length == 1) {
parent.setProperty(propName, vs[0]);
} else {
throw new DiffException("Unexpected number of values in multipart. Was " + vs.length + " but expected 1.");
}
} else if (diffValue.startsWith("[") && diffValue.endsWith("]")) {
// multivalued property
if (diffValue.length() == 2) {
// empty array OR values in multipart
Value[] vs = extractValuesFromRequest(targetPath);
parent.setProperty(propName, vs);
} else {
// json array
Value[] vs = extractValues(diffValue);
parent.setProperty(propName, vs);
}
} else {
// single prop value included in the diff
Value v = extractValue(diffValue);
parent.setProperty(propName, v);
}
}
} catch (RepositoryException e) {
throw new DiffException(e.getMessage(), e);
} catch (IOException e) {
if (e instanceof DiffException) {
throw (DiffException) e;
} else {
throw new DiffException(e.getMessage(), e);
}
}
}
use of javax.jcr.Item in project jackrabbit by apache.
the class JsonDiffHandler method addNode.
private void addNode(String parentPath, String nodeName, String diffValue) throws DiffException, RepositoryException {
Item item = session.getItem(parentPath);
if (!item.isNode()) {
throw new ItemNotFoundException(parentPath);
}
Node parent = (Node) item;
try {
NodeHandler hndlr = new NodeHandler(parent, nodeName);
new JsonParser(hndlr).parse(diffValue);
} catch (IOException e) {
if (e instanceof DiffException) {
throw (DiffException) e;
} else {
throw new DiffException(e.getMessage(), e);
}
}
}
use of javax.jcr.Item in project jackrabbit by apache.
the class JsonDiffHandler method move.
/**
* @see DiffHandler#move(String, String)
*/
@Override
public void move(String targetPath, String diffValue) throws DiffException {
if (diffValue == null || diffValue.length() == 0) {
throw new DiffException("Invalid 'move' value '" + diffValue + "'");
}
try {
String srcPath = getItemPath(targetPath);
String orderPosition = getOrderPosition(diffValue);
if (orderPosition == null) {
// simple move
String destPath = getItemPath(diffValue);
session.move(srcPath, destPath);
} else {
String srcName = Text.getName(srcPath);
int pos = diffValue.lastIndexOf('#');
String destName = (pos == 0) ? null : Text.getName(diffValue.substring(0, pos));
Item item = session.getItem(Text.getRelativeParent(srcPath, 1));
if (!item.isNode()) {
throw new ItemNotFoundException(srcPath);
}
Node parent = (Node) item;
if (ORDER_POSITION_FIRST.equals(orderPosition)) {
if (destName != null) {
throw new DiffException(ORDER_POSITION_FIRST + " may not have a leading destination.");
}
destName = Text.getName(parent.getNodes().nextNode().getPath());
parent.orderBefore(srcName, destName);
} else if (ORDER_POSITION_LAST.equals(orderPosition)) {
if (destName != null) {
throw new DiffException(ORDER_POSITION_LAST + " may not have a leading destination.");
}
parent.orderBefore(srcName, null);
} else if (ORDER_POSITION_AFTER.equals(orderPosition)) {
if (destName == null) {
throw new DiffException(ORDER_POSITION_AFTER + " must have a leading destination.");
}
for (NodeIterator it = parent.getNodes(); it.hasNext(); ) {
Node child = it.nextNode();
if (destName.equals(child.getName())) {
if (it.hasNext()) {
destName = Text.getName(it.nextNode().getName());
} else {
destName = null;
}
break;
}
}
// reorder... if no child node matches the original destName
// the reorder will fail. no extra check.
parent.orderBefore(srcName, destName);
} else {
// standard jcr reorder (before)
parent.orderBefore(srcName, destName);
}
}
} catch (RepositoryException e) {
throw new DiffException(e.getMessage(), e);
}
}
use of javax.jcr.Item in project jackrabbit by apache.
the class DefaultHandler method canExport.
/**
* Returns true if the export root is a node and if it contains a child node
* with name {@link JcrConstants#JCR_CONTENT jcr:content} in case this
* export is not intended for a collection.
*
* @return true if the export root is a node. If the specified boolean parameter
* is false (not a collection export) the given export root must contain a
* child node with name {@link JcrConstants#JCR_CONTENT jcr:content}.
*
* @see IOHandler#canExport(ExportContext, boolean)
*/
public boolean canExport(ExportContext context, boolean isCollection) {
if (context == null || context.isCompleted()) {
return false;
}
Item exportRoot = context.getExportRoot();
boolean success = exportRoot != null && exportRoot.isNode();
if (success && !isCollection) {
try {
Node n = ((Node) exportRoot);
success = n.hasNode(JcrConstants.JCR_CONTENT);
} catch (RepositoryException e) {
// should never occur.
success = false;
}
}
return success;
}
Aggregations