1
package org.apache.jackrabbit.webdav;
4
* Licensed to the Apache Software Foundation (ASF) under one or more
5
* contributor license agreements. See the NOTICE file distributed with
6
* this work for additional information regarding copyright ownership.
7
* The ASF licenses this file to You under the Apache License, Version 2.0
8
* (the "License"); you may not use this file except in compliance with
9
* the License. You may obtain a copy of the License at
11
* http://www.apache.org/licenses/LICENSE-2.0
13
* Unless required by applicable law or agreed to in writing, software
14
* distributed under the License is distributed on an "AS IS" BASIS,
15
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
* See the License for the specific language governing permissions and
17
* limitations under the License.
20
import org.apache.jackrabbit.webdav.property.DavPropertyNameSet;
21
import org.apache.jackrabbit.webdav.xml.DomUtil;
22
import org.apache.jackrabbit.webdav.xml.ElementIterator;
23
import org.apache.jackrabbit.webdav.xml.XmlSerializable;
24
import org.w3c.dom.Document;
25
import org.w3c.dom.Element;
28
import java.util.LinkedHashMap;
31
* MultiStatus representing the content of a multistatus response body and
32
* allows to retrieve the Xml representation.
34
public class MultiStatus
35
implements DavConstants, XmlSerializable
39
* Map collecting the responses for this multistatus, where every href must
40
* only occur one single time.
42
private Map<String, MultiStatusResponse> responses = new LinkedHashMap<String, MultiStatusResponse>();
45
* A general response description at the multistatus top level is used to
46
* provide a general message describing the overarching nature of the response.
47
* If this value is available an application may use it instead of
48
* presenting the individual response descriptions contained within the
51
private String responseDescription;
54
* Add response(s) to this multistatus, in order to build a multistatus for
55
* responding to a PROPFIND request.
57
* @param resource The resource to add property from
58
* @param propNameSet The requested property names of the PROPFIND request
62
public void addResourceProperties( DavResource resource, DavPropertyNameSet propNameSet, int propFindType,
65
addResponse( new MultiStatusResponse( resource, propNameSet, propFindType ) );
66
if ( depth > 0 && resource.isCollection() )
68
DavResourceIterator iter = resource.getMembers();
69
while ( iter.hasNext() )
71
addResourceProperties( iter.nextResource(), propNameSet, propFindType, depth - 1 );
77
* Add response(s) to this multistatus, in order to build a multistatus e.g.
78
* in order to respond to a PROPFIND request. Please note, that in terms
79
* of PROPFIND, this method would correspond to a
80
* {@link DavConstants#PROPFIND_BY_PROPERTY} propfind type.
82
* @param resource The resource to add property from
83
* @param propNameSet The requested property names of the PROPFIND request
85
* @see #addResourceProperties(DavResource, DavPropertyNameSet, int, int) for
86
* the corresponding method that allows to specify the type explicitly.
88
public void addResourceProperties( DavResource resource, DavPropertyNameSet propNameSet, int depth )
90
addResourceProperties( resource, propNameSet, PROPFIND_BY_PROPERTY, depth );
94
* Add response(s) to this multistatus, in order to build a multistatus
95
* as returned for COPY, MOVE, LOCK or DELETE requests resulting in an error
96
* with a resource other than the resource identified in the Request-URI.
102
public void addResourceStatus( DavResource resource, int status, int depth )
104
addResponse( new MultiStatusResponse( resource.getHref(), status ) );
105
if ( depth > 0 && resource.isCollection() )
107
DavResourceIterator iter = resource.getMembers();
108
while ( iter.hasNext() )
110
addResourceStatus( iter.nextResource(), status, depth - 1 );
116
* Add a <code>MultiStatusResponse</code> element to this <code>MultiStatus</code>
120
public void addResponse( MultiStatusResponse response )
122
responses.put( response.getHref(), response );
126
* Returns the multistatus responses present as array.
128
* @return array of all {@link MultiStatusResponse responses} present in this
131
public MultiStatusResponse[] getResponses()
133
return (MultiStatusResponse[]) responses.values().toArray( new MultiStatusResponse[responses.size()] );
137
* Set the response description.
139
* @param responseDescription
141
public void setResponseDescription( String responseDescription )
143
this.responseDescription = responseDescription;
147
* Returns the response description.
149
* @return responseDescription
151
public String getResponseDescription()
153
return responseDescription;
157
* Return the Xml representation of this <code>MultiStatus</code>.
159
* @return Xml document
162
public Element toXml( Document document )
164
Element multistatus = DomUtil.createElement( document, XML_MULTISTATUS, NAMESPACE );
165
for ( MultiStatusResponse response : responses.values() )
167
multistatus.appendChild( response.toXml( document ) );
169
if ( responseDescription != null )
172
DomUtil.createElement( document, XML_RESPONSEDESCRIPTION, NAMESPACE, responseDescription );
173
multistatus.appendChild( respDesc );
179
* Build a <code>MultiStatus</code> from the specified xml element.
181
* @param multistatusElement
182
* @return new <code>MultiStatus</code> instance.
183
* @throws IllegalArgumentException if the given document is <code>null</code>
184
* or does not provide the required element.
186
public static MultiStatus createFromXml( Element multistatusElement )
188
if ( !DomUtil.matches( multistatusElement, XML_MULTISTATUS, NAMESPACE ) )
190
throw new IllegalArgumentException( "DAV:multistatus element expected." );
193
MultiStatus multistatus = new MultiStatus();
195
ElementIterator it = DomUtil.getChildren( multistatusElement, XML_RESPONSE, NAMESPACE );
196
while ( it.hasNext() )
198
Element respElem = it.nextElement();
199
MultiStatusResponse response = MultiStatusResponse.createFromXml( respElem );
200
multistatus.addResponse( response );
203
// optional response description on the multistatus element
204
multistatus.setResponseDescription( DomUtil.getChildText( multistatusElement, XML_RESPONSEDESCRIPTION,