Search in sources :

Example 16 with MultiStatusResponse

use of org.apache.jackrabbit.webdav.MultiStatusResponse in project jackrabbit by apache.

the class VersionControlledItemCollection method merge.

/**
     * Merge the repository node represented by this resource according to the
     * information present in the given {@link MergeInfo} object.
     *
     * @param mergeInfo
     * @return <code>MultiStatus</code> recording all repository items modified
     * by this merge call as well as the resources that a client must modify to
     * complete the merge (see <a href="http://www.webdav.org/specs/rfc3253.html#METHOD_MERGE">RFC 3253</a>)
     * @throws org.apache.jackrabbit.webdav.DavException
     * @see org.apache.jackrabbit.webdav.version.VersionControlledResource#merge(org.apache.jackrabbit.webdav.version.MergeInfo)
     * @see Node#merge(String, boolean)
     */
//TODO: with jcr the node must not be versionable in order to perform Node.merge
@Override
public MultiStatus merge(MergeInfo mergeInfo) throws DavException {
    if (mergeInfo == null) {
        throw new DavException(DavServletResponse.SC_BAD_REQUEST);
    }
    if (!exists()) {
        throw new DavException(DavServletResponse.SC_NOT_FOUND);
    }
    MultiStatus ms = new MultiStatus();
    try {
        // NOTE: RFC requires that all modified resources are reported in the
        // multistatus response. this doesn't work however with the remoting
        // there is no way to distinguish the 'failedId's from any other
        // resources that got modified by this merge operation -> omitted.
        // todo: RFC allows multiple href elements inside the DAV:source element
        String workspaceName = getLocatorFromHref(mergeInfo.getSourceHrefs()[0]).getWorkspaceName();
        String depth = DomUtil.getChildTextTrim(mergeInfo.getMergeElement(), DavConstants.XML_DEPTH, DavConstants.NAMESPACE);
        boolean isShallow = "0".equals(depth);
        NodeIterator failed = getVersionManager().merge(item.getPath(), workspaceName, !mergeInfo.isNoAutoMerge(), isShallow);
        // add resources to the multistatus, that failed to be merged
        while (failed.hasNext()) {
            Node failedNode = failed.nextNode();
            DavResourceLocator loc = getLocatorFromItem(failedNode);
            DavResource res = createResourceFromLocator(loc);
            ms.addResponse(new MultiStatusResponse(res, mergeInfo.getPropertyNameSet()));
        }
    } catch (RepositoryException e) {
        throw new JcrDavException(e);
    }
    return ms;
}
Also used : NodeIterator(javax.jcr.NodeIterator) DavResource(org.apache.jackrabbit.webdav.DavResource) DavException(org.apache.jackrabbit.webdav.DavException) Node(javax.jcr.Node) MultiStatusResponse(org.apache.jackrabbit.webdav.MultiStatusResponse) MultiStatus(org.apache.jackrabbit.webdav.MultiStatus) RepositoryException(javax.jcr.RepositoryException) DavResourceLocator(org.apache.jackrabbit.webdav.DavResourceLocator)

Example 17 with MultiStatusResponse

use of org.apache.jackrabbit.webdav.MultiStatusResponse in project jackrabbit by apache.

the class RepositoryServiceImpl method isGranted.

