CookieDecoratorMapper

Paul Hammant <[email protected]>
Newsgroups gmane.comp.web.sitemesh.general
Message-ID <[email protected]>
Folks,

I have been fiddling with a new decorator mapper (in the sitemesh latest 
codebase from java.net cvs). See attached. I've also plugged the 
decorator-mapper into the main sitemesh-default.xml file (also attached).

The idea is that using javascript snippets like .....

     // in the jsp source for decorator one
     switchToDecoratorTwo() {
       document.cookie = "sitemesh-decorator=dec-two";    
       document.location.reload(true);
     }

     // in the jsp source for decorator two
     switchToDecoratorOne() {
       document.cookie = "sitemesh-decorator=dec-one";    
       document.location.reload(true);
     }

..... the user via a browser can click links that will cause the 
decoration of the current page to change.

It seems to switch quite nicely under contrived testing, but I can't get 
the decorators.xml thing coded to allow it to toggle back and forth.  
One decorator seems to remain dominant over the other.

  <decorators defaultdir="/decorators">
      <decorator name="main" page="One.jsp"/>
      <decorator name="dec-two" page="Two.jsp"/>
     <decorator name="panel" page="panel.jsp"/>
     <decorator name="printable" page="printable.jsp"/>
  </decorators>

The thing that is confusing me to hell is how to code the above to not 
just use the first (in the file) of "main" and "dec-two".  I've tried 
specifying patterns. but don't understand advanced coding of 
decorators.xml enough.

Someone shoot me down if this is something that Sitemesh cannot do.  Or 
show me where I am going wrong with decorators.xml

Regards,

- Paul
Greetings to the other asshats here present!
CookieDecoratorMapper.java (text/java, 2.1 KB)
/*
 * Title:        ParameterDecoratorMapper
 * Description:
 *
 * This software is published under the terms of the OpenSymphony Software
 * License version 1.1, of which a copy has been included with this
 * distribution in the LICENSE.txt file.
 */

package com.opensymphony.module.sitemesh.mapper;

import com.opensymphony.module.sitemesh.Config;
import com.opensymphony.module.sitemesh.Decorator;
import com.opensymphony.module.sitemesh.DecoratorMapper;
import com.opensymphony.module.sitemesh.Page;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Cookie;
import java.util.Properties;

/**
 * The CookieDecoratorMapper is a sample DecoratorMapper that will choose the decorator
 * based on cookie settings.
 * <p/>
 * <p>The CookieDecoratorMapper is configured via one properties.
 * <code>cookie.name</code> - the cookie which contains the name of the decorator which will be mapped.
 *
 * @author Paul Hammant
 * @version $Revision: 1.1 $
 * @see com.opensymphony.module.sitemesh.DecoratorMapper
 */
public class CookieDecoratorMapper extends AbstractDecoratorMapper {
    private String cookieName;

    public void init(Config config, Properties properties, DecoratorMapper parent) throws InstantiationException {
        super.init(config, properties, parent);
        cookieName = properties.getProperty("cookie.name", null);
        if (cookieName == null) {
            throw new InstantiationException("'cookie.name' name parameter not set for this decorator mapper");
        }
    }

    public Decorator getDecorator(HttpServletRequest request, Page page) {
        Decorator result = null;
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (int i = 0; i < cookies.length; i++) {
                Cookie cookie = cookies[i];
                if (cookie.getName().equals(cookieName)) {
                    result = getNamedDecorator(request, cookie.getValue());
                    System.out.println("--> " + cookie.getValue() + " " + result != null);
                }
            }
        }
        return result == null ? super.getDecorator(request, page) : result;
    }
}
sitemesh-default.xml (text/xml, 1.2 KB)
<sitemesh>
	<page-parsers>
		<parser default="true" class="com.opensymphony.module.sitemesh.parser.DefaultPageParser" />
		<parser content-type="text/html" class="com.opensymphony.module.sitemesh.parser.FastPageParser" />
	</page-parsers>
	<decorator-mappers>
		<mapper class="com.opensymphony.module.sitemesh.mapper.PageDecoratorMapper">
			<param name="property.1" value="meta.decorator" />
			<param name="property.2" value="decorator" />
		</mapper>
		<mapper class="com.opensymphony.module.sitemesh.mapper.FrameSetDecoratorMapper"/>
		<mapper class="com.opensymphony.module.sitemesh.mapper.PrintableDecoratorMapper">
			<param name="decorator" value="printable" />
			<param name="parameter.name" value="printable" />
			<param name="parameter.value" value="true" />
		</mapper>
 		<mapper class="com.opensymphony.module.sitemesh.mapper.CookieDecoratorMapper">
			<param name="cookie.name" value="sitemesh-decorator" />
		</mapper>
		<mapper class="com.opensymphony.module.sitemesh.mapper.FileDecoratorMapper"/>
		<mapper class="com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper">
			<param name="config" value="/WEB-INF/decorators.xml" />
		</mapper>
	</decorator-mappers>
</sitemesh>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.