1 2 package org.roller.presentation; 3 4 import java.text.ParsePosition ; 5 import java.text.SimpleDateFormat ; 6 import java.util.Calendar ; 7 import java.util.Date ; 8 9 import javax.servlet.ServletContext ; 10 import javax.servlet.ServletRequest ; 11 import javax.servlet.http.HttpServletRequest ; 12 import javax.servlet.jsp.PageContext ; 13 14 import org.apache.commons.lang.StringUtils; 15 import org.apache.commons.logging.Log; 16 import org.apache.commons.logging.LogFactory; 17 import org.roller.RollerException; 18 import org.roller.config.RollerRuntimeConfig; 19 import org.roller.model.ParsedRequest; 20 import org.roller.model.Roller; 21 import org.roller.model.UserManager; 22 import org.roller.model.WeblogManager; 23 import org.roller.pojos.BookmarkData; 24 import org.roller.pojos.FolderData; 25 import org.roller.pojos.PageData; 26 import org.roller.pojos.UserData; 27 import org.roller.pojos.WeblogCategoryData; 28 import org.roller.pojos.WeblogEntryData; 29 import org.roller.pojos.WebsiteData; 30 import org.roller.pojos.PingTargetData; 31 import org.roller.util.DateUtil; 32 import org.roller.util.Utilities; 33 34 55 public class RollerRequest implements ParsedRequest 56 { 57 59 private static Log mLogger = 60 LogFactory.getFactory().getInstance(RollerRequest.class); 61 62 private BookmarkData mBookmark; 63 private ServletContext mContext = null; 64 private Date mDate = null; 65 private String mDateString = null; 66 private String mPathInfo = null; 67 private String mPageLink = null; 68 private PageData mPage; 69 private PageContext mPageContext = null; 70 private HttpServletRequest mRequest = null; 71 private WebsiteData mWebsite; 72 private WeblogEntryData mWeblogEntry; 73 private WeblogCategoryData mWeblogCategory; 74 private boolean mIsDateSpecified = false; 75 76 private static ThreadLocal mRollerRequestTLS = new ThreadLocal (); 77 78 public static final String ANCHOR_KEY = "entry"; 79 public static final String ANCHOR_KEY_OLD = "anchor"; 80 public static final String USERNAME_KEY = "username"; 81 public static final String WEBSITEID_KEY = "websiteid"; 82 public static final String FOLDERID_KEY = "folderid"; 83 public static final String PARENTID_KEY = "parentid"; 84 public static final String NEWSFEEDID_KEY = "feedid"; 85 public static final String PAGEID_KEY = "pageid"; 86 public static final String PAGELINK_KEY = "pagelink"; 87 public static final String PINGTARGETID_KEY = "pingtargetid"; 88 public static final String EXCERPTS_KEY = "excerpts"; 89 public static final String BOOKMARKID_KEY = "bookmarkid"; 90 public static final String REFERERID_KEY = "refid"; 91 public static final String WEBLOGENTRYID_KEY = "entryid"; 92 public static final String WEBLOGENTRY_COUNT = "count"; 93 public static final String WEBLOGCATEGORYNAME_KEY = "catname"; 94 public static final String WEBLOGCATEGORYID_KEY = "catid"; 95 public static final String WEBLOGENTRIES_KEY = "entries"; 96 public static final String WEBLOGDAY_KEY = "day"; 97 public static final String WEBLOGCOMMENTID_KEY = "catid"; 98 public static final String LOGIN_COOKIE = "sessionId"; 99 100 public static final String OWNING_USER = "OWNING_USER"; 101 102 private static final String ROLLER_REQUEST = "roller_request"; 103 104 private SimpleDateFormat mFmt = DateUtil.get8charDateFormat(); 105 106 108 109 public RollerRequest( HttpServletRequest req, ServletContext ctx ) 110 throws RollerException 111 { 112 mRequest = req; 113 mContext = ctx; 114 init(); 115 } 116 117 119 public RollerRequest( ServletRequest req, ServletContext ctx ) 120 throws RollerException 121 { 122 mRequest = (HttpServletRequest )req; 123 mContext = ctx; 124 init(); 125 } 126 127 public RollerRequest( PageContext pCtx) throws RollerException 129 { 130 mRequest = (HttpServletRequest ) pCtx.getRequest(); 131 mContext = pCtx.getServletContext(); 132 mPageContext = pCtx; 133 init(); 134 } 135 136 private void init() throws RollerException 138 { 139 mRollerRequestTLS.set(this); 140 if (mRequest.getContextPath().equals("/atom")) 141 { 142 return; } 144 145 RollerContext rctx = RollerContext.getRollerContext(mContext); 147 Authenticator auth = rctx.getAuthenticator(); 148 String userName = auth.getAuthenticatedUserName(mRequest); 149 if (userName != null) 150 { 151 UserManager userMgr = getRoller().getUserManager(); 152 UserData currentUser = userMgr.getUser(userName); 153 getRoller().setUser(currentUser); 154 } 155 156 mPathInfo = mRequest.getPathInfo(); 158 mPathInfo = (mPathInfo!=null) ? mPathInfo : ""; 159 160 String [] pathInfo = StringUtils.split(mPathInfo,"/"); 161 if ( pathInfo.length > 0 ) 162 { 163 if (!"j_security_check".equals(pathInfo[0])) 165 { 166 parsePathInfo( pathInfo ); 167 return; 168 } 169 } 170 171 parseRequestParams(); 173 } 174 175 179 private void bindUser() throws RollerException 180 { 181 } 182 183 185 private void parsePathInfo( String [] pathInfo ) throws RollerException 186 { 187 try 188 { 189 UserManager userMgr = getRoller().getUserManager(); 199 mWebsite = userMgr.getWebsite(pathInfo[0]); 200 if (mWebsite != null) 201 { 202 if ( pathInfo.length == 1 ) 203 { 204 mDate = getDate(true); 206 mDateString = DateUtil.format8chars(mDate); 207 mPage = userMgr.retrievePage(mWebsite.getDefaultPageId()); 208 } 209 else if ( pathInfo.length == 2 ) 210 { 211 mDate = parseDate(pathInfo[1]); 212 if ( mDate == null ) { 214 mDate = getDate(true); 216 mDateString = DateUtil.format8chars(mDate); 217 mPageLink = pathInfo[1]; 218 mPage = userMgr.getPageByLink(mWebsite, pathInfo[1]); 219 } 220 else 221 { 222 mDateString = pathInfo[1]; 224 mPage = userMgr.retrievePage(mWebsite.getDefaultPageId()); 225 mIsDateSpecified = true; 226 } 227 } 228 else if ( pathInfo.length == 3 ) 229 { 230 mPageLink = pathInfo[1]; 231 mPage = userMgr.getPageByLink(mWebsite, pathInfo[1]); 232 233 mDate = parseDate(pathInfo[2]); 234 if ( mDate == null ) { 236 try 238 { 239 WeblogManager weblogMgr = getRoller().getWeblogManager(); 240 mWeblogEntry = weblogMgr.getWeblogEntryByAnchor( 241 mWebsite, pathInfo[2]); 242 mDate = mWeblogEntry.getPubTime(); 243 mDateString = DateUtil.format8chars(mDate); 244 } 245 catch (Exception damn) 246 { 247 mLogger.debug("Damn", damn); 249 } 250 } 251 else 252 { 253 mDateString = pathInfo[2]; 255 mIsDateSpecified = true; 256 } 257 } 258 else if ( pathInfo.length == 4 ) 259 { 260 mPageLink = pathInfo[1]; 262 mPage = userMgr.getPageByLink(mWebsite, pathInfo[1]); 263 264 mDate = parseDate(pathInfo[2]); 265 mDateString = pathInfo[2]; 266 mIsDateSpecified = true; 267 268 WeblogManager weblogMgr = getRoller().getWeblogManager(); 270 mWeblogEntry = weblogMgr.getWeblogEntryByAnchor( 271 mWebsite, pathInfo[3]); 272 } 273 } 274 } 275 catch ( Exception ignored ) 276 { 277 mLogger.debug("Exception parsing pathInfo",ignored); 278 } 279 280 if ( mWebsite==null || mDate==null || mPage==null ) 281 { 282 String msg = "Invalid pathInfo: "+StringUtils.join(pathInfo,"|"); 283 mLogger.info(msg); 284 throw new RollerException(msg); 285 } 286 } 287 288 290 private void parseRequestParams() 291 { 292 try 293 { 294 296 UserManager userMgr = getRoller().getUserManager(); 298 String userName = mRequest.getParameter(USERNAME_KEY); 299 if ( userName == null ) 300 { 301 userName = mRequest.getRemoteUser(); 303 } 304 305 if ( userName != null ) 306 { 307 mWebsite = userMgr.getWebsite(userName); 308 } 309 310 String pageId = mRequest.getParameter(RollerRequest.PAGEID_KEY); 312 if ( pageId != null ) 313 { 314 mPage = userMgr.retrievePage(pageId); 315 316 if ( mWebsite == null ) 318 { 319 mWebsite = mPage.getWebsite(); 320 } 321 } 322 else if (mWebsite != null) 323 { 324 mPage = userMgr.retrievePage( mWebsite.getDefaultPageId() ); 325 } 326 327 String daystr = mRequest.getParameter(WEBLOGDAY_KEY); 329 if ( daystr != null ) 330 { 331 mDate = parseDate(daystr); 332 if ( mDate != null ) 333 { 334 mDateString = daystr; 336 mIsDateSpecified = true; 337 } 338 } 339 } 340 catch ( Exception ignored ) 341 { 342 mLogger.debug("Exception parsing request params",ignored); 343 } 344 } 345 346 348 public PageContext getPageContext() 349 { 350 return mPageContext; 351 } 352 353 public void setPageContext(PageContext p) 354 { 355 mPageContext = p; 356 } 357 358 360 public HttpServletRequest getRequest() 361 { 362 return mRequest; 363 } 364 365 370 public static RollerRequest getRollerRequest( 371 HttpServletRequest r, ServletContext ctx ) throws RollerException 372 { 373 RollerRequest ret= (RollerRequest)r.getAttribute(ROLLER_REQUEST); 374 if ( ret == null ) 375 { 376 ret = new RollerRequest(r,ctx); 377 r.setAttribute( ROLLER_REQUEST, ret ); 378 } 379 return ret; 380 } 381 382 387 public static RollerRequest getRollerRequest( HttpServletRequest r ) 388 { 389 try 390 { 391 return getRollerRequest(r, RollerContext.getServletContext()); 392 } 393 catch (Exception e) 394 { 395 mLogger.debug("Unable to create a RollerRequest", e); 396 } 397 return null; 398 } 399 400 405 public static RollerRequest getRollerRequest( PageContext p ) 406 { 407 HttpServletRequest r = (HttpServletRequest )p.getRequest(); 408 RollerRequest ret = (RollerRequest)r.getAttribute(ROLLER_REQUEST); 409 if (ret == null) 410 { 411 try 412 { 413 ret = new RollerRequest( p ); 414 r.setAttribute( ROLLER_REQUEST, ret ); 415 } 416 catch (Exception e) 417 { 418 mLogger.debug("Unable to create a RollerRequest", e); 419 } 420 } 421 else 422 { 423 ret.setPageContext( p ); 424 } 425 return ret; 426 } 427 428 434 public static RollerRequest getRollerRequest() 435 { 436 return (RollerRequest)mRollerRequestTLS.get(); 437 } 438 439 444 public ServletContext getServletContext() 445 { 446 return mContext; 447 } 448 449 451 public Roller getRoller() 452 { 453 return RollerContext.getRoller( mRequest ); 454 } 455 456 458 public boolean isAdminUser() throws RollerException 459 { 460 UserData user = getUser(); 461 if (user != null && user.hasRole("admin")) 462 { 463 return true; 464 } 465 else 466 { 467 return false; 468 } 469 } 470 471 473 public boolean isUserAuthorizedToEdit() throws RollerException 474 { 475 478 RollerContext rctx = RollerContext.getRollerContext(mContext); 479 Authenticator auth = rctx.getAuthenticator(); 480 481 String userName = auth.getAuthenticatedUserName(mRequest); 482 483 UserData owningUser = null; 485 if (mRequest.getAttribute(OWNING_USER) != null) 486 { 487 owningUser = (UserData)mRequest.getAttribute(OWNING_USER); 488 } 489 else 490 { 491 owningUser = getUser(); 492 } 493 494 if ( userName != null 495 && owningUser != null 496 && userName.equalsIgnoreCase( owningUser.getUserName() ) 497 && auth.isAuthenticatedUserInRole(mRequest,"editor")) 498 { 499 return true; 500 } 501 else 502 { 503 return false; 504 } 505 } 506 507 511 public UserData getUser( String userName ) throws Exception 512 { 513 return getRoller().getUserManager().getUser(userName); 514 } 515 516 518 public boolean isDateSpecified() 519 { 520 return mIsDateSpecified; 521 } 522 523 525 public int getWeblogEntryCount() 526 { 527 int count = 20; 529 if ( mRequest.getParameter("count") != null ) 530 { 531 count= Utilities.stringToInt(mRequest.getParameter("count")); 532 if ( count==0 || count>50 ) 533 { 534 count = 20; 535 } 536 } 537 return count; 538 } 539 540 542 546 public Date getDate() 547 { 548 return getDate(false); 549 } 550 551 557 public Date getDate( boolean orToday ) 558 { 559 Date ret = null; 560 if ( mDate != null ) 561 { 562 ret = mDate; 563 } 564 else if ( orToday ) 565 { 566 ret = getToday(); 567 } 568 return ret; 569 } 570 571 575 private Date getToday() 576 { 577 Calendar todayCal = Calendar.getInstance(); 578 if (this.getWebsite() != null) 579 { 580 todayCal = Calendar.getInstance( 581 this.getWebsite().getTimeZoneInstance(), 582 this.getWebsite().getLocaleInstance()); 583 } 584 todayCal.setTime( new Date () ); 585 return todayCal.getTime(); 586 } 587 588 593 public String getDateString() 594 { 595 return getDateString(false); 596 } 597 598 604 public String getDateString( boolean orToday ) 605 { 606 String ret = null; 607 if ( mDateString != null ) 608 { 609 ret = mDateString; 610 } 611 else if ( orToday ) 612 { 613 Calendar todayCal = Calendar.getInstance(); 614 if (this.getWebsite() != null) 615 { 616 todayCal = Calendar.getInstance( 617 this.getWebsite().getTimeZoneInstance(), 618 this.getWebsite().getLocaleInstance()); 619 } 620 todayCal.setTime( new Date () ); 621 ret = mFmt.format(todayCal.getTime()); 622 } 623 return ret; 624 } 625 626 631 public String getPathInfo() 632 { 633 return mPathInfo; 634 } 635 636 641 public String getPageLink() 642 { 643 return mPageLink; 644 } 645 646 651 public BookmarkData getBookmark( ) 652 { 653 if ( mBookmark == null ) 654 { 655 String id = getFromRequest(BOOKMARKID_KEY); 656 if ( id != null ) 657 { 658 try 659 { 660 mBookmark = 661 getRoller().getBookmarkManager().retrieveBookmark(id); 662 } 663 catch (RollerException e) 664 { 665 mLogger.error("Getting bookmark from request",e); 666 } 667 } 668 } 669 return mBookmark; 670 } 671 672 677 public WeblogCategoryData getWeblogCategory() 678 { 679 if ( mWeblogCategory == null ) 680 { 681 String id = getFromRequest(WEBLOGCATEGORYID_KEY); 682 if ( id != null ) 683 { 684 try 685 { 686 mWeblogCategory = 687 getRoller().getWeblogManager().retrieveWeblogCategory(id); 688 } 689 catch (RollerException e) 690 { 691 mLogger.error("Getting weblog category by id from request",e); 692 } 693 } 694 else if (StringUtils.isNotEmpty(id = getFromRequest(WEBLOGCATEGORYNAME_KEY))) 695 { 696 try 697 { 698 mWeblogCategory = 699 getRoller().getWeblogManager().getWeblogCategoryByPath( 700 getWebsite(), null, id); 701 } 702 catch (RollerException e) 703 { 704 mLogger.error("Getting weblog category by name from request",e); 705 } 706 } 707 } 708 return mWeblogCategory; 709 } 710 711 716 public FolderData getFolder( ) 717 { 718 FolderData folder = null; 719 String id = getFromRequest(FOLDERID_KEY); 722 if ( id != null ) 723 { 724 try 725 { 726 folder = 727 getRoller().getBookmarkManager().retrieveFolder(id); 728 } 729 catch (RollerException e) 730 { 731 mLogger.error("Getting folder from request",e); 732 } 733 } 734 return folder; 736 } 737 738 743 public PageData getPage() 744 { 745 if (mPage == null) 746 { 747 String id = getFromRequest(PAGEID_KEY); 748 if ( id != null ) 749 { 750 try 751 { 752 mPage = getRoller().getUserManager().retrievePage(id); 753 } 754 catch (RollerException e) 755 { 756 mLogger.error("Getting page from request",e); 757 } 758 } 759 } 760 return mPage; 761 } 762 763 766 public void setPage(PageData page) 767 { 768 mPage = page; 769 } 770 771 776 public String getRequestURL( ) 777 { 778 return mRequest.getRequestURL().toString(); 779 } 780 781 783 787 public String getRefererURL( ) 788 { 789 return mRequest.getHeader("referer"); 790 } 791 792 796 public UserData getUser() 797 { 798 if (mWebsite != null) return mWebsite.getUser(); 799 return null; 800 } 801 802 806 public WebsiteData getWebsite() 807 { 808 return mWebsite; 809 } 810 public void setWebsite(WebsiteData wd) 811 { 812 mWebsite = wd; 813 } 814 815 828 public WeblogEntryData getWeblogEntry( ) 829 { 830 if ( mWeblogEntry == null ) 831 { 832 String anchor = mRequest.getParameter(ANCHOR_KEY); 834 if (anchor == null) anchor = mRequest.getParameter(ANCHOR_KEY_OLD); 835 String entryid = mRequest.getParameter(WEBLOGENTRYID_KEY); 836 if (entryid == null) 837 { 838 entryid = (String )mRequest.getAttribute(WEBLOGENTRYID_KEY); 839 } 840 try 841 { 842 if ( entryid != null ) 843 { 844 WeblogManager weblogMgr = getRoller().getWeblogManager(); 845 mWeblogEntry = weblogMgr.retrieveWeblogEntry(entryid); 846 847 if ( mWeblogEntry != null && mWebsite == null ) 849 { 850 mWebsite = mWeblogEntry.getWebsite(); 851 } 852 } 853 else if ( mWebsite != null && anchor != null ) 854 { 855 WeblogManager weblogMgr = getRoller().getWeblogManager(); 856 mWeblogEntry = weblogMgr.getWeblogEntryByAnchor( 857 mWebsite,anchor); 858 } 859 } 860 catch (RollerException e) 861 { 862 mLogger.error("EXCEPTION getting weblog entry",e); 863 mLogger.error("user=" + mWebsite.getUser()); 864 mLogger.error("anchor=" + anchor); 865 mLogger.error("entryid=" + entryid); 866 } 867 } 868 return mWeblogEntry; 869 } 870 871 873 874 private String getFromRequest( String key ) 875 { 876 String ret = (String )mRequest.getAttribute( key ); 877 if ( ret == null ) 878 { 879 ret = mRequest.getParameter( key ); 880 if (ret == null && mRequest.getSession(false) != null) 881 { 882 ret = (String )mRequest.getSession().getAttribute( key ); 883 } 884 } 885 return ret; 886 } 887 888 890 private Date parseDate( String dateString ) 891 { 892 Date ret = null; 893 if ( dateString!=null 894 && dateString.length()==8 895 && StringUtils.isNumeric(dateString) ) 896 { 897 ParsePosition pos = new ParsePosition (0); 898 ret = mFmt.parse( dateString, pos ); 899 900 Date today = getToday(); 902 if (ret.after(today)) ret = today; 903 904 ret = DateUtil.getEndOfDay(ret); 906 } 907 return ret; 908 } 909 910 912 public String toString() 913 { 914 StringBuffer sb = new StringBuffer (); 915 sb.append("["); 916 sb.append(getRequestURL()); 917 sb.append(", "); 918 sb.append(getRefererURL()); 919 sb.append("]"); 920 return sb.toString(); 921 } 922 923 925 928 public boolean isEnableLinkback() 929 { 930 return RollerRuntimeConfig.getBooleanProperty("site.linkbacks.enabled"); 931 } 932 } 933 934 | Popular Tags |