@Override
public boolean isGranted(SessionInfo sessionInfo, ItemId itemId, String[] actions) throws RepositoryException {
    HttpReport request = null;
    try {
        String uri = obtainAbsolutePathFromUri(getItemUri(itemId, sessionInfo));
        ReportInfo reportInfo = new ReportInfo(JcrRemotingConstants.REPORT_PRIVILEGES, ItemResourceConstants.NAMESPACE);
        reportInfo.setContentElement(DomUtil.hrefToXml(uri, DomUtil.createDocument()));
        request = new HttpReport(uriResolver.getWorkspaceUri(sessionInfo.getWorkspaceName()), reportInfo);
        HttpResponse response = executeRequest(sessionInfo, request);
        request.checkSuccess(response);
        MultiStatusResponse[] responses = request.getResponseBodyAsMultiStatus(response).getResponses();
        if (responses.length < 1) {
            throw new ItemNotFoundException("Unable to retrieve permissions for item " + saveGetIdString(itemId, sessionInfo));
        }
        DavProperty<?> p = responses[0].getProperties(DavServletResponse.SC_OK).get(SecurityConstants.CURRENT_USER_PRIVILEGE_SET);
        if (p == null) {
            return false;
        }
        // build set of privileges from given actions. NOTE: since the actions
        // have no qualifying namespace, the {@link ItemResourceConstants#NAMESPACE}
        // is used.
        Set<Privilege> requiredPrivileges = new HashSet<Privilege>();
        for (String action : actions) {
            requiredPrivileges.add(Privilege.getPrivilege(action, ItemResourceConstants.NAMESPACE));
        }
        // build set of privileges granted to the current user.
        CurrentUserPrivilegeSetProperty privSet = new CurrentUserPrivilegeSetProperty(p);
        Collection<Privilege> privileges = privSet.getValue();
        // check privileges present against required privileges.
        return privileges.containsAll(requiredPrivileges);
    } catch (IOException e) {
        throw new RepositoryException(e);
    } catch (ParserConfigurationException e) {
        throw new RepositoryException(e);
    } catch (DavException e) {
        throw ExceptionConverter.generate(e);
    } finally {
        if (request != null) {
            request.releaseConnection();
        }
    }
}
Also used : HttpReport(org.apache.jackrabbit.webdav.client.methods.HttpReport) DavException(org.apache.jackrabbit.webdav.DavException) MultiStatusResponse(org.apache.jackrabbit.webdav.MultiStatusResponse) ReportInfo(org.apache.jackrabbit.webdav.version.report.ReportInfo) HttpResponse(org.apache.http.HttpResponse) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) CurrentUserPrivilegeSetProperty(org.apache.jackrabbit.webdav.security.CurrentUserPrivilegeSetProperty) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SupportedPrivilege(org.apache.jackrabbit.webdav.security.SupportedPrivilege) Privilege(org.apache.jackrabbit.webdav.security.Privilege) ItemNotFoundException(javax.jcr.ItemNotFoundException) HashSet(java.util.HashSet)

Example 18 with MultiStatusResponse

use of org.apache.jackrabbit.webdav.MultiStatusResponse in project jackrabbit by apache.

the class RepositoryServiceImpl method obtain.

private SessionInfo obtain(CredentialsWrapper credentials, String workspaceName) throws RepositoryException {
    // check if the workspace with the given name is accessible
    HttpPropfind request = null;
    SessionInfoImpl sessionInfo = new SessionInfoImpl(credentials, workspaceName);
    try {
        DavPropertyNameSet nameSet = new DavPropertyNameSet();
        // for backwards compat. -> retrieve DAV:workspace if the newly
        // added property (workspaceName) is not supported by the server.
        nameSet.add(DeltaVConstants.WORKSPACE);
        nameSet.add(JcrRemotingConstants.JCR_WORKSPACE_NAME_LN, ItemResourceConstants.NAMESPACE);
        request = new HttpPropfind(uriResolver.getWorkspaceUri(workspaceName), nameSet, DEPTH_0);
        HttpResponse response = executeRequest(sessionInfo, request);
        request.checkSuccess(response);
        MultiStatusResponse[] responses = request.getResponseBodyAsMultiStatus(response).getResponses();
        if (responses.length != 1) {
            throw new LoginException("Login failed: Unknown workspace '" + workspaceName + "'.");
        }
        DavPropertySet props = responses[0].getProperties(DavServletResponse.SC_OK);
        DavProperty<?> prop = props.get(JcrRemotingConstants.JCR_WORKSPACE_NAME_LN, ItemResourceConstants.NAMESPACE);
        if (prop != null) {
            String wspName = prop.getValue().toString();
            if (workspaceName == null) {
                // login with 'null' workspace name -> retrieve the effective
                // workspace name from the property and recreate the SessionInfo.
                sessionInfo = new SessionInfoImpl(credentials, wspName);
            } else if (!wspName.equals(workspaceName)) {
                throw new LoginException("Login failed: Invalid workspace name '" + workspaceName + "'.");
            }
        } else if (props.contains(DeltaVConstants.WORKSPACE)) {
            String wspHref = new HrefProperty(props.get(DeltaVConstants.WORKSPACE)).getHrefs().get(0);
            String wspName = Text.unescape(Text.getName(wspHref, true));
            if (!wspName.equals(workspaceName)) {
                throw new LoginException("Login failed: Invalid workspace name " + workspaceName);
            }
        } else {
            throw new LoginException("Login failed: Unknown workspace '" + workspaceName + "'.");
        }
    } catch (IOException e) {
        throw new RepositoryException(e.getMessage());
    } catch (DavException e) {
        throw ExceptionConverter.generate(e);
    } finally {
        if (request != null) {
            request.releaseConnection();
        }
    }
    // mappings.
    if (nsCache.prefixToURI.isEmpty()) {
        try {
            getRegisteredNamespaces(sessionInfo);
        } catch (RepositoryException e) {
        // ignore
        }
    }
    // return the sessionInfo
    return sessionInfo;
}
Also used : DavException(org.apache.jackrabbit.webdav.DavException) MultiStatusResponse(org.apache.jackrabbit.webdav.MultiStatusResponse) HttpResponse(org.apache.http.HttpResponse) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) DavPropertySet(org.apache.jackrabbit.webdav.property.DavPropertySet) HttpPropfind(org.apache.jackrabbit.webdav.client.methods.HttpPropfind) HrefProperty(org.apache.jackrabbit.webdav.property.HrefProperty) DavPropertyNameSet(org.apache.jackrabbit.webdav.property.DavPropertyNameSet) LoginException(javax.jcr.LoginException)

