1 22 23 package org.meshcms.taglib; 24 25 import java.io.*; 26 import java.util.*; 27 import javax.servlet.http.*; 28 import javax.servlet.jsp.*; 29 import org.meshcms.core.*; 30 import org.meshcms.util.*; 31 32 36 public final class SetLocale extends AbstractTag { 37 public static final String REDIRECT_ATTRIBUTE = "meshcms-redirect"; 38 39 private String value; 40 private String defaultValue; 41 private String redirectRoot; 42 43 public void writeTag() throws IOException { 44 if (Utils.isTrue(redirectRoot) && 45 webSite.getSiteMap().getPathInMenu(pagePath).isRoot()) { 46 if (setRedirectToLanguage(request, 47 (HttpServletResponse) pageContext.getResponse())) { 48 return; 49 } 50 } 51 52 Locale locale = null; 53 54 if (value != null) { 55 locale = Utils.getLocale(value); 56 } else if (!pagePath.isRoot()) { 57 for (int i = pagePath.getElementCount() - 1; locale == null && i >= 0; i--) { 58 locale = Utils.getLocale(pagePath.getElementAt(i)); 59 } 60 } 61 62 if (locale == null) { 63 locale = Utils.getLocale(defaultValue); 64 } 65 66 if (locale != null) { 67 pageContext.setAttribute(HitFilter.LOCALE_ATTRIBUTE, locale, 68 PageContext.REQUEST_SCOPE); 69 } 70 } 71 72 public String getValue() { 73 return value; 74 } 75 76 public void setValue(String value) { 77 this.value = value; 78 } 79 80 public String getDefaultValue() { 81 return defaultValue; 82 } 83 84 public void setDefaultValue(String defaultValue) { 85 this.defaultValue = defaultValue; 86 } 87 88 public String getRedirectRoot() { 89 return redirectRoot; 90 } 91 92 public void setRedirectRoot(String redirectRoot) { 93 this.redirectRoot = redirectRoot; 94 } 95 96 public static boolean setRedirectToLanguage(HttpServletRequest request, 97 HttpServletResponse response) throws IOException { 98 if (response.isCommitted()) { 99 return false; 100 } 101 102 WebSite webSite = (WebSite) request.getAttribute(HitFilter.WEBSITE_ATTRIBUTE); 103 List available = webSite.getSiteMap().getLangList(); 104 String [] accepted = WebUtils.getAcceptedLanguages(request); 105 SiteMap.CodeLocalePair chosen = null; 106 107 if (available != null && available.size() > 0) { 108 for (int i = 0; chosen == null && i < accepted.length; i++) { 109 Iterator iter = available.iterator(); 110 111 while (chosen == null && iter.hasNext()) { 112 SiteMap.CodeLocalePair clp = (SiteMap.CodeLocalePair) iter.next(); 113 114 if (clp.getCode().equalsIgnoreCase(accepted[i])) { 115 chosen = clp; 116 } 117 } 118 } 119 120 if (chosen == null) { 121 chosen = (SiteMap.CodeLocalePair) available.get(0); 122 } 123 124 WebUtils.setBlockCache(request); 125 request.setAttribute(REDIRECT_ATTRIBUTE, request.getContextPath() + '/' + 126 chosen.getCode() + '/'); 127 return true; 128 } 129 130 return false; 131 } 132 } 133
| Popular Tags
|