KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > presentation > RollerRequest


1
2 package org.roller.presentation;
3
4 import java.text.ParsePosition JavaDoc;
5 import java.text.SimpleDateFormat JavaDoc;
6 import java.util.Calendar JavaDoc;
7 import java.util.Date JavaDoc;
8
9 import javax.servlet.ServletContext JavaDoc;
10 import javax.servlet.ServletRequest JavaDoc;
11 import javax.servlet.http.HttpServletRequest JavaDoc;
12 import javax.servlet.jsp.PageContext JavaDoc;
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 //////////////////////////////////////////////////////////////////////////////
35
/**
36  * Access to objects and values specified by request. Parses out arguments from
37  * request URL needed for various parts of Roller and makes them available via
38  * getter methods.
39  * <br/><br/>
40  *
41  * These forms of pathinfo get special support:
42  * <br/><br/>
43  *
44  * <pre>
45  * [username] - get default page for user for today's date
46  * [username]/[date] - get default page for user for specified date
47  * [username]/[pagelink] - get specified page for today's date
48  * [username]/[pagelink]/[date] - get specified page for specified date
49  * [username]/[pagelink]/[anchor] - get specified page & entry (by anchor)
50  * [username]/[pagelink]/[date]/[anchor] - get specified page & entry (by anchor)
51  * </pre>
52  *
53  * @author David M Johnson
54  */

