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);
}
}
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);
}
}
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);
}
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;
}
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);
}
Aggregations