Example 19 with MultiStatusResponse

use of org.apache.jackrabbit.webdav.MultiStatusResponse in project jackrabbit by apache.

the class RepositoryServiceImpl method getPrivilegeNames.

@Override
public Name[] getPrivilegeNames(SessionInfo sessionInfo, NodeId nodeId) throws RepositoryException {
    String uri = (nodeId == null) ? uriResolver.getWorkspaceUri(sessionInfo.getWorkspaceName()) : getItemUri(nodeId, sessionInfo);
    DavPropertyNameSet nameSet = new DavPropertyNameSet();
    nameSet.add(SecurityConstants.CURRENT_USER_PRIVILEGE_SET);
    HttpPropfind propfindRequest = null;
    try {
        propfindRequest = new HttpPropfind(uri, nameSet, DEPTH_0);
        HttpResponse response = execute(propfindRequest, sessionInfo);
        propfindRequest.checkSuccess(response);
        MultiStatusResponse[] mresponses = propfindRequest.getResponseBodyAsMultiStatus(response).getResponses();
        if (mresponses.length < 1) {
            throw new PathNotFoundException("Unable to retrieve privileges definitions.");
        }
        DavPropertyName displayName = SecurityConstants.CURRENT_USER_PRIVILEGE_SET;
        DavProperty<?> p = mresponses[0].getProperties(DavServletResponse.SC_OK).get(displayName);
        if (p == null) {
            return new Name[0];
        } else {
            Collection<Privilege> privs = new CurrentUserPrivilegeSetProperty(p).getValue();
            Set<Name> privNames = new HashSet<Name>(privs.size());
            for (Privilege priv : privs) {
                privNames.add(nameFactory.create(priv.getNamespace().getURI(), priv.getName()));
            }
            return privNames.toArray(new Name[privNames.size()]);
        }
    } catch (IOException e) {
        throw new RepositoryException(e);
    } catch (DavException e) {
        throw ExceptionConverter.generate(e);
    } finally {
        if (propfindRequest != null) {
            propfindRequest.releaseConnection();
        }
    }
}
Also used : DavException(org.apache.jackrabbit.webdav.DavException) MultiStatusResponse(org.apache.jackrabbit.webdav.MultiStatusResponse) HttpResponse(org.apache.http.HttpResponse) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) DavPropertyName(org.apache.jackrabbit.webdav.property.DavPropertyName) DavPropertyName(org.apache.jackrabbit.webdav.property.DavPropertyName) Name(org.apache.jackrabbit.spi.Name) HttpPropfind(org.apache.jackrabbit.webdav.client.methods.HttpPropfind) CurrentUserPrivilegeSetProperty(org.apache.jackrabbit.webdav.security.CurrentUserPrivilegeSetProperty) DavPropertyNameSet(org.apache.jackrabbit.webdav.property.DavPropertyNameSet) PathNotFoundException(javax.jcr.PathNotFoundException) SupportedPrivilege(org.apache.jackrabbit.webdav.security.SupportedPrivilege) Privilege(org.apache.jackrabbit.webdav.security.Privilege) HashSet(java.util.HashSet)

