1 package org.roller.presentation.weblog.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.apache.velocity.VelocityContext; 12 import org.apache.velocity.app.Velocity; 13 import org.apache.velocity.exception.MethodInvocationException; 14 import org.apache.velocity.exception.ParseErrorException; 15 import org.apache.velocity.exception.ResourceNotFoundException; 16 import org.roller.RollerException; 17 import org.roller.model.UserManager; 18 import org.roller.model.WeblogManager; 19 import org.roller.pojos.PageData; 20 import org.roller.pojos.WebsiteData; 21 import org.roller.presentation.RollerContext; 22 import org.roller.presentation.RollerRequest; 23 import org.roller.presentation.RollerSession; 24 import org.roller.presentation.velocity.ContextLoader; 25 import org.roller.presentation.weblog.tags.WeblogEntryMacros; 26 import org.roller.util.DateUtil; 27 28 import java.io.File ; 29 import java.io.FileOutputStream ; 30 import java.io.IOException ; 31 import java.io.OutputStream ; 32 import java.io.StringWriter ; 33 import java.text.SimpleDateFormat ; 34 import java.util.Date ; 35 import java.util.Iterator ; 36 import java.util.List ; 37 import java.util.Map ; 38 39 import javax.servlet.ServletException ; 40 import javax.servlet.http.HttpServletRequest ; 41 import javax.servlet.http.HttpServletResponse ; 42 import javax.servlet.jsp.JspFactory ; 43 import javax.servlet.jsp.PageContext ; 44 45 56 public class BakeWeblogAction extends DispatchAction { 57 private static Log mLogger = 58 LogFactory.getFactory().getInstance(BakeWeblogAction.class); 59 60 public ActionForward load( 61 ActionMapping mapping, 62 ActionForm actionForm, 63 HttpServletRequest request, 64 HttpServletResponse response) 65 throws IOException , ServletException { 66 RollerRequest rreq = RollerRequest.getRollerRequest(request); 67 try { 68 if ( !rreq.isUserAuthorizedToEdit() ) { 69 return mapping.findForward("access-denied"); 70 } 71 } catch (Exception e) { 72 return mapping.findForward("access-denied"); 73 } 74 return mapping.findForward("baking.done"); 75 } 76 77 public ActionForward bake( 78 ActionMapping mapping, 79 ActionForm actionForm, 80 HttpServletRequest request, 81 HttpServletResponse response) 82 throws IOException , ServletException { 83 ActionForward forward = mapping.findForward("baking.done"); 84 ActionErrors errors = new ActionErrors(); 85 try { 86 PageContext pageContext = 87 JspFactory.getDefaultFactory().getPageContext( 88 this.getServlet(), request, response,"", true, 8192, true); 89 RollerRequest rreq = RollerRequest.getRollerRequest(pageContext); 90 if ( !rreq.isUserAuthorizedToEdit() ) { 91 return mapping.findForward("access-denied"); 92 } 93 94 String pid = getDefaultPageId( rreq ); 95 96 VelocityContext context = new VelocityContext(); 97 ContextLoader.setupContext( context, rreq, response ); 98 99 WeblogManager mgr = rreq.getRoller().getWeblogManager(); 101 108 Map entryMap = mgr.getWeblogEntryObjectMap( 109 rreq.getWebsite(), 110 null, rreq.getDate(true), null, WeblogManager.PUB_ONLY, null); 116 if (mLogger.isDebugEnabled()) { 117 mLogger.debug("Num Days to Bake: " + entryMap.size()); 118 } 119 120 Iterator iter = entryMap.keySet().iterator(); 121 if (iter.hasNext()) { 122 Date d = (Date )iter.next(); 123 124 if (mLogger.isDebugEnabled()) { 125 mLogger.debug("Bake Weblog for date:" + d); 126 } 127 128 WeblogEntryMacros macros = 130 new WeblogEntryMacros( rreq.getPageContext(), d ); 131 context.put( "macros", macros ); 132 133 List entries = (List ) entryMap.get( d ); 134 String content = transformTemplate(pid, entries, context, rreq); 135 136 String fileName = writeToFile(content, d, rreq); 137 request.getSession().setAttribute( 138 RollerSession.STATUS_MESSAGE, 139 "Weblog baked to :" + fileName); 140 } 141 142 143 String message = (String ) request.getSession().getAttribute( 144 RollerSession.STATUS_MESSAGE); 145 if (message == null) message = "No Files Written"; 146 request.getSession().setAttribute( 147 RollerSession.STATUS_MESSAGE, 148 message); 149 } catch (Exception e) { 150 forward = mapping.findForward("error"); 151 152 errors.add(ActionErrors.GLOBAL_ERROR, 153 new ActionError("error.bake.weblog", e.toString())); 154 saveErrors(request,errors); 155 156 mLogger.error(getResources(request).getMessage("error.bake.weblog") 157 + e.toString(),e); 158 } 159 160 return forward; 161 } 162 163 171 private String writeToFile(String content, Date d, RollerRequest rreq) 172 throws IOException { 173 if (mLogger.isDebugEnabled()) { 174 mLogger.debug("Write file for:" + d); 175 } 176 177 String dir = RollerContext.getUploadDir( rreq.getServletContext() ); 178 String username = rreq.getUser().getUserName(); 179 File dirF = new File (dir + username + File.separator + "baked"); 180 if (!dirF.exists()) { 181 dirF.mkdirs(); 182 } 183 184 SimpleDateFormat mFmt = DateUtil.get8charDateFormat(); 185 String dString = mFmt.format( d ) + ".html"; 186 File pFile = new File (dirF, dString); 187 188 OutputStream bos = null; 189 try { 190 bos = new FileOutputStream (pFile); 191 bos.write( content.getBytes() ); 192 } catch (IOException e) { 193 mLogger.error("Unable to write to " + pFile.getName()); 194 } finally { 195 try { 196 if (bos != null) bos.close(); 197 } catch (IOException e1) { 198 mLogger.error("Unable to close stream to " + pFile.getName()); 199 } 200 } 201 202 return pFile.getName(); 203 } 204 205 216 private String transformTemplate(String pid, List entries, 217 VelocityContext context, RollerRequest rreq) 218 throws 219 ResourceNotFoundException, 220 ParseErrorException, 221 MethodInvocationException, 222 Exception { 223 224 context.put( "entries", entries ); 225 226 StringWriter sw = new StringWriter (); 227 Velocity.mergeTemplate(pid, Velocity.ENCODING_DEFAULT, context, sw ); 228 return sw.toString(); 229 } 230 231 239 private String getDefaultPageId(RollerRequest rreq) throws RollerException { 240 UserManager userMgr = rreq.getRoller().getUserManager(); 241 WebsiteData wd = rreq.getWebsite(); 242 PageData pd = userMgr.retrievePage(wd.getDefaultPageId()); 243 return pd.getId(); 244 } 245 246 } 247 | Popular Tags |