1 18 19 package org.apache.roller.ui.core; 20 21 import java.text.ParsePosition ; 22 import java.text.SimpleDateFormat ; 23 import java.util.Calendar ; 24 import java.util.Date ; 25 26 import javax.servlet.ServletContext ; 27 import javax.servlet.ServletRequest ; 28 import javax.servlet.http.HttpServletRequest ; 29 import javax.servlet.jsp.PageContext ; 30 31 import org.apache.commons.lang.StringUtils; 32 import org.apache.commons.logging.Log; 33 import org.apache.commons.logging.LogFactory; 34 import org.apache.roller.RollerException; 35 import org.apache.roller.config.RollerRuntimeConfig; 36 import org.apache.roller.model.Roller; 37 import org.apache.roller.model.RollerFactory; 38 import org.apache.roller.pojos.Template; 39 import org.apache.roller.model.UserManager; 40 import org.apache.roller.model.WeblogManager; 41 import org.apache.roller.pojos.BookmarkData; 42 import org.apache.roller.pojos.FolderData; 43 import org.apache.roller.pojos.UserData; 44 import org.apache.roller.pojos.WeblogCategoryData; 45 import org.apache.roller.pojos.WeblogEntryData; 46 import org.apache.roller.pojos.WebsiteData; 47 import org.apache.roller.util.DateUtil; 48 import org.apache.roller.util.Utilities; 49 50 71 public class RollerRequest 72 { 73 75 private static Log mLogger = 76 LogFactory.getFactory().getInstance(RollerRequest.class); 77 78 private BookmarkData mBookmark; 79 private ServletContext mContext = null; 80 private Date mDate = null; 81 private String mDateString = null; 82 private String mPathInfo = null; 83 private String mPageLink = null; 84 private Template mPage; 85 private PageContext mPageContext = null; 86 private HttpServletRequest mRequest = null; 87 private WebsiteData mWebsite; 88 private WeblogEntryData mWeblogEntry; 89 private WeblogCategoryData mWeblogCategory; 90 91 private boolean mIsDaySpecified = false; 92 private boolean mIsMonthSpecified = false; 93 94 private static ThreadLocal mRollerRequestTLS = new ThreadLocal (); 95 96 public static final String ROLLER_REQUEST = "roller_request"; 97 98 private SimpleDateFormat m8charDateFormat = DateUtil.get8charDateFormat(); 99 private SimpleDateFormat m6charDateFormat = DateUtil.get6charDateFormat(); 100 101 103 104 public RollerRequest( HttpServletRequest req, ServletContext ctx ) 105 throws RollerException 106 { 107 mRequest = req; 108 mContext = ctx; 109 init(); 110 } 111 112 113 public RollerRequest( ServletRequest req, ServletContext ctx ) 114 throws RollerException 115 { 116 mRequest = (HttpServletRequest )req; 117 mContext = ctx; 118 init(); 119 } 120 121 public RollerRequest( PageContext pCtx) throws RollerException 122 { 123 mRequest = (HttpServletRequest ) pCtx.getRequest(); 124 mContext = pCtx.getServletContext(); 125 mPageContext = pCtx; 126 init(); 127 } 128 129 private void init() throws RollerException 130 { 131 mRollerRequestTLS.set(this); 132 if (mRequest.getContextPath().equals("/atom")) 133 { 134 return; } 136 137 RollerContext rctx = RollerContext.getRollerContext(); 139 Authenticator auth = rctx.getAuthenticator(); 140 String userName = auth.getAuthenticatedUserName(mRequest); 141 if (userName != null) 142 { 143 UserManager userMgr = RollerFactory.getRoller().getUserManager(); 144 UserData currentUser = userMgr.getUserByUserName(userName); 145 } 148 149 mPathInfo = mRequest.getPathInfo(); 151 mPathInfo = (mPathInfo!=null) ? mPathInfo : ""; 152 153 String [] pathInfo = StringUtils.split(mPathInfo,"/"); 154 if ( pathInfo.length > 0 ) 155 { 156 if (!"j_security_check".equals(pathInfo[0])) 158 { 159 parsePathInfo( pathInfo ); 160 return; 161 } 162 } 163 164 parseRequestParams(); 166 } 167 168 public String toString() 169 { 170 StringBuffer sb = new StringBuffer (); 171 sb.append("["); 172 sb.append(getRequestURL()); 173 sb.append(", "); 174 sb.append(getRefererURL()); 175 sb.append("]"); 176 return sb.toString(); 177 } 178 179 183 private void bindUser() throws RollerException 184 { 185 } 186 187 189 private void parsePathInfo( String [] pathInfo ) throws RollerException 190 { 191 try 192 { 193 Roller roller = RollerFactory.getRoller(); 203 UserManager userMgr = roller.getUserManager(); 204 mWebsite = userMgr.getWebsiteByHandle(pathInfo[0]); 205 if (mWebsite != null) 206 { 207 if ( pathInfo.length == 1 ) 208 { 209 mPage = mWebsite.getDefaultPage(); 211 } 212 else if ( pathInfo.length == 2 ) 213 { 214 mDate = parseDate(pathInfo[1]); 215 if ( mDate == null ) { 217 mPageLink = pathInfo[1]; 219 mPage = mWebsite.getPageByLink(pathInfo[1]); 220 } 221 else 222 { 223 mDateString = pathInfo[1]; 225 mPage = mWebsite.getDefaultPage(); 226 if (pathInfo[1].length() == 8) { 227 mIsDaySpecified = true; 228 } else { 229 mIsMonthSpecified = true; 230 } 231 } 232 } 233 else if ( pathInfo.length == 3 ) 234 { 235 mPageLink = pathInfo[1]; 236 mPage = mWebsite.getPageByLink(pathInfo[1]); 237 238 mDate = parseDate(pathInfo[2]); 239 if ( mDate == null ) { 241 try 243 { 244 WeblogManager weblogMgr = roller.getWeblogManager(); 245 mWeblogEntry = weblogMgr.getWeblogEntryByAnchor( 246 mWebsite, pathInfo[2]); 247 } 248 catch (Exception e) 249 { 250 mLogger.debug("Can't find entry by anchor", e); 251 } 252 } 253 else 254 { 255 mDateString = pathInfo[2]; 257 if (pathInfo[1].length() == 8) { 258 mIsDaySpecified = true; 259 } else { 260 mIsMonthSpecified = true; 261 } 262 } 263 } 264 else if ( pathInfo.length == 4 ) 265 { 266 mPageLink = pathInfo[1]; 268 mPage = mWebsite.getPageByLink(pathInfo[1]); 269 270 mDate = parseDate(pathInfo[2]); 271 mDateString = pathInfo[2]; 272 if (pathInfo[1].length() == 8) { 273 mIsDaySpecified = true; 274 } else { 275 mIsMonthSpecified = true; 276 } 277 278 WeblogManager weblogMgr = RollerFactory.getRoller().getWeblogManager(); 280 mWeblogEntry = weblogMgr.getWeblogEntryByAnchor( 281 mWebsite, pathInfo[3]); 282 } 283 } 284 if (mDate == null && mWeblogEntry != null) 285 { 286 mDate = mWeblogEntry.getPubTime(); 287 } 289 } 290 catch ( Exception ignored ) 291 { 292 mLogger.debug("Exception parsing pathInfo",ignored); 293 } 294 295 if ( mWebsite==null || mPage==null ) 297 { 298 String msg = "Invalid pathInfo: "+StringUtils.join(pathInfo,"|"); 299 throw new RollerException(msg); 300 } 301 } 302 303 305 private void parseRequestParams() 306 { 307 try 308 { 309 311 UserManager userMgr = RollerFactory.getRoller().getUserManager(); 313 String userName = mRequest.getParameter(RequestConstants.USERNAME); 314 if ( userName == null ) 315 { 316 userName = mRequest.getRemoteUser(); 318 } 319 320 String handle = mRequest.getParameter(RequestConstants.WEBLOG); 321 String websiteid = mRequest.getParameter(RequestConstants.WEBLOG_ID); 322 if (handle != null && mWebsite == null) 323 { 324 mWebsite = userMgr.getWebsiteByHandle(handle); 325 } 326 else if (websiteid != null && mWebsite == null ) 327 { 328 mWebsite = userMgr.getWebsite(websiteid); 329 } 330 331 String pageId = mRequest.getParameter(RequestConstants.PAGE_ID); 333 if ( pageId != null ) 334 { 335 mPage = userMgr.getPage(pageId); 336 } 337 else if (mWebsite != null) 338 { 339 mPage = mWebsite.getDefaultPage(); 340 } 341 342 } 356 catch ( Exception ignored ) 357 { 358 mLogger.debug("Exception parsing request params",ignored); 359 } 360 } 361 362 364 public PageContext getPageContext() 365 { 366 return mPageContext; 367 } 368 369 public void setPageContext(PageContext p) 370 { 371 mPageContext = p; 372 } 373 374 376 public HttpServletRequest getRequest() 377 { 378 return mRequest; 379 } 380 381 386 public static RollerRequest getRollerRequest( 387 HttpServletRequest r, ServletContext ctx ) throws RollerException 388 { 389 RollerRequest ret= (RollerRequest)r.getAttribute(ROLLER_REQUEST); 390 if ( ret == null ) 391 { 392 ret = new RollerRequest(r,ctx); 393 r.setAttribute( ROLLER_REQUEST, ret ); 394 } 395 return ret; 396 } 397 398 403 public static RollerRequest getRollerRequest( HttpServletRequest r ) 404 { 405 try 406 { 407 return getRollerRequest(r, RollerContext.getServletContext()); 408 } 409 catch (Exception e) 410 { 411 mLogger.debug("Unable to create a RollerRequest", e); 412 } 413 return null; 414 } 415 416 421 public static RollerRequest getRollerRequest( PageContext p ) 422 throws RollerException { 423 424 HttpServletRequest r = (HttpServletRequest )p.getRequest(); 425 RollerRequest ret = (RollerRequest)r.getAttribute(ROLLER_REQUEST); 426 if (ret == null) 427 { 428 ret = new RollerRequest( p ); 429 r.setAttribute( ROLLER_REQUEST, ret ); 430 } 431 else 432 { 433 ret.setPageContext( p ); 434 } 435 return ret; 436 } 437 438 444 public static RollerRequest getRollerRequest() 445 { 446 return (RollerRequest)mRollerRequestTLS.get(); 447 } 448 449 454 public ServletContext getServletContext() 455 { 456 return mContext; 457 } 458 459 461 public boolean isDaySpecified() 462 { 463 return mIsDaySpecified; 464 } 465 466 public boolean isMonthSpecified() 467 { 468 return mIsMonthSpecified; 469 } 470 471 473 477 public Date getDate() 478 { 479 return getDate(false); 480 } 481 482 487 public Date getDate( boolean orToday ) 488 { 489 Date ret = null; 490 if ( mDate != null ) 491 { 492 ret = mDate; 493 } 494 else if ( orToday ) 495 { 496 ret = getToday(); 497 } 498 return ret; 499 } 500 501 505 private Date getToday() 506 { 507 Calendar todayCal = Calendar.getInstance(); 508 if (this.getWebsite() != null) 509 { 510 todayCal = Calendar.getInstance( 511 this.getWebsite().getTimeZoneInstance(), 512 this.getWebsite().getLocaleInstance()); 513 } 514 todayCal.setTime( new Date () ); 515 return todayCal.getTime(); 516 } 517 518 522 public String getDateString1() 523 { 524 return mDateString; 525 } 526 527 532 public String getPathInfo() 533 { 534 return mPathInfo; 535 } 536 537 542 public String getPageLink() 543 { 544 return mPageLink; 545 } 546 547 public int getWeblogEntryCount() 548 { 549 int count = 20; 551 if ( mRequest.getParameter("count") != null ) 552 { 553 count= Utilities.stringToInt(mRequest.getParameter("count")); 554 if ( count==0 || count>50 ) 555 { 556 count = 20; 557 } 558 } 559 return count; 560 } 561 562 567 public BookmarkData getBookmark( ) 568 { 569 if ( mBookmark == null ) 570 { 571 String id = getFromRequest(RequestConstants.BOOKMARK_ID); 572 if ( id != null ) 573 { 574 try 575 { 576 mBookmark = RollerFactory.getRoller() 577 .getBookmarkManager().getBookmark(id); 578 } 579 catch (RollerException e) 580 { 581 mLogger.error("Getting bookmark from request",e); 582 } 583 } 584 } 585 return mBookmark; 586 } 587 588 593 public WeblogCategoryData getWeblogCategory() 594 { 595 if ( mWeblogCategory == null ) 596 { 597 String id = getFromRequest(RequestConstants.WEBLOGCATEGORY_ID); 598 if ( id != null ) 599 { 600 try 601 { 602 mWeblogCategory = 603 RollerFactory.getRoller() 604 .getWeblogManager().getWeblogCategory(id); 605 } 606 catch (RollerException e) 607 { 608 mLogger.error("Getting weblog category by id from request",e); 609 } 610 } 611 else if (StringUtils.isNotEmpty(id = getFromRequest(RequestConstants.WEBLOGCATEGORY))) 612 { 613 try 614 { 615 mWeblogCategory = 616 RollerFactory.getRoller() 617 .getWeblogManager().getWeblogCategoryByPath( 618 getWebsite(), null, id); 619 } 620 catch (RollerException e) 621 { 622 mLogger.error("Getting weblog category by name from request",e); 623 } 624 } 625 } 626 return mWeblogCategory; 627 } 628 629 634 public FolderData getFolder( ) 635 { 636 FolderData folder = null; 637 String id = getFromRequest(RequestConstants.FOLDER_ID); 640 if ( id != null ) 641 { 642 try 643 { 644 folder = RollerFactory.getRoller() 645 .getBookmarkManager().getFolder(id); 646 } 647 catch (RollerException e) 648 { 649 mLogger.error("Getting folder from request",e); 650 } 651 } 652 return folder; 654 } 655 656 661 public Template getPage() 662 { 663 if (mPage == null) 664 { 665 String id = getFromRequest(RequestConstants.PAGE_ID); 666 if ( id != null ) 667 { 668 try 669 { 670 mPage = RollerFactory.getRoller() 671 .getUserManager().getPage(id); 672 } 673 catch (RollerException e) 674 { 675 mLogger.error("Getting page from request",e); 676 } 677 } 678 } 679 return mPage; 680 } 681 682 685 public void setPage(org.apache.roller.pojos.Template page) 686 { 687 mPage = page; 688 } 689 690 695 public String getRequestURL( ) 696 { 697 return mRequest.getRequestURL().toString(); 698 } 699 700 702 706 public String getRefererURL( ) 707 { 708 return mRequest.getHeader("referer"); 709 } 710 711 717 public WebsiteData getWebsite() 718 { 719 return mWebsite; 720 } 721 public void setWebsite(WebsiteData wd) 722 { 723 mWebsite = wd; 724 } 725 726 739 public WeblogEntryData getWeblogEntry( ) 740 { 741 if ( mWeblogEntry == null ) 742 { 743 String anchor = mRequest.getParameter(RequestConstants.ANCHOR); 745 if (anchor == null) anchor = mRequest.getParameter(RequestConstants.ANCHOR); 746 String entryid = mRequest.getParameter(RequestConstants.WEBLOGENTRY_ID); 747 if (entryid == null) 748 { 749 entryid = (String )mRequest.getAttribute(RequestConstants.WEBLOGENTRY_ID); 750 } 751 try 752 { 753 if ( entryid != null ) 754 { 755 WeblogManager weblogMgr = RollerFactory.getRoller().getWeblogManager(); 756 mWeblogEntry = weblogMgr.getWeblogEntry(entryid); 757 758 if ( mWeblogEntry != null && mWebsite == null ) 760 { 761 mWebsite = mWeblogEntry.getWebsite(); 762 } 763 } 764 else if ( mWebsite != null && anchor != null ) 765 { 766 WeblogManager weblogMgr = 767 RollerFactory.getRoller().getWeblogManager(); 768 mWeblogEntry = weblogMgr.getWeblogEntryByAnchor( 769 mWebsite,anchor); 770 } 771 } 772 catch (RollerException e) 773 { 774 mLogger.error("EXCEPTION getting weblog entry",e); 775 mLogger.error("anchor=" + anchor); 776 mLogger.error("entryid=" + entryid); 777 } 778 if (mDate == null && mWeblogEntry != null) 779 { 780 mDate = mWeblogEntry.getPubTime(); 781 mDateString = DateUtil.format8chars(mDate); 782 } 783 } 784 return mWeblogEntry; 785 } 786 787 789 790 private String getFromRequest( String key ) 791 { 792 String ret = (String )mRequest.getAttribute( key ); 793 if ( ret == null ) 794 { 795 ret = mRequest.getParameter( key ); 796 if (ret == null && mRequest.getSession(false) != null) 797 { 798 ret = (String )mRequest.getSession().getAttribute( key ); 799 } 800 } 801 return ret; 802 } 803 804 private Date parseDate( String dateString ) 805 { 806 Date ret = null; 807 if ( dateString!=null 808 && dateString.length()==8 809 && StringUtils.isNumeric(dateString) ) 810 { 811 ParsePosition pos = new ParsePosition (0); 812 ret = m8charDateFormat.parse( dateString, pos ); 813 814 Date today = getToday(); 816 if (ret.after(today)) ret = today; 817 818 } 821 if ( dateString!=null 822 && dateString.length()==6 823 && StringUtils.isNumeric(dateString) ) 824 { 825 ParsePosition pos = new ParsePosition (0); 826 ret = m6charDateFormat.parse( dateString, pos ); 827 828 Date today = getToday(); 830 if (ret.after(today)) ret = today; 831 832 } 835 return ret; 836 } 837 838 841 public boolean isEnableLinkback() 842 { 843 return RollerRuntimeConfig.getBooleanProperty("site.linkbacks.enabled"); 844 } 845 846 } 847 848 | Popular Tags |