Search in sources :

Example 56 with DavException

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

the class BaseDavRequest method getResponseBodyAsSubscriptionDiscovery.

/**
     * Return response body as {@link SubscriptionDiscovery} object.
     * @throws IllegalStateException when response does not represent a {@link SubscriptionDiscovery}
     * @throws DavException for failures in obtaining/parsing the response body
     */
public SubscriptionDiscovery getResponseBodyAsSubscriptionDiscovery(HttpResponse response) throws DavException {
    try {
        Document doc = getResponseBodyAsDocument(response.getEntity());
        if (doc == null) {
            throw new DavException(response.getStatusLine().getStatusCode(), "no response body");
        }
        Element root = doc.getDocumentElement();
        if (!DomUtil.matches(root, DavConstants.XML_PROP, DavConstants.NAMESPACE) && DomUtil.hasChildElement(root, ObservationConstants.SUBSCRIPTIONDISCOVERY.getName(), ObservationConstants.SUBSCRIPTIONDISCOVERY.getNamespace())) {
            throw new DavException(response.getStatusLine().getStatusCode(), "Missing DAV:prop response body in SUBSCRIBE response.");
        }
        Element sde = DomUtil.getChildElement(root, ObservationConstants.SUBSCRIPTIONDISCOVERY.getName(), ObservationConstants.SUBSCRIPTIONDISCOVERY.getNamespace());
        SubscriptionDiscovery sd = SubscriptionDiscovery.createFromXml(sde);
        if (((Subscription[]) sd.getValue()).length > 0) {
            return sd;
        } else {
            throw new DavException(response.getStatusLine().getStatusCode(), "Missing 'subscription' elements in SUBSCRIBE response body. At least a single subscription must be present if SUBSCRIBE was successful.");
        }
    } catch (IOException ex) {
        throw new DavException(response.getStatusLine().getStatusCode(), ex);
    }
}
Also used : DavException(org.apache.jackrabbit.webdav.DavException) Element(org.w3c.dom.Element) SubscriptionDiscovery(org.apache.jackrabbit.webdav.observation.SubscriptionDiscovery) IOException(java.io.IOException) Document(org.w3c.dom.Document) Subscription(org.apache.jackrabbit.webdav.observation.Subscription)

Example 57 with DavException

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

the class BaseDavRequest method getResponseBodyAsLockDiscovery.

/**
     * Return response body as {@link LockDiscovery} object.
     * @throws IllegalStateException when response does not represent a {@link LockDiscovery}
     * @throws DavException for failures in obtaining/parsing the response body
     */
public LockDiscovery getResponseBodyAsLockDiscovery(HttpResponse response) throws DavException {
    try {
        Document doc = getResponseBodyAsDocument(response.getEntity());
        if (doc == null) {
            throw new DavException(response.getStatusLine().getStatusCode(), "no response body");
        }
        Element root = doc.getDocumentElement();
        if (!DomUtil.matches(root, DavConstants.XML_PROP, DavConstants.NAMESPACE) && DomUtil.hasChildElement(root, DavConstants.PROPERTY_LOCKDISCOVERY, DavConstants.NAMESPACE)) {
            throw new DavException(response.getStatusLine().getStatusCode(), "Missing DAV:prop response body in LOCK response.");
        }
        Element lde = DomUtil.getChildElement(root, DavConstants.PROPERTY_LOCKDISCOVERY, DavConstants.NAMESPACE);
        if (!DomUtil.hasChildElement(lde, DavConstants.XML_ACTIVELOCK, DavConstants.NAMESPACE)) {
            throw new DavException(response.getStatusLine().getStatusCode(), "The DAV:lockdiscovery must contain a least a single DAV:activelock in response to a successful LOCK request.");
        }
        return LockDiscovery.createFromXml(lde);
    } catch (IOException ex) {
        throw new DavException(response.getStatusLine().getStatusCode(), ex);
    }
}
Also used : DavException(org.apache.jackrabbit.webdav.DavException) Element(org.w3c.dom.Element) IOException(java.io.IOException) Document(org.w3c.dom.Document)

Example 58 with DavException

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

the class BaseDavRequest method getResponseException.

/**
     * Obtain a {@link DavException} representing the response.
     * @throws IllegalStateException when the response is considered to be successful
     */
