1 24 package org.riotfamily.pages.mapping; 25 26 import java.util.ArrayList ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Locale ; 30 import java.util.Map ; 31 32 import javax.servlet.http.HttpServletRequest ; 33 import javax.servlet.http.HttpServletResponse ; 34 35 import org.apache.commons.logging.Log; 36 import org.apache.commons.logging.LogFactory; 37 import org.riotfamily.common.web.controller.HttpErrorController; 38 import org.riotfamily.common.web.controller.RedirectController; 39 import org.riotfamily.common.web.mapping.AbstractReverseHandlerMapping; 40 import org.riotfamily.common.web.mapping.AttributePattern; 41 import org.riotfamily.pages.Page; 42 import org.riotfamily.pages.PageAlias; 43 import org.riotfamily.pages.PageLocation; 44 import org.riotfamily.pages.dao.PageDao; 45 import org.riotfamily.riot.security.AccessController; 46 import org.springframework.util.AntPathMatcher; 47 import org.springframework.util.PathMatcher; 48 import org.springframework.web.servlet.HandlerMapping; 49 import org.springframework.web.servlet.support.RequestContextUtils; 50 51 56 public class PageHandlerMapping extends AbstractReverseHandlerMapping { 57 58 private static final String PAGE_ATTRIBUTE = 59 PageHandlerMapping.class.getName() + ".page"; 60 61 private static final Log log = LogFactory.getLog(PageHandlerMapping.class); 62 63 private PathMatcher pathMatcher = new AntPathMatcher(); 64 65 private PageDao pageDao; 66 67 private PageLocationResolver locationResolver; 68 69 private Object defaultPageHandler; 70 71 public PageHandlerMapping(PageDao pageDao, 72 PageLocationResolver pathAndLocaleResolver) { 73 74 this.pageDao = pageDao; 75 this.locationResolver = pathAndLocaleResolver; 76 } 77 78 public void setDefaultPageHandler(Object defaultPageHandler) { 79 this.defaultPageHandler = defaultPageHandler; 80 } 81 82 protected Object getHandlerInternal(HttpServletRequest request) 83 throws Exception { 84 85 PageLocation location = locationResolver.getPageLocation(request); 86 String urlPath = location.getPath(); 87 if (location == null) { 88 return null; 89 } 90 Page page = pageDao.findPage(location); 91 if (page == null) { 92 String bestMatch = null; 93 for (Iterator it = pageDao.getWildcardPaths(location).iterator(); it.hasNext();) { 94 String path = (String ) it.next(); 95 String antPattern = AttributePattern.convertToAntPattern(path); 96 if (pathMatcher.match(antPattern, urlPath) && 97 (bestMatch == null || bestMatch.length() <= path.length())) { 98 99 bestMatch = path; 100 } 101 } 102 if (bestMatch != null) { 103 location.setPath(bestMatch); 104 page = pageDao.findPage(location); 105 exposeAttributes(bestMatch, urlPath, request); 106 exposePathWithinMapping(pathMatcher.extractPathWithinPattern(bestMatch, urlPath), request); 107 } 108 } 109 else { 110 exposePathWithinMapping(urlPath, request); 111 } 112 113 log.debug("Page: " + page); 114 if (page != null) { 115 if (isRequestable(page)) { 116 request.setAttribute(PAGE_ATTRIBUTE, page); 117 String handlerName = page.getHandlerName(); 118 if (handlerName != null) { 119 return getApplicationContext().getBean(handlerName); 120 } 121 return defaultPageHandler; 122 } 123 } 124 else { 125 PageAlias alias = pageDao.findPageAlias(location); 126 if (alias != null) { 127 page = alias.getPage(); 128 if (page != null) { 129 String url = locationResolver.getUrl(page); 130 131 return new RedirectController(url, true, false); 132 } 133 else { 134 return new HttpErrorController(HttpServletResponse.SC_GONE); 135 } 136 } 137 } 138 return null; 139 } 140 141 144 protected void exposePathWithinMapping(String pathWithinMapping, HttpServletRequest request) { 145 request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, pathWithinMapping); 146 } 147 148 protected void exposeAttributes(String antPattern, String urlPath, 149 HttpServletRequest request) { 150 151 AttributePattern pattern = new AttributePattern(antPattern); 152 pattern.expose(urlPath, request); 153 } 154 155 public static Map getWildcardAttributes(HttpServletRequest request) { 156 return (Map ) request.getAttribute(AttributePattern.EXPOSED_ATTRIBUTES); 157 } 158 159 private boolean isRequestable(Page page) { 160 return page.isEnabled() || AccessController.isAuthenticatedUser(); 161 } 162 163 public static Page getPage(HttpServletRequest request) { 164 return (Page) request.getAttribute(PAGE_ATTRIBUTE); 165 } 166 167 168 protected List getPatternsForHandler(String beanName, 169 HttpServletRequest request) { 170 171 Locale locale = RequestContextUtils.getLocale(request); 172 List pages = pageDao.findPagesForHandler(beanName, locale); 173 ArrayList patterns = new ArrayList (pages.size()); 174 Iterator it = pages.iterator(); 175 while (it.hasNext()) { 176 Page page = (Page) it.next(); 177 patterns.add(new AttributePattern(locationResolver.getUrl(page))); 178 } 179 return patterns; 180 } 181 182 } 183 | Popular Tags |