1 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 33 public class ConfigDecoratorMapper extends AbstractDecoratorMapper { 34 private ConfigLoader configLoader = null; 35 36 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 49 public Decorator getDecorator(HttpServletRequest request, Page page) { 50 String thisPath = request.getServletPath(); 51 52 if (thisPath == null) { 54 String requestURI = request.getRequestURI(); 55 if (request.getPathInfo() != null) { 56 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 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 return super.getNamedDecorator(request, name); 89 } 90 else { 91 return result; 92 } 93 } 94 } 95 | Popular Tags |