55 public class RollerRequest implements ParsedRequest
56 {
57     //----------------------------------------------------------------- Fields
58

59     private static Log mLogger =
60         LogFactory.getFactory().getInstance(RollerRequest.class);
61
62     private BookmarkData mBookmark;
63     private ServletContext JavaDoc mContext = null;
64     private Date JavaDoc mDate = null;
65     private String JavaDoc mDateString = null;
66     private String JavaDoc mPathInfo = null;
67     private String JavaDoc mPageLink = null;
68     private PageData mPage;
69     private PageContext JavaDoc mPageContext = null;
70     private HttpServletRequest JavaDoc mRequest = null;
71     private WebsiteData mWebsite;
72     private WeblogEntryData mWeblogEntry;
73     private WeblogCategoryData mWeblogCategory;
74     private boolean mIsDateSpecified = false;
75         
76     private static ThreadLocal JavaDoc mRollerRequestTLS = new ThreadLocal JavaDoc();
77     
78     public static final String JavaDoc ANCHOR_KEY = "entry";
79     public static final String JavaDoc ANCHOR_KEY_OLD = "anchor";
80     public static final String JavaDoc USERNAME_KEY = "username";
81     public static final String JavaDoc WEBSITEID_KEY = "websiteid";
82     public static final String JavaDoc FOLDERID_KEY = "folderid";
83     public static final String JavaDoc PARENTID_KEY = "parentid";
84     public static final String JavaDoc NEWSFEEDID_KEY = "feedid";
85     public static final String JavaDoc PAGEID_KEY = "pageid";
86     public static final String JavaDoc PAGELINK_KEY = "pagelink";
87     public static final String JavaDoc PINGTARGETID_KEY = "pingtargetid";
88     public static final String JavaDoc EXCERPTS_KEY = "excerpts";
89     public static final String JavaDoc BOOKMARKID_KEY = "bookmarkid";
90     public static final String JavaDoc REFERERID_KEY = "refid";
91     public static final String JavaDoc WEBLOGENTRYID_KEY = "entryid";
92     public static final String JavaDoc WEBLOGENTRY_COUNT = "count";
93     public static final String JavaDoc WEBLOGCATEGORYNAME_KEY = "catname";
94     public static final String JavaDoc WEBLOGCATEGORYID_KEY = "catid";
95     public static final String JavaDoc WEBLOGENTRIES_KEY = "entries";
96     public static final String JavaDoc WEBLOGDAY_KEY = "day";
97     public static final String JavaDoc WEBLOGCOMMENTID_KEY = "catid";
98     public static final String JavaDoc LOGIN_COOKIE = "sessionId";
99     
100     public static final String JavaDoc OWNING_USER = "OWNING_USER";
101     
102     private static final String JavaDoc ROLLER_REQUEST = "roller_request";
103     
104     private SimpleDateFormat JavaDoc mFmt = DateUtil.get8charDateFormat();
105
106     //----------------------------------------------------------- Constructors
107

108     /** Construct Roller request for a Servlet request */
109     public RollerRequest( HttpServletRequest JavaDoc req, ServletContext JavaDoc ctx )
110         throws RollerException
111     {
112         mRequest = req;
113         mContext = ctx;
114         init();
115     }
116     
117     //------------------------------------------------------------------------
118
/** Convenience */
119     public RollerRequest( ServletRequest JavaDoc req, ServletContext JavaDoc ctx )
120         throws RollerException
121     {
122         mRequest = (HttpServletRequest JavaDoc)req;
123         mContext = ctx;
124         init();
125     }
126     
127     //------------------------------------------------------------------------
128
public RollerRequest( PageContext JavaDoc pCtx) throws RollerException
129     {
130         mRequest = (HttpServletRequest JavaDoc) pCtx.getRequest();
131         mContext = pCtx.getServletContext();
132         mPageContext = pCtx;
133         init();
134     }
135
136     //------------------------------------------------------------------------
137
private void init() throws RollerException
138     {
139         mRollerRequestTLS.set(this);
140         if (mRequest.getContextPath().equals("/atom"))
141         {
142             return; // Atom servlet request needs no init
143
}
144         
145         // Bind persistence session to authenticated user, if we have one
146
RollerContext rctx = RollerContext.getRollerContext(mContext);
147         Authenticator auth = rctx.getAuthenticator();
148         String JavaDoc 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         // path info may be null, (e.g. on JSP error page)
157
mPathInfo = mRequest.getPathInfo();
158         mPathInfo = (mPathInfo!=null) ? mPathInfo : "";
159         
160         String JavaDoc[] pathInfo = StringUtils.split(mPathInfo,"/");
161         if ( pathInfo.length > 0 )
162         {
163             // Parse pathInfo and throw exception if it is invalid
164
if (!"j_security_check".equals(pathInfo[0]))
165             {
166                 parsePathInfo( pathInfo );
167                 return;
168             }
169         }
170
171         // Parse user, page, and entry from request params if possible
172
parseRequestParams();
173     }
174     
175     //------------------------------------------------------------------------
176
/**
177      * Bind persistence session to specific user.
178      */

179     private void bindUser() throws RollerException
180     {
181     }
182     
183     //------------------------------------------------------------------------
184
/** Parse pathInfo and throw exception if it is invalid */
185     private void parsePathInfo( String JavaDoc[] pathInfo ) throws RollerException
186     {
187         try
188         {
189             // If there is any path info, it must be in one of the
190
// below formats or the request is considered to be invalid.
191
//
192
// /username
193
// /username/datestring
194
// /username/pagelink
195
// /username/pagelink/datestring
196
// /username/pagelink/anchor (specific entry)
197
// /username/pagelink/datestring/anchor (specific entry)
198
UserManager userMgr = getRoller().getUserManager();
199             mWebsite = userMgr.getWebsite(pathInfo[0]);
200             if (mWebsite != null)
201             {
202                 if ( pathInfo.length == 1 )
203                 {
204                     // we have the /username form of URL
205
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 ) // pre-jdk1.4 --> || mDate.getYear() <= 70 )
213
{
214                         // we have the /username/pagelink form of URL
215
mDate = getDate(true);
216                         mDateString = DateUtil.format8chars(mDate);
217                         mPageLink = pathInfo[1];
218                         mPage = userMgr.getPageByLink(mWebsite, pathInfo[1]);
219                     }
220                     else
221                     {
222                         // we have the /username/datestring form of URL
223
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 ) // pre-jdk1.4 --> || mDate.getYear() <= 70 )
235
{
236                         // we have the /username/pagelink/anchor form of URL
237
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 JavaDoc damn)
246                         {
247                             // doesn't really matter, we've got the Page anyway
248
mLogger.debug("Damn", damn);
249                         }
250                     }
251                     else
252                     {
253                         // we have the /username/pagelink/datestring form of URL
254
mDateString = pathInfo[2];
255                         mIsDateSpecified = true;
256                     }
257                 }
258                 else if ( pathInfo.length == 4 )
259                 {
260                     // we have the /username/pagelink/datestring/anchor form of URL
261
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                     // we have the /username/pagelink/date/anchor form of URL
269
WeblogManager weblogMgr = getRoller().getWeblogManager();
270                     mWeblogEntry = weblogMgr.getWeblogEntryByAnchor(
271                                     mWebsite, pathInfo[3]);
272                 }
273             }
274         }
275         catch ( Exception JavaDoc ignored )
276         {
277             mLogger.debug("Exception parsing pathInfo",ignored);
278         }
279         
280         if ( mWebsite==null || mDate==null || mPage==null )
281         {
282             String JavaDoc msg = "Invalid pathInfo: "+StringUtils.join(pathInfo,"|");
283             mLogger.info(msg);
284             throw new RollerException(msg);
285         }
286     }
287     
288     //------------------------------------------------------------------------
289
/** Parse user, page, and entry from request params if possible */
290     private void parseRequestParams()
291     {
292         try
293         {
294             // No path info means that we need to parse request params
295

296             // First, look for user in the request params
297
UserManager userMgr = getRoller().getUserManager();
298             String JavaDoc userName = mRequest.getParameter(USERNAME_KEY);
299             if ( userName == null )
300             {
301                 // then try remote user
302
userName = mRequest.getRemoteUser();
303             }
304             
305             if ( userName != null )
306             {
307                 mWebsite = userMgr.getWebsite(userName);
308             }
309             
310             // Look for page ID in request params
311
String JavaDoc pageId = mRequest.getParameter(RollerRequest.PAGEID_KEY);
312             if ( pageId != null )
313             {
314                 mPage = userMgr.retrievePage(pageId);
315                 
316                 // We can use page to find the user, if we don't have one yet
317
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             // Look for day in request params
328
String JavaDoc daystr = mRequest.getParameter(WEBLOGDAY_KEY);
329             if ( daystr != null )
330             {
331                 mDate = parseDate(daystr);
332                 if ( mDate != null )
333                 {
334                     // we have the /username/datestring form of URL
335
mDateString = daystr;
336                     mIsDateSpecified = true;
337                 }
338             }
339         }
340         catch ( Exception JavaDoc ignored )
341         {
342             mLogger.debug("Exception parsing request params",ignored);
343         }
344     }
345
346     //------------------------------------------------------------------------
347
/** Get HttpServletmRequest that is wrapped by this RollerRequest */
348     public PageContext JavaDoc getPageContext()
349     {
350         return mPageContext;
351     }
352     
353     public void setPageContext(PageContext JavaDoc p)
354     {
355         mPageContext = p;
356     }
357
358     //------------------------------------------------------------------------
359
/** Get HttpServletmRequest that is wrapped by this RollerRequest */
360     public HttpServletRequest JavaDoc getRequest()
361     {
362         return mRequest;
363     }
364
365     //------------------------------------------------------------------------
366
/**
367      * Get the RollerRequest object that is stored in the request. Creates
368      * RollerRequest if one not found in mRequest.
369      */

370     public static RollerRequest getRollerRequest(
371         HttpServletRequest JavaDoc r, ServletContext JavaDoc 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     //------------------------------------------------------------------------
383
/**
384      * Get the RollerRequest object that is stored in the request. Creates
385      * RollerRequest if one not found in mRequest.
386      */

387     public static RollerRequest getRollerRequest( HttpServletRequest JavaDoc r )
388     {
389         try
390         {
391             return getRollerRequest(r, RollerContext.getServletContext());
392         }
393         catch (Exception JavaDoc e)
394         {
395             mLogger.debug("Unable to create a RollerRequest", e);
396         }
397         return null;
398     }
399
400     //------------------------------------------------------------------------
401
/**
402      * Get the RollerRequest object that is stored in the request. Creates
403      * RollerRequest if one not found in mRequest.
404      */

405     public static RollerRequest getRollerRequest( PageContext JavaDoc p )
406     {
407         HttpServletRequest JavaDoc r = (HttpServletRequest JavaDoc)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 JavaDoc 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     //------------------------------------------------------------------------
429
/**
430      * Get RollerRequest object for the current thread using EVIL MAGIC,
431      * do not use unless you absolutely, positively, cannot use on of the
432      * getRollerRequest() methods.
433      */

434     public static RollerRequest getRollerRequest()
435     {
436         return (RollerRequest)mRollerRequestTLS.get();
437     }
438
439     //------------------------------------------------------------------------
440
/**
441      * Get the RollerRequest object that is stored in the requeset.
442      * Creates RollerRequest if one not found in mRequest.
443      */

444     public ServletContext JavaDoc getServletContext()
445     {
446         return mContext;
447     }
448
449     //------------------------------------------------------------------------
450
/** Get Roller instance from */
451     public Roller getRoller()
452     {
453         return RollerContext.getRoller( mRequest );
454     }
455     
456     //------------------------------------------------------------------------
457
/** Is mRequest's user the admin user? */
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     //------------------------------------------------------------------------
472
/** Is mRequest's user authorized to edit the mRequested resource */
473     public boolean isUserAuthorizedToEdit() throws RollerException
474     {
475         // Make sure authenticated user is the owner of this item
476
// Session's principal's name must match user name in mRequest
477

478         RollerContext rctx = RollerContext.getRollerContext(mContext);
479         Authenticator auth = rctx.getAuthenticator();
480         
481         String JavaDoc userName = auth.getAuthenticatedUserName(mRequest);
482             
483         // TODO: A hack to be replaced by Object.canEdit()
484
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     //------------------------------------------------------------------------
508
/**
509      * Get user by name.
510      */

511     public UserData getUser( String JavaDoc userName ) throws Exception JavaDoc
512     {
513         return getRoller().getUserManager().getUser(userName);
514     }
515
516     //------------------------------------------------------------------------
517

518     public boolean isDateSpecified()
519     {
520         return mIsDateSpecified;
521     }
522             
523     //------------------------------------------------------------------------
524

525     public int getWeblogEntryCount()
526     {
527         // Get count of entries to return, or 20 if null
528
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     //------------------------------------------------------------------------
541

542     /**
543      * Gets the date specified by the request, or null.
544      * @return Date
545      */

546     public Date JavaDoc getDate()
547     {
548         return getDate(false);
549     }
550     
551     //------------------------------------------------------------------------
552
/**
553      * Gets the date specified by the request
554      * @param orToday If no date specified, then use today's date.
555      * @return Date
556      */

557     public Date JavaDoc getDate( boolean orToday )
558     {
559         Date JavaDoc 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     /**
572      * Gets the current date based on Website's Locale & Timezone.
573      * @return
574      */

575     private Date JavaDoc getToday()
576     {
577         Calendar JavaDoc 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 JavaDoc() );
585         return todayCal.getTime();
586     }
587
588     //------------------------------------------------------------------------
589
/**
590      * Gets the YYYYMMDD date string specified by the request, or null.
591      * @return String
592      */

593     public String JavaDoc getDateString()
594     {
595         return getDateString(false);
596     }
597
598     //------------------------------------------------------------------------
599
/**
600      * Gets the date specified by the request
601      * @param orToday If no date specified, then use today's date.
602      * @return Date
603      */

604     public String JavaDoc getDateString( boolean orToday )
605     {
606         String JavaDoc ret = null;
607         if ( mDateString != null )
608         {
609             ret = mDateString;
610         }
611         else if ( orToday )
612         {
613             Calendar JavaDoc 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 JavaDoc() );
621             ret = mFmt.format(todayCal.getTime());
622         }
623         return ret;
624     }
625     
626     //------------------------------------------------------------------------
627
/**
628      * Gets the path-info specified by the request, or null.
629      * @return String
630      */

631     public String JavaDoc getPathInfo()
632     {
633         return mPathInfo;
634     }
635
636     //------------------------------------------------------------------------
637
/**
638      * Gets the page link name specified by the request, or null.
639      * @return String
640      */

641     public String JavaDoc getPageLink()
642     {
643         return mPageLink;
644     }
645
646     //------------------------------------------------------------------------
647
/**
648      * Gets the BookmarkData specified by the request, or null.
649      * @return BookmarkData
650      */

651     public BookmarkData getBookmark( )
652     {
653         if ( mBookmark == null )
654         {
655             String JavaDoc 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     //------------------------------------------------------------------------
673
/**
674      * Gets the WeblogCategoryData specified by the request, or null.
675      * @return
676      */

677     public WeblogCategoryData getWeblogCategory()
678     {
679         if ( mWeblogCategory == null )
680         {
681             String JavaDoc 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     //------------------------------------------------------------------------
712
/**
713      * Gets the FolderData specified by the request, or null.
714      * @return FolderData
715      */

716     public FolderData getFolder( )
717     {
718         FolderData folder = null;
719         //if ( folder == null )
720
//{
721
String JavaDoc 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         //}
735
return folder;
736     }
737
738     //------------------------------------------------------------------------
739
/**
740      * Gets the PageData specified by the request, or null.
741      * @return PageData
742      */

743     public PageData getPage()
744     {
745         if (mPage == null)
746         {
747             String JavaDoc 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     /**
764      * Allow comment servlet to inject page that it has chosen.
765      */

766     public void setPage(PageData page)
767     {
768         mPage = page;
769     }
770     
771     //------------------------------------------------------------------------
772
/**
773      * Gets the Request URL specified by the request, or null.
774      * @return String
775      */

776     public String JavaDoc getRequestURL( )
777     {
778         return mRequest.getRequestURL().toString();
779     }
780     
781     //------------------------------------------------------------------------
782

783     /**
784      * Gets the Referer URL specified by the request, or null.
785      * @return String
786      */

787     public String JavaDoc getRefererURL( )
788     {
789         return mRequest.getHeader("referer");
790     }
791     
792     /**
793      * Gets the UserData specified by the request, or null.
794      * @return UserData
795      */

796     public UserData getUser()
797     {
798         if (mWebsite != null) return mWebsite.getUser();
799         return null;
800     }
801
802     /**
803      * Gets the WebsiteData specified by the request, or null.
804      * @return WeblogCategory
805      */

806     public WebsiteData getWebsite()
807     {
808         return mWebsite;
809     }
810     public void setWebsite(WebsiteData wd)
811     {
812         mWebsite = wd;
813     }
814
815     /**
816      * Gets the WeblogEntryData specified by the request, or null.
817      *
818      * Why is this done lazily in the parseRequestParameters() method?
819      *
820      * Because: that method is called from init(), which is called from
821      * a ServletFilter, and sometimes request parameters are not available
822      * in a ServletFiler. They ARE available when the URL points to a JSP,
823      * but they ARE NOT available when the URL points to the PageServlet.
824      * This may be a Tomcat bug, I'm not sure.
825      *
826      * @return WeblogEntryData
827      */

828     public WeblogEntryData getWeblogEntry( )
829     {
830         if ( mWeblogEntry == null )
831         {
832             // Look for anchor or entry ID that identifies a specific entry
833
String JavaDoc anchor = mRequest.getParameter(ANCHOR_KEY);
834             if (anchor == null) anchor = mRequest.getParameter(ANCHOR_KEY_OLD);
835             String JavaDoc entryid = mRequest.getParameter(WEBLOGENTRYID_KEY);
836             if (entryid == null)
837             {
838                 entryid = (String JavaDoc)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                     // We can use entry to find the website, if we don't have one
848
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     //------------------------------------------------------------------------
872

873     /** Get attribute from mRequest, and if that fails try session */
874     private String JavaDoc getFromRequest( String JavaDoc key )
875     {
876         String JavaDoc ret = (String JavaDoc)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 JavaDoc)mRequest.getSession().getAttribute( key );
883             }
884         }
885         return ret;
886     }
887
888     //------------------------------------------------------------------------
889

890     private Date JavaDoc parseDate( String JavaDoc dateString )
891     {
892         Date JavaDoc ret = null;
893         if ( dateString!=null
894             && dateString.length()==8
895             && StringUtils.isNumeric(dateString) )
896         {
897             ParsePosition JavaDoc pos = new ParsePosition JavaDoc(0);
898             ret = mFmt.parse( dateString, pos );
899             
900             // make sure the requested date is not in the future
901
Date JavaDoc today = getToday();
902             if (ret.after(today)) ret = today;
903             
904             // since a specific date was requested set time to end of day
905
ret = DateUtil.getEndOfDay(ret);
906         }
907         return ret;
908     }
909     
910     //------------------------------------------------------------------------
911

912     public String JavaDoc toString()
913     {
914         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
915         sb.append("[");
916         sb.append(getRequestURL());
917         sb.append(", ");
918         sb.append(getRefererURL());
919         sb.append("]");
920         return sb.toString();
921     }
922
923     //------------------------------------------------------------------------
924

925     /**
926      * @see org.roller.pojos.ParsedRequest#isLinkbackEnabled()
927      */

928     public boolean isEnableLinkback()
929     {
930         return RollerRuntimeConfig.getBooleanProperty("site.linkbacks.enabled");
931     }
932 }
933
934
Popular Tags