Overlapping Contexts in Tomcat are powerfull

I want to create a custom xforms widget for one of my Web Forms and found some useful information on the Alfresco wiki (http://wiki.alfresco.com/wiki/Creating_XForms_Widgets). Although you can't call that info a manual.

So, I started and if you do know your way around Alfresco, you can get it done. But, off course we work with AMP packages and that's where I found 2 mayor draw-backs.

  1. The most annoying issue is also detected by someone else and is logged as ALFCOM-332. So, it means that at this moment, you should put (and so overwrite) xforms.js in your amp under /web/scripts/ajax. Or if you use maven, like us, under /src/main/webapp/scripts/ajax.
    I don't like overwriting files in the original alfresco, since that slims down your chances of a successful upgrade to a newer Alfresco version.
  2. Another thing, which is about the same as the above, is about the web-client-config-wcm.xml. As usual I just use my module bootstrapped web-client-config-custom.xml and I tried to just add:

    <config><wcm><xforms><widget ..../></xforms></wcm></config>
    

    But that doesn't seam to work. I can use my web-client-config-custom.xml, but then I need to put replace="true" on the config element and copy the entire wcm element of web-client-config-wcm.xml into it.

For the first issue I have found a great, but rather tricky (complex) solution. So, I am not going to use it myself now, but it's great knowledge to blog about, since this trick can be handy for other issues in the future.
To be short:

  • I created another very small webapp on tomcat and used the contextPath "/alfresco/scripts/ajax". And yes, this works. So tomcat allows overlapping contexts. /alfresco goes to the alfresco app, while a request to /alfresco/scripts/ajax/xforms.js for example goes to the other small webapp.
    You do need to write a small context file off course and you can't put the new webapp in the webapps dir, but I placed it under the server dir.
  • Then I created a symbolic link to "{exploded-alfresco-webapp}/scripts/ajax" inside this new webapp's root and i named it "ajax". So a request to /alfresco/scripts/ajax/xforms.js gives a 404, but a request to /alfresco/scripts/ajax/ajax/xforms.js works.
  • Then I used urlrewrite filter to forward all requests to the correct ajax folder, except for xforms.js. So here is the configuration.

    <urlrewrite>
        <rule>
            <from>/xforms.js</from>
            <to>/ajax/xforms.jsp</to>
        </rule>
        <rule>
            <from>/(.*)</from>
            <to>/ajax/$1</to>
        </rule>
    </urlrewrite>
    
  • The result is that all requests from the client to the ajax javascript files keep working, but the call to xforms.js is forwarded to xforms.jsp. Now, all I need to do is put an xforms.jsp inside an AMP under /web/scripts/ajax (note that it doesn't overwrite a thing) and put following code in it.

    <%@ page contentType="text/javascript; charset=UTF-8" %>
    <jsp:include page="xforms.js" />
    
    //some more widgets here
    <jsp:include page="custom-widgets.js" />