1 18 19 package org.apache.roller.ui.rendering.util; 20 21 import java.util.Locale ; 22 import javax.servlet.http.HttpServletRequest ; 23 import org.apache.commons.logging.Log; 24 import org.apache.commons.logging.LogFactory; 25 import org.apache.roller.RollerException; 26 import org.apache.roller.model.RollerFactory; 27 import org.apache.roller.model.UserManager; 28 import org.apache.roller.pojos.WebsiteData; 29 30 31 45 public class WeblogRequest extends ParsedRequest { 46 47 private static Log log = LogFactory.getLog(WeblogRequest.class); 48 49 private String weblogHandle = null; 51 private String locale = null; 52 private String pathInfo = null; 53 54 private WebsiteData weblog = null; 56 private Locale localeInstance = null; 57 58 59 public WeblogRequest() {} 60 61 62 public WeblogRequest(HttpServletRequest request) 63 throws InvalidRequestException { 64 65 super(request); 67 68 String path = request.getPathInfo(); 69 70 log.debug("parsing path "+path); 71 72 if(path != null && path.trim().length() > 1) { 74 75 path = path.substring(1); 77 78 if(path.endsWith("/")) { 80 path = path.substring(0, path.length() - 1); 81 } 82 83 String [] pathElements = path.split("/", 2); 84 if(pathElements[0].trim().length() > 0) { 85 this.weblogHandle = pathElements[0]; 86 } else { 87 throw new InvalidRequestException("not a weblog request, "+ 89 request.getRequestURL()); 90 } 91 92 if(pathElements.length == 2) { 94 path = pathElements[1]; 95 } else { 96 path = null; 97 } 98 } 99 100 if(path != null && path.trim().length() > 0) { 102 103 String [] pathElements = path.split("/", 2); 104 if(this.isLocale(pathElements[0])) { 105 this.locale = pathElements[0]; 106 107 if(pathElements.length == 2) { 109 this.pathInfo = pathElements[1]; 110 } 111 } else { 112 this.pathInfo = path; 114 } 115 } 116 117 if(log.isDebugEnabled()) { 118 log.debug("handle = "+this.weblogHandle); 119 log.debug("locale = "+this.locale); 120 log.debug("pathInfo = "+this.pathInfo); 121 } 122 } 123 124 125 129 private boolean isLocale(String potentialLocale) { 130 131 boolean isLocale = false; 132 133 if(potentialLocale != null && 135 (potentialLocale.length() == 2 || potentialLocale.length() == 5)) { 136 137 String [] langCountry = potentialLocale.split("_"); 140 if(langCountry.length == 1 && 141 langCountry[0] != null && langCountry[0].length() == 2) { 142 isLocale = true; 143 144 } else if(langCountry.length == 2 && 145 langCountry[0] != null && langCountry[0].length() == 2 && 146 langCountry[1] != null && langCountry[1].length() == 2) { 147 148 isLocale = true; 149 } 150 } 151 152 return isLocale; 153 } 154 155 156 public String getWeblogHandle() { 157 return weblogHandle; 158 } 159 160 public void setWeblogHandle(String weblogHandle) { 161 this.weblogHandle = weblogHandle; 162 } 163 164 public String getLocale() { 165 return locale; 166 } 167 168 public void setLocale(String locale) { 169 this.locale = locale; 170 } 171 172 public String getPathInfo() { 173 return pathInfo; 174 } 175 176 public void setPathInfo(String pathInfo) { 177 this.pathInfo = pathInfo; 178 } 179 180 public WebsiteData getWeblog() { 181 182 if(weblog == null && weblogHandle != null) { 183 try { 184 UserManager umgr = RollerFactory.getRoller().getUserManager(); 185 weblog = umgr.getWebsiteByHandle(weblogHandle, Boolean.TRUE); 186 } catch (RollerException ex) { 187 log.error("Error looking up weblog "+weblogHandle, ex); 188 } 189 } 190 191 return weblog; 192 } 193 194 public void setWeblog(WebsiteData weblog) { 195 this.weblog = weblog; 196 } 197 198 199 206 public Locale getLocaleInstance() { 207 208 if(localeInstance == null && locale != null) { 209 String [] langCountry = locale.split("_"); 210 if(langCountry.length == 1) { 211 localeInstance = new Locale (langCountry[0]); 212 } else if(langCountry.length == 2) { 213 localeInstance = new Locale (langCountry[0], langCountry[1]); 214 } 215 } else if(localeInstance == null) { 216 localeInstance = getWeblog().getLocaleInstance(); 217 } 218 219 return localeInstance; 220 } 221 222 public void setLocaleInstance(Locale localeInstance) { 223 this.localeInstance = localeInstance; 224 } 225 226 } 227 | Popular Tags |