KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /home/cvs/jakarta-slide/webdavclient/clientlib/src/java/org/apache/webdav/lib/methods/AclMethod.java,v 1.8 2004/08/02 15:45:48 unico Exp $
3  * $Revision: 1.8 $
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.Enumeration JavaDoc;
28 import java.util.Vector JavaDoc;
29 import org.apache.commons.httpclient.HttpConnection;
30 import org.apache.commons.httpclient.HttpException;
31 import org.apache.commons.httpclient.HttpState;
32 import org.apache.webdav.lib.Ace;
33 import org.apache.webdav.lib.Privilege;
34 import org.apache.webdav.lib.PropertyName;
35 import org.apache.webdav.lib.util.XMLPrinter;
36
37 /**
38  * ACL Method.
39  *
40  */

41 public class AclMethod
42     extends XMLResponseMethodBase {
43
44
45     // -------------------------------------------------------------- Constants
46

47
48     // ----------------------------------------------------------- Constructors
49

50
51     /**
52      * Method constructor.
53      */

54     public AclMethod() {
55     }
56
57
58     /**
59      * Method constructor.
60      */

61     public AclMethod(String JavaDoc path) {
62         super(path);
63     }
64
65
66     // ----------------------------------------------------- Instance Variables
67

68
69     protected Vector JavaDoc aces = new Vector JavaDoc();
70
71
72     // --------------------------------------------------------- Public Methods
73

74
75     /**
76      * Add an ace to the ace list which will be set by the method.
77      *
78      * @param ace Access control entry
79      */

80     public void addAce(Ace ace) {
81         checkNotUsed();
82         aces.addElement(ace);
83     }
84
85
86     // --------------------------------------------------- WebdavMethod Methods
87

88
89     public void recycle() {
90         super.recycle();
91         aces.clear();
92     }
93
94
95     /**
96      * Generate additional headers needed by the request.
97      *
98      * @param state State token
99      * @param conn the connection
100      */

101     public void addRequestHeaders(HttpState state, HttpConnection conn)
102     throws IOException JavaDoc, HttpException {
103
104         // set the default utf-8 encoding, if not already present
105
if (getRequestHeader("Content-Type") == null ) super.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
106         super.addRequestHeaders(state, conn);
107
108     }
109
110     /**
111      * DAV requests that contain a body must override this function to
112      * generate that body.
113      *
114      * <p>The default behavior simply returns an empty body.</p>
115      */

116     protected String JavaDoc generateRequestBody() {
117
118         XMLPrinter printer = new XMLPrinter();
119
120         printer.writeXMLHeader();
121         printer.writeElement("D", "DAV:", "acl",
122                              XMLPrinter.OPENING);
123
124         Enumeration JavaDoc aceList = aces.elements();
125
126         while (aceList.hasMoreElements()) {
127             Ace ace = (Ace) aceList.nextElement();
128
129             if (ace.isInherited() || ace.isProtected()) {
130                 // draft-ietf-webdav-acl-06
131
// do not submit inherited and protected aces
132
// continue with next ace
133
continue;
134             }
135
136             printer.writeElement("D", null, "ace",
137                                  XMLPrinter.OPENING);
138
139             printer.writeElement("D", null, "principal",
140                                  XMLPrinter.OPENING);
141
142             boolean found = false;
143             String JavaDoc principal = ace.getPrincipal();
144             String JavaDoc[] types = {"all", "authenticated", "unauthenticated",
145                               "property", "self"};
146             for (int i = 0; i < types.length && !found; i++) {
147                 if (types[i].equals(principal)) {
148                     found = true;
149                     if ("property".equals(principal)) {
150                         printer.writeElement("D", null, principal,
151                                              XMLPrinter.OPENING);
152                         PropertyName property = ace.getProperty();
153                         String JavaDoc nsURI = property.getNamespaceURI();
154                         if ("DAV:".equals(nsURI)) {
155                             printer.writeElement("D", null,
156                                                  property.getLocalName(),
157                                                  XMLPrinter.NO_CONTENT);
158                         } else {
159                             printer.writeElement("Z", nsURI,
160                                                  property.getLocalName(),
161                                                  XMLPrinter.NO_CONTENT);
162                         }
163                         printer.writeElement("D", null, principal,
164                                              XMLPrinter.CLOSING);
165                     } else {
166                         printer.writeElement("D", null, principal,
167                                              XMLPrinter.NO_CONTENT);
168                     }
169                 }
170             }
171             if (!found) {
172                 printer.writeElement("D", null, "href", XMLPrinter.OPENING);
173                 printer.writeText(principal);
174                 printer.writeElement("D", null, "href", XMLPrinter.CLOSING);
175             }
176
177             printer.writeElement("D", null, "principal",
178                                  XMLPrinter.CLOSING);
179
180             String JavaDoc positive = (ace.isNegative()) ? "deny" : "grant";
181
182             printer.writeElement("D", null, positive,
183                                  XMLPrinter.OPENING);
184
185             Enumeration JavaDoc privilegeList = ace.enumeratePrivileges();
186             while (privilegeList.hasMoreElements()) {
187                 Privilege privilege = (Privilege) privilegeList.nextElement();
188                 printer.writeElement("D", null, "privilege",
189                                      XMLPrinter.OPENING);
190                 String JavaDoc nsURI = privilege.getNamespace();
191                 if ("DAV:".equals(nsURI)) {
192                     printer.writeElement("D", null, privilege.getName(),
193                                          XMLPrinter.NO_CONTENT);
194                 } else {
195                     printer.writeElement("Z", nsURI, privilege.getName(),
196                                          XMLPrinter.NO_CONTENT);
197                 }
198                 printer.writeElement("D", null, "privilege",
199                                      XMLPrinter.CLOSING);
200             }
201
202             printer.writeElement("D", null, positive,
203                                  XMLPrinter.CLOSING);
204
205             printer.writeElement("D", null, "ace",
206                                  XMLPrinter.CLOSING);
207
208         }
209
210         printer.writeElement("D", "acl", XMLPrinter.CLOSING);
211
212         return printer.toString();
213     }
214
215     public String JavaDoc getName() {
216         return "ACL";
217     }
218 }
219
Popular Tags