contextloader.java
来自「这个weblogging 设计得比较精巧」· Java 代码 · 共 600 行 · 第 1/2 页
JAVA
600 行
} //------------------------------------------------------------------------ protected static void loadRequestParamKeys(Context ctx) { // Since Velocity *requires* accessor methods, these values from // RollerRequest are not available to it, put them into the context ctx.put("USERNAME_KEY", RollerRequest.USERNAME_KEY); ctx.put("WEBSITEID_KEY", RollerRequest.WEBSITEID_KEY); ctx.put("FOLDERID_KEY", RollerRequest.FOLDERID_KEY); ctx.put("NEWSFEEDID_KEY", RollerRequest.NEWSFEEDID_KEY); ctx.put("PAGEID_KEY", RollerRequest.PAGEID_KEY); ctx.put("PAGELINK_KEY", RollerRequest.PAGELINK_KEY); ctx.put("ANCHOR_KEY", RollerRequest.ANCHOR_KEY); ctx.put("EXCERPTS_KEY", RollerRequest.EXCERPTS_KEY); ctx.put("BOOKMARKID_KEY", RollerRequest.BOOKMARKID_KEY); ctx.put("REFERERID_KEY", RollerRequest.REFERERID_KEY); ctx.put("WEBLOGENTRYID_KEY", RollerRequest.WEBLOGENTRYID_KEY); ctx.put("WEBLOGCATEGORYNAME_KEY", RollerRequest.WEBLOGCATEGORYNAME_KEY); ctx.put("WEBLOGCATEGORYID_KEY", RollerRequest.WEBLOGENTRIES_KEY); ctx.put("WEBLOGENTRIES_KEY", RollerRequest.WEBLOGENTRIES_KEY); ctx.put("WEBLOGDAY_KEY", RollerRequest.WEBLOGDAY_KEY); ctx.put("WEBLOGCOMMENTID_KEY", RollerRequest.WEBLOGCOMMENTID_KEY); } //------------------------------------------------------------------------ protected static void loadRssValues( Context ctx, RollerRequest rreq, String userName) throws RollerException { HttpServletRequest request = rreq.getRequest(); int entryLength = -1; String sExcerpts = request.getParameter("excerpts"); if ( sExcerpts!=null && sExcerpts.equalsIgnoreCase("true")) { entryLength = 150; } ctx.put("entryLength", new Integer(entryLength)); int entryCount = 15; String sCount = request.getParameter("count"); if ( sCount!=null && sExcerpts.trim().equals("")) { try { entryCount = Integer.parseInt(sCount); } catch (NumberFormatException e) { mLogger.warn("Improperly formatted count parameter"); } if ( entryCount > 50 ) entryCount = 50; if ( entryCount < 0 ) entryCount = 15; } ctx.put("entryCount", new Integer(entryCount)); String catname = null; String catPath = null; if ( rreq.getWeblogCategory() != null ) { catname = rreq.getWeblogCategory().getName(); catPath = rreq.getWeblogCategory().getPath(); } ctx.put("catname", (catname!=null) ? catname : ""); ctx.put("catPath", (catPath != null) ? catPath : ""); ctx.put("updateTime", request.getAttribute("updateTime")); ctx.put("now", new Date()); } //------------------------------------------------------------------------ protected static void loadUtilityObjects( Context ctx, RollerRequest rreq, RollerContext rollerCtx, String username) throws RollerException { // date formatter for macro's // set this up with the Locale to make sure we can reuse it with other patterns // in the macro's Locale viewLocale = (Locale) ctx.get("viewLocale"); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd", viewLocale); WebsiteData website = rreq.getRoller().getUserManager().getWebsite(username); if (website != null) { sdf.setTimeZone(website.getTimeZoneInstance()); } // add formatter to context ctx.put("dateFormatter", sdf ); // Note: in the macro's, the formats are taken from the ResourceBundles. // Only the plainFormat is specified here, because it is used to render // the Entry Day link. ctx.put("plainFormat", "yyyyMMdd"); ctx.put("page", rreq.getPage() ); ctx.put("utilities", new Utilities() ); ctx.put("stringUtils", new StringUtils() ); ctx.put("rollerVersion", rollerCtx.getRollerVersion() ); ctx.put("rollerBuildTime", rollerCtx.getRollerBuildTime() ); ctx.put("rollerBuildUser", rollerCtx.getRollerBuildUser() ); ctx.put("newsfeedCache", NewsfeedCache.getInstance() ); ctx.put("requestParameters", rreq.getRequest().getParameterMap()); } //------------------------------------------------------------------------ protected static String loadWebsiteValues( Context ctx, RollerRequest rreq, RollerContext rollerCtx ) throws RollerException { String userName = null; UserData user = null; WebsiteData website = null; Roller mRoller = RollerFactory.getRoller(); Map props = mRoller.getPropertiesManager().getProperties(); if ( rreq.getRequest().getAttribute(RollerRequest.OWNING_USER) != null) { user = (UserData) rreq.getRequest().getAttribute(RollerRequest.OWNING_USER); } else if ( rreq.getUser() != null ) { user = rreq.getUser(); } if ( user != null ) { userName = user.getUserName(); website = rreq.getRoller().getUserManager().getWebsite(userName); ctx.put("userName", user.getUserName() ); ctx.put("fullName", user.getFullName() ); ctx.put("emailAddress", user.getEmailAddress() ); ctx.put("encodedEmail", RegexUtil.encode(user.getEmailAddress())); ctx.put("obfuscatedEmail", RegexUtil.obfuscateEmail(user.getEmailAddress())); // setup Locale for future rendering ctx.put("locale", website.getLocaleInstance()); // setup Timezone for future rendering ctx.put("timezone", website.getTimeZoneInstance()); } else { website = new WebsiteData(); website.setName(((RollerPropertyData)props.get("site.name")).getValue()); website.setAllowComments(Boolean.FALSE); website.setDescription(((RollerPropertyData)props.get("site.description")).getValue()); userName = "zzz_none_zzz"; ctx.put("userName",userName ); ctx.put("fullName","zzz_none_zzz"); ctx.put("emailAddress", ((RollerPropertyData)props.get("site.adminemail")).getValue()); ctx.put("locale", Locale.getDefault()); ctx.put("timezone", TimeZone.getDefault()); } ctx.put("website", website ); String siteName = ((RollerPropertyData)props.get("site.name")).getValue(); if ("Roller-based Site".equals(siteName)) siteName = "Main"; ctx.put("siteName", siteName); // add language of the session (using locale of viewer set by Struts) ctx.put( "viewLocale", LanguageUtil.getViewLocale(rreq.getRequest())); mLogger.debug("context viewLocale = "+ctx.get( "viewLocale")); return userName; } //------------------------------------------------------------------------ /** * Initialize PagePlugins declared in web.xml. By using the full class * name we also allow for the implementation of "external" Plugins * (maybe even packaged seperately). These classes are then later * instantiated by PageHelper. * * @param mContext */ public static void initializePagePlugins(ServletContext mContext) { String pluginStr = RollerConfig.getProperty("plugins.page"); if (mLogger.isDebugEnabled()) mLogger.debug(pluginStr); if (pluginStr != null) { String[] plugins = StringUtils.stripAll( StringUtils.split(pluginStr, ",") ); for (int i=0; i<plugins.length; i++) { if (mLogger.isDebugEnabled()) mLogger.debug("try " + plugins[i]); try { Class pluginClass = Class.forName(plugins[i]); if (isPagePlugin(pluginClass)) { mPagePlugins.add(pluginClass.newInstance()); } else { mLogger.warn(pluginClass + " is not a PagePlugin"); } } catch (ClassNotFoundException e) { mLogger.error("ClassNotFoundException for " + plugins[i]); } catch (InstantiationException e) { mLogger.error("InstantiationException for " + plugins[i]); } catch (IllegalAccessException e) { mLogger.error("IllegalAccessException for " + plugins[i]); } } } } /** * @param pluginClass * @return */ private static boolean isPagePlugin(Class pluginClass) { Class[] interfaces = pluginClass.getInterfaces(); for (int i=0; i<interfaces.length; i++) { if (interfaces[i].equals(PagePlugin.class)) return true; } return false; } public static boolean hasPlugins() { mLogger.debug("mPluginClasses.size(): " + mPagePlugins.size()); return (mPagePlugins != null && mPagePlugins.size() > 0); } public static List getPagePlugins() { return mPagePlugins; } private static final String TOOLBOX_KEY = "org.roller.presentation.velocity.toolbox"; private static final String TOOLBOX_MANAGER_KEY = "org.roller.presentation.velocity.toolboxManager"; private static ToolboxContext loadToolboxContext( HttpServletRequest request, HttpServletResponse response, Context ctx) { ServletContext servletContext = RollerContext.getServletContext(); // get the toolbox manager ServletToolboxManager toolboxManager = (ServletToolboxManager)servletContext.getAttribute(TOOLBOX_MANAGER_KEY); if (toolboxManager==null) { String file = RollerConfig.getProperty("velocity.toolbox.file"); mLogger.debug("Creating new toolboxContext using config-file: "+file); toolboxManager = ServletToolboxManager.getInstance(servletContext, file); servletContext.setAttribute(TOOLBOX_MANAGER_KEY, toolboxManager); } // load a toolbox context ChainedContext chainedContext = new ChainedContext(ctx, request, response, servletContext); ToolboxContext toolboxContext = toolboxManager.getToolboxContext(chainedContext); if (toolboxContext != null) { // add MessageTool to VelocityContext ctx.put("text", toolboxContext.internalGet("text")); /* Object[] keys = toolboxContext.internalGetKeys(); for (int i=0;i<keys.length;i++) { String key = (String)keys[i]; System.out.println("key = "+key); Object tool = toolboxContext.get(key); System.out.println("tool = "+tool); ctx.put(key, tool); } */ } return toolboxContext; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?