KickJava   Java API By Example, From Geeks To Geeks.

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


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

38 public class UnlockMethod
39     extends XMLResponseMethodBase {
40
41     public final static int NO_TRANSACTION = -1;
42     public final static int ABORT_TRANSACTION = 0;
43     public final static int COMMIT_TRANSACTION = 1;
44     
45     // ----------------------------------------------------- Instance Variables
46

47
48     private String JavaDoc lockToken = null;
49
50     private int transactionStatus = NO_TRANSACTION;
51
52     // ----------------------------------------------------------- Constructors
53

54     /**
55      * Creates an unlock method that <em>ends a transaction</em> when server supports
56      * them in a
57      * <a HREF="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wss/wss/_webdav_lock.asp">MS like style</a>.
58      * The transacion handle of transaction is stored as the lock token.
59      * <br><br>
60      * To start a transaction
61      * use {@link LockMethod}.
62
63      * @param path any path inside Slide's scope
64      * @param txHandle lock token specifying transaction handle
65      * @param transactionStatus status of transaction as described in {@link #setTransactionStatus(int)}
66      *
67      */

68     public UnlockMethod(String JavaDoc path, String JavaDoc txHandle, int transactionStatus) {
69         this(path);
70         setLockToken(txHandle);
71         setTransactionStatus(transactionStatus);
72     }
73     
74     /**
75      * Method constructor.
76      */

77     public UnlockMethod() {
78     }
79
80
81     /**
82      * Method constructor.
83      */

84     public UnlockMethod(String JavaDoc path) {
85         super(path);
86     }
87
88
89     /**
90      * Method constructor.
91      */

92     public UnlockMethod(String JavaDoc path, String JavaDoc lockToken) {
93         this(path);
94         setLockToken(lockToken);
95     }
96
97
98     // ------------------------------------------------------------- Properties
99

100
101     public void setLockToken(String JavaDoc lockToken) {
102         checkNotUsed();
103         this.lockToken = lockToken;
104     }
105
106     /**
107      * Gets the parameter described in {@link #setTransactionStatus(int)}.
108      *
109      * @return either {@link UnlockMethod#COMMIT_TRANSACTION} or {@link UnlockMethod#ABORT_TRANSACTION} as the real
110      * transaction status or {@link UnlockMethod#NO_TRANSACTION} to indicate this method is not used for
111      * transaction control
112      */

113     public int getTransactionStatus() {
114         return transactionStatus;
115     }
116
117     /**
118      * Sets the transaction status of this method when it is used to end a externally controlled
119      * transaction.
120      *
121      * @param transactionStatus {@link UnlockMethod#COMMIT_TRANSACTION} to set the status to successful commit or
122      * {@link UnlockMethod#ABORT_TRANSACTION} to let the transaction abort discarding all changes associated to it.
123      *
124      */

125     public void setTransactionStatus(int transactionStatus) {
126         this.transactionStatus = transactionStatus;
127     }
128
129     // --------------------------------------------------- WebdavMethod Methods
130

131     public String JavaDoc getName() {
132         return "UNLOCK";
133     }
134
135     public void recycle() {
136         this.transactionStatus = NO_TRANSACTION;
137     }
138
139     /**
140      * Set header, handling the special case of the lock-token header so
141      * that it calls {@link #setLockToken} instead.
142      *
143      * @param headerName Header name
144      * @param headerValue Header value
145      */

146     public void setRequestHeader(String JavaDoc headerName, String JavaDoc headerValue) {
147         if (headerName.equalsIgnoreCase("Lock-Token")){
148             setLockToken(headerValue);
149         }
150         else{
151             super.setRequestHeader(headerName, headerValue);
152         }
153     }
154
155     /**
156      * Generate additional headers needed by the request.
157      *
158      * @param state HttpState token
159      * @param conn The connection being used to send the request.
160      */

161     public void addRequestHeaders(HttpState state, HttpConnection conn)
162     throws IOException JavaDoc, HttpException {
163
164         super.addRequestHeaders(state, conn);
165
166         super.setRequestHeader("Lock-Token", "<" + lockToken + ">");
167
168     }
169
170     protected String JavaDoc generateRequestBody() {
171         if (getTransactionStatus() == NO_TRANSACTION) {
172             return "";
173         } else {
174             return "<D:transactioninfo xmlns:D='DAV:'>\n <D:transactionstatus>"
175                     + (getTransactionStatus() == ABORT_TRANSACTION ? "<D:abort/>" : "<D:commit/>")
176                     + "</D:transactionstatus>\n</D:transactioninfo>";
177         }
178     }
179     
180     protected void processResponseBody(HttpState state, HttpConnection conn) {
181         if ((getStatusLine().getStatusCode() == HttpStatus.SC_NO_CONTENT) &&
182             (state instanceof WebdavState)) {
183             ((WebdavState) state).removeLock(getPath(), lockToken);
184         }
185     }
186 }
187
Popular Tags