1 18 19 package org.apache.roller.ui.rendering; 20 21 import java.io.IOException ; 22 import java.util.HashSet ; 23 import java.util.Set ; 24 import javax.servlet.RequestDispatcher ; 25 import javax.servlet.ServletException ; 26 import javax.servlet.http.HttpServletRequest ; 27 import javax.servlet.http.HttpServletResponse ; 28 import org.apache.commons.lang.StringUtils; 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 import org.apache.roller.config.RollerConfig; 32 import org.apache.roller.model.RollerFactory; 33 import org.apache.roller.model.UserManager; 34 import org.apache.roller.pojos.WebsiteData; 35 36 37 46 public class WeblogRequestMapper implements RequestMapper { 47 48 private static Log log = LogFactory.getLog(WeblogRequestMapper.class); 49 50 private static final String PAGE_SERVLET = "/roller-ui/rendering/page"; 51 private static final String FEED_SERVLET = "/roller-ui/rendering/feed"; 52 private static final String RESOURCE_SERVLET = "/roller-ui/rendering/resources"; 53 private static final String SEARCH_SERVLET = "/roller-ui/rendering/search"; 54 private static final String RSD_SERVLET = "/roller-ui/rendering/rsd"; 55 56 private static final String COMMENT_SERVLET = "/roller-ui/rendering/comment"; 57 private static final String TRACKBACK_SERVLET = "/roller-ui/rendering/trackback"; 58 59 60 Set restricted = null; 62 63 64 public WeblogRequestMapper() { 65 66 this.restricted = new HashSet (); 67 68 String restrictList = 70 RollerConfig.getProperty("rendering.weblogMapper.rollerProtectedUrls"); 71 if(restrictList != null && restrictList.trim().length() > 0) { 72 String [] restrict = restrictList.split(","); 73 for(int i=0; i < restrict.length; i++) { 74 this.restricted.add(restrict[i]); 75 } 76 } 77 78 restrictList = 80 RollerConfig.getProperty("rendering.weblogMapper.userProtectedUrls"); 81 if(restrictList != null && restrictList.trim().length() > 0) { 82 String [] restrict = restrictList.split(","); 83 for(int i=0; i < restrict.length; i++) { 84 this.restricted.add(restrict[i]); 85 } 86 } 87 } 88 89 90 public boolean handleRequest(HttpServletRequest request, HttpServletResponse response) 91 throws ServletException , IOException { 92 93 boolean trailingSlash = false; 96 97 String weblogHandle = null; 98 String weblogLocale = null; 99 String weblogRequestContext = null; 100 String weblogRequestData = null; 101 102 log.debug("evaluating ["+request.getServletPath()+"]"); 103 104 String servlet = request.getServletPath(); 106 String pathInfo = null; 107 108 if (StringUtils.isEmpty(request.getServletPath())) { 110 servlet = request.getPathInfo(); 111 } 112 113 if(servlet != null && servlet.trim().length() > 1) { 114 115 servlet = servlet.substring(1); 117 118 if(servlet.endsWith("/")) { 120 servlet = servlet.substring(0, servlet.length() - 1); 121 trailingSlash = true; 122 } 123 124 if(servlet.indexOf("/") != -1) { 125 weblogHandle = servlet.substring(0, servlet.indexOf("/")); 126 pathInfo = servlet.substring(servlet.indexOf("/")+1); 127 } else { 128 weblogHandle = servlet; 129 } 130 } 131 132 log.debug("potential weblog handle = "+weblogHandle); 133 134 if(restricted.contains(weblogHandle) || !this.isWeblog(weblogHandle)) { 136 log.debug("SKIPPED "+weblogHandle); 137 return false; 138 } 139 140 log.debug("WEBLOG_URL "+request.getServletPath()); 141 142 if(pathInfo != null) { 144 145 String [] urlPath = pathInfo.split("/", 3); 148 149 if(this.isLocale(urlPath[0])) { 151 weblogLocale = urlPath[0]; 152 153 if(urlPath.length == 2) { 155 weblogRequestContext = urlPath[1]; 156 weblogRequestData = null; 157 158 } else if(urlPath.length == 3) { 160 weblogRequestContext = urlPath[1]; 161 weblogRequestData = urlPath[2]; 162 } 163 164 } else { 166 weblogLocale = null; 167 weblogRequestContext = urlPath[0]; 168 169 if(urlPath.length == 2) { 171 weblogRequestData = urlPath[1]; 172 173 } else if(urlPath.length == 3) { 176 weblogRequestData = urlPath[1] + "/" + urlPath[2]; 177 } 178 } 179 180 } 181 182 if(weblogRequestContext == null && !trailingSlash) { 186 String redirectUrl = request.getRequestURI() + "/"; 190 if(request.getQueryString() != null) { 191 redirectUrl += "?"+request.getQueryString(); 192 } 193 194 response.sendRedirect(redirectUrl); 195 return true; 196 197 } else if(weblogRequestContext != null && trailingSlash) { 198 response.sendError(HttpServletResponse.SC_NOT_FOUND); 202 return true; 203 } 204 205 String forwardUrl = calculateForwardUrl(request, weblogHandle, weblogLocale, 207 weblogRequestContext, weblogRequestData); 208 209 if(forwardUrl == null) { 211 return false; 212 } 213 214 log.debug("forwarding to "+forwardUrl); 216 RequestDispatcher dispatch = request.getRequestDispatcher(forwardUrl); 217 dispatch.forward(request, response); 218 219 return true; 221 } 222 223 224 230 private String calculateForwardUrl(HttpServletRequest request, 231 String handle, String locale, 232 String context, String data) { 233 234 log.debug(handle+","+locale+","+context+","+data); 235 236 StringBuffer forwardUrl = new StringBuffer (); 237 238 if("POST".equals(request.getMethod())) { 240 if(context.equals("entry")) { 242 if(request.getParameter("excerpt") != null) { 244 245 forwardUrl.append(TRACKBACK_SERVLET); 246 forwardUrl.append("/"); 247 forwardUrl.append(handle); 248 if(locale != null) { 249 forwardUrl.append("/"); 250 forwardUrl.append(locale); 251 } 252 forwardUrl.append("/"); 253 forwardUrl.append(context); 254 if(data != null) { 255 forwardUrl.append("/"); 256 forwardUrl.append(data); 257 } 258 259 } else if(request.getParameter("content") != null) { 261 262 forwardUrl.append(COMMENT_SERVLET); 263 forwardUrl.append("/"); 264 forwardUrl.append(handle); 265 if(locale != null) { 266 forwardUrl.append("/"); 267 forwardUrl.append(locale); 268 } 269 forwardUrl.append("/"); 270 forwardUrl.append(context); 271 if(data != null) { 272 forwardUrl.append("/"); 273 forwardUrl.append(data); 274 } 275 } 276 277 } else { 278 return null; 280 } 281 282 } else { 283 if(context == null) { 285 286 forwardUrl.append(PAGE_SERVLET); 287 forwardUrl.append("/"); 288 forwardUrl.append(handle); 289 if(locale != null) { 290 forwardUrl.append("/"); 291 forwardUrl.append(locale); 292 } 293 294 } else if(context.equals("page") || context.equals("entry") || 296 context.equals("date") || context.equals("category")) { 297 298 forwardUrl.append(PAGE_SERVLET); 299 forwardUrl.append("/"); 300 forwardUrl.append(handle); 301 if(locale != null) { 302 forwardUrl.append("/"); 303 forwardUrl.append(locale); 304 } 305 forwardUrl.append("/"); 306 forwardUrl.append(context); 307 if(data != null) { 308 forwardUrl.append("/"); 309 forwardUrl.append(data); 310 } 311 312 } else if(context.equals("feed")) { 314 315 forwardUrl.append(FEED_SERVLET); 316 forwardUrl.append("/"); 317 forwardUrl.append(handle); 318 if(locale != null) { 319 forwardUrl.append("/"); 320 forwardUrl.append(locale); 321 } 322 if(data != null) { 323 forwardUrl.append("/"); 324 forwardUrl.append(data); 325 } 326 327 } else if(context.equals("resource")) { 329 330 forwardUrl.append(RESOURCE_SERVLET); 331 forwardUrl.append("/"); 332 forwardUrl.append(handle); 333 if(data != null) { 334 forwardUrl.append("/"); 335 forwardUrl.append(data); 336 } 337 338 } else if(context.equals("search")) { 340 341 forwardUrl.append(SEARCH_SERVLET); 342 forwardUrl.append("/"); 343 forwardUrl.append(handle); 344 345 } else if(context.equals("rsd")) { 347 348 forwardUrl.append(RSD_SERVLET); 349 forwardUrl.append("/"); 350 forwardUrl.append(handle); 351 352 } else { 354 return null; 355 } 356 } 357 358 log.debug("FORWARD_URL "+forwardUrl.toString()); 359 360 return forwardUrl.toString(); 361 } 362 363 364 370 private boolean isWeblog(String potentialHandle) { 371 372 log.debug("checking weblog handle "+potentialHandle); 373 374 boolean isWeblog = false; 375 376 try { 377 UserManager mgr = RollerFactory.getRoller().getUserManager(); 378 WebsiteData weblog = mgr.getWebsiteByHandle(potentialHandle); 379 380 if(weblog != null) { 381 isWeblog = true; 382 } 383 } catch(Exception ex) { 384 } 386 387 return isWeblog; 388 } 389 390 391 395 private boolean isLocale(String potentialLocale) { 396 397 boolean isLocale = false; 398 399 if(potentialLocale != null && 401 (potentialLocale.length() == 2 || potentialLocale.length() == 5)) { 402 403 String [] langCountry = potentialLocale.split("_"); 406 if(langCountry.length == 1 && 407 langCountry[0] != null && langCountry[0].length() == 2) { 408 isLocale = true; 409 410 } else if(langCountry.length == 2 && 411 langCountry[0] != null && langCountry[0].length() == 2 && 412 langCountry[1] != null && langCountry[1].length() == 2) { 413 414 isLocale = true; 415 } 416 } 417 418 return isLocale; 419 } 420 421 } 422 | Popular Tags |