cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Marc Portier <>
Subject Re: finding the URL of the current Cocoon application?
Date Mon, 06 Sep 2004 09:55:45 GMT
we've used this trick a number of times:

add an action with:

   public Map act( Redirector redirector, SourceResolver resolver,
     Map objectModel, String source, Parameters parameters)
     throws Exception
     Request request = ObjectModelHelper.getRequest(objectModel);

     if (request.getAttribute("mountpoint") == null) {
       String mountpoint = "";
       String requestURI = NetUtils.decodePath(request.getRequestURI());
       String sitemapURI = request.getSitemapURI();
       int pos = requestURI.indexOf(sitemapURI);
       if (pos != -1)
         mountpoint = requestURI.substring(0, pos);
       request.setAttribute("mountpoint", mountpoint);
       getLogger().debug("mountpoint=" + mountpoint);
     } else
         "mountpoint=" + request.getAttribute("mountpoint") + " already 

     return null;

declare it in your sitemap

       <map:action name="mountpoint"

use it on top of those matchers that need it


           | all pipes below need the mountpoint available in the
           | request-attribute 'mountpoint'
       <map:act type="mountpoint"/>

and below this point have one doing this:

       <map:match pattern="menu-part/**">
         <map:generate src="menu.xml" />
         <map:transform src="xslt/menu2div.xsl" >
           <map:parameter name="section" value="{1}" />
           <map:parameter name="mountpoint"
                          value="{request-param:mountpoint}" />
         <map:serialize />

known issues:
- URL's with special characters (even behind the mountpoint section) are 
likely to fail:



Lars Huttar wrote:
> Hi all,
> The short question:
> How do I find the URI of the current Cocoon subapplication?
> E.g. if the sitemap is in cocoon/mount/gem/, I want the URI "mount/gem".
> Here's what I'm trying to do:
> Suppose I'm deep in a stylesheet somewhere and want to produce a URI
> for creating a file (this will be sent to SourceWritingTransformer
> as the href attribute).
>>>From experience, the URIs that work are
>   context:/path...
> where "path" is relative to the Cocoon webapp top-level directory.
> My "subapplication" (what's the right term?) is in cocoon/mount/gem,
> i.e. that's where my main sitemap is.
> So I want to produce something like
>   context:/mount/gem/foo/bar.xml
> I've found that I can use the "request" InputModule in the sitemap to
> get some of this information.
> {request:requestURI} gives something like:
>   "/mount/gem/zip/zang.xml"
> while {request:sitemapURI} gives
>   "zip/zang.xml"
> for the same request.
> Therefore, I can just cut sitemapURI off the end of requestURI
> and get "/mount/gem", the URI of my subapplication.
> This works just fine, at least when you're not recursively calling
> pipelines within pipelines.
> if you have something in your sitemap with
>   src="cocoon:/foo/bar.xml"
> then there are two request URLs in play; and while the sitemapURI
> gives a value based on the outer URL, the requestURI
> gives a value based on the inner URL. So trying to cut off the sitemapURI
> from the requestURI can give garbage.
> If you know the name of the application ("gem") for sure, you
> can do string surgery based on it. But in our practice, this
> has sometimes changed ... gem2, gem-tmp, gem-bak, gem-trunk, etc.
> So... does anyone have a foolproof way to find the URI of the application
> (mount/gem)?
> Thanks,
> Lars
> ---------------------------------------------------------------------
> To unsubscribe, e-mail:
> For additional commands, e-mail:

Marc Portier                  
Outerthought - Open Source, Java & XML Competence Support Center
Read my weblog at                          

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message