KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /home/cvs/jakarta-slide/webdavclient/clientlib/src/java/org/apache/webdav/lib/methods/LabelMethod.java,v 1.6 2004/08/02 15:45:47 unico Exp $
3  * $Revision: 1.6 $
4  * $Date: 2004/08/02 15:45:47 $
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.io.InputStream 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.WebdavStatus;
32 import org.apache.webdav.lib.util.XMLPrinter;
33
34
35 /**
36  * The Label method is used to manipulate labels on resources on the server.
37  *
38  * <h3>Example Request</h3>
39  * <pre>
40  * LABEL /files/testfile.xml HTTP/1.1
41  * Host: www.webdav.org
42  * Content-Type: text/xml; charset="utf-8"
43  *
44  * <?xml version="1.0" encoding="utf-8"?>
45  * <D:label xmlns:D="DAV:">
46  * <D:set>
47  * <D:label-name>newlabel</D:label-name>
48  * </D:set>
49  * </D:label>
50  * </pre>
51  *
52  * <h3>Example Response</h3>
53  * <pre>
54  * HTTP/1.1 200 OK
55  * Cache-Control: no-cache
56  * </pre>
57  *
58  */

59 public class LabelMethod extends XMLResponseMethodBase {
60
61     // ----------------------------------------------------- Instance Variables
62

63     /**
64      * The constant for setting a label.
65      */

66     public static final int LABEL_SET = 1;
67
68
69     /**
70      * The constant for adding a label.
71      */

72     public static final int LABEL_ADD = 2;
73
74
75     /**
76      * The constant for removing a label.
77      */

78     public static final int LABEL_REMOVE = 3;
79
80
81     /**
82      * The label name.
83      */

84     private String JavaDoc labelName = null;
85
86
87     /**
88      * The lable type.
89      */

90     private int type = 0;
91
92     // ----------------------------------------------------------- Constructors
93

94     /**
95      * The default constructor.
96      */

97     public LabelMethod() {
98     }
99
100
101     /**
102      * The label method constructor.
103      *
104      * @param path the path
105      * @param action the action
106      * @param labelName the label name
107      */

108     public LabelMethod(String JavaDoc path, int action, String JavaDoc labelName) {
109         super(path);
110         this.labelName = labelName;
111         this.type = action;
112     }
113
114
115     /**
116      * Set the type of label action to take.
117      *
118      * @param type the type of the label action
119      */

120     public void setType(int type) {
121         this.type = type;
122     }
123
124
125     /**
126      * Get the label type which has been set.
127      *
128      * @return the type
129      */

130     public int getType() {
131         return type;
132     }
133
134
135     /**
136      * Set the label-name this action will manipulate.
137      *
138      * @param labelName the label name
139      */

140     public void setLabelName(String JavaDoc labelName) {
141         this.labelName = labelName;
142     }
143
144
145     /**
146      * Get the label-name this action will manipulate.
147      *
148      * @return the label-name
149      */

150     public String JavaDoc getLabelName() {
151         return labelName;
152     }
153
154
155     /**
156      * Generate the protocol headers.
157      *
158      * @param state the state
159      * @param conn the connection
160      */

161     public void addRequestHeaders(HttpState state, HttpConnection conn)
162     throws IOException JavaDoc, HttpException {
163
164         // set the default utf-8 encoding, if not already present
165
if (getRequestHeader("Content-Type") == null ) super.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
166         super.addRequestHeaders(state, conn);
167     }
168
169     /**
170      * DAV requests that contain a body must override this function to
171      * generate that body.
172      *
173      * <p>The default behavior simply returns an empty body.</p>
174      */

175     protected String JavaDoc generateRequestBody() {
176
177         if (type <= 0 || labelName == null)
178             throw new IllegalStateException JavaDoc
179                 ("Action type and label name must be set before " +
180                  "calling this function");
181
182         XMLPrinter printer = new XMLPrinter();
183  
184         printer.writeXMLHeader();
185         printer.writeElement("D", "DAV:", "label", XMLPrinter.OPENING);
186
187         String JavaDoc typeElement = null;
188         switch (type) {
189             case LABEL_SET:
190                 typeElement = "set";
191                 break;
192             case LABEL_REMOVE:
193                 typeElement = "remove";
194                 break;
195             case LABEL_ADD:
196                 typeElement = "add";
197                 break;
198         }
199
200         printer.writeElement("D", typeElement, XMLPrinter.OPENING);
201         printer.writeElement("D", "label-name", XMLPrinter.OPENING);
202         printer.writeText(labelName);
203         printer.writeElement("D", "label-name", XMLPrinter.CLOSING);
204         printer.writeElement("D", typeElement, XMLPrinter.CLOSING);
205         printer.writeElement("D", "label", XMLPrinter.CLOSING);
206
207         return printer.toString();
208     }
209
210     /**
211      * Parse response.
212      *
213      * @param input Input stream
214      */

215     public void parseResponse(InputStream JavaDoc input, HttpState state, HttpConnection conn)
216         throws IOException JavaDoc, HttpException {
217         try
218         {
219             int code = getStatusLine().getStatusCode();
220             if (code == WebdavStatus.SC_CONFLICT ||
221                 code == WebdavStatus.SC_MULTI_STATUS ||
222                 code == WebdavStatus.SC_FORBIDDEN ) {
223                 parseXMLResponse(input);
224 }
225         }
226         catch (IOException JavaDoc e) {
227                 // FIX ME: provide a way to deliver non xml data
228
}
229     }
230
231     public String JavaDoc getName() {
232         return "LABEL";
233     }
234 }
235
236
237
Popular Tags