001: /*
002: * @author <a href="mailto:novotny@gridsphere.org">Jason Novotny</a>
003: * @version $Id: GridSphereEventImpl.java 6400 2007-12-06 14:41:00Z docentt $
004: */
005: package org.gridsphere.portletcontainer.impl;
006:
007: import org.apache.commons.logging.Log;
008: import org.apache.commons.logging.LogFactory;
009: import org.gridsphere.layout.event.PortletComponentEvent;
010: import org.gridsphere.portlet.impl.*;
011: import org.gridsphere.portletcontainer.Client;
012: import org.gridsphere.portletcontainer.DefaultPortletAction;
013: import org.gridsphere.portletcontainer.DefaultPortletRender;
014: import org.gridsphere.portletcontainer.GridSphereEvent;
015:
016: import javax.portlet.*;
017: import javax.servlet.http.HttpServletRequest;
018: import javax.servlet.http.HttpServletResponse;
019: import java.util.*;
020:
021: /**
022: * The <code>GridSphereEventImpl</code> is an implementation of the <code>GridSphereEvent</code> interface.
023: * <p/>
024: * A <code>GridSphereEvent</code> represents a general portlet container
025: * event. The <code>GridSphereEvent</code> is passed into components that
026: * need to access the <code>PortletRequest</code>
027: * <code>PortletResponse</code> objects, such as the layout components.
028: */
029: public class GridSphereEventImpl implements GridSphereEvent {
030:
031: protected Log log = LogFactory.getLog(GridSphereEventImpl.class);
032:
033: protected HttpServletRequest req;
034: protected HttpServletResponse res;
035:
036: protected PortletContext portletContext;
037:
038: protected RenderRequest renderRequest;
039: protected RenderResponse renderResponse;
040: protected ActionRequest actionRequest;
041: protected ActionResponse actionResponse;
042:
043: protected String componentID = null;
044: protected String layoutID = null;
045:
046: protected DefaultPortletAction action = null;
047: protected DefaultPortletRender render = null;
048:
049: protected Stack<PortletComponentEvent> events = null;
050:
051: public GridSphereEventImpl(PortletContext ctx,
052: HttpServletRequest req, HttpServletResponse res) {
053:
054: this .req = req;
055: this .res = res;
056: this .portletContext = ctx;
057:
058: this .renderRequest = new RenderRequestImpl(req, ctx);
059: this .renderResponse = new RenderResponseImpl(req, res);
060: this .actionRequest = new ActionRequestImpl(req, ctx);
061: this .actionResponse = new ActionResponseImpl(req, res);
062:
063: renderRequest.setAttribute(SportletProperties.RENDER_REQUEST,
064: renderRequest);
065: renderRequest.setAttribute(SportletProperties.RENDER_RESPONSE,
066: renderResponse);
067:
068: events = new Stack<PortletComponentEvent>();
069:
070: //req.setAttribute(SportletProperties.COMPONENT_ID, componentID);
071:
072: componentID = req.getParameter(SportletProperties.COMPONENT_ID);
073: if (componentID == null) {
074: log.debug("Received a null component ID");
075: componentID = "";
076: } else {
077: log.debug("Received cid= " + componentID);
078: }
079:
080: layoutID = req
081: .getParameter(SportletProperties.LAYOUT_PAGE_PARAM);
082:
083: action = createAction();
084: render = createRender();
085:
086: if (action != null) {
087: log.debug("Received action event=" + action.getName());
088: } else if (render != null) {
089: log.debug("Received render event=" + render.getName());
090: }
091:
092: }
093:
094: public DefaultPortletAction createAction() {
095: String isAction = req
096: .getParameter(SportletProperties.IS_ACTION);
097: if (null == isAction)
098: return null;
099:
100: DefaultPortletAction myaction = new DefaultPortletAction("");
101: String actionStr = req
102: .getParameter(SportletProperties.DEFAULT_PORTLET_ACTION);
103: if (actionStr != null) {
104: myaction = new DefaultPortletAction(actionStr);
105: myaction
106: .setParameters(getPhaseParams(SportletProperties.DEFAULT_PORTLET_ACTION));
107: } else {
108: Map<String, String> params = parsePhaseParams(SportletProperties.DEFAULT_PORTLET_ACTION);
109: if (params != null) {
110: actionStr = (String) params
111: .get(SportletProperties.DEFAULT_PORTLET_ACTION);
112: params
113: .remove(SportletProperties.DEFAULT_PORTLET_ACTION);
114: myaction = new DefaultPortletAction(actionStr);
115: myaction.setParameters(params);
116: }
117: }
118: return myaction;
119: }
120:
121: public DefaultPortletRender createRender() {
122: // create empty render event by default
123: DefaultPortletRender myrender = new DefaultPortletRender("");
124: String renderStr = req
125: .getParameter(SportletProperties.DEFAULT_PORTLET_RENDER);
126: if (renderStr != null) {
127: myrender = new DefaultPortletRender(renderStr);
128: myrender
129: .setParameters(getPhaseParams(SportletProperties.DEFAULT_PORTLET_RENDER));
130: } else {
131: Map<String, String> params = parsePhaseParams(SportletProperties.DEFAULT_PORTLET_RENDER);
132: if (params != null) {
133: renderStr = (String) params
134: .get(SportletProperties.DEFAULT_PORTLET_RENDER);
135: params
136: .remove(SportletProperties.DEFAULT_PORTLET_RENDER);
137: myrender = new DefaultPortletRender(renderStr);
138: myrender.setParameters(params);
139: }
140: }
141: return myrender;
142: }
143:
144: protected Map<String, String> getPhaseParams(String phase) {
145: String prefix = null;
146: String name, newname, value;
147: if (phase.equals(SportletProperties.DEFAULT_PORTLET_RENDER)) {
148: prefix = SportletProperties.RENDER_PARAM_PREFIX
149: + req
150: .getParameter(SportletProperties.RENDER_PARAM_PREFIX
151: + SportletProperties.PREFIX);
152: } else {
153: prefix = req.getParameter(SportletProperties.PREFIX);
154: }
155: Enumeration e = req.getParameterNames();
156: Map<String, String> params = new HashMap<String, String>();
157: if ((prefix != null) && (e != null)) {
158: //System.err.println("prefix= " + prefix);
159: while (e.hasMoreElements()) {
160: name = ((String) e.nextElement());
161: //System.err.println("name= " + name);
162: if (name.startsWith(prefix)) {
163: newname = name.substring(prefix.length() + 1);
164: value = req.getParameter(name);
165: params.put(newname, value);
166: }
167: }
168: }
169: return params;
170: }
171:
172: protected Map<String, String> parsePhaseParams(String phase) {
173: /* This is where a DefaultPortletAction or a DefaultPortletRender gets put together if one exists */
174:
175: Enumeration e = null;
176: String name, newname;
177: Map<String, String> params = new HashMap<String, String>();
178: e = req.getParameterNames();
179: if (e != null) {
180:
181: /// Check to see if action is of form action_name generated by submit button
182: while (e.hasMoreElements()) {
183: name = (String) e.nextElement();
184: if (name.startsWith(phase)) {
185: // check for parameter names and values
186:
187: name = name.substring(phase.length() + 1);
188:
189: StringTokenizer st = new StringTokenizer(name, "&");
190: if (st.hasMoreTokens()) {
191: newname = st.nextToken();
192: } else {
193: newname = "";
194: }
195:
196: params.put(phase, newname);
197: //log.debug("Received " + myaction);
198: String paramName;
199: String paramVal = "";
200: Map<String, String> tmpParams = new HashMap<String, String>();
201: String prefix = "";
202: while (st.hasMoreTokens()) {
203: // now check for "=" separating name and value
204: String namevalue = st.nextToken();
205: int hasvalue = namevalue.indexOf("=");
206: if (hasvalue > 0) {
207: paramName = namevalue
208: .substring(0, hasvalue);
209: paramVal = namevalue
210: .substring(hasvalue + 1);
211: if (paramName
212: .equals(SportletProperties.PREFIX)) {
213: prefix = paramVal;
214: } else {
215: tmpParams.put(paramName, paramVal);
216: }
217: } else {
218: tmpParams.put(namevalue, paramVal);
219: }
220: }
221: // put unprefixed params in action
222: for (Object o : tmpParams.keySet()) {
223: String n = (String) o;
224: String v = (String) tmpParams.get(n);
225: if (!prefix.equals("")) {
226: n = n.substring(prefix.length() + 1);
227: }
228:
229: params.put(n, v);
230: }
231: tmpParams = null;
232: return params;
233: }
234:
235: }
236: }
237: return null;
238: }
239:
240: /**
241: * Returns an object representing the client device that the user connects to the portal with.
242: *
243: * @return the client device
244: */
245: public Client getClient() {
246: Client client = (Client) this .getHttpServletRequest()
247: .getSession().getAttribute(SportletProperties.CLIENT);
248: if (client == null) {
249: client = new ClientImpl(this .getHttpServletRequest());
250: this .getHttpServletRequest().getSession().setAttribute(
251: SportletProperties.CLIENT, client);
252: }
253: return client;
254: }
255:
256: public HttpServletRequest getHttpServletRequest() {
257: return req;
258: }
259:
260: public HttpServletResponse getHttpServletResponse() {
261: return res;
262: }
263:
264: public RenderRequest getRenderRequest() {
265: return renderRequest;
266: }
267:
268: public RenderResponse getRenderResponse() {
269: return renderResponse;
270: }
271:
272: public ActionRequest getActionRequest() {
273: return actionRequest;
274: }
275:
276: public ActionResponse getActionResponse() {
277: return actionResponse;
278: }
279:
280: public PortletContext getPortletContext() {
281: return portletContext;
282: }
283:
284: public DefaultPortletAction getAction() {
285: return action;
286: }
287:
288: public DefaultPortletRender getRender() {
289: return componentID.equals(req
290: .getAttribute(SportletProperties.COMPONENT_ID)) ? render
291: : null;
292: }
293:
294: public void setAction(DefaultPortletAction action) {
295: this .action = action;
296: }
297:
298: public boolean hasAction() {
299: return (action != null);
300: }
301:
302: public boolean hasMessage() {
303: return false;
304: }
305:
306: public void setComponentID(String componentID) {
307: this .componentID = componentID;
308: }
309:
310: public String getComponentID() {
311: return componentID;
312: }
313:
314: public String getLayoutID() {
315: return layoutID;
316: }
317:
318: public void addNewRenderEvent(PortletComponentEvent evt) {
319: if (evt != null)
320: events.push(evt);
321: }
322:
323: public PortletComponentEvent getLastRenderEvent() {
324: return (events.isEmpty() ? null
325: : (PortletComponentEvent) events.pop());
326: }
327: }
|