1 24 package org.riotfamily.pages.mapping; 25 26 import java.util.Collection ; 27 import java.util.Locale ; 28 29 import javax.servlet.http.HttpServletRequest ; 30 31 import org.riotfamily.common.web.util.PathCompleter; 32 import org.riotfamily.common.web.util.ServletUtils; 33 import org.riotfamily.pages.Page; 34 import org.riotfamily.pages.PageLocation; 35 import org.springframework.util.StringUtils; 36 37 42 public class DefaultPageLocationResolver implements PageLocationResolver { 43 44 private Collection locales; 45 46 private Locale fixedLocale = null; 47 48 private PathCompleter pathCompleter; 49 50 public DefaultPageLocationResolver(PathCompleter pathCompleter) { 51 this.pathCompleter = pathCompleter; 52 } 53 54 public void setLocales(Collection locales) { 55 this.locales = locales; 56 if (locales != null && locales.size() == 1) { 57 fixedLocale = (Locale ) locales.iterator().next(); 58 } 59 } 60 61 protected boolean localesInPath() { 62 return locales != null && locales.size() > 1; 63 } 64 65 public PageLocation getPageLocation(HttpServletRequest request) { 66 String path = ServletUtils.getPathWithoutServletMapping(request); 67 if (path.length() > 1 && path.endsWith("/")) { 68 path = path.substring(0, path.length() - 1); 69 } 70 Locale locale = null; 71 if (localesInPath()) { 72 if (path.length() > 1) { 73 int i = path.indexOf('/', 1); 74 if (i > 1) { 75 String localeString = path.substring(1, i); 76 locale = StringUtils.parseLocaleString(localeString); 77 path = path.substring(i); 78 } 79 } 80 } 81 else { 82 locale = fixedLocale; 83 } 84 return new PageLocation(null, path, locale); 85 } 86 87 public String getUrl(Page page) { 88 PageLocation location = new PageLocation(page); 89 StringBuffer url = new StringBuffer (); 90 if (localesInPath() && location.getLocale() != null) { 91 url.append('/'); 92 url.append(location.getLocale().toString().toLowerCase()); 93 } 94 url.append(location.getPath()); 95 if (!page.isFolder() || pathCompleter.isPrefixMapping()) { 96 pathCompleter.addServletMapping(url); 97 } 98 return url.toString(); 99 } 100 } 101 | Popular Tags |