1 17 package org.alfresco.repo.webdav; 18 19 import java.util.List ; 20 21 import javax.servlet.http.HttpServletResponse ; 22 23 import org.alfresco.model.ContentModel; 24 import org.alfresco.service.cmr.lock.LockService; 25 import org.alfresco.service.cmr.lock.LockStatus; 26 import org.alfresco.service.cmr.lock.LockType; 27 import org.alfresco.service.cmr.model.FileFolderService; 28 import org.alfresco.service.cmr.model.FileInfo; 29 import org.alfresco.service.cmr.model.FileNotFoundException; 30 import org.alfresco.service.cmr.repository.NodeRef; 31 import org.dom4j.io.XMLWriter; 32 33 38 public class LockMethod extends WebDAVMethod 39 { 40 private String m_strLockToken = null; 41 private int m_timeoutDuration = WebDAV.DEPTH_INFINITY; 42 43 46 public LockMethod() 47 { 48 } 49 50 55 protected final boolean hasLockToken() 56 { 57 return m_strLockToken != null ? true : false; 58 } 59 60 65 protected final String getLockToken() 66 { 67 return m_strLockToken; 68 } 69 70 75 protected final int getLockTimeout() 76 { 77 return m_timeoutDuration; 78 } 79 80 85 protected void parseRequestHeaders() throws WebDAVServerException 86 { 87 89 m_strLockToken = parseIfHeader(); 90 91 93 String strTimeout = m_request.getHeader(WebDAV.HEADER_TIMEOUT); 94 95 98 if (strTimeout != null && strTimeout.startsWith(WebDAV.SECOND)) 99 { 100 try 101 { 102 105 int idx = strTimeout.indexOf(" "); 106 107 if (idx != -1) 108 { 109 111 strTimeout = strTimeout.substring(WebDAV.SECOND.length(), idx); 112 } 113 else 114 { 115 117 strTimeout = strTimeout.substring(WebDAV.SECOND.length()); 118 } 119 m_timeoutDuration = Integer.parseInt(strTimeout); 120 } 121 catch (Exception e) 122 { 123 126 logger.warn("Failed to parse Timeout header: " + strTimeout); 127 } 128 } 129 130 132 if (logger.isDebugEnabled()) 133 logger.debug("Lock lockToken=" + getLockToken() + ", timeout=" + getLockTimeout()); 134 } 135 136 141 protected void parseRequestBody() throws WebDAVServerException 142 { 143 } 147 148 153 protected void executeImpl() throws WebDAVServerException, Exception 154 { 155 FileFolderService fileFolderService = getFileFolderService(); 156 String path = getPath(); 157 NodeRef rootNodeRef = getRootNodeRef(); 158 String userName = getDAVHelper().getAuthenticationService().getCurrentUserName(); 160 161 if (logger.isDebugEnabled()) 162 { 163 logger.debug("Locking node: \n" + 164 " user: " + userName + "\n" + 165 " path: " + path); 166 } 167 168 FileInfo lockNodeInfo = null; 169 try 170 { 171 lockNodeInfo = getDAVHelper().getNodeForPath(getRootNodeRef(), getPath(), m_request.getServletPath()); 173 } 174 catch (FileNotFoundException e) 175 { 176 String [] splitPath = getDAVHelper().splitPath(path); 178 if (splitPath[1].length() == 0) 180 { 181 throw new WebDAVServerException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); 182 } 183 184 List <String > dirPathElements = getDAVHelper().splitAllPaths(splitPath[0]); 185 FileInfo dirInfo = fileFolderService.makeFolders(rootNodeRef, dirPathElements, ContentModel.TYPE_FOLDER); 186 if (dirInfo == null) 187 { 188 throw new WebDAVServerException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); 189 } 190 lockNodeInfo = fileFolderService.create(dirInfo.getNodeRef(), splitPath[1], ContentModel.TYPE_CONTENT); 192 193 if (logger.isDebugEnabled()) 194 { 195 logger.debug("Created new node for lock: \n" + 196 " path: " + path + "\n" + 197 " node: " + lockNodeInfo); 198 } 199 } 200 201 202 if (hasLockToken()) 204 { 205 refreshLock(lockNodeInfo.getNodeRef(), userName); 207 } 208 else 209 { 210 createLock(lockNodeInfo.getNodeRef(), userName); 212 } 213 214 generateResponse(lockNodeInfo.getNodeRef(), userName); 216 } 217 218 225 private final void createLock(NodeRef lockNode, String userName) throws WebDAVServerException 226 { 227 LockService lockService = getLockService(); 228 229 LockStatus lockSts = lockService.getLockStatus(lockNode); 231 232 if (logger.isDebugEnabled()) 234 logger.debug("Create lock status=" + lockSts); 235 236 if (lockSts == LockStatus.LOCKED || lockSts == LockStatus.LOCK_OWNER) 237 { 238 throw new WebDAVServerException(WebDAV.WEBDAV_SC_LOCKED); 240 } 241 242 lockService.lock(lockNode, LockType.WRITE_LOCK, getLockTimeout()); 244 } 245 246 253 private final void refreshLock(NodeRef lockNode, String userName) throws WebDAVServerException 254 { 255 LockService lockService = getLockService(); 256 257 LockStatus lockSts = lockService.getLockStatus(lockNode); 259 260 if (logger.isDebugEnabled()) 262 logger.debug("Refresh lock status=" + lockSts); 263 264 if (lockSts != LockStatus.LOCK_OWNER) 265 { 266 throw new WebDAVServerException(WebDAV.WEBDAV_SC_LOCKED); 268 } 269 270 lockService.lock(lockNode, LockType.WRITE_LOCK, getLockTimeout()); 272 } 273 274 277 private void generateResponse(NodeRef lockNode, String userName) throws Exception 278 { 279 XMLWriter xml = createXMLWriter(); 280 281 xml.startDocument(); 282 283 String nsdec = generateNamespaceDeclarations(null); 284 xml.startElement(WebDAV.DAV_NS, WebDAV.XML_MULTI_STATUS + nsdec, WebDAV.XML_NS_MULTI_STATUS + nsdec, 285 getDAVHelper().getNullAttributes()); 286 287 generateLockDiscoveryXML(xml, lockNode); 289 290 xml.endElement(WebDAV.DAV_NS, WebDAV.XML_MULTI_STATUS, WebDAV.XML_NS_MULTI_STATUS); 292 293 m_response.setStatus(HttpServletResponse.SC_OK); 295 xml.flush(); 296 } 297 } 298 | Popular Tags |