1 18 19 package org.apache.roller.ui.rendering.velocity.deprecated; 20 21 import java.net.MalformedURLException ; 22 import java.net.URL ; 23 import java.text.SimpleDateFormat ; 24 import java.util.ArrayList ; 25 import java.util.Date ; 26 import java.util.Locale ; 27 import java.util.Map ; 28 import javax.servlet.ServletContext ; 29 import javax.servlet.http.HttpServletRequest ; 30 import javax.servlet.http.HttpServletResponse ; 31 import javax.servlet.http.HttpSession ; 32 import javax.servlet.jsp.PageContext ; 33 import org.apache.commons.logging.Log; 34 import org.apache.commons.logging.LogFactory; 35 import org.apache.roller.RollerException; 36 import org.apache.roller.config.RollerConfig; 37 import org.apache.roller.config.RollerRuntimeConfig; 38 import org.apache.roller.model.Roller; 39 import org.apache.roller.model.RollerFactory; 40 import org.apache.roller.pojos.CommentData; 41 import org.apache.roller.pojos.FolderData; 42 import org.apache.roller.pojos.RollerPropertyData; 43 import org.apache.roller.pojos.Template; 44 import org.apache.roller.pojos.WeblogCategoryData; 45 import org.apache.roller.pojos.WeblogEntryData; 46 import org.apache.roller.pojos.WebsiteData; 47 import org.apache.roller.pojos.wrapper.CommentDataWrapper; 48 import org.apache.roller.pojos.wrapper.TemplateWrapper; 49 import org.apache.roller.pojos.wrapper.WeblogEntryDataWrapper; 50 import org.apache.roller.pojos.wrapper.WebsiteDataWrapper; 51 import org.apache.roller.ui.core.RollerContext; 52 import org.apache.roller.ui.core.RollerSession; 53 import org.apache.roller.ui.rendering.velocity.deprecated.NewsfeedCache; 54 import org.apache.roller.ui.rendering.util.WeblogEntryCommentForm; 55 import org.apache.roller.ui.rendering.util.WeblogPageRequest; 56 import org.apache.roller.util.DateUtil; 57 import org.apache.roller.util.RegexUtil; 58 import org.apache.roller.util.URLUtilities; 59 import org.apache.struts.util.RequestUtils; 60 import org.apache.velocity.VelocityContext; 61 62 63 70 public class ContextLoader { 71 72 public static final String WEBLOG_KEY = "weblog"; 73 public static final String ANCHOR_KEY = "entry"; 74 public static final String ANCHOR_KEY_OLD = "anchor"; 75 public static final String USERNAME_KEY = "username"; 76 77 public static final String PAGELINK_KEY = "pagelink"; 78 public static final String EXCERPTS_KEY = "excerpts"; 79 public static final String WEBLOGENTRY_COUNT = "count"; 80 public static final String WEBLOGCATEGORYNAME_KEY = "cat"; 81 public static final String WEBLOGENTRIES_KEY = "entries"; 82 public static final String WEBLOGDAY_KEY = "day"; 83 84 public static final String WEBLOGENTRYID_KEY = "entryid"; 85 86 public static final String WEBLOGCATEGORYID_KEY = "categoryId"; 87 public static final String PINGTARGETID_KEY = "pingtargetId"; 88 public static final String REFERERID_KEY = "refId"; 89 public static final String WEBLOGCOMMENTID_KEY = "commentId"; 90 public static final String WEBSITEID_KEY = "websiteId"; 91 public static final String BOOKMARKID_KEY = "bookmarkId"; 92 public static final String FOLDERID_KEY = "folderId"; 93 public static final String PARENTID_KEY = "parentId"; 94 public static final String NEWSFEEDID_KEY = "feedId"; 95 public static final String PAGEID_KEY = "pageId"; 96 public static final String LOGIN_COOKIE = "sessionId"; 97 public static final String OWNING_WEBSITE = "OWNING_WEBSITE"; 98 99 private static Log mLogger = LogFactory.getLog(ContextLoader.class); 100 101 102 106 public static void setupContext( 107 Map ctx, 108 HttpServletRequest request, 109 HttpServletResponse response, 110 PageContext pageContext, 111 WeblogPageRequest pageRequest) throws RollerException { 112 113 mLogger.debug("setupContext( ctx = "+ctx+")"); 114 115 RollerContext rollerCtx = RollerContext.getRollerContext( ); 116 117 WebsiteData weblog = null; 118 WeblogEntryData entry = null; 119 WeblogCategoryData category = null; 120 Template page = null; 121 FolderData folder = null; Date date = null; 123 boolean isDay = false; 124 boolean isMonth = false; 125 String locale = null; 126 127 locale = pageRequest.getLocale(); 129 weblog = pageRequest.getWeblog(); 130 entry = pageRequest.getWeblogEntry(); 131 category = pageRequest.getWeblogCategory(); 132 page = pageRequest.getWeblogPage(); 133 if(page == null) { 134 page = weblog.getDefaultPage(); 135 } 136 137 if(pageRequest.getWeblogDate() != null) { 139 140 Date now = new Date (); 141 if(pageRequest.getWeblogDate().length() == 8) { 142 isDay = true; 143 try { 144 date = DateUtil.get8charDateFormat().parse(pageRequest.getWeblogDate()); 145 if(date.after(now)) { 146 date = now; 147 } 148 } catch(Exception e) { 149 } 151 } else if(pageRequest.getWeblogDate().length() == 6) { 152 isMonth = true; 153 try { 154 date = DateUtil.get6charDateFormat().parse(pageRequest.getWeblogDate()); 155 if(date.after(now)) { 156 date = now; 157 } 158 } catch(Exception e) { 159 } 161 } else { 162 isMonth = true; 163 } 164 } 165 166 try { 167 OldWeblogPageModel pageModel = new OldWeblogPageModel(); 169 pageModel.init(request, 170 weblog, 171 entry, 172 category, 173 date, 174 isDay, 175 isMonth, 176 locale); 177 ctx.put("pageModel", pageModel); 178 179 ctx.put("pages", pageModel.getPages()); 181 182 } catch (Exception e) { 183 throw new RollerException("ERROR creating Page Model",e); 184 } 185 186 OldPageHelper pageHelper = new OldPageHelper(request, 188 response, 189 ctx, 190 weblog, 191 (date == null) ? new Date () : date, 192 folder, 193 page.getName(), 194 pageContext, 195 pageRequest); 196 ctx.put("pageHelper", pageHelper); 197 198 loadWeblogValues(ctx, weblog, pageRequest.getLocaleInstance(), request); 200 loadPathValues(ctx, request, rollerCtx, weblog, locale); 201 loadRssValues(ctx, request, weblog, category); 202 loadUtilityObjects(ctx, request, rollerCtx, weblog, page); 203 loadRequestParamKeys(ctx); 204 loadStatusMessage(ctx, request); 205 206 if (entry != null) { 208 loadCommentValues(ctx, request, entry); 209 } 210 } 211 212 213 216 private static void loadWeblogValues( 217 Map ctx, 218 WebsiteData weblog, 219 Locale locale, 220 HttpServletRequest request) throws RollerException { 221 222 if(weblog == null) 224 return; 225 226 Roller mRoller = RollerFactory.getRoller(); 227 Map props = mRoller.getPropertiesManager().getProperties(); 228 229 ctx.put("userName", weblog.getHandle()); 230 ctx.put("fullName", weblog.getName() ); 231 ctx.put("emailAddress", weblog.getEmailAddress() ); 232 ctx.put("encodedEmail", RegexUtil.encode(weblog.getEmailAddress())); 233 ctx.put("obfuscatedEmail", RegexUtil.obfuscateEmail(weblog.getEmailAddress())); 234 235 ctx.put("locale", weblog.getLocaleInstance()); 237 238 ctx.put("timezone", weblog.getTimeZoneInstance()); 240 ctx.put("timeZone", weblog.getTimeZoneInstance()); 241 ctx.put("website", WebsiteDataWrapper.wrap(weblog) ); 242 243 String siteName = ((RollerPropertyData)props.get("site.name")).getValue(); 244 if ("Roller-based Site".equals(siteName)) siteName = "Main"; 245 ctx.put("siteName", siteName); 246 247 String siteShortName = ((RollerPropertyData)props.get("site.shortName")).getValue(); 248 ctx.put("siteShortName", siteShortName); 249 250 ctx.put("viewLocale", locale); 252 mLogger.debug("context viewLocale = "+ctx.get( "viewLocale")); 253 254 Template entryPage = weblog.getPageByName("_entry"); 256 if (entryPage != null) { 257 ctx.put("entryPage", TemplateWrapper.wrap(entryPage)); 258 } 259 265 boolean commentsEnabled = 266 RollerRuntimeConfig.getBooleanProperty("users.comments.enabled"); 267 boolean trackbacksEnabled = 268 RollerRuntimeConfig.getBooleanProperty("users.trackbacks.enabled"); 269 boolean linkbacksEnabled = 270 RollerRuntimeConfig.getBooleanProperty("site.linkbacks.enabled"); 271 272 ctx.put("commentsEnabled", new Boolean (commentsEnabled) ); 273 ctx.put("trackbacksEnabled", new Boolean (trackbacksEnabled) ); 274 ctx.put("linkbacksEnabled", new Boolean (linkbacksEnabled) ); 275 } 276 277 278 281 private static void loadCommentValues( 282 Map ctx, 283 HttpServletRequest request, 284 WeblogEntryData entry) throws RollerException { 285 286 mLogger.debug("Loading comment values"); 287 288 String escapeHtml = 289 RollerRuntimeConfig.getProperty("users.comments.escapehtml"); 290 String autoFormat = 291 RollerRuntimeConfig.getProperty("users.comments.autoformat"); 292 ctx.put("isCommentPage", Boolean.TRUE); 293 ctx.put("escapeHtml", new Boolean (escapeHtml) ); 294 ctx.put("autoformat", new Boolean (autoFormat) ); 295 296 WeblogEntryCommentForm commentForm = 298 (WeblogEntryCommentForm) request.getAttribute("commentForm"); 299 if ( commentForm == null ) { 300 commentForm = new WeblogEntryCommentForm(); 301 302 commentForm.setName(""); 304 commentForm.setEmail(""); 305 commentForm.setUrl(""); 306 commentForm.setContent(""); 307 } 308 ctx.put("commentForm",commentForm); 309 310 if(commentForm.isPreview()) { 312 ArrayList list = new ArrayList (); 313 list.add(commentForm.getPreviewComment()); 314 ctx.put("previewComments", list); 315 } 316 317 if (entry.getStatus().equals(WeblogEntryData.PUBLISHED)) { 318 ctx.put("entry", WeblogEntryDataWrapper.wrap(entry)); 319 } 320 } 321 322 323 326 private static void loadRssValues( 327 Map ctx, 328 HttpServletRequest request, 329 WebsiteData website, 330 WeblogCategoryData category) 331 throws RollerException { 332 333 mLogger.debug("Loading rss values"); 334 335 int entryLength = -1; 336 String sExcerpts = request.getParameter("excerpts"); 337 if ( sExcerpts!=null && sExcerpts.equalsIgnoreCase("true")) { 338 entryLength = 150; 339 } 340 ctx.put("entryLength", new Integer (entryLength)); 341 342 int entryCount = website.getEntryDisplayCount(); 344 345 int maxEntries = 347 RollerRuntimeConfig.getIntProperty("site.newsfeeds.maxEntries"); 348 int defaultEntries = 349 RollerRuntimeConfig.getIntProperty("site.newsfeeds.defaultEntries"); 350 if (entryCount < 1) entryCount = defaultEntries; 351 if (entryCount > maxEntries) entryCount = maxEntries; 352 ctx.put("entryCount", new Integer (entryCount)); 353 354 String catname = null; 355 String catPath = null; 356 if (category != null ) { 357 catname = category.getName(); 358 catPath = category.getPath(); 359 } 360 ctx.put("catname", (catname!=null) ? catname : ""); 361 ctx.put("catPath", (catPath != null) ? catPath : ""); 362 ctx.put("updateTime", website.getLastModified()); 363 ctx.put("now", new Date ()); 364 } 365 366 367 370 private static void loadUtilityObjects( 371 Map ctx, 372 HttpServletRequest request, 373 RollerContext rollerCtx, 374 WebsiteData website, 375 Template page) throws RollerException { 376 377 mLogger.debug("Loading utility objects"); 378 379 Locale viewLocale = (Locale ) ctx.get("viewLocale"); 382 SimpleDateFormat sdf = new SimpleDateFormat ("yyyyMMdd", viewLocale); 383 if (website != null) { 384 sdf.setTimeZone(website.getTimeZoneInstance()); 385 } 386 ctx.put("dateFormatter", sdf ); 388 389 ctx.put("plainFormat", "yyyyMMdd"); 393 394 ctx.put("page", TemplateWrapper.wrap(page)); 395 ctx.put("utilities", new OldUtilities() ); 396 ctx.put("stringUtils", new OldStringUtils() ); 397 ctx.put("rollerVersion", rollerCtx.getRollerVersion() ); 398 ctx.put("rollerBuildTime", rollerCtx.getRollerBuildTime() ); 399 ctx.put("rollerBuildUser", rollerCtx.getRollerBuildUser() ); 400 ctx.put("newsfeedCache", NewsfeedCache.getInstance() ); 401 402 ctx.put("requestParameters", request.getParameterMap()); 403 } 404 405 406 409 private static void loadPathValues( 410 Map ctx, 411 HttpServletRequest request, 412 RollerContext rollerCtx, 413 WebsiteData website, 414 String locale) throws RollerException { 415 416 mLogger.debug("Loading path values"); 417 418 String url = null; 419 if (website != null && !"zzz_none_zzz".equals(website.getHandle())) { 420 url = URLUtilities.getWeblogURL(website, locale, true); 421 } else { 422 url= RollerRuntimeConfig.getAbsoluteContextURL(); 423 } 424 ctx.put("websiteURL", url); 425 ctx.put("baseURL", RollerRuntimeConfig.getRelativeContextURL() ); 426 ctx.put("absBaseURL", RollerRuntimeConfig.getAbsoluteContextURL() ); 427 ctx.put("ctxPath", RollerRuntimeConfig.getRelativeContextURL() ); 428 ctx.put("uploadPath", ContextLoader.figureResourcePath()); 429 430 try { 431 URL absUrl = RequestUtils.absoluteURL(request, "/"); 432 ctx.put("host", absUrl.getHost()); 433 } catch (MalformedURLException e) { 434 throw new RollerException(e); 435 } 436 } 437 438 439 442 private static String figureResourcePath() { 443 444 String uploadurl = null; 445 try { 446 uploadurl = RollerFactory.getRoller().getFileManager().getUploadUrl(); 447 } catch(Exception e) {} 448 449 return uploadurl; 450 } 451 452 453 457 private static void loadStatusMessage(Map ctx, HttpServletRequest req) { 458 459 mLogger.debug("Loading status message"); 460 461 HttpSession session = req.getSession(false); 462 String msg = null; 463 if (session != null) 464 msg = (String )session.getAttribute(RollerSession.ERROR_MESSAGE); 465 if (msg != null) { 466 ctx.put("errorMessage", msg); 467 session.removeAttribute(RollerSession.ERROR_MESSAGE); 468 } 469 470 if (session != null) 471 msg = (String )session.getAttribute(RollerSession.STATUS_MESSAGE); 472 if (msg != null) { 473 ctx.put("statusMessage", msg); 474 session.removeAttribute(RollerSession.STATUS_MESSAGE); 475 } 476 } 477 478 479 private static void loadRequestParamKeys(Map ctx) { 480 481 mLogger.debug("Loading request param keys"); 482 483 ctx.put("USERNAME_KEY", USERNAME_KEY); 486 ctx.put("WEBSITEID_KEY", WEBSITEID_KEY); 487 ctx.put("FOLDERID_KEY", FOLDERID_KEY); 488 ctx.put("NEWSFEEDID_KEY", NEWSFEEDID_KEY); 489 ctx.put("PAGEID_KEY", PAGEID_KEY); 490 ctx.put("PAGELINK_KEY", PAGELINK_KEY); 491 ctx.put("ANCHOR_KEY", ANCHOR_KEY); 492 ctx.put("EXCERPTS_KEY", EXCERPTS_KEY); 493 ctx.put("BOOKMARKID_KEY", BOOKMARKID_KEY); 494 ctx.put("REFERERID_KEY", REFERERID_KEY); 495 ctx.put("WEBLOGENTRYID_KEY", WEBLOGENTRYID_KEY); 496 ctx.put("WEBLOGCATEGORYNAME_KEY", WEBLOGCATEGORYNAME_KEY); 497 ctx.put("WEBLOGCATEGORYID_KEY", WEBLOGENTRIES_KEY); 498 ctx.put("WEBLOGENTRIES_KEY", WEBLOGENTRIES_KEY); 499 ctx.put("WEBLOGDAY_KEY", WEBLOGDAY_KEY); 500 ctx.put("WEBLOGCOMMENTID_KEY", WEBLOGCOMMENTID_KEY); 501 } 502 503 } 504 | Popular Tags |