1 package org.roller.presentation.velocity; 2 3 import java.io.IOException ; 4 import java.io.StringWriter ; 5 import java.util.Map ; 6 7 import javax.servlet.ServletConfig ; 8 import javax.servlet.ServletException ; 9 import javax.servlet.http.HttpServletRequest ; 10 import javax.servlet.http.HttpServletResponse ; 11 import javax.servlet.jsp.JspFactory ; 12 import javax.servlet.jsp.PageContext ; 13 14 import org.apache.commons.logging.Log; 15 import org.apache.commons.logging.LogFactory; 16 import org.apache.velocity.Template; 17 import org.apache.velocity.VelocityContext; 18 import org.apache.velocity.context.Context; 19 import org.apache.velocity.exception.ParseErrorException; 20 import org.apache.velocity.exception.ResourceNotFoundException; 21 import org.apache.velocity.servlet.VelocityServlet; 22 import org.roller.RollerException; 23 import org.roller.model.UserManager; 24 import org.roller.pojos.PageData; 25 import org.roller.pojos.UserData; 26 import org.roller.pojos.WebsiteData; 27 import org.roller.presentation.RollerRequest; 28 29 37 public abstract class BasePageServlet extends VelocityServlet 38 { 39 private static Log mLogger = 40 LogFactory.getFactory().getInstance(BasePageServlet.class); 41 46 public void init( ServletConfig config ) 47 throws ServletException 48 { 49 super.init( config ); 50 WebappResourceLoader.setServletContext( getServletContext() ); 51 } 52 public Template handleRequest( HttpServletRequest request, 53 HttpServletResponse response, 54 Context ctx ) throws Exception 55 { 56 String pid = null; 57 Template outty = null; 58 Exception pageException = null; 59 60 try 61 { 62 PageContext pageContext = 63 JspFactory.getDefaultFactory().getPageContext( 64 this, request, response,"", true, 8192, true); 65 RollerRequest rreq = RollerRequest.getRollerRequest(pageContext); 67 UserManager userMgr = rreq.getRoller().getUserManager(); 68 69 WebsiteData wd = null; 70 if (request.getAttribute(RollerRequest.OWNING_USER) != null) { 71 UserData user = (UserData) 72 request.getAttribute(RollerRequest.OWNING_USER); 73 wd = userMgr.getWebsite(user.getUserName()); 74 } 75 else 76 { 77 wd = rreq.getWebsite(); 78 } 79 80 PageData pd = null; 82 if (rreq.getPage() != null && request.getAttribute(RollerRequest.OWNING_USER) == null) 84 { 85 pd = rreq.getPage(); 86 pid = pd.getId(); 87 } 88 else if (wd != null) 90 { 91 pd = userMgr.retrievePage(wd.getDefaultPageId()); 92 pid = pd.getId(); 93 rreq.setPage(pd); 94 } 95 if ( pid == null ) 97 { 98 throw new ResourceNotFoundException("Page not found"); 99 } 100 101 outty = prepareForPageExecution(ctx, rreq, response, pd); 102 } 103 catch( Exception e ) 104 { 105 pageException = e; 106 response.setStatus( HttpServletResponse.SC_INTERNAL_SERVER_ERROR); 107 } 108 if (pageException != null) 109 { 110 mLogger.error("EXCEPTION: in RollerServlet", pageException); 111 request.setAttribute("DisplayException", pageException); 112 } 113 return outty; 114 } 115 116 125 private Template findDecorator(String decoratorName, UserManager userMgr, WebsiteData wd) 126 throws ResourceNotFoundException, ParseErrorException, RollerException, Exception 127 { 128 Template decorator = null; 129 PageData decoratorPage = null; 130 String decoratorId = null; 131 132 if (decoratorName != null) 134 { 135 decoratorPage = userMgr.getPageByName(wd, decoratorName); 136 if (decoratorPage != null) 137 { 138 decoratorId = decoratorPage.getId(); 139 } 140 } 141 142 if (decoratorPage == null) 144 { 145 decoratorPage = userMgr.getPageByName(wd, "_decorator"); 146 if (decoratorPage != null) 147 { 148 decoratorId = decoratorPage.getId(); 149 } 150 else 151 { 152 decoratorId = "_decorator"; 154 } 155 } 156 157 if (decoratorId != null) 159 { 160 try 161 { 162 decorator = getTemplate(decoratorId, "UTF-8"); 163 } 164 catch (Exception e) 165 { 166 } 168 } 169 170 if (decorator == null) 172 { 173 decorator = getTemplate("/themes/noop_decorator.vm", "UTF-8"); 174 } 175 return decorator; 176 } 177 178 182 protected Template prepareForPageExecution(Context ctx, RollerRequest rreq, 183 HttpServletResponse response, PageData pd) throws Exception 184 { 185 Template outty = null; 186 UserManager userMgr = rreq.getRoller().getUserManager(); 187 WebsiteData wd = pd.getWebsite(); 188 189 String pageLink = pd.getLink(); 191 String mimeType = getServletConfig().getServletContext().getMimeType(pageLink); 192 if(mimeType != null) { 193 response.setContentType(mimeType); 195 } 196 197 209 210 ContextLoader.setupContext( ctx, rreq, response ); 212 213 outty = getTemplate( pd.getId(), "UTF-8" ); 215 216 219 if (wd != null) 220 { 221 StringWriter sw = new StringWriter (); 223 outty.merge(ctx, sw); 224 ctx.put("decorator_body", sw.toString()); 225 226 outty = findDecorator((String )ctx.get("decorator"), userMgr, wd); 228 } 229 return outty; 230 } 231 232 236 protected void error( HttpServletRequest req, HttpServletResponse res, 237 Exception e) throws ServletException , IOException 238 { 239 mLogger.warn("ERROR in VelocityServlet",e); 240 } 241 242 247 protected Context createContext( 248 HttpServletRequest req, 249 HttpServletResponse res) { 250 251 VelocityContext context = new VelocityContext(); 252 context.put(REQUEST, new RequestWrapper(req.getParameterMap())); 253 return context; 254 255 } 256 257 258 public static class RequestWrapper 259 { 260 Map params = null; 261 public RequestWrapper(Map params) 262 { 263 this.params = params; 264 } 265 public String getParameter(String key) 266 { 267 String ret = null; 268 String [] array = (String [])params.get(key); 269 if (array != null && array.length > 0) 270 { 271 ret = array[0]; 272 } 273 return ret; 274 } 275 } 276 } 277 | Popular Tags |