Search in sources :

Example 1 with ReportInfo

use of org.apache.jackrabbit.webdav.version.report.ReportInfo in project jackrabbit by apache.

the class URIResolverImpl method getItemUri.

String getItemUri(ItemId itemId, String workspaceName, SessionInfo sessionInfo) throws RepositoryException {
    IdURICache cache = getCache(workspaceName);
    // check if uri is available from cache
    if (cache.containsItemId(itemId)) {
        return cache.getUri(itemId);
    } else {
        StringBuffer uriBuffer = new StringBuffer();
        Path path = itemId.getPath();
        String uniqueID = itemId.getUniqueID();
        // resolver uuid part
        if (uniqueID != null) {
            ItemId uuidId = (path == null) ? itemId : service.getIdFactory().createNodeId(uniqueID);
            if (path != null && cache.containsItemId(uuidId)) {
                // append uri of parent node, that is already cached
                uriBuffer.append(cache.getUri(uuidId));
            } else {
                // try to request locate-by-uuid report to build the uri
                ReportInfo rInfo = new ReportInfo(JcrRemotingConstants.REPORT_LOCATE_BY_UUID, ItemResourceConstants.NAMESPACE);
                rInfo.setContentElement(DomUtil.hrefToXml(uniqueID, domFactory));
                HttpReport request = null;
                try {
                    String wspUri = getWorkspaceUri(workspaceName);
                    request = new HttpReport(wspUri, rInfo);
                    HttpResponse response = service.executeRequest(sessionInfo, request);
                    request.checkSuccess(response);
                    MultiStatus ms = request.getResponseBodyAsMultiStatus(response);
                    if (ms.getResponses().length == 1) {
                        String absoluteUri = resolve(wspUri, ms.getResponses()[0].getHref());
                        uriBuffer.append(absoluteUri);
                        cache.add(absoluteUri, uuidId);
                    } else {
                        throw new ItemNotFoundException("Cannot identify item with uniqueID " + uniqueID);
                    }
                } catch (IOException e) {
                    throw new RepositoryException(e.getMessage());
                } catch (DavException e) {
                    throw ExceptionConverter.generate(e);
                } finally {
                    if (request != null) {
                        request.releaseConnection();
                    }
                }
            }
        } else {
            // start build uri from root-item
            uriBuffer.append(getRootItemUri(workspaceName));
        }
        // resolve relative-path part unless it denotes the root-item
        if (path != null && !path.denotesRoot()) {
            String jcrPath = service.getNamePathResolver(sessionInfo).getJCRPath(path);
            if (!path.isAbsolute() && !uriBuffer.toString().endsWith("/")) {
                uriBuffer.append("/");
            }
            uriBuffer.append(Text.escapePath(jcrPath));
        }
        String itemUri = uriBuffer.toString();
        if (!cache.containsItemId(itemId)) {
            cache.add(itemUri, itemId);
        }
        return itemUri;
    }
}
Also used : Path(org.apache.jackrabbit.spi.Path) HttpReport(org.apache.jackrabbit.webdav.client.methods.HttpReport) DavException(org.apache.jackrabbit.webdav.DavException) ReportInfo(org.apache.jackrabbit.webdav.version.report.ReportInfo) HttpResponse(org.apache.http.HttpResponse) MultiStatus(org.apache.jackrabbit.webdav.MultiStatus) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) ItemId(org.apache.jackrabbit.spi.ItemId) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Example 2 with ReportInfo

use of org.apache.jackrabbit.webdav.version.report.ReportInfo in project jackrabbit by apache.

the class RepositoryServiceImpl method getRegisteredNamespaces.

@Override
public Map<String, String> getRegisteredNamespaces(SessionInfo sessionInfo) throws RepositoryException {
    ReportInfo info = new ReportInfo(JcrRemotingConstants.REPORT_REGISTERED_NAMESPACES, ItemResourceConstants.NAMESPACE);
    HttpReport request = null;
    try {
        request = new HttpReport(uriResolver.getWorkspaceUri(sessionInfo.getWorkspaceName()), info);
        HttpResponse response = executeRequest(sessionInfo, request);
        request.checkSuccess(response);
        Document doc = request.getResponseBodyAsDocument(response.getEntity());
        Map<String, String> namespaces = new HashMap<String, String>();
        if (doc != null) {
            Element rootElement = doc.getDocumentElement();
            ElementIterator nsElems = DomUtil.getChildren(rootElement, JcrRemotingConstants.XML_NAMESPACE, ItemResourceConstants.NAMESPACE);
            while (nsElems.hasNext()) {
                Element elem = nsElems.nextElement();
                String prefix = DomUtil.getChildText(elem, JcrRemotingConstants.XML_PREFIX, ItemResourceConstants.NAMESPACE);
                String uri = DomUtil.getChildText(elem, JcrRemotingConstants.XML_URI, ItemResourceConstants.NAMESPACE);
                // default namespace
                if (prefix == null && uri == null) {
                    prefix = uri = "";
                }
                // any other uri must not be null
                if (uri != null) {
                    namespaces.put(prefix, uri);
                    // TODO: not correct since nsRegistry is retrieved from each session
                    nsCache.add(prefix, uri);
                } else {
                    log.error("Invalid prefix / uri pair: " + prefix + " -> " + uri);
                }
            }
        }
        return namespaces;
    } catch (IOException 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) ElementIterator(org.apache.jackrabbit.webdav.xml.ElementIterator) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) DavException(org.apache.jackrabbit.webdav.DavException) Element(org.w3c.dom.Element) ReportInfo(org.apache.jackrabbit.webdav.version.report.ReportInfo) HttpResponse(org.apache.http.HttpResponse) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) Document(org.w3c.dom.Document)

