use of org.apache.jackrabbit.webdav.DavException in project jackrabbit by apache.
the class WorkspaceResourceImpl method update.
/**
* While RFC 3253 does not define any version-related operations for the
* workspace resource, this implementation uses {@link VersionControlledResource#update(UpdateInfo)}
* to map {@link Workspace#restore(javax.jcr.version.Version[], boolean)} to
* a WebDAV call.
* <p>
* Limitation: note that the <code>MultiStatus</code> returned by this method
* will not list any nodes that have been removed due to an Uuid conflict.
*
* @param updateInfo
* @return
* @throws org.apache.jackrabbit.webdav.DavException
* @see org.apache.jackrabbit.webdav.version.VersionControlledResource#update(org.apache.jackrabbit.webdav.version.UpdateInfo)
*/
@Override
public MultiStatus update(UpdateInfo updateInfo) throws DavException {
if (updateInfo == null) {
throw new DavException(DavServletResponse.SC_BAD_REQUEST, "Valid update request body required.");
}
if (!exists()) {
throw new DavException(DavServletResponse.SC_NOT_FOUND);
}
Session session = getRepositorySession();
MultiStatus ms = new MultiStatus();
try {
Element udElem = updateInfo.getUpdateElement();
boolean removeExisting = DomUtil.hasChildElement(udElem, ItemResourceConstants.XML_REMOVEEXISTING, ItemResourceConstants.NAMESPACE);
// register eventListener in order to be able to report the modified resources.
EventListener el = new EListener(updateInfo.getPropertyNameSet(), ms);
registerEventListener(el, session.getRootNode().getPath());
String[] hrefs = updateInfo.getVersionHref();
if (hrefs == null || hrefs.length < 1) {
throw new DavException(DavServletResponse.SC_BAD_REQUEST, "Invalid update request body: at least a single version href must be specified.");
}
// perform the update/restore according to the update info
Version[] versions = new Version[hrefs.length];
for (int i = 0; i < hrefs.length; i++) {
DavResourceLocator vLoc = getLocator().getFactory().createResourceLocator(getLocator().getPrefix(), hrefs[i]);
String versionPath = vLoc.getRepositoryPath();
Item item = getRepositorySession().getItem(versionPath);
if (item instanceof Version) {
versions[i] = (Version) item;
} else {
throw new DavException(DavServletResponse.SC_BAD_REQUEST, "Invalid update request body: href does not identify a version " + hrefs[i]);
}
}
session.getWorkspace().restore(versions, removeExisting);
// unregister the event listener again
unregisterEventListener(el);
} catch (RepositoryException e) {
throw new JcrDavException(e);
}
return ms;
}
use of org.apache.jackrabbit.webdav.DavException in project jackrabbit by apache.
the class WorkspaceResourceImpl method getMembers.
/**
* Returns the resource representing the JCR root node.
*
* @return
*/
@Override
public DavResourceIterator getMembers() {
try {
DavResourceLocator loc = getLocatorFromItem(getRepositorySession().getRootNode());
List<DavResource> list = Collections.singletonList(createResourceFromLocator(loc));
return new DavResourceIteratorImpl(list);
} catch (DavException e) {
log.error("Internal error while building resource for the root node.", e);
return DavResourceIteratorImpl.EMPTY;
} catch (RepositoryException e) {
log.error("Internal error while building resource for the root node.", e);
return DavResourceIteratorImpl.EMPTY;
}
}
use of org.apache.jackrabbit.webdav.DavException in project jackrabbit by apache.
the class WorkspaceResourceImpl method setProperty.
/**
* Allows to alter the registered namespaces ({@link ItemResourceConstants#JCR_NAMESPACES})
* or register node types {@link ItemResourceConstants#JCR_NODETYPES_CND}
* where the passed value is a cnd string containing the definition
* and forwards any other property to the super class.
* <p>
* Note that again no property status is set. Any failure while setting
* a property results in an exception (violating RFC 2518).
*
* @param property
* @throws DavException
* @see DavResource#setProperty(org.apache.jackrabbit.webdav.property.DavProperty)
*/
@Override
public void setProperty(DavProperty<?> property) throws DavException {
if (ItemResourceConstants.JCR_NAMESPACES.equals(property.getName())) {
NamespacesProperty nsp = new NamespacesProperty(property);
try {
Map<String, String> changes = new HashMap<String, String>(nsp.getNamespaces());
NamespaceRegistry nsReg = getRepositorySession().getWorkspace().getNamespaceRegistry();
for (String prefix : nsReg.getPrefixes()) {
if (!changes.containsKey(prefix)) {
// prefix not present amongst the new values any more > unregister
nsReg.unregisterNamespace(prefix);
} else if (changes.get(prefix).equals(nsReg.getURI(prefix))) {
// present with same uri-value >> no action required
changes.remove(prefix);
}
}
// it has not been present before.
for (String prefix : changes.keySet()) {
String uri = changes.get(prefix);
nsReg.registerNamespace(prefix, uri);
}
} catch (RepositoryException e) {
throw new JcrDavException(e);
}
} else if (ItemResourceConstants.JCR_NODETYPES_CND.equals(property.getName())) {
try {
Object value = property.getValue();
List<?> cmds;
if (value instanceof List) {
cmds = (List) value;
} else if (value instanceof Element) {
cmds = Collections.singletonList(value);
} else {
log.warn("Unexpected structure of dcr:nodetypes-cnd property.");
throw new DavException(DavServletResponse.SC_INTERNAL_SERVER_ERROR);
}
String registerCnd = null;
boolean allowUpdate = false;
List<String> unregisterNames = new ArrayList<String>();
for (Object listEntry : cmds) {
if (listEntry instanceof Element) {
Element e = (Element) listEntry;
String localName = e.getLocalName();
if (ItemResourceConstants.XML_CND.equals(localName)) {
registerCnd = DomUtil.getText(e);
} else if (ItemResourceConstants.XML_ALLOWUPDATE.equals(localName)) {
String allow = DomUtil.getTextTrim(e);
allowUpdate = Boolean.parseBoolean(allow);
} else if (ItemResourceConstants.XML_NODETYPENAME.equals(localName)) {
unregisterNames.add(DomUtil.getTextTrim(e));
}
}
}
// TODO: for simplicity it's currently either registration or unregistration as nt-modifications are immediately persisted.
Session s = getRepositorySession();
NodeTypeManager ntMgr = s.getWorkspace().getNodeTypeManager();
if (registerCnd != null) {
StringReader reader = new StringReader(registerCnd);
DefinitionBuilderFactory<NodeTypeTemplate, NamespaceRegistry> factory = new TemplateBuilderFactory(ntMgr, s.getValueFactory(), s.getWorkspace().getNamespaceRegistry());
CompactNodeTypeDefReader<NodeTypeTemplate, NamespaceRegistry> cndReader = new CompactNodeTypeDefReader<NodeTypeTemplate, NamespaceRegistry>(reader, "davex", factory);
List<NodeTypeTemplate> ntts = cndReader.getNodeTypeDefinitions();
ntMgr.registerNodeTypes(ntts.toArray(new NodeTypeTemplate[ntts.size()]), allowUpdate);
} else if (!unregisterNames.isEmpty()) {
ntMgr.unregisterNodeTypes(unregisterNames.toArray(new String[unregisterNames.size()]));
}
} catch (ParseException e) {
throw new DavException(DavServletResponse.SC_BAD_REQUEST, e);
} catch (RepositoryException e) {
throw new JcrDavException(e);
}
} else {
// only jcr:namespace or node types can be modified
throw new DavException(DavServletResponse.SC_CONFLICT);
}
}
use of org.apache.jackrabbit.webdav.DavException in project jackrabbit by apache.
the class JCRWebdavServerServlet method isPreconditionValid.
/**
* Returns true if the preconditions are met. This includes validation of
* {@link WebdavRequest#matchesIfHeader(DavResource) If header} and validation
* of {@link org.apache.jackrabbit.webdav.transaction.TransactionConstants#HEADER_TRANSACTIONID
* TransactionId header}. This method will also return false if the requested
* resource resides within a different workspace as is assigned to the repository
* session attached to the given request.
*
* @see AbstractWebdavServlet#isPreconditionValid(WebdavRequest, DavResource)
*/
@Override
protected boolean isPreconditionValid(WebdavRequest request, DavResource resource) {
// first check matching If header
if (!request.matchesIfHeader(resource)) {
return false;
}
// this may occur if the session was retrieved from the cache.
try {
Session repositorySesssion = JcrDavSession.getRepositorySession(request.getDavSession());
String reqWspName = resource.getLocator().getWorkspaceName();
String wsName = repositorySesssion.getWorkspace().getName();
// root-collection and the request not MKWORKSPACE.
if (DavMethods.DAV_MKWORKSPACE != DavMethods.getMethodCode(request.getMethod()) && reqWspName != null && !reqWspName.equals(wsName)) {
return false;
}
} catch (DavException e) {
log.error("Internal error: " + e.toString());
return false;
}
// make sure, the TransactionId header is valid
String txId = request.getTransactionId();
return txId == null || txMgr.hasLock(txId, resource);
}
use of org.apache.jackrabbit.webdav.DavException in project jackrabbit by apache.
the class DefaultItemCollection method internalSetProperty.
/**
* Internal method used to set or add the given property
*
* @param property
* @throws DavException
* @see #setProperty(DavProperty)
*/
private void internalSetProperty(DavProperty<?> property) throws DavException {
if (!exists()) {
throw new DavException(DavServletResponse.SC_NOT_FOUND);
}
DavPropertyName propName = property.getName();
if (JCR_MIXINNODETYPES.equals(propName)) {
Node n = (Node) item;
try {
NodeTypeProperty mix = new NodeTypeProperty(property);
Set<String> mixins = mix.getNodeTypeNames();
for (NodeType existingMixin : n.getMixinNodeTypes()) {
String name = existingMixin.getName();
if (mixins.contains(name)) {
// do not add existing mixins
mixins.remove(name);
} else {
// remove mixin that are not contained in the new list
n.removeMixin(name);
}
}
// add the remaining mixing types that are not yet set
for (String mixin : mixins) {
n.addMixin(mixin);
}
} catch (RepositoryException e) {
throw new JcrDavException(e);
}
} else if (JCR_PRIMARYNODETYPE.equals(propName)) {
Node n = (Node) item;
try {
NodeTypeProperty ntProp = new NodeTypeProperty(property);
Set<String> names = ntProp.getNodeTypeNames();
if (names.size() == 1) {
String ntName = names.iterator().next();
n.setPrimaryType(ntName);
} else {
// only a single node type can be primary node type.
throw new DavException(DavServletResponse.SC_BAD_REQUEST);
}
} catch (RepositoryException e) {
throw new JcrDavException(e);
}
} else {
// all props except for mixin node types and primaryType are read-only
throw new DavException(DavServletResponse.SC_CONFLICT);
}
}
Aggregations