KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /home/cvs/jakarta-slide/webdavclient/clientlib/src/java/org/apache/webdav/lib/methods/MoveMethod.java,v 1.6 2004/07/28 09:30:40 ib Exp $
3  * $Revision: 1.6 $
4  * $Date: 2004/07/28 09:30:40 $
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.commons.httpclient.protocol.Protocol;
31
32
33 /**
34  * MOVE Method.
35  *
36  */

37 public class MoveMethod
38     extends XMLResponseMethodBase {
39
40
41     // ----------------------------------------------------------- Constructors
42

43
44     /**
45      * Method constructor.
46      */

47     public MoveMethod() {
48     }
49
50
51     /**
52      * Method constructor.
53      */

54     public MoveMethod(String JavaDoc source) {
55         super(source);
56     }
57
58
59     /**
60      * Method constructor.
61      */

62     public MoveMethod(String JavaDoc source, String JavaDoc destination) {
63         this(source);
64         setDestination(destination);
65     }
66
67
68     /**
69      * Method constructor.
70      */

71     public MoveMethod(String JavaDoc source, String JavaDoc destination, boolean overwrite) {
72         this(source, destination);
73         setOverwrite(overwrite);
74     }
75
76
77     // ----------------------------------------------------- Instance Variables
78

79
80     /**
81      * Destination.
82      */

83     private String JavaDoc destination;
84
85
86     /**
87      * Overwrite.
88      */

89     private boolean overwrite = true;
90
91
92     // ------------------------------------------------------------- Properties
93

94
95
96     /**
97      * Set a header value, redirecting the special case of the Overwrite and Destination
98      * headers to {@link #setOverwrite} and {@link #setDestination} as appropriate.
99      *
100      * @param headerName Header name
101      * @param headerValue Header value
102      */

103     public void setRequestHeader(String JavaDoc headerName, String JavaDoc headerValue) {
104         if (headerName.equalsIgnoreCase("Overwrite")){
105             setOverwrite(! (headerValue.equalsIgnoreCase("F") ||
106                            headerValue.equalsIgnoreCase("False") ) );
107         }
108         else if(headerName.equalsIgnoreCase("Destination")){
109             setDestination(headerValue);
110         }
111         else{
112             super.setRequestHeader(headerName, headerValue);
113         }
114     }
115
116
117
118     /**
119      * Destination setter.
120      *
121      * @param destination New destination value
122      */

123     public void setDestination(String JavaDoc destination) {
124         checkNotUsed();
125         this.destination = destination;
126     }
127
128
129     /**
130      * Destination getter.
131      *
132      * @return String destination value
133      */

134     public String JavaDoc getDestination() {
135         return destination;
136     }
137
138
139     /**
140      * Overwrite setter.
141      *
142      * @param overwrite New overwrite value
143      */

144     public void setOverwrite(boolean overwrite) {
145         checkNotUsed();
146         this.overwrite = overwrite;
147     }
148
149
150     /**
151      * Overwrite getter.
152      *
153      * @return boolean Overwrite value
154      */

155     public boolean isOverwrite() {
156         return overwrite;
157     }
158
159
160     /**
161      * Overwrite getter.
162      *
163      * @return boolean Overwrite value
164      */

165     public boolean getOverwrite() {
166         return overwrite;
167     }
168
169
170     // --------------------------------------------------- WebdavMethod Methods
171

172
173     public String JavaDoc getName() {
174         return "MOVE";
175     }
176
177     /**
178      * Generate additional headers needed by the request.
179      *
180      * @param state State token
181      * @param conn The connection being used to make the request.
182      */

183     public void addRequestHeaders(HttpState state, HttpConnection conn)
184     throws IOException JavaDoc, HttpException {
185
186         super.addRequestHeaders(state, conn);
187
188         String JavaDoc absoluteDestination = getAbsoluteDestination(conn, destination);
189         super.setRequestHeader("Destination", absoluteDestination);
190
191         if (!isOverwrite())
192             super.setRequestHeader("Overwrite", "F");
193
194     }
195
196     /**
197      * A client of the {@link MoveMethod} can specify a destination as either an
198      * absolute URL (possibly to a different server), or as a absolute path on
199      * the same server, but this function makes sure that the path sent to the
200      * server is always an absolute URL.
201      *
202      * <p>Note that this function will add server and port to the request -
203      * however, port is not added if it is the default port for the scheme
204      * in question. </p>
205      *
206      * <p>This function is static so that it can be reused by the {@link CopyMethod}.
207      * </p>
208      *
209      * @param conn The connection for the current request, in case the caller
210      * specifies an absolute path.
211      *
212      * @param absolutePathOrURL If an absolute URL, nothing done, but if an absolute
213      * path, it is converted into an absolute URL.
214      *
215      * @return An absolute URL
216      */

217     static String JavaDoc getAbsoluteDestination(HttpConnection conn, String JavaDoc absolutePathOrURL) {
218
219         String JavaDoc absoluteDestination = absolutePathOrURL;
220
221         // is this an absolute path?
222
if (absolutePathOrURL.startsWith("/")) {
223
224             // yes - get the protocol to start the URL with the appropriate scheme.
225
Protocol protocol = conn.getProtocol();
226             StringBuffer JavaDoc bufDest = new StringBuffer JavaDoc(protocol.getScheme());
227             bufDest.append("://").append(conn.getHost());
228
229             // only add in the port if it is not the default port.
230
if (conn.getPort() != protocol.getDefaultPort()) {
231                 bufDest.append(':').append(conn.getPort());
232             }
233
234             // append the path.
235
bufDest.append(absolutePathOrURL);
236             absoluteDestination = bufDest.toString();
237         }
238         return absoluteDestination;
239     }
240
241
242 }
243
244
Popular Tags