📄 contextloader.java
字号:
package org.roller.presentation.velocity;import java.net.MalformedURLException;import java.net.URL;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.List;import java.util.Locale;import java.util.Map;import java.util.TimeZone;import javax.servlet.ServletContext;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.apache.struts.util.RequestUtils;import org.apache.velocity.context.Context;import org.apache.velocity.tools.view.context.ChainedContext;import org.apache.velocity.tools.view.context.ToolboxContext;import org.apache.velocity.tools.view.servlet.ServletToolboxManager;import org.roller.RollerException;import org.roller.config.RollerConfig;import org.roller.config.RollerRuntimeConfig;import org.roller.model.Roller;import org.roller.model.RollerFactory;import org.roller.pojos.CommentData;import org.roller.pojos.PageData;import org.roller.pojos.RollerPropertyData;import org.roller.pojos.UserData;import org.roller.pojos.WeblogEntryData;import org.roller.pojos.WebsiteData;import org.roller.presentation.LanguageUtil;import org.roller.presentation.RollerContext;import org.roller.presentation.RollerRequest;import org.roller.presentation.RollerSession;import org.roller.presentation.newsfeeds.NewsfeedCache;import org.roller.presentation.weblog.formbeans.CommentFormEx;import org.roller.util.RegexUtil;import org.roller.util.StringUtils;import org.roller.util.Utilities;/** * Load Velocity Context with Roller objects, values, and custom plugins. * * @author llavandowska * @author David M Johnson */public class ContextLoader{ private RollerRequest mRollerReq = null; // List of PagePlugins for "transforming" WeblogEntries static List mPagePlugins = new ArrayList(); private static Log mLogger = LogFactory.getFactory().getInstance(ContextLoader.class); //------------------------------------------------------------------------ /** * Setup the a Velocity context by loading it with objects, values, and * RollerPagePlugins needed for Roller page execution. */ public static void setupContext( Context ctx, RollerRequest rreq, HttpServletResponse response ) throws RollerException { mLogger.debug("setupContext( ctx = "+ctx+")"); HttpServletRequest request = rreq.getRequest(); RollerContext rollerCtx = RollerContext.getRollerContext( request ); try { // Add page model object to context String pageModelClassName = RollerConfig.getProperty("velocity.pagemodel.classname"); Class pageModelClass = Class.forName(pageModelClassName); PageModel pageModel = (PageModel)pageModelClass.newInstance(); pageModel.init(rreq); ctx.put("pageModel", pageModel ); ctx.put("pages", pageModel.getPages()); } catch (Exception e) { throw new RollerException("ERROR creating Page Model",e); } // Add Velocity page helper to context PageHelper pageHelper = new PageHelper(rreq, response, ctx); pageHelper.initializePlugins(mPagePlugins); ctx.put("pageHelper", pageHelper ); // Add legacy macros too, so that old-school pages still work Macros macros= new Macros(rreq.getPageContext(), pageHelper); ctx.put("macros", macros); // Load standard Roller objects and values into the context String userName = loadWebsiteValues(ctx, rreq, rollerCtx ); loadWeblogValues( ctx, rreq, rollerCtx, userName ); loadPathValues( ctx, rreq, rollerCtx, userName ); loadRssValues( ctx, rreq, userName ); loadUtilityObjects( ctx, rreq, rollerCtx, userName ); loadRequestParamKeys(ctx); loadStatusMessage( ctx, rreq ); // If single entry is specified, load comments too if ( rreq.getWeblogEntry() != null ) { loadCommentValues( ctx, rreq, rollerCtx ); } // add Velocity Toolbox tools to context loadToolboxContext(request, response, ctx); } //------------------------------------------------------------------------ /** * If there is an ERROR or STATUS message in the session, * place it into the Context for rendering later. * * @param rreq */ private static void loadStatusMessage(Context ctx, RollerRequest rreq) { HttpSession session = rreq.getRequest().getSession(false); String msg = null; if (session != null) msg = (String)session.getAttribute(RollerSession.ERROR_MESSAGE); if (msg != null) { ctx.put("errorMessage", msg); session.removeAttribute(RollerSession.ERROR_MESSAGE); } if (session != null) msg = (String)session.getAttribute(RollerSession.STATUS_MESSAGE); if (msg != null) { ctx.put("statusMessage", msg); session.removeAttribute(RollerSession.STATUS_MESSAGE); } } //------------------------------------------------------------------------ /** * @param ctx * @param rreq * @param rollerCtx * @param userName */ private static void loadWeblogValues( Context ctx, RollerRequest rreq, RollerContext rollerCtx, String userName) throws RollerException { // if there is an "_entry" page, only load it once WebsiteData website = rreq.getRoller().getUserManager().getWebsite(userName); PageModel pageModel = (PageModel)ctx.get("pageModel"); if (website != null && pageModel != null) { /* alternative display pages - customization */ PageData entryPage = pageModel.getUsersPageByName(website, "_entry"); if (entryPage != null) { ctx.put("entryPage", entryPage); } PageData descPage = pageModel.getUsersPageByName(website, "_desc"); if (descPage != null) { ctx.put("descPage", descPage); } } } private static String figureResourcePath( RollerRequest rreq ) { /* old way -- Allen G HttpServletRequest request = rreq.getRequest(); RollerContext rCtx = RollerContext.getRollerContext( request ); RollerConfigData rollerConfig = rCtx.getRollerConfig(); StringBuffer sb = new StringBuffer(); String uploadPath = rollerConfig.getUploadPath(); if ( uploadPath != null && uploadPath.trim().length() > 0 ) { sb.append( uploadPath ); } else { sb.append( request.getContextPath() ); sb.append( RollerContext.USER_RESOURCES ); } return sb.toString(); */ String uploadurl = null; try { uploadurl = RollerFactory.getRoller().getFileManager().getUploadUrl(); } catch(Exception e) {} return uploadurl; } //------------------------------------------------------------------------ public boolean isUserAuthorizedToEdit() { try { return mRollerReq.isUserAuthorizedToEdit(); } catch (Exception e) { mLogger.warn("PageHelper.isUserAuthorizedToEdit)", e); } return false; } //------------------------------------------------------------------------ protected static void loadCommentValues( Context ctx, RollerRequest rreq, RollerContext rollerCtx ) throws RollerException { HttpServletRequest request = rreq.getRequest(); String escapeHtml = RollerRuntimeConfig.getProperty("users.comments.escapehtml"); String autoFormat = RollerRuntimeConfig.getProperty("users.comments.autoformat"); // Add comments related values to context ctx.put("isCommentPage", Boolean.TRUE); ctx.put("escapeHtml", new Boolean(escapeHtml) ); ctx.put("autoformat", new Boolean(autoFormat) ); // Make sure comment form object is available in context CommentFormEx commentForm = (CommentFormEx)request.getAttribute("commentForm"); if ( commentForm == null ) { commentForm = new CommentFormEx(); // Set fields to spaces to please Velocity commentForm.setName(""); commentForm.setEmail(""); commentForm.setUrl(""); commentForm.setContent(""); } ctx.put("commentForm",commentForm); // Either put a preview comment in to context if ( request.getAttribute("previewComments")!=null ) { ArrayList list = new ArrayList(); CommentData cd = new CommentData(); commentForm.copyTo(cd, request.getLocale()); list.add(cd); ctx.put("previewComments",list); } WeblogEntryData entry = rreq.getWeblogEntry(); ctx.put("entry",entry); } //------------------------------------------------------------------------ protected static void loadPathValues( Context ctx, RollerRequest rreq, RollerContext rollerCtx, String userName) throws RollerException { HttpServletRequest request = rreq.getRequest(); String url = null; if ( userName != null && !userName.equals("zzz_none_zzz")) { url = Utilities.escapeHTML( rollerCtx.getAbsoluteContextUrl(request)+"/page/"+userName); } else { url= Utilities.escapeHTML(rollerCtx.getAbsoluteContextUrl(request)); } ctx.put("websiteURL", url); ctx.put("baseURL", rollerCtx.getContextUrl( request ) ); ctx.put("absBaseURL", rollerCtx.getAbsoluteContextUrl( request ) ); ctx.put("ctxPath", request.getContextPath() ); ctx.put("uploadPath", ContextLoader.figureResourcePath( rreq ) ); try { URL absUrl = RequestUtils.absoluteURL(request, "/"); ctx.put("host", absUrl.getHost()); } catch (MalformedURLException e) { throw new RollerException(e); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -