1 | package org.apache.pluto.driver.tags; |
2 | |
3 | import java.util.Iterator; |
4 | import java.util.List; |
5 | import java.util.Map; |
6 | |
7 | import javax.portlet.RenderRequest; |
8 | import javax.portlet.RenderResponse; |
9 | import javax.portlet.WindowState; |
10 | import javax.servlet.ServletContext; |
11 | import javax.servlet.http.HttpServletRequest; |
12 | import javax.servlet.http.HttpServletResponse; |
13 | import javax.servlet.jsp.JspException; |
14 | import javax.servlet.jsp.tagext.BodyTagSupport; |
15 | |
16 | import org.apache.commons.logging.Log; |
17 | import org.apache.commons.logging.LogFactory; |
18 | import org.apache.pluto.PortletContainer; |
19 | import org.apache.pluto.PortletWindow; |
20 | import org.apache.pluto.driver.AttributeKeys; |
21 | import org.apache.pluto.driver.config.DriverConfiguration; |
22 | import org.apache.pluto.driver.core.PortalEnvironment; |
23 | import org.apache.pluto.driver.core.PortalServletRequest; |
24 | import org.apache.pluto.driver.core.PortalServletResponse; |
25 | import org.apache.pluto.driver.core.PortletWindowImpl; |
26 | import org.apache.pluto.driver.services.portal.PortletWindowConfig; |
27 | import org.apache.pluto.driver.url.PortalURL; |
28 | import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager; |
29 | |
30 | import edu.iu.uis.sit.portal.portlet.attribute.PortletAttribute; |
31 | import edu.iu.uis.sit.portal.portlet.attribute.PortletAttributeManager; |
32 | |
33 | /** |
34 | * The portlet tag is used to render a portlet specified by the portlet ID. |
35 | * |
36 | * @see javax.portlet.Portlet#render(RenderRequest, RenderResponse) |
37 | * @see org.apache.pluto.PortletContainer#doRender(PortletWindow, HttpServletRequest, HttpServletResponse) |
38 | * |
39 | * @author <a href="mailto:ddewolf@apache.org">David H. DeWolf</a> |
40 | * @author <a href="mailto:zheng@apache.org">ZHENG Zhong</a> |
41 | */ |
42 | public class PortletTag extends BodyTagSupport { |
43 | |
44 | /** Logger. */ |
45 | private static final Log LOG = LogFactory.getLog(PortletTag.class); |
46 | |
47 | /** Status constant for failed rendering. */ |
48 | public static final int FAILED = 0; |
49 | |
50 | /** Status constant for successful rendering. */ |
51 | public static final int SUCCESS = 1; |
52 | |
53 | |
54 | // Private Member Variables ------------------------------------------------ |
55 | |
56 | /** The portlet ID attribute passed into this tag. */ |
57 | private String portletId = null; |
58 | |
59 | /** The evaluated value of the portlet ID attribute. */ |
60 | private String evaluatedPortletId = null; |
61 | |
62 | /** The cached portal servlet response holding rendering result. */ |
63 | private PortalServletResponse response = null; |
64 | |
65 | /** The cached rendering status: SUCCESS or FAILED. */ |
66 | private int status; |
67 | |
68 | /** The cached Throwable instance when fail to render the portlet. */ |
69 | private Throwable throwable = null; |
70 | |
71 | |
72 | // Tag Attribute Accessors ------------------------------------------------- |
73 | |
74 | /** |
75 | * Returns the portlet ID attribute. |
76 | * @return the portlet ID attribute. |
77 | */ |
78 | public String getPortletId() { |
79 | return portletId; |
80 | } |
81 | |
82 | /** |
83 | * Sets the portlet ID attribute. |
84 | * @param portletId the portlet ID attribute. |
85 | */ |
86 | public void setPortletId(String portletId) { |
87 | this.portletId = portletId; |
88 | } |
89 | |
90 | |
91 | // BodyTagSupport Impl ----------------------------------------------------- |
92 | |
93 | /** |
94 | * Method invoked when the start tag is encountered. |
95 | * @throws JspException if an error occurs. |
96 | */ |
97 | public int doStartTag() throws JspException { |
98 | |
99 | // Evaluate portlet ID attribute. |
100 | evaluatePortletId(); |
101 | |
102 | // Retrieve the portlet window config for the evaluated portlet ID. |
103 | ServletContext servletContext = pageContext.getServletContext(); |
104 | DriverConfiguration driverConfig = (DriverConfiguration) |
105 | servletContext.getAttribute(AttributeKeys.DRIVER_CONFIG); |
106 | PortletWindowConfig windowConfig = driverConfig |
107 | .getPortletWindowConfig(evaluatedPortletId); |
108 | if (LOG.isDebugEnabled()) { |
109 | LOG.debug("Rendering Portlet Window: " + windowConfig); |
110 | } |
111 | |
112 | // Retrieve the current portal URL. |
113 | PortalEnvironment portalEnv = PortalEnvironment.getPortalEnvironment( |
114 | (HttpServletRequest) pageContext.getRequest()); |
115 | PortalURL portalURL = portalEnv.getRequestedPortalURL(); |
116 | |
117 | // Create the portlet window to render. |
118 | PortletWindow window = new PortletWindowImpl(windowConfig, portalURL); |
119 | |
120 | // Check if someone else is maximized. If yes, don't show content. |
121 | Map windowStates = portalURL.getWindowStates(); |
122 | for (Iterator it = windowStates.keySet().iterator(); it.hasNext(); ) { |
123 | String windowId = (String) it.next(); |
124 | WindowState windowState = (WindowState) windowStates.get(windowId); |
125 | if (WindowState.MAXIMIZED.equals(windowState) |
126 | && !window.getId().getStringId().equals(windowId)) { |
127 | return SKIP_BODY; |
128 | } |
129 | } |
130 | |
131 | // Create portal servlet request and response to wrap the original |
132 | // HTTP servlet request and response. |
133 | PortalServletRequest portalRequest = new PortalServletRequest( |
134 | (HttpServletRequest) pageContext.getRequest(), window); |
135 | PortalServletResponse portalResponse = new PortalServletResponse( |
136 | (HttpServletResponse) pageContext.getResponse()); |
137 | |
138 | // Retrieve the portlet container from servlet context. |
139 | PortletContainer container = (PortletContainer) |
140 | servletContext.getAttribute(AttributeKeys.PORTLET_CONTAINER); |
141 | |
142 | // IU_MOD--START |
143 | // TODO: Should we factor this into our own tag or merge with new |
144 | // versions of the pluto tag library? |
145 | List portletAttributes = PortletAttributeManager.getInstance().getAttributes(portletId); |
146 | for (Iterator iter = portletAttributes.iterator(); iter.hasNext();) { |
147 | PortletAttribute portletAttribute = (PortletAttribute) iter.next(); |
148 | portalRequest.setAttribute(portletAttribute.getName(), portletAttribute.getValue()); |
149 | } |
150 | // IU_MOD--END |
151 | |
152 | // Render the portlet and cache the response. |
153 | try { |
154 | container.doRender(window, portalRequest, portalResponse); |
155 | response = portalResponse; |
156 | status = SUCCESS; |
157 | } catch (Throwable th) { |
158 | status = FAILED; |
159 | throwable = th; |
160 | } |
161 | |
162 | // Continue to evaluate the tag body. |
163 | return EVAL_BODY_INCLUDE; |
164 | } |
165 | |
166 | |
167 | // Package Methods --------------------------------------------------------- |
168 | |
169 | /** |
170 | * Returns the rendering status. |
171 | * @return the rendering status. |
172 | */ |
173 | int getStatus() { |
174 | return status; |
175 | } |
176 | |
177 | /** |
178 | * Returns the portal servlet response holding rendering result. |
179 | * @return the portal servlet response holding rendering result. |
180 | */ |
181 | PortalServletResponse getPortalServletResponse() { |
182 | return response; |
183 | } |
184 | |
185 | /** |
186 | * Returns the error that has occurred when rendering portlet. |
187 | * @return the error that has occurred when rendering portlet. |
188 | */ |
189 | Throwable getThrowable() { |
190 | return throwable; |
191 | } |
192 | |
193 | /** |
194 | * Returns the evaluated portlet ID. |
195 | * @return the evaluated portlet ID. |
196 | */ |
197 | String getEvaluatedPortletId() { |
198 | return evaluatedPortletId; |
199 | } |
200 | |
201 | |
202 | |
203 | // Private Methods --------------------------------------------------------- |
204 | |
205 | /** |
206 | * Evaluates the portlet ID attribute passed into this tag. This method |
207 | * evaluates the member variable <code>portletId</code> and saves the |
208 | * evaluated result to <code>evaluatedPortletId</code> |
209 | * @throws JspException if an error occurs. |
210 | */ |
211 | private void evaluatePortletId() throws JspException { |
212 | Object obj = ExpressionEvaluatorManager.evaluate( |
213 | "portletId", portletId, String.class, this, pageContext); |
214 | if (LOG.isDebugEnabled()) { |
215 | LOG.debug("Evaluated portletId to: " + obj); |
216 | } |
217 | evaluatedPortletId = (String) obj; |
218 | } |
219 | |
220 | } |