Save This Page
Home » sitemesh-2.3 » com.opensymphony.module » sitemesh » mapper » [javadoc | source]
    1   /*
    2    * Title:        ConfigDecoratorMapper
    3    * Description:
    4    *
    5    * This software is published under the terms of the OpenSymphony Software
    6    * License version 1.1, of which a copy has been included with this
    7    * distribution in the LICENSE.txt file.
    8    */
    9   
   10   package com.opensymphony.module.sitemesh.mapper;
   11   
   12   import com.opensymphony.module.sitemesh.Config;
   13   import com.opensymphony.module.sitemesh.Decorator;
   14   import com.opensymphony.module.sitemesh.DecoratorMapper;
   15   import com.opensymphony.module.sitemesh.Page;
   16   
   17   import javax.servlet.ServletException;
   18   import javax.servlet.http.HttpServletRequest;
   19   import java.util.Properties;
   20   
   21   /**
   22    * Default implementation of DecoratorMapper. Reads decorators and
   23    * mappings from the <code>config</code> property (default '/WEB-INF/decorators.xml').
   24    *
   25    * @author <a href="joe@truemesh.com">Joe Walnes</a>
   26    * @author <a href="mcannon@internet.com">Mike Cannon-Brookes</a>
   27    * @version $Revision: 1.1 $
   28    *
   29    * @see com.opensymphony.module.sitemesh.DecoratorMapper
   30    * @see com.opensymphony.module.sitemesh.mapper.DefaultDecorator
   31    * @see com.opensymphony.module.sitemesh.mapper.ConfigLoader
   32    */
   33   public class ConfigDecoratorMapper extends AbstractDecoratorMapper {
   34       private ConfigLoader configLoader = null;
   35   
   36       /** Create new ConfigLoader using '/WEB-INF/decorators.xml' file. */
   37       public void init(Config config, Properties properties, DecoratorMapper parent) throws InstantiationException {
   38           super.init(config, properties, parent);
   39           try {
   40               String fileName = properties.getProperty("config", "/WEB-INF/decorators.xml");
   41               configLoader = new ConfigLoader(fileName, config);
   42           }
   43           catch (Exception e) {
   44               throw new InstantiationException(e.toString());
   45           }
   46       }
   47   
   48       /** Retrieve {@link com.opensymphony.module.sitemesh.Decorator} based on 'pattern' tag. */
   49       public Decorator getDecorator(HttpServletRequest request, Page page) {
   50           String thisPath = request.getServletPath();
   51   
   52           // getServletPath() returns null unless the mapping corresponds to a servlet
   53           if (thisPath == null) {
   54               String requestURI = request.getRequestURI();
   55               if (request.getPathInfo() != null) {
   56                   // strip the pathInfo from the requestURI
   57                   thisPath = requestURI.substring(0, requestURI.indexOf(request.getPathInfo()));
   58               }
   59               else {
   60                   thisPath = requestURI;
   61               }
   62           }
   63   
   64           String name = null;
   65           try {
   66               name = configLoader.getMappedName(thisPath);
   67           }
   68           catch (ServletException e) {
   69               e.printStackTrace();
   70           }
   71   
   72           Decorator result = getNamedDecorator(request, name);
   73           return result == null ? super.getDecorator(request, page) : result;
   74       }
   75   
   76       /** Retrieve Decorator named in 'name' attribute. Checks the role if specified. */
   77       public Decorator getNamedDecorator(HttpServletRequest request, String name) {
   78           Decorator result = null;
   79           try {
   80               result = configLoader.getDecoratorByName(name);
   81           }
   82           catch (ServletException e) {
   83               e.printStackTrace();
   84           }
   85   
   86           if (result == null || (result.getRole() != null && !request.isUserInRole(result.getRole()))) {
   87               // if the result is null or the user is not in the role
   88               return super.getNamedDecorator(request, name);
   89           }
   90           else {
   91               return result;
   92           }
   93       }
   94   }

Save This Page
Home » sitemesh-2.3 » com.opensymphony.module » sitemesh » mapper » [javadoc | source]