KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > pages > mapping > PageHandlerMapping


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2007
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.pages.mapping;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Locale JavaDoc;
30 import java.util.Map JavaDoc;
31
32 import javax.servlet.http.HttpServletRequest JavaDoc;
33 import javax.servlet.http.HttpServletResponse JavaDoc;
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 /**
52  * @author Felix Gnass [fgnass at neteye dot de]
53  * @author Jan-Frederic Linde [jfl at neteye dot de]
54  * @since 6.5
55  */

56 public class PageHandlerMapping extends AbstractReverseHandlerMapping {
57
58     private static final String JavaDoc 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 JavaDoc 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 JavaDoc defaultPageHandler) {
79         this.defaultPageHandler = defaultPageHandler;
80     }
81
82     protected Object JavaDoc getHandlerInternal(HttpServletRequest JavaDoc request)
83             throws Exception JavaDoc {
84
85         PageLocation location = locationResolver.getPageLocation(request);
86         String JavaDoc urlPath = location.getPath();
87         if (location == null) {
88             return null;
89         }
90         Page page = pageDao.findPage(location);
91         if (page == null) {
92             String JavaDoc bestMatch = null;
93             for (Iterator JavaDoc it = pageDao.getWildcardPaths(location).iterator(); it.hasNext();) {
94                 String JavaDoc path = (String JavaDoc) it.next();
95                 String JavaDoc 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 JavaDoc 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 JavaDoc 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     /**
142      * <strong>Copied from AbstractUrlHandlerMapping</strong>
143      */

144     protected void exposePathWithinMapping(String JavaDoc pathWithinMapping, HttpServletRequest JavaDoc request) {
145         request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, pathWithinMapping);
146     }
147     
148     protected void exposeAttributes(String JavaDoc antPattern, String JavaDoc urlPath,
149             HttpServletRequest JavaDoc request) {
150
151         AttributePattern pattern = new AttributePattern(antPattern);
152         pattern.expose(urlPath, request);
153     }
154     
155     public static Map JavaDoc getWildcardAttributes(HttpServletRequest JavaDoc request) {
156         return (Map JavaDoc) 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 JavaDoc request) {
164         return (Page) request.getAttribute(PAGE_ATTRIBUTE);
165     }
166     
167
168     protected List JavaDoc getPatternsForHandler(String JavaDoc beanName,
169             HttpServletRequest JavaDoc request) {
170         
171         Locale JavaDoc locale = RequestContextUtils.getLocale(request);
172         List JavaDoc pages = pageDao.findPagesForHandler(beanName, locale);
173         ArrayList JavaDoc patterns = new ArrayList JavaDoc(pages.size());
174         Iterator JavaDoc 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