/**
   * Create a WebDAV method handler
   *
   * @param request HttpServletRequest
   * @param response HttpServletResponse
   * @return WebDAVMethod
   */
  protected WebDAVMethod createMethod(HttpServletRequest request, HttpServletResponse response) {
    // Get the type of the current request

    String strHttpMethod = request.getMethod();

    if (logger.isDebugEnabled())
      logger.debug("WebDAV request " + strHttpMethod + " on path " + request.getRequestURI());

    Class<? extends WebDAVMethod> methodClass = m_davMethods.get(strHttpMethod);
    WebDAVMethod method = null;

    if (methodClass != null) {
      try {
        // Create the handler method
        method = methodClass.newInstance();
        method.setDetails(request, response, m_davHelper, getRootNodeRef());

        // A very few WebDAV methods produce activity posts.
        if (method instanceof ActivityPostProducer) {
          ActivityPostProducer activityPostProducer = (ActivityPostProducer) method;
          activityPostProducer.setActivityPoster(activityPoster);
        }
      } catch (Exception ex) {
        // Debug

        if (logger.isDebugEnabled()) logger.debug(ex);
      }
    }

    // Return the WebDAV method handler, or null if not supported

    return method;
  }
  /**
   * @see javax.servlet.http.HttpServlet#service(javax.servlet.http.HttpServletRequest,
   *     javax.servlet.http.HttpServletResponse)
   */
  protected void service(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    if (!initParams.getEnabled()) {
      response.sendError(HttpServletResponse.SC_NOT_FOUND);
      return;
    }

    long startTime = 0;
    if (logger.isTraceEnabled()) {
      startTime = System.currentTimeMillis();
    }

    FileFilterMode.setClient(Client.webdav);

    try {
      // Create the appropriate WebDAV method for the request and execute it
      final WebDAVMethod method = createMethod(request, response);

      if (method == null) {
        if (logger.isErrorEnabled())
          logger.error("WebDAV method not implemented - " + request.getMethod());

        // Return an error status

        response.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED);
        return;
      } else if (method.getRootNodeRef() == null) {
        if (logger.isDebugEnabled()) {
          logger.debug(
              "No root node for request ["
                  + request.getMethod()
                  + " "
                  + request.getRequestURI()
                  + "]");
        }

        // Return an error status
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        return;
      }

      // Execute the WebDAV request, which must take care of its own transaction
      method.execute();
    } catch (Throwable e) {
      ExceptionHandler exHandler = new ExceptionHandler(e, request, response);
      exHandler.handle();
    } finally {
      if (logger.isTraceEnabled()) {
        logger.trace(
            request.getMethod()
                + " took "
                + (System.currentTimeMillis() - startTime)
                + "ms to execute ["
                + request.getRequestURI()
                + "]");
      }

      FileFilterMode.clearClient();
    }
  }