logo       

roller/src/org/roller/presentation RollerRequest.java,1.36,1.37: msg#00288

Subject: roller/src/org/roller/presentation RollerRequest.java,1.36,1.37
Update of /cvsroot/roller/roller/src/org/roller/presentation
In directory sc8-pr-cvs1:/tmp/cvs-serv14960/src/org/roller/presentation

Modified Files:
        RollerRequest.java 
Log Message:
Invalid URLs should return 404s

Index: RollerRequest.java
===================================================================
RCS file: 
/cvsroot/roller/roller/src/org/roller/presentation/RollerRequest.java,v
retrieving revision 1.36
retrieving revision 1.37
diff -C2 -d -r1.36 -r1.37
*** RollerRequest.java  29 Jan 2003 22:31:25 -0000      1.36
--- RollerRequest.java  31 Jan 2003 00:46:33 -0000      1.37
***************
*** 16,19 ****
--- 16,20 ----
  import javax.servlet.jsp.PageContext;
  
+ import org.apache.commons.lang.StringUtils;
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
***************
*** 31,34 ****
--- 32,36 ----
  import org.roller.model.WeblogManager;
  import org.roller.model.WebsiteData;
+ import org.roller.util.DateUtil;
  import org.roller.util.Utilities;
  
***************
*** 56,60 ****
  {
      //----------------------------------------------------------------- Fields
! 
      private static Log mLogger = 
          LogFactory.getFactory().getInstance(RollerRequest.class);
--- 58,62 ----
  {
      //----------------------------------------------------------------- Fields
!     
      private static Log mLogger = 
          LogFactory.getFactory().getInstance(RollerRequest.class);
***************
*** 104,107 ****
--- 106,110 ----
      /** Construct Roller request for a Servlet request */
      public RollerRequest( HttpServletRequest req, ServletContext ctx )
+         throws RollerException
      {
          mRequest = req;
***************
*** 110,115 ****
      }
      
      /** Convenience */
!     public RollerRequest( ServletRequest req, ServletContext ctx )
      {
          mRequest = (HttpServletRequest)req;
--- 113,120 ----
      }
      
+     //------------------------------------------------------------------------
      /** Convenience */
!     public RollerRequest( ServletRequest req, ServletContext ctx ) 
!         throws RollerException
      {
          mRequest = (HttpServletRequest)req;
***************
*** 118,122 ****
      }
      
!     public RollerRequest( PageContext pCtx)
      {
        mRequest = (HttpServletRequest) pCtx.getRequest();
--- 123,128 ----
      }
      
!     //------------------------------------------------------------------------
!     public RollerRequest( PageContext pCtx) throws RollerException
      {
        mRequest = (HttpServletRequest) pCtx.getRequest();
***************
*** 125,219 ****
        init();
      }
      
!     private void init()
      {
          try 
          {
!             String      userName = null;            
              UserManager userMgr = getRoller().getUserManager();            
! 
!             // path info may be null, (e.g. on JSP error page)
!             mPathInfo = mRequest.getPathInfo();
!             mPathInfo = (mPathInfo!=null) ? mPathInfo : "";            
!             StringTokenizer toker = new StringTokenizer(mPathInfo,"/");
!             
!             // Try to parse out userName
!             if ( toker.hasMoreElements() )
              {
!                 userName = toker.nextToken();
!             }           
!             // If that fails, try remote user           
!             if ( userName == null )
              {
!                 if ( mRequest.getRemoteUser() != null )
                  {
!                     userName = mRequest.getRemoteUser();
                  }
!             }            
!             // If that fails try to pull from request
              if ( userName == null )
              {
!                 userName = mRequest.getParameter(USERNAME_KEY);
              }
              
              if ( userName != null )
!             {   
                  mUser = userMgr.getUser(userName);
                  mWebsite = userMgr.getWebsite(userName);
              }
- 
-             // Next will be a page link or a date                    
-             if ( toker.hasMoreElements() )
-             {
-                 // Attempt to parse next token as date
-                 mDateString = toker.nextToken();
-                 
-                 // some JDK's apparently don't return NULL like
-                 // they're supposed to, instead returning EPOCH date
-                 mDate = mFmt.parse( mDateString, mPos );
-                 if ( mDate != null && mDate.getYear() > 70 )
-                 {
-                     // It parses, so take it as date
-                     // This means default page link will be used
-                 }
-                 else // Can't parse it so it must be a page link
-                 {                           
-                     mPageLink = mDateString;
-                     
-                     // See if a date follows the page link
-                     if ( toker.hasMoreElements() )
-                     {
-                         // Attempt to parse next token as date
-                         mDateString = toker.nextToken();
-                         mDate = mFmt.parse( mDateString, mPos );
-                         if ( mDate != null )
-                         {
-                             // we have a valid date
-                         }                              
-                     }
-                 }
-             }
              
!             String pageId = 
(String)mRequest.getParameter(RollerRequest.PAGEID_KEY);
!                             
!             if ( mPageLink != null )
!             {
!                 mPage = getRoller().getUserManager().getPageByLink(
!                             getUser().getUserName(), mPageLink);
!             }
!             else if ( pageId != null )
              {
                  mPage = userMgr.retrievePage(pageId);
              }
- 
-             // Don't fallback to default page here, let PageServlet do that.
-             // Otherwise the navigation tags will think that a valid page
-             // was chosen when in fact it was not.
-             //            
-             // if ( mPage == null && mWebsite != null )
-             // {
-             //     mPage = userMgr.retrievePage(mWebsite.getDefaultPageId());
-             // }
              
              String entryid = mRequest.getParameter(WEBLOGENTRYID_KEY);
              if ( entryid != null )
--- 131,258 ----
        init();
      }
+ 
+     //------------------------------------------------------------------------
+     private void init() throws RollerException
+     {
+         // path info may be null, (e.g. on JSP error page)
+         mPathInfo = mRequest.getPathInfo();
+         mPathInfo = (mPathInfo!=null) ? mPathInfo : "";            
+         
+         String[] pathInfo = StringUtils.split(mPathInfo,"/");            
+         if ( pathInfo.length > 0 )
+         {
+             // Parse pathInfo and throw exception if it is invalid
+             parsePathInfo( pathInfo );
+         }
+         else
+         {
+             // Parse user, page, and entry from request params if possible
+             parseRequestParams();
+         }
+     }
      
!     //------------------------------------------------------------------------
!     /** Parse pathInfo and throw exception if it is invalid */
!     private void parsePathInfo( String[] pathInfo ) throws RollerException
      {
          try 
          {
!             // If there is any path info, it must be in one of the 
!             // below formats or the request is considered to be invalid.
!             //
!             //   /username 
!             //   /username/datestring 
!             //   /username/pagelink
!             //   /username/pagelink/datestring
!             //
!                                   
              UserManager userMgr = getRoller().getUserManager();            
!             mUser = userMgr.getUser(pathInfo[0]);
!             mWebsite = userMgr.getWebsite(mUser.getUserName());
!             if ( pathInfo.length == 1 )
              {
!                 // we have the /username form of URL
!                 mDate = new Date();
!                 mDateString = DateUtil.format8chars(mDate);
!                 mPage = userMgr.retrievePage(mWebsite.getDefaultPageId());
!             }
!             else if ( pathInfo.length == 2 )
              {
!                 mPos.setIndex(0);  
!                 mDate = parseDate(pathInfo[1]);
!                 if ( mDate == null || mDate.getYear() <= 70 )
                  {
!                     // we have the /username/pagelink form of URL
!                     mDate = new Date();
!                     mDateString = DateUtil.format8chars(mDate);
!                     mPage = userMgr.getPageByLink(
!                         mUser.getUserName(),pathInfo[1]);
                  }
!                 else
!                 {
!                     // we have the /username/datestring form of URL
!                     mDateString = pathInfo[1];
!                     mPage = userMgr.retrievePage(mWebsite.getDefaultPageId());
!                 }               
!             }
!             else if ( pathInfo.length == 3 )
!             {
!                 // we have the /username/pagelink/datestring form of URL
!                 mPage = userMgr.getPageByLink(pathInfo[0],pathInfo[1]);
!                 mDateString = pathInfo[2];
!                 mPos.setIndex(0);  
!                 mDate = parseDate(pathInfo[1]);
!             }
!         }
!         catch ( Exception ignored )
!         {
!             mLogger.debug("Exception parsing pathInfo",ignored);
!         }
!         
!         if ( mUser==null || mDate==null || mPage==null )
!         {
!             throw new RollerException("Invalid PathInfo");
!         }
!     }       
!     
!     //------------------------------------------------------------------------
!     /** Parse user, page, and entry from request params if possible */
!     private void parseRequestParams()
!     {
!         try
!         {
!             // No path info means that we need to parse request params
!             
!             // First, look for user in the request params
!             UserManager userMgr = getRoller().getUserManager();            
!             String userName = mRequest.getParameter(USERNAME_KEY);
              if ( userName == null )
              {
!                 // then try remote user
!                 userName = mRequest.getRemoteUser(); 
              }
              
              if ( userName != null )
!             {
                  mUser = userMgr.getUser(userName);
                  mWebsite = userMgr.getWebsite(userName);
              }
              
!             // Look for page ID in request params
!             String pageId = (String)mRequest.getParameter(
!                 RollerRequest.PAGEID_KEY);                    
!             if ( pageId != null )
              {
                  mPage = userMgr.retrievePage(pageId);
+                 
+                 // We can use page to find the user, if we don't have one yet
+                 if ( mUser == null )
+                 {
+                     mWebsite = userMgr.retrieveWebsite(mPage.getWebsiteId());
+                     mUser = userMgr.retrieveUser(mWebsite.getUserId());
+                 }                    
              }
              
+             // Look for entry ID in request params 
              String entryid = mRequest.getParameter(WEBLOGENTRYID_KEY);
              if ( entryid != null )
***************
*** 221,224 ****
--- 260,265 ----
                  WeblogManager weblogMgr = getRoller().getWeblogManager();
                  mWeblogEntry = weblogMgr.retrieveWeblogEntry(entryid);
+                 
+                 // We can use entry to find the user, if we don't have one yet
                  if ( mUser == null )
                  {
***************
*** 227,248 ****
                      mUser = userMgr.retrieveUser(mWebsite.getUserId());
                  }
!             }  
!                       
!             // If mUser is still null and mPage is not, then use page's user.
!             // This only occurs when page is requested by pageId alone.
!             if ( mUser == null && mPage != null )
!             {
!                 mWebsite = userMgr.retrieveWebsite(mPage.getWebsiteId());
!                 mUser = userMgr.retrieveUser(mWebsite.getUserId());
!             }
          }
!         catch (Exception e)
          {
!             mLogger.error("Parsing args from URL ["
!               
+((HttpServletRequest)mRequest).getRequestURL().toString()+"]");              
          }
      }
  
- 
      //------------------------------------------------------------------------
      /** Get HttpServletmRequest that is wrapped by this RollerRequest */
--- 268,279 ----
                      mUser = userMgr.retrieveUser(mWebsite.getUserId());
                  }
!             }                  
          }
