01: package com.opensymphony.sitemesh.webapp.decorator;
02:
03: import com.opensymphony.sitemesh.Content;
04: import com.opensymphony.sitemesh.webapp.SiteMeshWebAppContext;
05:
06: import javax.servlet.RequestDispatcher;
07: import javax.servlet.ServletContext;
08: import javax.servlet.ServletException;
09: import javax.servlet.http.HttpServletRequest;
10: import javax.servlet.http.HttpServletResponse;
11: import java.io.IOException;
12:
13: /**
14: * Decorator that dispatches to another path in the Servlet Container (such as a JSP or path mapped to a Servlet).
15: * <p/>
16: * The Content and SiteMeshContext objects are passed to the decorator using the HttpServletRequest attributes
17: * {@link #CONTENT_KEY} and {@link #CONTEXT_KEY}.
18: * <p/>
19: * To dispatch to a decorator in another web-app on the same server, use {@link ExternalDispatchedDecorator}.
20: *
21: * @author Joe Walnes
22: * @since SiteMesh 3.0
23: */
24: public class DispatchedDecorator extends BaseWebAppDecorator {
25:
26: public static final String CONTENT_KEY = "com.opensymphony.sitemesh.CONTENT";
27: public static final String CONTEXT_KEY = "com.opensymphony.sitemesh.CONTEXT";
28:
29: private final String path;
30:
31: public DispatchedDecorator(String path) {
32: this .path = path;
33: }
34:
35: protected void render(Content content, HttpServletRequest request,
36: HttpServletResponse response,
37: ServletContext servletContext,
38: SiteMeshWebAppContext webAppContext) throws IOException,
39: ServletException {
40: Object oldContent = request.getAttribute(CONTENT_KEY);
41: Object oldWebAppContext = request.getAttribute(CONTEXT_KEY);
42:
43: request.setAttribute(CONTENT_KEY, content);
44: request.setAttribute(CONTEXT_KEY, webAppContext);
45:
46: try {
47: RequestDispatcher dispatcher = servletContext
48: .getRequestDispatcher(path);
49: dispatcher.include(request, response);
50: } finally {
51: request.setAttribute(CONTENT_KEY, oldContent);
52: request.setAttribute(CONTEXT_KEY, oldWebAppContext);
53: }
54: }
55:
56: protected ServletContext locateWebApp(ServletContext context) {
57: // Overriden by ExternalDispatchedDecorator, which finds the context of another web-app.
58: return context;
59: }
60: }
|