public DavException getResponseException(HttpResponse response) {
    if (succeeded(response)) {
        String msg = "Cannot retrieve exception from successful response.";
        log.warn(msg);
        throw new IllegalStateException(msg);
    }
    StatusLine st = response.getStatusLine();
    Element responseRoot = null;
    try {
        responseRoot = getResponseBodyAsDocument(response.getEntity()).getDocumentElement();
    } catch (IOException e) {
    // non-parseable body -> use null element
    }
    return new DavException(st.getStatusCode(), st.getReasonPhrase(), null, responseRoot);
}
Also used : StatusLine(org.apache.http.StatusLine) DavException(org.apache.jackrabbit.webdav.DavException) Element(org.w3c.dom.Element) IOException(java.io.IOException)

Example 59 with DavException

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

the class SearchInfo method createFromXml.

/**
     * Create a new <code>SearchInfo</code> from the specifying document
     * retrieved from the request body.
     *
     * @param searchRequest
     * @throws DavException if the root element's name is other than
     * 'searchrequest' or if it does not contain a single child element specifying
     * the query language to be used.
     */
public static SearchInfo createFromXml(Element searchRequest) throws DavException {
    if (searchRequest == null || !XML_SEARCHREQUEST.equals(searchRequest.getLocalName())) {
        log.warn("The root element must be 'searchrequest'.");
        throw new DavException(DavServletResponse.SC_BAD_REQUEST);
    }
    Element first = DomUtil.getFirstChildElement(searchRequest);
    Attr[] nsAttributes = DomUtil.getNamespaceAttributes(searchRequest);
    Map<String, String> namespaces = new HashMap<String, String>();
    for (Attr nsAttribute : nsAttributes) {
        // filter out xmlns namespace and DAV namespace
        if (!IGNORED_NAMESPACES.contains(nsAttribute.getValue())) {
            namespaces.put(nsAttribute.getLocalName(), nsAttribute.getValue());
        }
    }
    SearchInfo sInfo;
    if (first != null) {
        sInfo = new SearchInfo(first.getLocalName(), DomUtil.getNamespace(first), DomUtil.getText(first), namespaces);
    } else {
        log.warn("A single child element is expected with the 'DAV:searchrequest'.");
        throw new DavException(DavServletResponse.SC_BAD_REQUEST);
    }
    Element limit = DomUtil.getChildElement(searchRequest, LIMIT, NAMESPACE);
    if (limit != null) {
        // try to get the value DAV:nresults element
        String nresults = DomUtil.getChildTextTrim(limit, NRESULTS, NAMESPACE);
        if (nresults != null) {
            try {
                sInfo.setNumberResults(Long.valueOf(nresults));
            } catch (NumberFormatException e) {
                log.error("DAV:nresults cannot be parsed into a long -> ignore.");
            }
        }
        // try of an offset is defined within the DAV:limit element.
        String offset = DomUtil.getChildTextTrim(limit, OFFSET, Namespace.EMPTY_NAMESPACE);
        if (offset != null) {
            try {
                sInfo.setOffset(Long.valueOf(offset));
            } catch (NumberFormatException e) {
                log.error("'offset' cannot be parsed into a long -> ignore.");
            }
        }
    }
    return sInfo;
}
Also used : DavException(org.apache.jackrabbit.webdav.DavException) HashMap(java.util.HashMap) Element(org.w3c.dom.Element) Attr(org.w3c.dom.Attr)

Example 60 with DavException

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

the class AclProperty method createFromXml.

/**
     * Build a new <code>AclProperty</code> object from the request body of the
     * ACL method call.
     *
     * @param aclElement
     * @return new <code>AclProperty</code>
     * @throws DavException
     */
public static AclProperty createFromXml(Element aclElement) throws DavException {
    if (!DomUtil.matches(aclElement, SecurityConstants.ACL.getName(), SecurityConstants.ACL.getNamespace())) {
        throw new DavException(DavServletResponse.SC_BAD_REQUEST, "ACL request requires a DAV:acl body.");
    }
    List<Ace> aces = new ArrayList<Ace>();
    ElementIterator it = DomUtil.getChildren(aclElement, Ace.XML_ACE, SecurityConstants.NAMESPACE);
    while (it.hasNext()) {
        Element aceElem = it.nextElement();
        aces.add(Ace.createFromXml(aceElem));
    }
    return new AclProperty(aces);
}
Also used : ElementIterator(org.apache.jackrabbit.webdav.xml.ElementIterator) DavException(org.apache.jackrabbit.webdav.DavException) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList)

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