KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > pages > LocaleListController


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  * Jan-Frederic Linde [jfl at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.pages;
25
26 import java.util.List JavaDoc;
27 import java.util.Locale JavaDoc;
28
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30 import javax.servlet.http.HttpServletResponse JavaDoc;
31
32 import org.riotfamily.cachius.spring.AbstractCacheableController;
33 import org.riotfamily.cachius.spring.CacheableController;
34 import org.riotfamily.common.web.util.ServletUtils;
35 import org.riotfamily.pages.dao.PageDao;
36 import org.riotfamily.pages.mapping.PageLocationResolver;
37 import org.riotfamily.riot.security.AccessController;
38 import org.springframework.web.servlet.ModelAndView;
39 import org.springframework.web.servlet.view.RedirectView;
40
41 /**
42  * @author Jan-Frederic Linde [jfl at neteye dot de]
43  * @since 6.5
44  */

45 public class LocaleListController extends AbstractCacheableController {
46
47     private PageDao pageDao;
48
49     private PageLocationResolver locationResolver;
50
51     private String JavaDoc siteName;
52
53     private String JavaDoc viewName;
54
55     public LocaleListController(PageDao pageDao,
56                     PageLocationResolver locationResolver) {
57
58         this.pageDao = pageDao;
59         this.locationResolver = locationResolver;
60     }
61
62     public void setSiteName(String JavaDoc siteName) {
63         this.siteName = siteName;
64     }
65
66     public void setViewName(String JavaDoc viewName) {
67         this.viewName = viewName;
68     }
69
70     public ModelAndView handleRequest(HttpServletRequest JavaDoc request,
71                     HttpServletResponse JavaDoc response) throws Exception JavaDoc {
72
73
74         Site site = null;
75         if (siteName != null) {
76             site = pageDao.getSite(siteName);
77         }
78         else {
79             site = pageDao.getDefaultSite();
80         }
81
82         if (site != null) {
83             List JavaDoc locales = null;
84             if (AccessController.isAuthenticatedUser()) {
85                 locales = pageDao.getLocales();
86             }
87             else if (site.isEnabled()) {
88                 locales = site.getLocales();
89             }
90             if (locales != null) {
91                 if (locales.size() == 1) {
92                     Locale JavaDoc locale = (Locale JavaDoc)locales.get(0);
93                     PageNode root = pageDao.findRootNode(site);
94                     Page page = (Page) root.getChildPages(locale).iterator().next();
95                     String JavaDoc url = locationResolver.getUrl(page);
96                     url = ServletUtils.resolveUrl(url, request);
97                     return new ModelAndView(new RedirectView(url));
98                 }
99                 if (!locales.isEmpty()) {
100                     return new ModelAndView(viewName, "locales", locales);
101                 }
102             }
103         }
104
105         response.sendError(HttpServletResponse.SC_NOT_FOUND);
106         return null;
107     }
108
109     public long getTimeToLive(HttpServletRequest JavaDoc request) {
110         return CacheableController.CACHE_ETERNALLY;
111     }
112
113 }
114
Popular Tags