Example 20 with MultiStatusResponse

use of org.apache.jackrabbit.webdav.MultiStatusResponse in project jackrabbit by apache.

the class RepositoryServiceImpl method getWorkspaceNames.

@Override
public String[] getWorkspaceNames(SessionInfo sessionInfo) throws RepositoryException {
    DavPropertyNameSet nameSet = new DavPropertyNameSet();
    nameSet.add(DeltaVConstants.WORKSPACE);
    HttpPropfind request = null;
    try {
        request = new HttpPropfind(uriResolver.getRepositoryUri(), nameSet, DEPTH_1);
        HttpResponse response = executeRequest(sessionInfo, request);
        request.checkSuccess(response);
        MultiStatusResponse[] mresponses = request.getResponseBodyAsMultiStatus(response).getResponses();
        Set<String> wspNames = new HashSet<String>();
        for (MultiStatusResponse mresponse : mresponses) {
            DavPropertySet props = mresponse.getProperties(DavServletResponse.SC_OK);
            if (props.contains(DeltaVConstants.WORKSPACE)) {
                HrefProperty hp = new HrefProperty(props.get(DeltaVConstants.WORKSPACE));
                String wspHref = hp.getHrefs().get(0);
                String name = Text.unescape(Text.getName(wspHref, true));
                wspNames.add(name);
            }
        }
        return wspNames.toArray(new String[wspNames.size()]);
    } catch (IOException e) {
        throw new RepositoryException(e);
    } catch (DavException e) {
        throw ExceptionConverter.generate(e);
    } finally {
        if (request != null) {
            request.releaseConnection();
        }
    }
}
Also used : DavException(org.apache.jackrabbit.webdav.DavException) MultiStatusResponse(org.apache.jackrabbit.webdav.MultiStatusResponse) HttpResponse(org.apache.http.HttpResponse) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) DavPropertySet(org.apache.jackrabbit.webdav.property.DavPropertySet) HttpPropfind(org.apache.jackrabbit.webdav.client.methods.HttpPropfind) HrefProperty(org.apache.jackrabbit.webdav.property.HrefProperty) DavPropertyNameSet(org.apache.jackrabbit.webdav.property.DavPropertyNameSet) HashSet(java.util.HashSet)

Aggregations

MultiStatusResponse (org.apache.jackrabbit.webdav.MultiStatusResponse)27 RepositoryException (javax.jcr.RepositoryException)18 HttpResponse (org.apache.http.HttpResponse)18 DavException (org.apache.jackrabbit.webdav.DavException)17 HttpPropfind (org.apache.jackrabbit.webdav.client.methods.HttpPropfind)16 DavPropertyNameSet (org.apache.jackrabbit.webdav.property.DavPropertyNameSet)15 IOException (java.io.IOException)13 DavPropertySet (org.apache.jackrabbit.webdav.property.DavPropertySet)11 ItemNotFoundException (javax.jcr.ItemNotFoundException)10 ArrayList (java.util.ArrayList)5 MultiStatus (org.apache.jackrabbit.webdav.MultiStatus)5 NodeId (org.apache.jackrabbit.spi.NodeId)4 DavProperty (org.apache.jackrabbit.webdav.property.DavProperty)4 DavPropertyName (org.apache.jackrabbit.webdav.property.DavPropertyName)4 HrefProperty (org.apache.jackrabbit.webdav.property.HrefProperty)4 Element (org.w3c.dom.Element)4 HashSet (java.util.HashSet)3 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)3 CurrentUserPrivilegeSetProperty (org.apache.jackrabbit.webdav.security.CurrentUserPrivilegeSetProperty)3 Privilege (org.apache.jackrabbit.webdav.security.Privilege)3