1 package org.roller.presentation.website.actions; 2 3 import org.apache.commons.logging.Log; 4 import org.apache.commons.logging.LogFactory; 5 import org.apache.struts.action.ActionError; 6 import org.apache.struts.action.ActionErrors; 7 import org.apache.struts.action.ActionForm; 8 import org.apache.struts.action.ActionForward; 9 import org.apache.struts.action.ActionMapping; 10 import org.apache.struts.actions.DispatchAction; 11 import org.roller.RollerException; 12 import org.roller.model.UserManager; 13 import org.roller.pojos.PageData; 14 import org.roller.pojos.UserData; 15 import org.roller.pojos.WebsiteData; 16 import org.roller.presentation.RollerContext; 17 import org.roller.presentation.RollerRequest; 18 import org.roller.presentation.pagecache.PageCacheFilter; 19 import org.roller.presentation.velocity.PreviewResourceLoader; 20 import org.roller.presentation.website.ThemeCache; 21 import org.roller.presentation.website.formbeans.ThemeEditorForm; 22 23 import java.io.BufferedReader ; 24 import java.io.FileReader ; 25 import java.io.IOException ; 26 import java.util.Date ; 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 30 import javax.servlet.ServletContext ; 31 import javax.servlet.ServletException ; 32 import javax.servlet.http.HttpServletRequest ; 33 import javax.servlet.http.HttpServletResponse ; 34 import javax.servlet.http.HttpSession ; 35 36 import org.roller.config.RollerRuntimeConfig; 37 38 39 41 51 public class ThemeEditorAction extends DispatchAction 52 { 53 private static final String SESSION_TEMPLATE = "weblog.template"; 54 private static final String LAST_THEME = "weblog.prev.theme"; 55 private static Log mLogger = 56 LogFactory.getFactory().getInstance(ThemeEditorAction.class); 57 private ThemeCache themeCache = ThemeCache.getInstance(); 58 59 70 public ActionForward edit( 72 ActionMapping mapping, 73 ActionForm form, 74 HttpServletRequest request, 75 HttpServletResponse response) 76 throws IOException , ServletException 77 { 78 ActionErrors errors = new ActionErrors(); 79 ActionForward forward = mapping.findForward("editTheme.page"); 80 try 81 { 82 RollerRequest rreq = RollerRequest.getRollerRequest(request); 83 if ( rreq.isUserAuthorizedToEdit() ) 84 { 85 loadThemes( rreq, errors, true); 86 ThemeEditorForm teForm = (ThemeEditorForm)form; 87 88 teForm.setThemeName(null); 90 if (mLogger.isDebugEnabled()) 91 { 92 mLogger.debug("loaded themes, form="+teForm); 93 } 94 95 120 clearThemePages(rreq, 122 (String )request.getSession(true).getAttribute(LAST_THEME)); 123 124 PageData page = getDefaultPage( rreq ); 126 teForm.setThemeTemplate( page.getTemplate() ); 127 128 129 } 131 else 132 { 133 forward = mapping.findForward("access-denied"); 134 } 135 } 136 catch (Exception e) 137 { 138 mLogger.error("ERROR in action",e); 139 throw new ServletException (e); 140 } 141 return forward; 142 } 143 144 157 public ActionForward preview( 158 ActionMapping mapping, 159 ActionForm form, 160 HttpServletRequest request, 161 HttpServletResponse response) 162 throws IOException , ServletException 163 { 164 ActionErrors errors = new ActionErrors(); 165 ActionForward forward = mapping.findForward("editTheme.page"); 166 try 167 { 168 RollerRequest rreq = RollerRequest.getRollerRequest(request); 169 if ( rreq.isUserAuthorizedToEdit() ) 170 { 171 HttpSession session = request.getSession(); 172 ThemeEditorForm teForm = (ThemeEditorForm)form; 173 174 String theme = teForm.getThemeName(); 175 ServletContext ctx = rreq.getServletContext(); 176 RollerContext rollerContext = 177 RollerContext.getRollerContext( ctx ); 178 179 boolean showCustom = false; 180 if ( !"Custom".equals( theme ) ) 181 { 182 String sb = this.readTheme(rollerContext, theme); 184 teForm.setThemeTemplate( sb ); 185 186 clearThemePages(rreq, 188 (String ) session.getAttribute(LAST_THEME)); 189 190 setThemePages(rreq, theme); 191 session.setAttribute(LAST_THEME, theme); 192 } 193 else 194 { 195 showCustom = true; 196 clearThemePages(rreq, 197 (String ) session.getAttribute(LAST_THEME)); 198 199 session.removeAttribute(LAST_THEME); 200 202 } 205 loadThemes( rreq, errors, showCustom); 206 207 PageData page = getDefaultPage( rreq ); 210 PreviewResourceLoader.setTemplate(page.getId(), 211 teForm.getThemeTemplate(), rreq.getUser().getUserName() ); 212 213 session.setAttribute(SESSION_TEMPLATE, 215 teForm.getThemeTemplate() ); 216 } 217 else 218 { 219 forward = mapping.findForward("access-denied"); 220 } 221 } 222 catch (Exception e) 223 { 224 mLogger.error("ERROR in action",e); 225 throw new ServletException (e); 226 } 227 return forward; 228 } 229 230 242 public ActionForward save( 243 ActionMapping mapping, 244 ActionForm form, 245 HttpServletRequest request, 246 HttpServletResponse response) 247 throws IOException , ServletException 248 { 249 ActionErrors errors = new ActionErrors(); 250 ActionForward forward = mapping.findForward("editTheme.page"); 251 try 252 { 253 RollerRequest rreq = RollerRequest.getRollerRequest(request); 254 if ( rreq.isUserAuthorizedToEdit() ) 255 { 256 loadThemes( rreq, errors, true); 257 ThemeEditorForm teForm = (ThemeEditorForm)form; 258 String theme = teForm.getThemeName(); 259 ServletContext ctx = rreq.getServletContext(); 260 RollerContext rollerContext = 261 RollerContext.getRollerContext( ctx ); 262 263 String template = ""; 266 if ( "Custom".equals( theme ) ) 267 { 268 template = teForm.getThemeTemplate(); 270 } 271 else 272 { 273 String sb = this.readTheme(rollerContext, theme); 275 template = sb; 276 } 277 278 UserData ud = rreq.getUser(); 280 281 PreviewResourceLoader.clearAllTemplates(ud.getUserName()); 283 request.getSession().removeAttribute(SESSION_TEMPLATE); 284 285 UserManager mgr = rreq.getRoller().getUserManager(); 287 PageData page = getDefaultPage( rreq ); 288 289 page.setTemplate( template ); 290 mgr.storePage( page); 291 292 saveThemePages( rreq, theme); 293 294 setThemePages(rreq, theme); 296 297 PageCacheFilter.removeFromCache( request, ud ); 299 teForm.setThemeName("Custom"); 300 } 301 else 302 { 303 forward = mapping.findForward("access-denied"); 304 } 305 } 306 catch (Exception e) 307 { 308 mLogger.error("ERROR in action",e); 309 throw new ServletException (e); 310 } 311 return forward; 312 } 313 314 315 326 public ActionForward cancel( 327 ActionMapping mapping, 328 ActionForm form, 329 HttpServletRequest request, 330 HttpServletResponse response) 331 throws IOException , ServletException 332 { 333 ActionForward forward = mapping.findForward("editTheme"); 334 try 335 { 336 RollerRequest rreq = RollerRequest.getRollerRequest(request); 337 if ( rreq.isUserAuthorizedToEdit() ) 338 { 339 UserData ud = rreq.getUser(); 341 PageCacheFilter.removeFromCache( request, ud ); 342 ThemeEditorForm teForm = (ThemeEditorForm)form; 343 344 PreviewResourceLoader.clearAllTemplates( ud.getUserName() ); 346 request.getSession().removeAttribute(SESSION_TEMPLATE); 347 teForm.setThemeName("Custom"); 348 } 349 else 350 { 351 forward = mapping.findForward("access-denied"); 352 } 353 } 354 catch (Exception e) 355 { 356 mLogger.error("ERROR in action",e); 357 throw new ServletException (e); 358 } 359 return forward; 360 } 361 362 368 private void loadThemes( 369 RollerRequest rreq, ActionErrors errors, boolean listCustom) 370 { 371 HttpSession session = rreq.getRequest().getSession(false); 372 try 373 { 374 ServletContext ctx = rreq.getServletContext(); 376 String [] themes = null; 377 if (ctx.getAttribute("themeStore") != null) 378 { 379 themes = (String []) ctx.getAttribute("themeStore"); 380 } 381 else 382 { 383 RollerContext rollerContext = 384 RollerContext.getRollerContext( ctx ); 385 themes = rollerContext.getThemeNames(); 386 ctx.setAttribute("themeStore", themes); 387 } 388 389 if (listCustom) 392 { 393 String [] themes2 = new String [ themes.length+1 ]; 395 themes2[0] = "Custom"; 396 for (int i=1; i<themes2.length; i++) 397 { 398 themes2[i] = themes[i-1]; 399 } 400 themes = themes2; 401 } 402 session.setAttribute( "themes", themes ); 403 } 404 catch (Exception e) 405 { 406 errors.add(ActionErrors.GLOBAL_ERROR, 407 new ActionError("error.editing.user", e.toString())); 408 } 409 } 410 411 417 private PageData getDefaultPage(RollerRequest rreq) throws RollerException 418 { 419 try 420 { 421 UserData ud = rreq.getUser(); 422 UserManager mgr = rreq.getRoller().getUserManager(); 423 WebsiteData wd = mgr.getWebsite( ud.getUserName() ); 424 String defaultPageId = wd.getDefaultPageId(); 425 return mgr.retrievePage( defaultPageId ); 426 } 427 catch (Exception e) 428 { 429 mLogger.error("ERROR in action",e); 430 throw new RollerException( e ); 431 } 432 } 433 434 441 private void setThemePages( RollerRequest rreq, String theme ) 442 throws RollerException 443 { 444 RollerContext rollerContext = 445 RollerContext.getRollerContext(rreq.getRequest()); 446 447 try 448 { 449 UserData ud = rreq.getUser(); 450 UserManager mgr = rreq.getRoller().getUserManager(); 451 String username = ud.getUserName(); 452 453 HashMap pages = rollerContext.readThemeMacros(theme); 454 Iterator iter = pages.keySet().iterator(); 455 while ( iter.hasNext() ) 456 { 457 String pageName = (String ) iter.next(); 458 String sb = (String )pages.get( pageName ); 459 460 PageData page = mgr.getPageByName( rreq.getWebsite(), pageName ); 461 if (page != null) 462 { 463 PreviewResourceLoader.setTemplate(page.getId(),sb,username); 464 } 465 else 466 { 467 PreviewResourceLoader.setTemplate(pageName, sb, username); 468 } 469 } 470 } 471 catch (Exception e) 472 { 473 mLogger.error("ERROR in action",e); 474 throw new RollerException( e ); 475 } 476 477 } 478 479 486 private void clearThemePages( RollerRequest rreq, String theme ) 487 throws RollerException 488 { 489 if (mLogger.isDebugEnabled()) 490 { 491 mLogger.debug("theme="+theme); 492 } 493 494 if (theme == null) return; 495 496 RollerContext rollerContext = 497 RollerContext.getRollerContext(rreq.getRequest()); 498 499 try 500 { 501 UserData ud = rreq.getUser(); 502 UserManager mgr = rreq.getRoller().getUserManager(); 503 String username = ud.getUserName(); 504 505 String themeDir = rollerContext.getThemePath(theme); 506 String [] children = RollerContext.getThemeFilenames(themeDir); 507 508 if (children == null) return; 510 511 for (int i = 0; i < children.length; i++) 512 { 513 String pageName = children[i].substring( 514 0,children[i].length()-3); 515 516 PageData page = mgr.getPageByName(rreq.getWebsite(), pageName); 517 if (page != null) 518 { 519 PreviewResourceLoader.clearTemplate( page.getId() ); 520 } 521 else 522 { 523 PreviewResourceLoader.clearTemplate( pageName ); 524 } 525 } 526 } 527 catch (Exception e) 528 { 529 if (mLogger.isDebugEnabled()) 530 { 531 mLogger.debug("clearThemePages error: ", e); 532 } 533 534 throw new RollerException( e ); 535 } 536 537 } 538 539 547 private void saveThemePages( RollerRequest rreq, String theme ) 548 throws RollerException 549 { 550 RollerContext rollerContext = 551 RollerContext.getRollerContext(rreq.getRequest()); 552 553 try 554 { 555 UserData ud = rreq.getUser(); 556 UserManager mgr = rreq.getRoller().getUserManager(); 557 String username = ud.getUserName(); 558 WebsiteData website = mgr.getWebsite( username ); 559 560 HashMap pages = rollerContext.readThemeMacros(theme); 561 Iterator iter = pages.keySet().iterator(); 562 while ( iter.hasNext() ) 563 { 564 String pageName = (String ) iter.next(); 565 String pageContent = (String )pages.get( pageName ); 566 567 PageData page = mgr.getPageByName( rreq.getWebsite(), pageName ); 568 if (page != null) 569 { 570 page.setTemplate( pageContent ); 572 } 573 else 574 { 575 page = new PageData( null, 577 website, pageName, pageName, pageName, pageContent, new Date () ); 584 mgr.storePage( page ); 585 } 586 } 587 rreq.getRoller().commit(); 588 } 589 catch (Exception e) 590 { 591 mLogger.error("ERROR in action",e); 592 throw new RollerException( e ); 593 } 594 595 } 596 597 605 public String readTheme(RollerContext ctx, String theme) 606 throws RollerException 607 { 608 String fileName = "Weblog.vm"; 609 if (themeCache.getFromCache(theme, fileName) != null) 610 { 611 return themeCache.getFromCache(theme, fileName); 612 } 613 614 String themesDir = RollerRuntimeConfig.getProperty("users.themes.path"); 615 616 String themeFile = RollerContext.getServletContext( 617 ).getRealPath( "/" + themesDir 618 + "/" + theme + "/" + fileName ); 619 620 StringBuffer sb = new StringBuffer (); 622 BufferedReader rdr = null; 623 try 624 { 625 rdr = new BufferedReader ( 626 new FileReader (themeFile)); 627 String line = null; 628 while ( null != (line = rdr.readLine()) ) 629 { 630 sb.append( line ); 631 sb.append("\n"); 632 } 633 themeCache.putIntoCache(theme, fileName, sb.toString()); 634 } 635 catch (Exception e) 636 { 637 mLogger.error("themeFile:" + themeFile, e); 638 throw new RollerException( e ); 639 } 640 finally 641 { 642 try { 643 if (rdr != null) rdr.close(); 644 } catch (IOException ioe) { 645 mLogger.warn("unable to close " + themeFile); 646 } 647 } 648 649 return sb.toString(); 650 } 651 } 652 | Popular Tags |