Fork me on GitHub

Example - WebFiles via WebDAV

Introduction

In this page, a scenario utilizing Jacktabbit Simple Webdav Server with Hippo CMS is explained as an example.

WebFiles in Hippo CMS uses the default JCR folders (nt:folder) and files (nt:file) to store the files. So, it is easily mapped to WebDAV representations as collections and files.

We will expose the WebFiles as WebDAV collection and show how to browse, download and upload files by using a WebDAV client tool below.

WebDAV Client Tools

There are many WebDAV client tools including populr web browsers (for browsing and reading at least), according to a Comparison of WebDAV software Wikipedia page. So, it is almost impossible to introduce all the available client tools here, but the following tools are found useful:

  • Cadaver: Command line tool, providing more than similar features to SFTP such as get, mget, put, mput, copy, move, delete, cat, edit, ...
  • CyberDuck: GUI tool for MacOS and Windows, providing a synchronization feature between local folder and WebDAV collection.
  • DaviX: Command line tool, used at CERN by several projects.
  • Eclipse supports WebDAV plugin

In this page, we are going to use Cadaver to demonstrate how to access WebFiles through WebDAV.

To install Cadaver,

Exposing WebFiles as WebDAV Collection

If you followed the Simple Webdav Server Installation step, then the /webfiles JCR node is already exposed through WebDAV path: /default/webfiles/. e.g. http://localhost:8080/cms/webdav/default/webfiles/.

Note that /cms is the context path of the web application where you've set up the Simple WebDAV Server, /webdav is the servlet mapping path you've configured in the web.xml, /default is the JCR workspace name and Hippo CMS uses the default workspace only, and finally /webfiles is the JCR node path you want to access.

However, for security reason, you might want to limit WebDAV access only to /webfiles node, not anywhere else such as /content or /hippo:configuration. To achieve that, the demo project simply leveraged the Tuckey UrlRewriteFilter. by setting UrlRewriteFilter servlet filter in /WEB-INF/web.xml with a custom /WEB-INF/urlrewrite.xml like the following examples:

 

