KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /home/cvs/jakarta-slide/webdavclient/clientlib/src/java/org/apache/webdav/lib/methods/RebindMethod.java,v 1.5 2004/08/02 15:45:47 unico Exp $
3  * $Revision: 1.5 $
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 org.apache.commons.httpclient.HttpConnection;
28 import org.apache.commons.httpclient.HttpException;
29 import org.apache.commons.httpclient.HttpState;
30 import org.apache.webdav.lib.util.XMLPrinter;
31
32 /**
33  * The REBIND method removes a binding to a resource from one collection,
34  * and adds a binding to that resource into another collection. It is
35  * effectively an atomic form of a MOVE request.
36  *
37  * REBIND Method Example:
38  * >> Request:
39  * REBIND /CollX HTTP/1.1
40  * Host: www.example.com
41  * Content-Type: text/xml; charset="utf-8"
42  * Content-Length: xxx
43  * <?xml version="1.0" encoding="utf-8" ?>
44  * <D:rebind xmlns:D="DAV:">
45  * <D:segment>foo.html</D:segment>
46  * <D:href>http://www.example.com/CollY/bar.html</D:href>
47  * </D:rebind>
48  *
49  * >> Response:
50  * HTTP/1.1 200 OK
51  * The server added a new binding to the collection,
52  * "http://www.example.com/CollX", associating "foo.html" with the resource
53  * identified by the URI "http://www.example.com/CollY/bar.html",
54  * and removes the binding named "bar.html" from the collection identified
55  * by the URI "http://www.example.com/CollY".
56  * Clients can now use the URI "http://www.example.com/CollX/foo.html" to
57  * submit requests to that resource, and requests on the URI
58  * "http://www.example.com/CollY/bar.html" will fail with a 404 (Not Found)
59  * response.
60  *
61  */

62 public class RebindMethod
63     extends XMLResponseMethodBase {
64
65
66     public static final String JavaDoc NAME = "REBIND";
67
68     private boolean overwrite = true;
69     private String JavaDoc segment = null;
70     private String JavaDoc href = null;
71
72     // ----------------------------------------------------------- Constructors
73

74
75     /**
76      * Method constructor.
77      */

78     public RebindMethod() {
79     }
80
81     public RebindMethod(String JavaDoc existingBinding, String JavaDoc newBinding) {
82         super(newBinding.substring(0, newBinding.lastIndexOf('/')));
83         this.href = existingBinding;
84         this.segment = newBinding.substring(newBinding.lastIndexOf('/') + 1);
85     }
86
87     public String JavaDoc getName() {
88         return NAME;
89     }
90
91     /**
92      * By default, if there already is a binding for the specified segment
93      * in the collection, the new binding replaces the existing binding.
94      * This default binding replacement behavior can be overridden using
95      * the Overwrite header.
96      *
97      * @return the current value of the overwrite flag
98      */

99     public boolean isOverwrite() {
100         return overwrite;
101     }
102
103
104     /**
105      * By default, if there already is a binding for the specified segment
106      * in the collection, the new binding replaces the existing binding.
107      * This default binding replacement behavior can be overridden using
108      * the Overwrite header.
109      *
110      * @param overwrite New overwrite value
111      */

112     public void setOverwrite(boolean overwrite) {
113         checkNotUsed();
114         this.overwrite = overwrite;
115     }
116
117
118     /**
119      * Generate additional headers needed by the request.
120      *
121      * @param state HttpState token
122      * @param conn The connection being used for the request.
123      */

124     public void addRequestHeaders(HttpState state, HttpConnection conn)
125         throws IOException JavaDoc, HttpException {
126
127         super.addRequestHeaders(state, conn);
128
129         if (!isOverwrite())
130             super.setRequestHeader("Overwrite", "F");
131
132     }
133
134     /**
135      * DAV requests that contain a body must override this function to
136      * generate that body.
137      *
138      * <p>The default behavior simply returns an empty body.</p>
139      */

140     protected String JavaDoc generateRequestBody() {
141
142         if (segment == null || href == null)
143             throw new IllegalStateException JavaDoc
144                 ("Segment and Href must be set before " +
145                  "calling this function.");
146
147         XMLPrinter printer = new XMLPrinter();
148
149         printer.writeXMLHeader();
150         printer.writeElement("D", "DAV:", "rebind", XMLPrinter.OPENING);
151         printer.writeElement("D", "segment", XMLPrinter.OPENING);
152         printer.writeText(segment);
153         printer.writeElement("D", "segment", XMLPrinter.CLOSING);
154         printer.writeElement("D", "href", XMLPrinter.OPENING);
155         printer.writeText(href);
156         printer.writeElement("D", "href", XMLPrinter.CLOSING);
157         printer.writeElement("D", "rebind", XMLPrinter.CLOSING);
158
159         return printer.toString();
160     }
161
162     /**
163      * @return path of the resource to be rebound
164      */

165     public String JavaDoc getHref() {
166         return href;
167     }
168
169     /**
170      * @return new resource name
171      */

172     public String JavaDoc getSegment() {
173         return segment;
174     }
175
176     /**
177      * @param href path of the resource to be rebound
178      */

179     public void setHref(String JavaDoc href) {
180         this.href = href;
181     }
182
183     /**
184      * @param segment new resource name
185      */

186     public void setSegment(String JavaDoc segment) {
187         this.segment = segment;
188     }
189
190 }
191
192
Popular Tags