KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > webdav > UnlockMethod


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.webdav;
18
19 import javax.servlet.http.HttpServletResponse JavaDoc;
20
21 import org.alfresco.service.cmr.lock.LockService;
22 import org.alfresco.service.cmr.lock.LockStatus;
23 import org.alfresco.service.cmr.model.FileInfo;
24 import org.alfresco.service.cmr.model.FileNotFoundException;
25
26 /**
27  * Implements the WebDAV UNLOCK method
28  *
29  * @author gavinc
30  */

31 public class UnlockMethod extends WebDAVMethod
32 {
33     private String JavaDoc m_strLockToken = null;
34
35     /**
36      * Default constructor
37      */

38     public UnlockMethod()
39     {
40     }
41
42     /**
43      * Return the lock token of an existing lock
44      *
45      * @return String
46      */

47     protected final String JavaDoc getLockToken()
48     {
49         return m_strLockToken;
50     }
51
52     /**
53      * Parse the request headers
54      *
55      * @exception WebDAVServerException
56      */

57     protected void parseRequestHeaders() throws WebDAVServerException
58     {
59         // Get the lock token, if any
60
String JavaDoc strLockTokenHeader = m_request.getHeader(WebDAV.HEADER_LOCK_TOKEN);
61
62         // DEBUG
63
if (logger.isDebugEnabled())
64             logger.debug("Parsing Lock-Token header: " + strLockTokenHeader);
65
66         // Validate the lock token
67
if (strLockTokenHeader != null && strLockTokenHeader.startsWith("<") && strLockTokenHeader.endsWith(">"))
68         {
69             try
70             {
71                 m_strLockToken = strLockTokenHeader.substring(
72                         WebDAV.OPAQUE_LOCK_TOKEN.length() + 1,
73                         strLockTokenHeader.length() - 1);
74             }
75             catch (IndexOutOfBoundsException JavaDoc e)
76             {
77                 logger.warn("Failed to parse If header: " + strLockTokenHeader);
78             }
79         }
80
81         // If there is no token this is a bad request so send an error back
82
if (m_strLockToken == null)
83         {
84             throw new WebDAVServerException(HttpServletResponse.SC_BAD_REQUEST);
85         }
86     }
87
88     /**
89      * Parse the request body
90      *
91      * @exception WebDAVServerException
92      */

93     protected void parseRequestBody() throws WebDAVServerException
94     {
95         // Nothing to do in this method
96
}
97
98     /**
99      * Exceute the request
100      *
101      * @exception WebDAVServerException
102      */

103     protected void executeImpl() throws WebDAVServerException
104     {
105         if (logger.isDebugEnabled())
106         {
107             logger.debug("Lock node; path=" + getPath() + ", token=" + getLockToken());
108         }
109
110         FileInfo lockNodeInfo = null;
111         try
112         {
113             lockNodeInfo = getDAVHelper().getNodeForPath(getRootNodeRef(), getPath(), getServletPath());
114         }
115         catch (FileNotFoundException e)
116         {
117             throw new WebDAVServerException(HttpServletResponse.SC_NOT_FOUND);
118         }
119
120         // Parse the lock token
121
String JavaDoc[] lockInfo = WebDAV.parseLockToken(getLockToken());
122         if (lockInfo == null)
123         {
124             // Bad lock token
125
throw new WebDAVServerException(HttpServletResponse.SC_PRECONDITION_FAILED);
126         }
127
128         // Get the lock status for the node
129
LockService lockService = getDAVHelper().getLockService();
130         // String nodeId = lockInfo[0];
131
// String userName = lockInfo[1];
132

133         LockStatus lockSts = lockService.getLockStatus(lockNodeInfo.getNodeRef());
134         if (lockSts == LockStatus.LOCK_OWNER)
135         {
136             // Unlock the node
137
lockService.unlock(lockNodeInfo.getNodeRef());
138
139             // Indicate that the unlock was successful
140
m_response.setStatus(HttpServletResponse.SC_NO_CONTENT);
141
142             // DEBUG
143
if (logger.isDebugEnabled())
144             {
145                 logger.debug("Unlock token=" + getLockToken() + " Successful");
146             }
147         }
148         else if (lockSts == LockStatus.NO_LOCK)
149         {
150             // DEBUG
151
if (logger.isDebugEnabled())
152                 logger.debug("Unlock token=" + getLockToken() + " Not locked");
153
154             // Node is not locked
155
throw new WebDAVServerException(HttpServletResponse.SC_PRECONDITION_FAILED);
156         }
157         else if (lockSts == LockStatus.LOCKED)
158         {
159             // DEBUG
160
if (logger.isDebugEnabled())
161                 logger.debug("Unlock token=" + getLockToken() + " Not lock owner");
162
163             // Node is locked but not by this user
164
throw new WebDAVServerException(HttpServletResponse.SC_PRECONDITION_FAILED);
165         }
166         else if (lockSts == LockStatus.LOCK_EXPIRED)
167         {
168             // DEBUG
169
if (logger.isDebugEnabled())
170                 logger.debug("Unlock token=" + getLockToken() + " Lock expired");
171
172             // Return a success status
173
m_response.setStatus(HttpServletResponse.SC_NO_CONTENT);
174         }
175     }
176 }
177
Popular Tags