001: package org.wings.portlet;
002:
003: import java.io.IOException;
004: import java.util.HashMap;
005: import java.util.Map;
006:
007: import javax.portlet.ActionRequest;
008: import javax.portlet.ActionResponse;
009: import javax.portlet.GenericPortlet;
010: import javax.portlet.PortletConfig;
011: import javax.portlet.PortletContext;
012: import javax.portlet.PortletException;
013: import javax.portlet.PortletMode;
014: import javax.portlet.PortletRequestDispatcher;
015: import javax.portlet.PortletSession;
016: import javax.portlet.RenderRequest;
017: import javax.portlet.RenderResponse;
018:
019: import org.apache.commons.logging.Log;
020: import org.apache.commons.logging.LogFactory;
021:
022: /**
023: *
024: * @author <a href="mailto:marc.musch@mercatis.com">Marc Musch</a>
025: */
026: public class WingSPortlet extends GenericPortlet {
027:
028: private final transient static Log log = LogFactory
029: .getLog(WingSPortlet.class);
030:
031: private static final String CONFIG_WINGS_SERVLET_URL = "WingSServletURL";
032:
033: private static final String CONFIG_VIEW_CLASS = "ViewClass";
034:
035: private static final String CONFIG_EDIT_CLASS = "EditClass";
036:
037: private static final String CONFIG_HELP_CLASS = "HelpClass";
038:
039: private String wingSServletURL;
040:
041: private String viewClass;
042:
043: private String editClass;
044:
045: private String helpClass;
046:
047: public void init(PortletConfig config) throws PortletException {
048:
049: log
050: .debug("WingS-Portlet-Bridge: start init(PortletConfig) for Portlet "
051: + config.getPortletName());
052: super .init(config);
053:
054: wingSServletURL = config
055: .getInitParameter(CONFIG_WINGS_SERVLET_URL);
056: viewClass = config.getInitParameter(CONFIG_VIEW_CLASS);
057: editClass = config.getInitParameter(CONFIG_EDIT_CLASS);
058: helpClass = config.getInitParameter(CONFIG_HELP_CLASS);
059:
060: if (viewClass == null) {
061: log
062: .error("WingS-Portlet-Bridge: the viewClass is null. Must be set in the portlet.xml");
063: throw new PortletException(
064: "WingS-Portlet-Bridge: wings-class for the view mode is null.");
065: }
066:
067: if (wingSServletURL == null) {
068: log
069: .error("WingS-Portlet-Bridge: the wingServletURL is null. Must be set in the portlet.xml");
070: throw new PortletException(
071: "WingS-Portlet-Bridge: wingSServletURL is null.");
072: }
073:
074: log.debug("WingS-Portlet-Bridge: using the url "
075: + wingSServletURL + " for wingSServletURL");
076: log.debug("WingS-Portlet-Bridge: using the class " + viewClass
077: + " for view-mode");
078: log.debug("WingS-Portlet-Bridge: using the class " + editClass
079: + " for edit-mode");
080: log.debug("WingS-Portlet-Bridge: using the class " + helpClass
081: + " for help-mode");
082: log.debug("WingS-Portlet-Bridge: ended init(PortletConfig)");
083: }
084:
085: protected void doEdit(RenderRequest request, RenderResponse response)
086: throws PortletException, IOException {
087:
088: log.debug("WingS-Portlet-Bridge: start doEdit() "
089: + response.getNamespace());
090:
091: processRendering(request, response);
092:
093: log.debug("WingS-Portlet-Bridge: ended doEdit() "
094: + response.getNamespace());
095:
096: }
097:
098: protected void doHelp(RenderRequest request, RenderResponse response)
099: throws PortletException, IOException {
100:
101: log.debug("WingS-Portlet-Bridge: start doHelp() "
102: + response.getNamespace());
103:
104: processRendering(request, response);
105:
106: log.debug("WingS-Portlet-Bridge: ended doHelp() "
107: + response.getNamespace());
108:
109: }
110:
111: protected void doView(RenderRequest request, RenderResponse response)
112: throws PortletException, IOException {
113:
114: log.debug("WingS-Portlet-Bridge: start doView() "
115: + response.getNamespace());
116:
117: processRendering(request, response);
118:
119: log.debug("WingS-Portlet-Bridge: ended doView() "
120: + response.getNamespace());
121:
122: }
123:
124: public void processAction(ActionRequest request,
125: ActionResponse response) throws PortletException,
126: IOException {
127:
128: log.debug("WingS-Portlet-Bridge: start with processAction()");
129:
130: // store the parameters
131: PortletSession portletSession = request.getPortletSession();
132: Map parameters = request.getParameterMap();
133: String portletMode = request.getPortletMode().toString();
134: portletSession.setAttribute(portletMode + ":"
135: + Const.SESSION_ATTR_PARAMETERS, parameters,
136: PortletSession.PORTLET_SCOPE);
137: log
138: .debug("WingS-Portlet-Bridge: stored parameters in sesseion under "
139: + portletMode
140: + ":"
141: + Const.SESSION_ATTR_PARAMETERS);
142:
143: log.debug("WingS-Portlet-Bridge: ended processAction()");
144: }
145:
146: protected void processRendering(RenderRequest request,
147: RenderResponse response) throws PortletException,
148: IOException {
149:
150: // get the parameters out of the session
151: // parameters are set into the session through the processAction
152: // TODO: maybe get the params directly out of the session in the servlet
153: PortletSession portletSession = request.getPortletSession();
154: String portletMode = request.getPortletMode().toString();
155: String parameterAttrName = portletMode + ":"
156: + Const.SESSION_ATTR_PARAMETERS;
157: Map sessionParameters = (Map) portletSession.getAttribute(
158: parameterAttrName, PortletSession.PORTLET_SCOPE);
159: log
160: .debug("WingS-Portlet-Bridge: loaded parameters in session from "
161: + portletMode
162: + ":"
163: + Const.SESSION_ATTR_PARAMETERS);
164: portletSession.removeAttribute(parameterAttrName,
165: PortletSession.PORTLET_SCOPE);
166:
167: // and get the Parameters directly from the request
168: Map requestParameters = request.getParameterMap();
169:
170: // store all parameters in the request for the PortletSessionServlet
171: Map allParameters = new HashMap();
172: if (sessionParameters != null) {
173: allParameters.putAll(sessionParameters);
174: }
175: if (requestParameters != null) {
176: allParameters.putAll(requestParameters);
177: }
178: request.setAttribute(
179: Const.REQUEST_ATTR_PARAMETERS_FROM_ACTION_MAP,
180: allParameters);
181:
182: // set the request and the response for the filter and the wings
183: // servlets
184: request
185: .setAttribute(Const.REQUEST_ATTR_RENDER_REQUEST,
186: request);
187: request.setAttribute(Const.REQUEST_ATTR_RENDER_RESPONSE,
188: response);
189:
190: // set the url of the wings servlet for the filter
191: request.setAttribute(Const.REQUEST_ATTR_WINGS_SERVLET_URL,
192: wingSServletURL);
193:
194: selectWingSClass(request);
195:
196: // dispatch to the wings servlet
197: PortletContext portletContext;
198: PortletRequestDispatcher requestDispatcher;
199:
200: portletContext = getPortletContext();
201: requestDispatcher = portletContext
202: .getRequestDispatcher(wingSServletURL);
203: requestDispatcher.include(request, response);
204:
205: }
206:
207: protected void selectWingSClass(RenderRequest request) {
208:
209: String portletMode = request.getPortletMode().toString();
210:
211: if (portletMode.equals(PortletMode.EDIT.toString())
212: && editClass != null)
213: request.setAttribute(Const.REQUEST_ATTR_WINGS_CLASS,
214: editClass);
215: else if (portletMode.equals(PortletMode.HELP.toString())
216: && helpClass != null)
217: request.setAttribute(Const.REQUEST_ATTR_WINGS_CLASS,
218: helpClass);
219: else
220: request.setAttribute(Const.REQUEST_ATTR_WINGS_CLASS,
221: viewClass);
222: }
223:
224: }
|