/WEB-INF/web.xml:


  <!-- SNIP -->

  <!-- This UrlRewriteFilter is a demo only to control requests to /webdav/*. -->
  <filter>
    <filter-name>UrlRewriteFilter</filter-name>
    <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
  </filter>

  <!-- SNIP -->

  <!-- This UrlRewriteFilter is a demo only to control requests to /webdav/*. -->
  <filter-mapping>
    <filter-name>UrlRewriteFilter</filter-name>
    <url-pattern>/webdav/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
  </filter-mapping>

  <!-- SNIP -->

        

 

/WEB-INF/urlrewrite.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 4.0//EN" "http://www.tuckey.org/res/dtds/urlrewrite4.0.dtd">
<urlrewrite>

    <rule match-type="regex">
        <note>
            The rule means that requests to /webdav will be redirected to /webdav/default/webfiles/
            the url will be rewritten.
        </note>
        <from>^\/webdav$</from>
        <to type="redirect" last="true">webdav/default/webfiles/</to>
    </rule>

    <rule match-type="regex">
        <note>
            The rule means that requests to /webdav/ will be redirected to /webdav/default/webfiles/
            the url will be rewritten.
        </note>
        <from>^\/webdav\/([^\/]*)$</from>
        <to type="redirect" last="true">default/webfiles/$1</to>
    </rule>

    <rule match-type="regex">
        <note>
            The rule means that requests to /webdav/*, other than /webdav/default/webfiles/*, will be resulted in 403 error.
        </note>
        <from>^(?!\/webdav\/default\/webfiles\/?.*).*$</from>
        <set type="status">403</set>
        <to last="true">null</to>
    </rule>

</urlrewrite>
        

Access with WebDAV Client Tool

Here's cadaver command examples to download files, edit and upload files.

$ cadaver http://localhost:8080/cms/webdav/default/webfiles/
Authentication required for Hippo Repository WebDAV Access Support on server `localhost':
Username: admin
Password: 

dav:/cms/webdav/default/webfiles/> help
Available commands: 
 ls         cd         pwd        put        get        mget       mput       
 edit       less       mkcol      cat        delete     rmcol      copy       
 move       lock       unlock     discover   steal      showlocks  version    
 checkin    checkout   uncheckout history    label      propnames  chexec     
 propget    propdel    propset    search     set        open       close      
 echo       quit       unset      lcd        lls        lpwd       logout     
 help       describe   about      
Aliases: rm=delete, mkdir=mkcol, mv=move, cp=copy, more=less, quit=exit=bye

dav:/cms/webdav/default/webfiles/> ls
Listing collection `/cms/webdav/default/webfiles/': succeeded.
Coll:   site                                   0  Oct  5 11:48

dav:/cms/webdav/default/webfiles/> cd site/freemarker/hipporepojcrdavdemo
dav:/cms/webdav/default/webfiles/site/freemarker/hipporepojcrdavdemo/> ls
Listing collection `/cms/webdav/default/webfiles/site/freemarker/hipporepojcrdavdemo/': succeeded.
        base-footer.ftl                      244  Oct  5 10:24
        base-layout.ftl                     1012  Oct  5 10:24
        base-top-menu.ftl                   1194  Oct  5 10:24
        homepage-main.ftl                    516  Oct  5 10:24
        pagenotfound-main.ftl                299  Oct  5 10:24

dav:/cms/webdav/default/webfiles/site/freemarker/hipporepojcrdavdemo/> mget *.ftl
[Matching... 5 matches.]
Downloading `/cms/webdav/default/webfiles/site/freemarker/hipporepojcrdavdemo/pagenotfound-main.ftl' to pagenotfound-main.ftl:
Progress: [=============================>] 100.0% of 299 bytes succeeded.
Downloading `/cms/webdav/default/webfiles/site/freemarker/hipporepojcrdavdemo/homepage-main.ftl' to homepage-main.ftl:
Progress: [=============================>] 100.0% of 516 bytes succeeded.
Downloading `/cms/webdav/default/webfiles/site/freemarker/hipporepojcrdavdemo/base-top-menu.ftl' to base-top-menu.ftl:
Progress: [=============================>] 100.0% of 1194 bytes succeeded.
Downloading `/cms/webdav/default/webfiles/site/freemarker/hipporepojcrdavdemo/base-layout.ftl' to base-layout.ftl:
Progress: [=============================>] 100.0% of 1012 bytes succeeded.
Downloading `/cms/webdav/default/webfiles/site/freemarker/hipporepojcrdavdemo/base-footer.ftl' to base-footer.ftl:
Progress: [=============================>] 100.0% of 244 bytes succeeded.
        

Now, you can edit and save the downloaded FreeMarker template files locally, using your favoirte editor. Afterward, you can upload the files. Of course, you may use get and put command instead for single file.

dav:/cms/webdav/default/webfiles/site/freemarker/hipporepojcrdavdemo/> mput *.ftl
[Matching... 5 matches.]
Uploading base-top-menu.ftl to `/cms/webdav/default/webfiles/site/freemarker/hipporepojcrdavdemo/base-top-menu.ftl':
Progress: [=============================>] 100.0% of 1194 bytes succeeded.
Uploading pagenotfound-main.ftl to `/cms/webdav/default/webfiles/site/freemarker/hipporepojcrdavdemo/pagenotfound-main.ftl':
Progress: [=============================>] 100.0% of 299 bytes succeeded.
Uploading base-layout.ftl to `/cms/webdav/default/webfiles/site/freemarker/hipporepojcrdavdemo/base-layout.ftl':
Progress: [=============================>] 100.0% of 1012 bytes succeeded.
Uploading base-footer.ftl to `/cms/webdav/default/webfiles/site/freemarker/hipporepojcrdavdemo/base-footer.ftl':
Progress: [=============================>] 100.0% of 244 bytes succeeded.
Uploading homepage-main.ftl to `/cms/webdav/default/webfiles/site/freemarker/hipporepojcrdavdemo/homepage-main.ftl':
Progress: [=============================>] 100.0% of 516 bytes succeeded.