Creating Custom Method Handler

Custom method handler is required if you would like to replace original HTTP verb processing performed by WebDAV framework with your own processing or would like to process verbs that are not regulated by WebDAV standard.

Usually, you will create a custom method handler for GET verb issued over WebDAV folder.

If user types WebDAV server URL in a browser, the browser will submit GET request to a WebDAV folder. The WebDAV Engine will respond with 405, "Method Not Allowed" and user will see a blank page (in Firefox and Opera) or a "Method Not Allowed" page in Internet Explorer. To present any meaningful HTML page you have to create the page in GET handler:

MyWebDavEngine engine = new MyWebDavEngine();
CustomFolderGetHandler handler = new CustomFolderGetHandler();
handler.setPreviousHandler(engine.registerMethodHandler("GET", handler));
engine.service(httpServletRequest, httpServletResponse);

...

public class CustomFolderGetHandler implements MethodHandler {
    private MethodHandler previousHandler;

    public void processRequest(HttpServletRequest request, HttpServletResponse response, HierarchyItem item)
            throws DavException, IOException {

        if (item instanceof Folder) {
            response.getWriter().write("<HTML><BODY>");
            for (HierarchyItem child : ((Folder) item).getChildren())
                response.getWriter().format("<a href='%1s'>%2s</a><br/>", child.getPath(), child.getName());
            response.getWriter().write("</BODY></HTML>");
            response.getWriter().flush();
        } else {
            previousHandler.processRequest(request, response, item);
        }
    }

    public void setPreviousHandler(MethodHandler methodHandler) {
        previousHandler = methodHandler;
    }
}

To add or replace handler call Engine.registerMethodHandler method passing method name and object instance implementing MethodHandler interface prior to calling Engine.service method. The original handler, if any, is returned from registerMethodHandler method.

The MethodHandler.processRequest method is called by the engine during Engine.service call. The hierarchy item returned from Engine.getHierarchyItem is passed to processRequest method as a parameter.