!         catch ( Exception ignored )
          {
!             mLogger.debug("Exception parsing request params",ignored);
          }
      }
  
      //------------------------------------------------------------------------
      /** Get HttpServletmRequest that is wrapped by this RollerRequest */
***************
*** 269,273 ****
       */
      public static RollerRequest getRollerRequest( 
!         HttpServletRequest r, ServletContext ctx )
      {
          RollerRequest ret= (RollerRequest)r.getAttribute(ROLLER_REQUEST);
--- 300,304 ----
       */
      public static RollerRequest getRollerRequest( 
!         HttpServletRequest r, ServletContext ctx ) throws RollerException
      {
          RollerRequest ret= (RollerRequest)r.getAttribute(ROLLER_REQUEST);
***************
*** 288,297 ****
      {
          RollerRequest ret = (RollerRequest)r.getAttribute(ROLLER_REQUEST);
-         if ( ret == null )
-         {
-             ServletContext ctx = r.getSession(true).getServletContext();
-             ret = new RollerRequest(r,ctx);
-             r.setAttribute( ROLLER_REQUEST, ret );
-         }
          return ret;
      }
--- 319,322 ----
***************
*** 304,318 ****
        public static RollerRequest getRollerRequest( PageContext p )
        {
!               ServletRequest r = p.getRequest();
                RollerRequest ret = 
(RollerRequest)r.getAttribute(ROLLER_REQUEST);
!               if ( ret == null )
!               {
!                       ret = new RollerRequest( p );
!                       r.setAttribute( ROLLER_REQUEST, ret );
!               }
!               else
!               {
!                       ret.setPageContext( p );
!               }
                return ret;
        }
--- 329,335 ----
        public static RollerRequest getRollerRequest( PageContext p )
        {
!         HttpServletRequest r = (HttpServletRequest)p.getRequest();
                RollerRequest ret = 
(RollerRequest)r.getAttribute(ROLLER_REQUEST);
!               ret.setPageContext( p );
                return ret;
        }
***************
*** 398,401 ****
--- 415,419 ----
      }
      
+     //------------------------------------------------------------------------
      /**
       * Gets the date specified by the request
***************
*** 417,420 ****
--- 435,439 ----
      }
   
+     //------------------------------------------------------------------------
      /**
       * Gets the YYYYMMDD date string specified by the request, or null.
***************
*** 426,429 ****
--- 445,449 ----
      }
  
+     //------------------------------------------------------------------------
      /**
       * Gets the date specified by the request
***************
*** 448,451 ****
--- 468,472 ----
      }
      
+     //------------------------------------------------------------------------
      /**
       * Gets the path-info specified by the request, or null.
***************
*** 457,460 ****
--- 478,482 ----
      }
  
+     //------------------------------------------------------------------------
      /**
       * Gets the page link name specified by the request, or null.
***************
*** 466,469 ****
--- 488,492 ----
      }
  
+     //------------------------------------------------------------------------
      /**
       * Gets the BookmarkData specified by the request, or null.
***************
*** 491,494 ****
--- 514,518 ----
      }
  
+     //------------------------------------------------------------------------
      /**
       * Gets the WeblogCategoryData specified by the request, or null.
***************
*** 529,532 ****
--- 553,557 ----
      }
  
+     //------------------------------------------------------------------------
      /**
       * Gets the FolderData specified by the request, or null.
***************
*** 554,557 ****
--- 579,583 ----
      }
  
+     //------------------------------------------------------------------------
      /**
       * Gets the NewsfeedData specified by the request, or null.
***************
*** 579,582 ****
--- 605,609 ----
      }
  
+     //------------------------------------------------------------------------
      /**
       * Gets the PageData specified by the request, or null.
***************
*** 604,607 ****
--- 631,635 ----
      }
      
+     //------------------------------------------------------------------------
      /**
       * Gets the Request URL specified by the request, or null.
***************
*** 635,638 ****
--- 663,668 ----
      }
      
+     //------------------------------------------------------------------------
+     
      /**
       * Gets the Referer URL specified by the request, or null.
***************
*** 670,674 ****
          return mWeblogEntry;
      }
- 
      
      //-----------------------------------------------------------------------
--- 700,703 ----
***************
*** 786,789 ****
--- 815,834 ----
          return ret;
      }
+ 
+     //------------------------------------------------------------------------
+ 
+     private Date parseDate( String dateString )
+     {
+         Date ret = null;
+         if (   dateString!=null 
+             && dateString.length()==8
+             && StringUtils.isNumeric(dateString) )
+         {
+             mPos.setIndex(0);
+             ret = mFmt.parse( dateString, mPos );    
+         }
+         return ret;
+     }
+     
      //------------------------------------------------------------------------
  




-------------------------------------------------------
This SF.NET email is sponsored by:
SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See!
http://www.vasoftware.com


<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

Recently Viewed:
web.pylons.gene...    hurd.l4/2002-10...    kernel.commits....    user-groups.lin...    yellowdog.gener...    java.drools.use...    security.openva...    package-managem...    linux.debian.us...    qnx.openqnx.dev...    genealogy.gramp...    file-systems.if...    voip.wengophone...    tex.context/200...    ietf.smime/2003...    audio.csound.de...    culture.region....    xfree86.devel/2...    mobile.kannel.u...    distributed.con...    education.engli...    org.user-groups...    bug-tracking.gn...    recreation.bicy...   
Home | blog view | USPTO Patent Archive | advertise | OSDir is an inevitable website. super tiny logo

Free Magazines

Cisco News
Receive a free quarterly e-newsletter with exclusive articles on how Cisco IT uses its own products and solutions to enable the business.
subscribe

Systems Management News, the newspaper for IT systems administration and data center managers! Each issue of Systems Management News is chock-full of news and analysis to help you understand what's happening in your field.
subscribe

The Enterprise Newsweekly eWeek is the essential technology information source for builders of e-business.
subscribe

Oracle Magazine Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Oracle (NASDAQ: ORCL) is the world's largest enterprise software company.
subscribe

Total Telecom Total Telecom is "The Economist of the communications industry".
subscribe