Example 3 with ReportInfo

use of org.apache.jackrabbit.webdav.version.report.ReportInfo in project jackrabbit by apache.

the class WebdavRequestImpl method getReportInfo.

/**
     * @see org.apache.jackrabbit.webdav.version.DeltaVServletRequest#getReportInfo()
     */
public ReportInfo getReportInfo() throws DavException {
    ReportInfo rInfo = null;
    Document requestDocument = getRequestDocument();
    if (requestDocument != null) {
        rInfo = new ReportInfo(requestDocument.getDocumentElement(), getDepth(DEPTH_0));
    }
    return rInfo;
}
Also used : ReportInfo(org.apache.jackrabbit.webdav.version.report.ReportInfo) Document(org.w3c.dom.Document)

Example 4 with ReportInfo

use of org.apache.jackrabbit.webdav.version.report.ReportInfo 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 5 with ReportInfo

use of org.apache.jackrabbit.webdav.version.report.ReportInfo in project jackrabbit by apache.

the class RepositoryServiceImpl method getRepositoryDescriptors.

@Override
public Map<String, QValue[]> getRepositoryDescriptors() throws RepositoryException {
    if (descriptors.isEmpty()) {
        ReportInfo info = new ReportInfo(JcrRemotingConstants.REPORT_REPOSITORY_DESCRIPTORS, ItemResourceConstants.NAMESPACE);
        HttpReport request = null;
        try {
            request = new HttpReport(uriResolver.getRepositoryUri(), info);
            HttpResponse response = executeRequest(null, request);
            int sc = response.getStatusLine().getStatusCode();
            if (sc == HttpStatus.SC_UNAUTHORIZED || sc == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED) {
                // JCR-3076: Mandatory authentication prevents us from
                // accessing the descriptors on the server, so instead
                // of failing with an exception we simply return an empty
                // set of descriptors
                log.warn("Authentication required to access repository descriptors");
                return descriptors;
            }
            request.checkSuccess(response);
            Document doc = request.getResponseBodyAsDocument(response.getEntity());
            if (doc != null) {
                Element rootElement = doc.getDocumentElement();
                ElementIterator nsElems = DomUtil.getChildren(rootElement, JcrRemotingConstants.XML_DESCRIPTOR, ItemResourceConstants.NAMESPACE);
                while (nsElems.hasNext()) {
                    Element elem = nsElems.nextElement();
                    String key = DomUtil.getChildText(elem, JcrRemotingConstants.XML_DESCRIPTORKEY, ItemResourceConstants.NAMESPACE);
                    ElementIterator it = DomUtil.getChildren(elem, JcrRemotingConstants.XML_DESCRIPTORVALUE, ItemResourceConstants.NAMESPACE);
                    List<QValue> vs = new ArrayList<QValue>();
                    while (it.hasNext()) {
                        Element dv = it.nextElement();
                        String descriptor = DomUtil.getText(dv);
                        if (key != null && descriptor != null) {
                            String typeStr = (DomUtil.getAttribute(dv, JcrRemotingConstants.ATTR_VALUE_TYPE, null));
                            int type = (typeStr == null) ? PropertyType.STRING : PropertyType.valueFromName(typeStr);
                            vs.add(getQValueFactory().create(descriptor, type));
                        } else {
                            log.error("Invalid descriptor key / value pair: " + key + " -> " + descriptor);
                        }
                    }
                    descriptors.put(key, vs.toArray(new QValue[vs.size()]));
                }
            }
        } catch (IOException e) {
            throw new RepositoryException(e);
        } catch (DavException e) {
            throw ExceptionConverter.generate(e);
        } finally {
            if (request != null) {
                request.releaseConnection();
            }
        }
    }
    return descriptors;
}
Also used : HttpReport(org.apache.jackrabbit.webdav.client.methods.HttpReport) ElementIterator(org.apache.jackrabbit.webdav.xml.ElementIterator) QValue(org.apache.jackrabbit.spi.QValue) DavException(org.apache.jackrabbit.webdav.DavException) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) ReportInfo(org.apache.jackrabbit.webdav.version.report.ReportInfo) HttpResponse(org.apache.http.HttpResponse) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) Document(org.w3c.dom.Document)

Aggregations

ReportInfo (org.apache.jackrabbit.webdav.version.report.ReportInfo)7 IOException (java.io.IOException)5 RepositoryException (javax.jcr.RepositoryException)5 HttpResponse (org.apache.http.HttpResponse)5 DavException (org.apache.jackrabbit.webdav.DavException)5 HttpReport (org.apache.jackrabbit.webdav.client.methods.HttpReport)5 Document (org.w3c.dom.Document)4 ItemNotFoundException (javax.jcr.ItemNotFoundException)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 ElementIterator (org.apache.jackrabbit.webdav.xml.ElementIterator)2 Element (org.w3c.dom.Element)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ItemId (org.apache.jackrabbit.spi.ItemId)1 Path (org.apache.jackrabbit.spi.Path)1 QValue (org.apache.jackrabbit.spi.QValue)1 MultiStatus (org.apache.jackrabbit.webdav.MultiStatus)1 MultiStatusResponse (org.apache.jackrabbit.webdav.MultiStatusResponse)1