package com.atlassian.stash.internal.web;

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

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

/**
 * Choose a decorator based on a request attribute
 */
public class RequestAttributeDecoratorMapper extends AbstractDecoratorMapper  {

        private String decoratorParameter = null;

        public void init(Config config, Properties properties, DecoratorMapper parent) throws InstantiationException {
            super.init(config, properties, parent);
            decoratorParameter = properties.getProperty("decorator.parameter", "decorator");
        }

        public Decorator getDecorator(HttpServletRequest request, Page page) {
            Decorator result = null;
            String decorator = (String) request.getAttribute(decoratorParameter);

            //get decorator from HttpSession
            if (decorator != null) {
                result = getNamedDecorator(request, decorator);
            }
            return result == null ? super.getDecorator(request, page) : result;
        }

}