1 18 19 package org.apache.roller.ui.rendering.util; 20 21 import java.util.HashMap ; 22 import java.util.Map ; 23 import javax.servlet.http.HttpServletRequest ; 24 import org.apache.commons.lang.StringUtils; 25 import org.apache.commons.logging.Log; 26 import org.apache.commons.logging.LogFactory; 27 import org.apache.roller.RollerException; 28 import org.apache.roller.model.RollerFactory; 29 import org.apache.roller.model.UserManager; 30 import org.apache.roller.model.WeblogManager; 31 import org.apache.roller.pojos.Template; 32 import org.apache.roller.pojos.WeblogCategoryData; 33 import org.apache.roller.pojos.WeblogEntryData; 34 import org.apache.roller.pojos.WeblogTemplate; 35 import org.apache.roller.util.URLUtilities; 36 37 38 46 public class WeblogPageRequest extends WeblogRequest { 47 48 private static Log log = LogFactory.getLog(WeblogPageRequest.class); 49 50 private static final String PAGE_SERVLET = "/roller-ui/rendering/page"; 51 52 private String context = null; 54 private String weblogAnchor = null; 55 private String weblogPageName = null; 56 private String weblogCategoryName = null; 57 private String weblogDate = null; 58 private int pageNum = 0; 59 private Map customParams = new HashMap (); 60 61 private WeblogEntryData weblogEntry = null; 63 private Template weblogPage = null; 64 private WeblogCategoryData weblogCategory = null; 65 66 67 public WeblogPageRequest() {} 68 69 70 73 public WeblogPageRequest(HttpServletRequest request) 74 throws InvalidRequestException { 75 76 super(request); 79 80 String servlet = request.getServletPath(); 81 82 String pathInfo = this.getPathInfo(); 84 85 log.debug("parsing path "+pathInfo); 87 88 if(!isValidDestination(servlet)) { 90 throw new InvalidRequestException("invalid destination for request, "+ 91 request.getRequestURL()); 92 } 93 94 95 107 if(pathInfo != null && pathInfo.trim().length() > 0) { 108 109 String [] pathElements = pathInfo.split("/"); 111 if(pathElements.length > 1 && "category".equals(pathElements[0])) { 112 113 pathElements = pathInfo.split("/", 2); 115 this.context = pathElements[0]; 116 this.weblogCategoryName = "/"+URLUtilities.decode(pathElements[1]); 117 118 if(!this.weblogCategoryName.startsWith("/")) { 120 this.weblogCategoryName = "/"+this.weblogCategoryName; 121 } 122 123 } else if(pathElements.length == 2) { 124 125 this.context = pathElements[0]; 126 if("entry".equals(this.context)) { 127 this.weblogAnchor = URLUtilities.decode(pathElements[1]); 128 129 } else if("date".equals(this.context)) { 130 if(this.isValidDateString(pathElements[1])) { 131 this.weblogDate = pathElements[1]; 132 } else { 133 throw new InvalidRequestException("invalid date, "+ 134 request.getRequestURL()); 135 } 136 137 } else if("page".equals(this.context)) { 138 this.weblogPageName = pathElements[1]; 139 140 } else { 141 throw new InvalidRequestException("context "+this.context+ 142 "not supported, "+request.getRequestURL()); 143 } 144 145 } else { 146 throw new InvalidRequestException("bad path info, "+ 147 request.getRequestURL()); 148 } 149 150 } else { 151 } 153 154 155 168 if(pathInfo == null || this.weblogPageName != null) { 169 170 if(request.getParameter("entry") != null) { 172 String anchor = request.getParameter("entry"); 173 if(StringUtils.isNotEmpty(anchor)) { 174 this.weblogAnchor = anchor; 175 } 176 } else if(request.getParameter("anchor") != null) { 177 String anchor = request.getParameter("anchor"); 178 if(StringUtils.isNotEmpty(anchor)) { 179 this.weblogAnchor = anchor; 180 } 181 } 182 183 if(this.weblogAnchor == null) { 185 if(request.getParameter("date") != null) { 186 String date = request.getParameter("date"); 187 if(this.isValidDateString(date)) { 188 this.weblogDate = date; 189 } else { 190 throw new InvalidRequestException("invalid date, "+ 191 request.getRequestURL()); 192 } 193 } 194 195 if(request.getParameter("cat") != null) { 196 this.weblogCategoryName = 197 URLUtilities.decode(request.getParameter("cat")); 198 199 if(!this.weblogCategoryName.startsWith("/")) { 201 this.weblogCategoryName = "/"+this.weblogCategoryName; 202 } 203 } 204 } 205 } 206 207 if(request.getParameter("page") != null) { 209 String pageInt = request.getParameter("page"); 210 try { 211 this.pageNum = Integer.parseInt(pageInt); 212 } catch(NumberFormatException e) { 213 } 215 } 216 217 customParams = new HashMap (request.getParameterMap()); 220 customParams.remove("entry"); 221 customParams.remove("anchor"); 222 customParams.remove("date"); 223 customParams.remove("cat"); 224 customParams.remove("page"); 225 226 if(log.isDebugEnabled()) { 227 log.debug("context = "+this.context); 228 log.debug("weblogAnchor = "+this.weblogAnchor); 229 log.debug("weblogDate = "+this.weblogDate); 230 log.debug("weblogCategory = "+this.weblogCategoryName); 231 log.debug("weblogPage = "+this.weblogPageName); 232 log.debug("pageNum = "+this.pageNum); 233 } 234 } 235 236 237 boolean isValidDestination(String servlet) { 238 return (servlet != null && PAGE_SERVLET.equals(servlet)); 239 } 240 241 242 private boolean isValidDateString(String dateString) { 243 return (dateString != null && StringUtils.isNumeric(dateString) && 245 (dateString.length() == 6 || dateString.length() == 8)); 246 } 247 248 public String getContext() { 249 return context; 250 } 251 252 public void setContext(String context) { 253 this.context = context; 254 } 255 256 public String getWeblogAnchor() { 257 return weblogAnchor; 258 } 259 260 public void setWeblogAnchor(String weblogAnchor) { 261 this.weblogAnchor = weblogAnchor; 262 } 263 264 public String getWeblogPageName() { 265 return weblogPageName; 266 } 267 268 public void setWeblogPageName(String weblogPage) { 269 this.weblogPageName = weblogPage; 270 } 271 272 public String getWeblogCategoryName() { 273 return weblogCategoryName; 274 } 275 276 public void setWeblogCategoryName(String weblogCategory) { 277 this.weblogCategoryName = weblogCategory; 278 } 279 280 public String getWeblogDate() { 281 return weblogDate; 282 } 283 284 public void setWeblogDate(String weblogDate) { 285 this.weblogDate = weblogDate; 286 } 287 288 public int getPageNum() { 289 return pageNum; 290 } 291 292 public void setPageNum(int pageNum) { 293 this.pageNum = pageNum; 294 } 295 296 public Map getCustomParams() { 297 return customParams; 298 } 299 300 public void setCustomParams(Map customParams) { 301 this.customParams = customParams; 302 } 303 304 public WeblogEntryData getWeblogEntry() { 305 306 if(weblogEntry == null && weblogAnchor != null) { 307 try { 308 WeblogManager wmgr = RollerFactory.getRoller().getWeblogManager(); 309 weblogEntry = wmgr.getWeblogEntryByAnchor(getWeblog(), weblogAnchor); 310 } catch (RollerException ex) { 311 log.error("Error getting weblog entry "+weblogAnchor, ex); 312 } 313 } 314 315 return weblogEntry; 316 } 317 318 public void setWeblogEntry(WeblogEntryData weblogEntry) { 319 this.weblogEntry = weblogEntry; 320 } 321 322 public Template getWeblogPage() { 323 324 if(weblogPage == null && weblogPageName != null) { 325 try { 326 UserManager umgr = RollerFactory.getRoller().getUserManager(); 327 weblogPage = getWeblog().getPageByLink(weblogPageName); 328 } catch (RollerException ex) { 329 log.error("Error getting weblog page "+weblogPageName, ex); 330 } 331 } 332 333 return weblogPage; 334 } 335 336 public void setWeblogPage(WeblogTemplate weblogPage) { 337 this.weblogPage = weblogPage; 338 } 339 340 public WeblogCategoryData getWeblogCategory() { 341 342 if(weblogCategory == null && weblogCategoryName != null) { 343 try { 344 WeblogManager wmgr = RollerFactory.getRoller().getWeblogManager(); 345 weblogCategory = wmgr.getWeblogCategoryByPath(getWeblog(), weblogCategoryName); 346 } catch (RollerException ex) { 347 log.error("Error getting weblog category "+weblogCategoryName, ex); 348 } 349 } 350 351 return weblogCategory; 352 } 353 354 public void setWeblogCategory(WeblogCategoryData weblogCategory) { 355 this.weblogCategory = weblogCategory; 356 } 357 358 } 359 | Popular Tags |