1 17 package org.alfresco.repo.webdav; 18 19 import java.io.IOException ; 20 import java.util.Hashtable ; 21 import java.util.List ; 22 23 import javax.servlet.ServletConfig ; 24 import javax.servlet.ServletException ; 25 import javax.servlet.http.HttpServlet ; 26 import javax.servlet.http.HttpServletRequest ; 27 import javax.servlet.http.HttpServletResponse ; 28 import javax.transaction.UserTransaction ; 29 30 import org.alfresco.filesys.server.config.ServerConfiguration; 31 import org.alfresco.repo.security.authentication.AuthenticationComponent; 32 import org.alfresco.repo.transaction.TransactionUtil; 33 import org.alfresco.repo.transaction.TransactionUtil.TransactionWork; 34 import org.alfresco.service.ServiceRegistry; 35 import org.alfresco.service.cmr.repository.NodeRef; 36 import org.alfresco.service.cmr.repository.NodeService; 37 import org.alfresco.service.cmr.repository.StoreRef; 38 import org.alfresco.service.cmr.search.SearchService; 39 import org.alfresco.service.cmr.security.AuthenticationService; 40 import org.alfresco.service.namespace.NamespaceService; 41 import org.alfresco.service.transaction.TransactionService; 42 import org.apache.commons.logging.Log; 43 import org.apache.commons.logging.LogFactory; 44 import org.springframework.web.context.WebApplicationContext; 45 import org.springframework.web.context.support.WebApplicationContextUtils; 46 47 53 public class WebDAVServlet extends HttpServlet 54 { 55 56 private static final long serialVersionUID = 6900069445027527165L; 57 58 private static Log logger = LogFactory.getLog("org.alfresco.webdav.protocol"); 60 61 public static final String WEBDAV_PREFIX = "webdav"; 63 private static final String INTERNAL_SERVER_ERROR = "Internal Server Error: "; 64 65 public static final String KEY_STORE = "store"; 67 public static final String KEY_ROOT_PATH = "rootPath"; 68 69 private ServiceRegistry m_serviceRegistry; 71 72 private TransactionService m_transactionService; 74 75 private Hashtable <String ,Class > m_davMethods; 77 78 private NodeRef m_rootNodeRef; 80 81 private WebDAVHelper m_davHelper; 83 84 88 protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException , 89 IOException 90 { 91 long startTime = 0; 92 if (logger.isDebugEnabled()) 93 { 94 startTime = System.currentTimeMillis(); 95 } 96 97 try 98 { 99 final WebDAVMethod method = createMethod(request, response); 101 102 if (method == null) 103 { 104 if ( logger.isErrorEnabled()) 105 logger.error("WebDAV method not implemented - " + request.getMethod()); 106 107 109 response.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED); 110 return; 111 } 112 else if (method.getRootNodeRef() == null) 113 { 114 if ( logger.isErrorEnabled()) 115 logger.error("No root node for request"); 116 117 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); 119 return; 120 } 121 122 TransactionWork<Object > methodWork = new TransactionWork<Object >() 124 { 125 public Object doWork() throws Exception 126 { 127 method.execute(); 128 return null; 129 } 130 }; 131 TransactionUtil.executeInUserTransaction(m_transactionService, methodWork); 132 } 133 catch (Throwable e) 134 { 135 if (!(e instanceof WebDAVServerException) && e.getCause() != null) 136 { 137 if (e.getCause() instanceof WebDAVServerException) 138 { 139 e = e.getCause(); 140 } 141 } 142 if (e instanceof WebDAVServerException) 144 { 145 WebDAVServerException error = (WebDAVServerException) e; 146 if (error.getCause() != null) 147 { 148 logger.error(INTERNAL_SERVER_ERROR, error.getCause()); 149 } 150 151 if (logger.isDebugEnabled()) 152 { 153 155 logger.debug(request.getMethod() + " is returning status code: " + error.getHttpStatusCode()); 156 } 157 158 if (response.isCommitted()) 159 { 160 logger.warn("Could not return the status code to the client as the response has already been committed!"); 161 } 162 else 163 { 164 response.sendError(error.getHttpStatusCode()); 165 } 166 } 167 else 168 { 169 logger.error(INTERNAL_SERVER_ERROR, e); 170 171 if (response.isCommitted()) 172 { 173 logger.warn("Could not return the internal server error code to the client as the response has already been committed!"); 174 } 175 else 176 { 177 response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); 178 } 179 } 180 } 181 finally 182 { 183 if (logger.isDebugEnabled()) 184 { 185 long endTime = System.currentTimeMillis(); 186 long duration = endTime - startTime; 187 logger.debug(request.getMethod() + " took " + duration + "ms to execute"); 188 } 189 } 190 } 191 192 199 private WebDAVMethod createMethod(HttpServletRequest request, HttpServletResponse response) 200 { 201 203 String strHttpMethod = request.getMethod(); 204 205 if (logger.isDebugEnabled()) 206 logger.debug("WebDAV request " + strHttpMethod + " on path " 207 + request.getRequestURI()); 208 209 Class methodClass = m_davMethods.get(strHttpMethod); 210 WebDAVMethod method = null; 211 212 if ( methodClass != null) 213 { 214 try 215 { 216 218 method = (WebDAVMethod) methodClass.newInstance(); 219 method.setDetails(request, response, m_davHelper, m_rootNodeRef); 220 } 221 catch (Exception ex) 222 { 223 225 if ( logger.isDebugEnabled()) 226 logger.debug(ex); 227 } 228 } 229 230 232 return method; 233 } 234 235 241 public void init(ServletConfig config) throws ServletException 242 { 243 super.init(config); 244 245 247 WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext()); 248 m_serviceRegistry = (ServiceRegistry)context.getBean(ServiceRegistry.SERVICE_REGISTRY); 249 250 m_transactionService = m_serviceRegistry.getTransactionService(); 251 252 AuthenticationService authService = (AuthenticationService) context.getBean("authenticationService"); 253 NodeService nodeService = (NodeService) context.getBean("NodeService"); 254 SearchService searchService = (SearchService) context.getBean("SearchService"); 255 NamespaceService namespaceService = (NamespaceService) context.getBean("NamespaceService"); 256 257 259 m_davHelper = new WebDAVHelper(m_serviceRegistry, authService); 260 261 263 ServerConfiguration fileSrvConfig = (ServerConfiguration) context.getBean(ServerConfiguration.SERVER_CONFIGURATION); 264 if ( fileSrvConfig == null) 265 throw new ServletException ("File server configuration not available"); 266 267 269 AuthenticationComponent authComponent = fileSrvConfig.getAuthenticationComponent(); 270 authComponent.setCurrentUser( authComponent.getSystemUserName()); 271 272 274 UserTransaction tx = m_transactionService.getUserTransaction(true); 275 276 try 277 { 278 280 if ( tx != null) 281 tx.begin(); 282 283 285 String storeValue = config.getInitParameter(KEY_STORE); 286 if (storeValue == null) 287 { 288 throw new ServletException ("Device missing init value: " + KEY_STORE); 289 } 290 StoreRef storeRef = new StoreRef(storeValue); 291 292 294 if (! nodeService.exists(storeRef)) 295 { 296 throw new ServletException ("Store not created prior to application startup: " + storeRef); 297 } 298 NodeRef storeRootNodeRef = nodeService.getRootNode(storeRef); 299 300 302 String rootPath = config.getInitParameter(KEY_ROOT_PATH); 303 if (rootPath == null) 304 { 305 throw new ServletException ("Device missing init value: " + KEY_ROOT_PATH); 306 } 307 308 310 List <NodeRef> nodeRefs = searchService.selectNodes(storeRootNodeRef, rootPath, null, namespaceService, false); 311 312 if (nodeRefs.size() > 1) 313 { 314 throw new ServletException ("Multiple possible roots for device: \n" + 315 " root path: " + rootPath + "\n" + 316 " results: " + nodeRefs); 317 } 318 else if (nodeRefs.size() == 0) 319 { 320 throw new ServletException ("No root found for device: \n" + 322 " root path: " + rootPath); 323 } 324 else 325 { 326 m_rootNodeRef = nodeRefs.get(0); 328 } 329 330 332 tx.commit(); 333 } 334 catch (Exception ex) 335 { 336 logger.error(ex); 337 } 338 finally 339 { 340 342 authComponent.clearCurrentSecurityContext(); 343 } 344 345 347 m_davMethods = new Hashtable <String ,Class >(); 348 349 m_davMethods.put(WebDAV.METHOD_PROPFIND, PropFindMethod.class); 350 m_davMethods.put(WebDAV.METHOD_COPY, CopyMethod.class); 351 m_davMethods.put(WebDAV.METHOD_DELETE, DeleteMethod.class); 352 m_davMethods.put(WebDAV.METHOD_GET, GetMethod.class); 353 m_davMethods.put(WebDAV.METHOD_HEAD, HeadMethod.class); 354 m_davMethods.put(WebDAV.METHOD_LOCK, LockMethod.class); 355 m_davMethods.put(WebDAV.METHOD_MKCOL, MkcolMethod.class); 356 m_davMethods.put(WebDAV.METHOD_MOVE, MoveMethod.class); 357 m_davMethods.put(WebDAV.METHOD_OPTIONS, OptionsMethod.class); 358 m_davMethods.put(WebDAV.METHOD_POST, PostMethod.class); 359 m_davMethods.put(WebDAV.METHOD_PUT, PutMethod.class); 360 m_davMethods.put(WebDAV.METHOD_UNLOCK, UnlockMethod.class); 361 } 362 } 363 | Popular Tags |