KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > webdav > lib > methods > AclReportMethod


1 /*
2  * $Header: /home/cvs/jakarta-slide/webdavclient/clientlib/src/java/org/apache/webdav/lib/methods/AclReportMethod.java,v 1.6 2004/08/02 15:45:48 unico Exp $
3  * $Revision: 1.6 $
4  * $Date: 2004/08/02 15:45:48 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.webdav.lib.methods;
25
26 import java.io.IOException JavaDoc;
27 import java.util.Collection JavaDoc;
28 import org.apache.commons.httpclient.HttpConnection;
29 import org.apache.commons.httpclient.HttpException;
30 import org.apache.commons.httpclient.HttpState;
31 import org.apache.webdav.lib.util.XMLPrinter;
32
33 /**
34  * WebDAV Report method
35  * This class is used to send an report
36  * from the ACL specification.
37  * In this version only the principal-property-search is supported.
38  *
39  */

40 public class AclReportMethod extends XMLResponseMethodBase implements DepthSupport {
41
42     //supported method types
43
public final static int PRINCIPAL_PROPERTY_SEARCH = 1;
44
45     private PropertyBodyHelper propertyBodyHelper = null;
46     private int depth = DepthSupport.DEPTH_INFINITY;
47     private int reportType = 0;
48
49     /**
50      * @param path
51      * @param propertyNames requested properties
52      * @param depth
53      * @param reportType - one of the supported report types
54      */

55     public AclReportMethod(
56         String JavaDoc path,
57         Collection JavaDoc propertyNames,
58         int depth,
59         int reportType) {
60
61         super(path);
62         setDepth(depth);
63         this.reportType = reportType;
64         propertyBodyHelper = new PropertyBodyHelper();
65         propertyBodyHelper.setPropertyNames(propertyNames);
66     }
67
68     /**
69      * @see org.apache.commons.httpclient.HttpMethod#getName()
70      */

71     public String JavaDoc getName() {
72         return "REPORT";
73     }
74
75     /**
76      * @see org.apache.webdav.lib.methods.DepthSupport#setDepth(int)
77      */

78     public void setDepth(int depth) {
79         checkNotUsed();
80         this.depth = depth;
81     }
82
83     /**
84      * @see org.apache.webdav.lib.methods.DepthSupport#getDepth()
85      */

86     public int getDepth() {
87         return depth;
88     }
89
90     /**
91      * Set a header value, redirecting the special case of header "Depth" to
92      * {@link #setDepth} as appropriate.
93      *
94      * @param headerName Header name
95      * @param headerValue Header value
96      */

97     public void setRequestHeader(String JavaDoc headerName, String JavaDoc headerValue) {
98         if (headerName.equalsIgnoreCase("Depth")) {
99             int depth = -1;
100             if (headerValue.equals("0")) {
101                 depth = DEPTH_0;
102             }
103             if (headerValue.equals("1")) {
104                 depth = DEPTH_1;
105             } else if (headerValue.equalsIgnoreCase("infinity")) {
106                 depth = DEPTH_INFINITY;
107             }
108             setDepth(depth);
109         } else {
110             super.setRequestHeader(headerName, headerValue);
111         }
112     }
113
114     /**
115      * Generate additional headers needed by the request.
116      *
117      * @param state State token
118      * @param conn The connection being used for the request.
119      */

120     public void addRequestHeaders(HttpState state, HttpConnection conn)
121         throws IOException JavaDoc, HttpException {
122
123         super.addRequestHeaders(state, conn);
124
125         // set the default utf-8 encoding, if not already present
126
if (getRequestHeader("Content-Type") == null ) super.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
127
128         switch (getDepth()) {
129             case DEPTH_0 :
130                 super.setRequestHeader("Depth", "0");
131                 break;
132             case DEPTH_1 :
133                 super.setRequestHeader("Depth", "1");
134                 break;
135             case DEPTH_INFINITY :
136                 super.setRequestHeader("Depth", "infinity");
137                 break;
138         }
139     }
140
141     /**
142      * @see org.apache.webdav.lib.methods.XMLResponseMethodBase#generateRequestBody()
143      */

144     protected String JavaDoc generateRequestBody() {
145         XMLPrinter printer = new XMLPrinter();
146
147         printer.writeXMLHeader();
148
149         switch (reportType) {
150             case PRINCIPAL_PROPERTY_SEARCH :
151                 generatePrincipalPropertySearchBody(printer);
152                 break;
153             default :
154                 System.err.println("AclReportMethod: type not supported " + reportType);
155         }
156         return printer.toString();
157     }
158
159     /**
160      * Generate the body for a principal-property-search report.
161      * Currently the <property-search> section is constant.
162      *
163      */

164     private void generatePrincipalPropertySearchBody(XMLPrinter printer) {
165         printer.writeElement(
166             "D",
167             "DAV:",
168             "principal-property-search",
169             XMLPrinter.OPENING);
170
171         //property-search
172
printer.writeElement("D", "property-search", XMLPrinter.OPENING);
173         printer.writeElement("D", "prop", XMLPrinter.OPENING);
174         printer.writeElement("D", "displayname", XMLPrinter.NO_CONTENT);
175         printer.writeElement("D", "prop", XMLPrinter.CLOSING);
176         printer.writeElement("D", "caseless-substring", XMLPrinter.NO_CONTENT);
177         printer.writeElement("D", "property-search", XMLPrinter.CLOSING);
178
179         //resulting properties
180
propertyBodyHelper.wirtePropElement(printer);
181
182         printer.writeElement("D", "principal-property-search", XMLPrinter.CLOSING);
183     }
184
185 }
186
Popular Tags