1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.portals.bridges.struts;
17
18 import java.io.IOException;
19
20 import javax.servlet.RequestDispatcher;
21 import javax.servlet.ServletException;
22 import javax.servlet.ServletRequest;
23 import javax.servlet.ServletResponse;
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.struts.Globals;
30 import org.apache.struts.action.ActionForm;
31 import org.apache.struts.action.ActionMessages;
32 import org.apache.struts.config.ActionConfig;
33
34 /***
35 * PortletServletRequestDispatcher
36 *
37 * @author <a href="mailto:ate@douma.nu">Ate Douma</a>
38 * @version $Id: PortletServletRequestDispatcher.java 188278 2005-01-28 01:09:23 +0100 (Fri, 28 Jan 2005) ate $
39 */
40 public class PortletServletRequestDispatcher implements RequestDispatcher
41 {
42 private static final Log log = LogFactory.getLog(PortletServletRequestDispatcher.class);
43 private RequestDispatcher dispatcher;
44 private String path;
45 private boolean named;
46
47 public PortletServletRequestDispatcher(RequestDispatcher dispatcher,
48 String path, boolean named)
49 {
50 this.dispatcher = dispatcher;
51 this.path = path;
52 this.named = named;
53 }
54
55 private void invoke(ServletRequest request, ServletResponse response,
56 boolean include) throws ServletException, IOException
57 {
58 String request_type = (String) request
59 .getAttribute(StrutsPortlet.REQUEST_TYPE);
60 if (request_type != null
61 && request_type.equals(StrutsPortlet.ACTION_REQUEST))
62 {
63 if (log.isDebugEnabled())
64 {
65 log.debug("saving " + (named ? "named " : " ")
66 + "dispatch to :" + path + ", from "
67 + request_type + " "
68 + StrutsPortletURL.getPageURL(request));
69 }
70 HttpServletRequest req = (HttpServletRequest) request;
71 StrutsPortletRenderContext context = new StrutsPortletRenderContext();
72 context.setPath(path);
73 context.setDispatchNamed(named);
74 ActionConfig actionConfig = (ActionConfig) request
75 .getAttribute(Globals.MAPPING_KEY);
76 if (actionConfig != null)
77 {
78 if (actionConfig.getAttribute() != null
79 && actionConfig.getScope().equals("request"))
80 {
81 ActionForm actionForm = (ActionForm) request
82 .getAttribute(actionConfig.getAttribute());
83 context.setActionForm(actionForm);
84 Boolean requestCancelled = (Boolean) request
85 .getAttribute(Globals.CANCEL_KEY);
86 if (requestCancelled != null
87 && requestCancelled.booleanValue())
88 context.setRequestCancelled(true);
89 }
90 }
91 context.setMessages((ActionMessages) request
92 .getAttribute(Globals.MESSAGE_KEY));
93 context.setErrors((ActionMessages) request
94 .getAttribute(Globals.ERROR_KEY));
95 if (context.getErrors() != null)
96 {
97 String originURL = StrutsPortletURL.getOriginURL(request);
98 if (originURL != null)
99 {
100 request.setAttribute(StrutsPortlet.REDIRECT_PAGE_URL,
101 originURL);
102 }
103 }
104 String portletName = (String) req.getAttribute(StrutsPortlet.PORTLET_NAME);
105 try
106 {
107 req.getSession(true).setAttribute(StrutsPortlet.RENDER_CONTEXT + "_" + portletName, context);
108 }
109 catch (IllegalStateException ise)
110 {
111
112 if (log.isDebugEnabled())
113 {
114 log.debug("Session invalidated: redirecting to: "+path+" instead.");
115 }
116 ((HttpServletResponse)response).sendRedirect(path);
117 }
118 }
119 else
120 {
121 if (log.isDebugEnabled())
122 {
123 log.debug("invoking " + (named ? "named " : " ")
124 + " dispatch to :" + path + ", from "
125 + request_type + " "
126 + StrutsPortletURL.getPageURL(request));
127 }
128 dispatcher.include(request, response);
129 }
130 }
131
132 public void forward(ServletRequest request, ServletResponse response) throws ServletException, IOException {
133 if ( PortletServlet.isPortletRequest(request) )
134 {
135 invoke(request, response, false);
136 }
137 else
138 {
139 dispatcher.forward(request,response);
140 }
141 }
142
143 public void include(ServletRequest request, ServletResponse response) throws ServletException, IOException {
144 if ( PortletServlet.isPortletRequest(request) )
145 {
146 invoke(request, response, true);
147 }
148 else
149 {
150 dispatcher.include(request,response);
151 }
152 }
153
154 public String toString() {
155 return dispatcher.toString();
156 }
157 }