1 24 package org.riotfamily.pages; 25 26 import java.util.List ; 27 import java.util.Locale ; 28 29 import javax.servlet.http.HttpServletRequest ; 30 import javax.servlet.http.HttpServletResponse ; 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 45 public class LocaleListController extends AbstractCacheableController { 46 47 private PageDao pageDao; 48 49 private PageLocationResolver locationResolver; 50 51 private String siteName; 52 53 private String 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 siteName) { 63 this.siteName = siteName; 64 } 65 66 public void setViewName(String viewName) { 67 this.viewName = viewName; 68 } 69 70 public ModelAndView handleRequest(HttpServletRequest request, 71 HttpServletResponse response) throws Exception { 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 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 locale = (Locale )locales.get(0); 93 PageNode root = pageDao.findRootNode(site); 94 Page page = (Page) root.getChildPages(locale).iterator().next(); 95 String 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 request) { 110 return CacheableController.CACHE_ETERNALLY; 111 } 112 113 } 114 | Popular Tags |