Search in sources :

Example 66 with DavException

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;
}
Also used : DavException(org.apache.jackrabbit.webdav.DavException) Element(org.w3c.dom.Element) MultiStatus(org.apache.jackrabbit.webdav.MultiStatus) RepositoryException(javax.jcr.RepositoryException) Item(javax.jcr.Item) Version(javax.jcr.version.Version) EventListener(javax.jcr.observation.EventListener) Session(javax.jcr.Session) DavResourceLocator(org.apache.jackrabbit.webdav.DavResourceLocator)

Example 67 with DavException

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;
    }
}
Also used : DavResource(org.apache.jackrabbit.webdav.DavResource) DavException(org.apache.jackrabbit.webdav.DavException) DavResourceIteratorImpl(org.apache.jackrabbit.webdav.DavResourceIteratorImpl) RepositoryException(javax.jcr.RepositoryException) DavResourceLocator(org.apache.jackrabbit.webdav.DavResourceLocator)

Example 68 with DavException

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);
    }
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) HashMap(java.util.HashMap) DavException(org.apache.jackrabbit.webdav.DavException) Element(org.w3c.dom.Element) TemplateBuilderFactory(org.apache.jackrabbit.commons.cnd.TemplateBuilderFactory) RepositoryException(javax.jcr.RepositoryException) NamespaceRegistry(javax.jcr.NamespaceRegistry) NodeTypeTemplate(javax.jcr.nodetype.NodeTypeTemplate) StringReader(java.io.StringReader) DefinitionBuilderFactory(org.apache.jackrabbit.commons.cnd.DefinitionBuilderFactory) List(java.util.List) ArrayList(java.util.ArrayList) ParseException(org.apache.jackrabbit.commons.cnd.ParseException) NamespacesProperty(org.apache.jackrabbit.webdav.jcr.property.NamespacesProperty) CompactNodeTypeDefReader(org.apache.jackrabbit.commons.cnd.CompactNodeTypeDefReader) Session(javax.jcr.Session)

Example 69 with DavException

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);
}
Also used : DavException(org.apache.jackrabbit.webdav.DavException) Session(javax.jcr.Session)

Example 70 with DavException

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);
    }
}
Also used : JcrDavPropertyNameSet(org.apache.jackrabbit.webdav.jcr.property.JcrDavPropertyNameSet) Set(java.util.Set) NodeTypeProperty(org.apache.jackrabbit.webdav.jcr.nodetype.NodeTypeProperty) DavException(org.apache.jackrabbit.webdav.DavException) Node(javax.jcr.Node) NodeType(javax.jcr.nodetype.NodeType) RepositoryException(javax.jcr.RepositoryException) DavPropertyName(org.apache.jackrabbit.webdav.property.DavPropertyName)

Aggregations

DavException (org.apache.jackrabbit.webdav.DavException)129 RepositoryException (javax.jcr.RepositoryException)89 IOException (java.io.IOException)51 HttpResponse (org.apache.http.HttpResponse)47 Element (org.w3c.dom.Element)29 DavResourceLocator (org.apache.jackrabbit.webdav.DavResourceLocator)25 JcrDavException (org.apache.jackrabbit.webdav.jcr.JcrDavException)25 DavResource (org.apache.jackrabbit.webdav.DavResource)21 ArrayList (java.util.ArrayList)19 MultiStatusResponse (org.apache.jackrabbit.webdav.MultiStatusResponse)17 Node (javax.jcr.Node)16 DavPropertyNameSet (org.apache.jackrabbit.webdav.property.DavPropertyNameSet)15 ItemNotFoundException (javax.jcr.ItemNotFoundException)13 DavPropertySet (org.apache.jackrabbit.webdav.property.DavPropertySet)13 HttpPropfind (org.apache.jackrabbit.webdav.client.methods.HttpPropfind)12 ElementIterator (org.apache.jackrabbit.webdav.xml.ElementIterator)12 HrefProperty (org.apache.jackrabbit.webdav.property.HrefProperty)8 Document (org.w3c.dom.Document)8 Session (javax.jcr.Session)7 Version (javax.jcr.version.Version)7