📄 rollerrequest.java
字号:
package org.roller.presentation;import java.text.ParsePosition;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import javax.servlet.ServletContext;import javax.servlet.ServletRequest;import javax.servlet.http.HttpServletRequest;import javax.servlet.jsp.PageContext;import org.apache.commons.lang.StringUtils;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.roller.RollerException;import org.roller.config.RollerRuntimeConfig;import org.roller.model.ParsedRequest;import org.roller.model.Roller;import org.roller.model.UserManager;import org.roller.model.WeblogManager;import org.roller.pojos.BookmarkData;import org.roller.pojos.FolderData;import org.roller.pojos.PageData;import org.roller.pojos.UserData;import org.roller.pojos.WeblogCategoryData;import org.roller.pojos.WeblogEntryData;import org.roller.pojos.WebsiteData;import org.roller.pojos.PingTargetData;import org.roller.util.DateUtil;import org.roller.util.Utilities; ///////////////////////////////////////////////////////////////////////////////** * Access to objects and values specified by request. Parses out arguments from * request URL needed for various parts of Roller and makes them available via * getter methods. * <br/><br/> * * These forms of pathinfo get special support: * <br/><br/> * * <pre> * [username] - get default page for user for today's date * [username]/[date] - get default page for user for specified date * [username]/[pagelink] - get specified page for today's date * [username]/[pagelink]/[date] - get specified page for specified date * [username]/[pagelink]/[anchor] - get specified page & entry (by anchor) * [username]/[pagelink]/[date]/[anchor] - get specified page & entry (by anchor) * </pre> * * @author David M Johnson */public class RollerRequest implements ParsedRequest{ //----------------------------------------------------------------- Fields private static Log mLogger = LogFactory.getFactory().getInstance(RollerRequest.class); private BookmarkData mBookmark; private ServletContext mContext = null; private Date mDate = null; private String mDateString = null; private String mPathInfo = null; private String mPageLink = null; private PageData mPage; private PageContext mPageContext = null; private HttpServletRequest mRequest = null; private WebsiteData mWebsite; private WeblogEntryData mWeblogEntry; private WeblogCategoryData mWeblogCategory; private boolean mIsDateSpecified = false; private static ThreadLocal mRollerRequestTLS = new ThreadLocal(); public static final String ANCHOR_KEY = "entry"; public static final String ANCHOR_KEY_OLD = "anchor"; public static final String USERNAME_KEY = "username"; public static final String WEBSITEID_KEY = "websiteid"; public static final String FOLDERID_KEY = "folderid"; public static final String PARENTID_KEY = "parentid"; public static final String NEWSFEEDID_KEY = "feedid"; public static final String PAGEID_KEY = "pageid"; public static final String PAGELINK_KEY = "pagelink"; public static final String PINGTARGETID_KEY = "pingtargetid"; public static final String EXCERPTS_KEY = "excerpts"; public static final String BOOKMARKID_KEY = "bookmarkid"; public static final String REFERERID_KEY = "refid"; public static final String WEBLOGENTRYID_KEY = "entryid"; public static final String WEBLOGENTRY_COUNT = "count"; public static final String WEBLOGCATEGORYNAME_KEY = "catname"; public static final String WEBLOGCATEGORYID_KEY = "catid"; public static final String WEBLOGENTRIES_KEY = "entries"; public static final String WEBLOGDAY_KEY = "day"; public static final String WEBLOGCOMMENTID_KEY = "catid"; public static final String LOGIN_COOKIE = "sessionId"; public static final String OWNING_USER = "OWNING_USER"; private static final String ROLLER_REQUEST = "roller_request"; private SimpleDateFormat mFmt = DateUtil.get8charDateFormat(); //----------------------------------------------------------- Constructors /** Construct Roller request for a Servlet request */ public RollerRequest( HttpServletRequest req, ServletContext ctx ) throws RollerException { mRequest = req; mContext = ctx; init(); } //------------------------------------------------------------------------ /** Convenience */ public RollerRequest( ServletRequest req, ServletContext ctx ) throws RollerException { mRequest = (HttpServletRequest)req; mContext = ctx; init(); } //------------------------------------------------------------------------ public RollerRequest( PageContext pCtx) throws RollerException { mRequest = (HttpServletRequest) pCtx.getRequest(); mContext = pCtx.getServletContext(); mPageContext = pCtx; init(); } //------------------------------------------------------------------------ private void init() throws RollerException { mRollerRequestTLS.set(this); if (mRequest.getContextPath().equals("/atom")) { return; // Atom servlet request needs no init } // Bind persistence session to authenticated user, if we have one RollerContext rctx = RollerContext.getRollerContext(mContext); Authenticator auth = rctx.getAuthenticator(); String userName = auth.getAuthenticatedUserName(mRequest); if (userName != null) { UserManager userMgr = getRoller().getUserManager(); UserData currentUser = userMgr.getUser(userName); getRoller().setUser(currentUser); } // 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 if (!"j_security_check".equals(pathInfo[0])) { parsePathInfo( pathInfo ); return; } } // Parse user, page, and entry from request params if possible parseRequestParams(); } //------------------------------------------------------------------------ /** * Bind persistence session to specific user. */ private void bindUser() throws RollerException { } //------------------------------------------------------------------------ /** 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 // /username/pagelink/anchor (specific entry) // /username/pagelink/datestring/anchor (specific entry) UserManager userMgr = getRoller().getUserManager(); mWebsite = userMgr.getWebsite(pathInfo[0]); if (mWebsite != null) { if ( pathInfo.length == 1 ) { // we have the /username form of URL mDate = getDate(true); mDateString = DateUtil.format8chars(mDate); mPage = userMgr.retrievePage(mWebsite.getDefaultPageId()); } else if ( pathInfo.length == 2 ) { mDate = parseDate(pathInfo[1]); if ( mDate == null ) // pre-jdk1.4 --> || mDate.getYear() <= 70 ) { // we have the /username/pagelink form of URL mDate = getDate(true); mDateString = DateUtil.format8chars(mDate); mPageLink = pathInfo[1]; mPage = userMgr.getPageByLink(mWebsite, pathInfo[1]); } else { // we have the /username/datestring form of URL mDateString = pathInfo[1]; mPage = userMgr.retrievePage(mWebsite.getDefaultPageId()); mIsDateSpecified = true; } } else if ( pathInfo.length == 3 ) { mPageLink = pathInfo[1]; mPage = userMgr.getPageByLink(mWebsite, pathInfo[1]); mDate = parseDate(pathInfo[2]); if ( mDate == null ) // pre-jdk1.4 --> || mDate.getYear() <= 70 ) { // we have the /username/pagelink/anchor form of URL try { WeblogManager weblogMgr = getRoller().getWeblogManager(); mWeblogEntry = weblogMgr.getWeblogEntryByAnchor( mWebsite, pathInfo[2]); mDate = mWeblogEntry.getPubTime(); mDateString = DateUtil.format8chars(mDate); } catch (Exception damn) { // doesn't really matter, we've got the Page anyway mLogger.debug("Damn", damn); } } else { // we have the /username/pagelink/datestring form of URL mDateString = pathInfo[2]; mIsDateSpecified = true; } } else if ( pathInfo.length == 4 ) { // we have the /username/pagelink/datestring/anchor form of URL mPageLink = pathInfo[1]; mPage = userMgr.getPageByLink(mWebsite, pathInfo[1]); mDate = parseDate(pathInfo[2]); mDateString = pathInfo[2]; mIsDateSpecified = true; // we have the /username/pagelink/date/anchor form of URL WeblogManager weblogMgr = getRoller().getWeblogManager(); mWeblogEntry = weblogMgr.getWeblogEntryByAnchor( mWebsite, pathInfo[3]); } } } catch ( Exception ignored ) { mLogger.debug("Exception parsing pathInfo",ignored); } if ( mWebsite==null || mDate==null || mPage==null ) { String msg = "Invalid pathInfo: "+StringUtils.join(pathInfo,"|"); mLogger.info(msg); throw new RollerException(msg); } } //------------------------------------------------------------------------ /** 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 ) { mWebsite = userMgr.getWebsite(userName); } // Look for page ID in request params String pageId = mRequest.getParameter(RollerRequest.PAGEID_KEY); if ( pageId != null )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -