KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOException JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.Iterator JavaDoc;
29
30 import javax.servlet.ServletException JavaDoc;
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33
34 import org.riotfamily.common.web.filter.FilterPlugin;
35 import org.riotfamily.common.web.filter.PluginChain;
36 import org.riotfamily.pages.Page;
37 import org.riotfamily.pages.PageLocation;
38 import org.riotfamily.pages.Site;
39 import org.riotfamily.pages.dao.PageDao;
40 import org.riotfamily.riot.security.AccessController;
41 import org.springframework.transaction.PlatformTransactionManager;
42 import org.springframework.transaction.TransactionDefinition;
43 import org.springframework.transaction.TransactionStatus;
44 import org.springframework.transaction.support.DefaultTransactionDefinition;
45
46 /**
47  * FilterPlugin that provides folder support. Normally the website-servlet is
48  * mapped using a suffix-mapping like <code>*.html</code> and therefore can't
49  * handle requests like </code>/some/folder</code>. In case a page with the
50  * requested path exists and is {@link Page#isFolder() marked as folder}, this
51  * plugin will send a redirect to the first <i>requestable</i> child-page.
52  * A page is requestable if it is {@link Page#isEnabled() enabled} <em>or</em>
53  * a Riot user {@link AccessController#isAuthenticatedUser() is logged in}.
54  *
55  * @author Felix Gnass [fgnass at neteye dot de]
56  * @since 6.5
57  */

58 public class FolderFilterPlugin extends FilterPlugin {
59
60     private static final TransactionDefinition TX_DEF =
61             new DefaultTransactionDefinition();
62     
63     private PageDao pageDao;
64     
65     private PageLocationResolver locationResolver;
66     
67     private PlatformTransactionManager tx;
68     
69     
70     public FolderFilterPlugin(PageDao pageDao,
71             PageLocationResolver locationResolver,
72             PlatformTransactionManager tx) {
73         
74         this.pageDao = pageDao;
75         this.locationResolver = locationResolver;
76         this.tx = tx;
77     }
78
79     public void doFilter(HttpServletRequest JavaDoc request,
80             HttpServletResponse JavaDoc response, PluginChain pluginChain)
81             throws IOException JavaDoc, ServletException JavaDoc {
82         
83         boolean requestHandled = false;
84         String JavaDoc uri = request.getRequestURI();
85         if (uri.lastIndexOf('.') < uri.lastIndexOf('/')) {
86             TransactionStatus status = tx.getTransaction(TX_DEF);
87             try {
88                 Collection JavaDoc indexPages = getIndexPages(request);
89                 if (indexPages != null) {
90                     requestHandled = true;
91                     sendRedirect(indexPages, request, response);
92                 }
93             }
94             catch (Exception JavaDoc ex) {
95                 tx.rollback(status);
96                 throw new ServletException JavaDoc(ex);
97             }
98             tx.commit(status);
99         }
100         if (!requestHandled) {
101             pluginChain.doFilter(request, response);
102         }
103     }
104
105     private boolean isRequestable(Page page) {
106         return page.isEnabled() || AccessController.isAuthenticatedUser();
107     }
108     
109     private Collection JavaDoc getIndexPages(HttpServletRequest JavaDoc request) {
110         Collection JavaDoc childPages = null;
111         PageLocation location = locationResolver.getPageLocation(request);
112         if (location != null) {
113             if ("/".equals(location.getPath())) {
114                 Site site = pageDao.getSite(location.getSiteName());
115                 childPages = pageDao.findRootNode(site).getChildPages(location.getLocale());
116             }
117             else {
118                 Page page = pageDao.findPage(location);
119                 if (page != null && page.isFolder()) {
120                     childPages = page.getChildPages();
121                 }
122             }
123         }
124         return childPages;
125     }
126     
127     private void sendRedirect(Collection JavaDoc pages, HttpServletRequest JavaDoc request,
128             HttpServletResponse JavaDoc response) throws IOException JavaDoc {
129         
130         String JavaDoc url = getFirstRequestableChildPageUrl(pages);
131         if (url != null) {
132             response.sendRedirect(response.encodeRedirectURL(
133                     request.getContextPath() + url));
134         }
135         else {
136             response.sendError(HttpServletResponse.SC_NOT_FOUND);
137         }
138     }
139     
140     private String JavaDoc getFirstRequestableChildPageUrl(Collection JavaDoc pages) {
141         Iterator JavaDoc it = pages.iterator();
142         while (it.hasNext()) {
143             Page page = (Page) it.next();
144             if (isRequestable(page)) {
145                 return locationResolver.getUrl(page);
146             }
147         }
148         return null;
149     }
150 }
151
Popular Tags