1 4 package com.opensymphony.webwork.portlet.servlet; 5 6 import com.opensymphony.module.sitemesh.*; 7 import com.opensymphony.module.sitemesh.filter.PageResponseWrapper; 8 import com.opensymphony.module.sitemesh.util.Container; 9 import com.opensymphony.module.sitemesh.util.OutputConverter; 10 import com.opensymphony.util.TextUtils; 11 import com.opensymphony.webwork.ServletActionContext; 12 import com.opensymphony.webwork.portlet.util.FileUtils; 13 import com.opensymphony.webwork.portlet.util.GeneralUtil; 14 import com.opensymphony.webwork.portlet.velocity.VelocityManager; 15 import com.opensymphony.xwork.ActionContext; 16 import com.opensymphony.xwork.util.OgnlValueStack; 17 import org.apache.velocity.Template; 18 import org.apache.velocity.app.VelocityEngine; 19 import org.apache.velocity.context.Context; 20 21 import javax.servlet.RequestDispatcher ; 22 import javax.servlet.ServletConfig ; 23 import javax.servlet.ServletContext ; 24 import javax.servlet.ServletException ; 25 import javax.servlet.http.HttpServlet ; 26 import javax.servlet.http.HttpServletRequest ; 27 import javax.servlet.http.HttpServletResponse ; 28 import java.io.IOException ; 29 import java.io.PrintWriter ; 30 import java.io.StringWriter ; 31 32 36 public abstract class WebWorkAbstractServlet extends HttpServlet implements RequestConstants { 37 40 private VelocityManager velocityManager; 41 42 private Factory factory = null; 43 44 private ServletConfig servletConfig = null; 45 46 private static final TextUtils TEXT_UTILS = new TextUtils(); 47 48 private static final FileUtils FILE_UTILS = new FileUtils(); 49 50 private static final GeneralUtil GENERAL_UTIL = new GeneralUtil(); 51 52 55 public WebWorkAbstractServlet() { 56 velocityManager = VelocityManager.getInstance(); 57 } 58 59 62 protected abstract void doRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException ; 63 64 public void init(ServletConfig servletConfig) throws ServletException { 65 super.init(servletConfig); 66 67 this.servletConfig = servletConfig; 68 factory = Factory.getInstance(new Config (servletConfig)); 69 70 servletConfig.getServletContext().setAttribute("webwork.servlet", this); 71 72 VelocityManager.getInstance().init(getServletContext()); 74 } 75 76 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException { 77 doFilter(request, response); 78 } 79 80 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException { 81 doFilter(request, response); 82 } 83 84 protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException { 85 doFilter(request, response); 86 } 87 88 public void doFilter(HttpServletRequest request, HttpServletResponse response) throws IOException , ServletException { 89 90 com.opensymphony.webwork.portlet.context.PortletContext.getContext().setSiteMeshFactory(factory); 92 93 OgnlValueStack stack = ActionContext.getContext().getValueStack(); 94 95 request.setAttribute(ServletActionContext.WEBWORK_VALUESTACK_KEY, stack); 96 stack.getContext().put(ServletActionContext.HTTP_REQUEST, request); 97 stack.getContext().put(ServletActionContext.HTTP_RESPONSE, response); 98 stack.getContext().put(ServletActionContext.SERVLET_CONTEXT, getServletContext()); 99 100 if (factory.isPathExcluded(extractRequestPath(request))) { 103 doRequest(request, response); 105 } else { 106 109 if (Container.get() == Container.TOMCAT) { 113 request.getSession(true); 114 } 115 116 Page page = parsePage(request, response); 119 120 if (page != null) { 121 page.setRequest(request); 122 123 Decorator decorator = factory.getDecoratorMapper().getDecorator(request, page); 124 if (decorator != null && decorator.getPage() != null) { 125 applyDecorator(page, decorator, request, response); 126 page = null; 127 return; 128 } 129 130 writeOriginal(request, response, page); 135 page = null; 136 } 137 138 } 139 } 140 141 protected void applyDecorator(Page page, Decorator decorator, HttpServletRequest request, HttpServletResponse response) 142 throws ServletException , IOException { 143 144 String decoratorPageName = decorator.getPage(); 145 146 if (decoratorPageName.lastIndexOf(".jsp") >= 0) { 147 applyDecoratorUsingJSP(page, decorator, request, response); 148 } else if (decoratorPageName.lastIndexOf(".vm") >= 0) { 149 applyDecoratorUsingVelocity(request, page, response, decorator); 150 } 151 } 152 153 private String extractRequestPath(HttpServletRequest request) { 154 String servletPath = request.getServletPath(); 155 String pathInfo = request.getPathInfo(); 156 String query = request.getQueryString(); 157 return (servletPath == null ? "" : servletPath) + (pathInfo == null ? "" : pathInfo) + (query == null ? "" : ("?" + query)); 158 } 159 160 166 protected Page parsePage(HttpServletRequest request, HttpServletResponse response) throws IOException , ServletException { 167 try { 168 PageResponseWrapper pageResponse = new PageResponseWrapper(response, factory); 169 doRequest(request, pageResponse); 170 Page result = (Page) request.getAttribute(PAGE); 173 if (result == null) { 174 result = pageResponse.getPage(); 176 } 177 request.setAttribute(USING_STREAM, new Boolean (pageResponse.isUsingStream())); 178 return result; 179 } catch (IllegalStateException e) { 180 if (Container.get() != Container.WEBLOGIC) 186 throw e; 187 return null; 188 } 189 } 190 191 194 private void writeOriginal(HttpServletRequest request, HttpServletResponse response, Page page) throws IOException { 195 response.setContentLength(page.getContentLength()); 196 if (request.getAttribute(USING_STREAM).equals(Boolean.TRUE)) { 197 PrintWriter writer = new PrintWriter (response.getOutputStream()); 198 page.writePage(writer); 199 writer.flush(); 201 response.getOutputStream().flush(); 202 } else { 203 page.writePage(response.getWriter()); 204 response.getWriter().flush(); 205 } 206 } 207 208 212 protected void applyDecoratorUsingJSP(Page page, Decorator decorator, HttpServletRequest request, HttpServletResponse response) 213 throws ServletException , IOException { 214 try { 215 request.setAttribute(PAGE, page); 216 ServletContext context = getServletContext(); 217 if (decorator.getURIPath() != null) { 219 if (context.getContext(decorator.getURIPath()) != null) { 222 context = context.getContext(decorator.getURIPath()); 223 } 224 } 225 RequestDispatcher dispatcher = context.getRequestDispatcher(decorator.getPage()); 227 dispatcher.include(request, response); 229 230 while (decorator.getInitParameterNames().hasNext()) { 232 String initParam = (String ) decorator.getInitParameterNames().next(); 233 if (initParam.startsWith("header.")) { 234 response.setHeader(initParam.substring(initParam.indexOf('.')), decorator.getInitParameter(initParam)); 235 } 236 } 237 238 request.removeAttribute(PAGE); 239 } catch (RuntimeException e) { 240 if (Container.get() == Container.TOMCAT) 243 e.printStackTrace(); 244 245 throw e; 246 } 247 } 248 249 protected void applyDecoratorUsingVelocity(HttpServletRequest request, Page page, HttpServletResponse response, Decorator decorator) 250 throws IOException { 251 try { 252 request.setAttribute(PAGE, page); 253 Context context = VelocityManager.getInstance().createContext(ActionContext.getContext().getValueStack(), request, response); 254 255 context.put("textUtils", TEXT_UTILS); 256 context.put("fileUtil", FILE_UTILS); 257 context.put("generalUtil", GENERAL_UTIL); 258 259 context.put("page", page); 260 context.put("title", page.getTitle()); 261 262 StringWriter buffer = new StringWriter (); 263 page.writeBody(OutputConverter.getWriter(buffer)); 264 context.put("body", buffer.toString()); 265 266 if (page instanceof HTMLPage) { 267 HTMLPage htmlPage = (HTMLPage) page; 268 buffer = new StringWriter (); 269 htmlPage.writeHead(OutputConverter.getWriter(buffer)); 270 context.put("head", buffer.toString()); 271 } 272 273 VelocityEngine velocityEngine = VelocityManager.getInstance().getVelocityEngine(); 275 276 Template template = velocityEngine.getTemplate(decorator.getPage()); 277 StringWriter tempWriter = new StringWriter (); 278 template.merge(context, tempWriter); 279 String renderResult = tempWriter.toString(); 280 281 response.getWriter().print(renderResult); 282 283 while (decorator.getInitParameterNames().hasNext()) { 284 String initParam = (String ) decorator.getInitParameterNames().next(); 285 if (initParam.startsWith("header.")) 286 response.setHeader(initParam.substring(initParam.indexOf('.')), decorator.getInitParameter(initParam)); 287 } 288 289 request.removeAttribute(PAGE); 290 } catch (RuntimeException e) { 291 if (Container.get() == 1) 292 e.printStackTrace(); 293 throw e; 294 } catch (Exception e) { 295 e.printStackTrace(); 296 } 297 298 } 299 } | Popular Tags |