001: package com.icesoft.faces.webapp.http.portlet;
002:
003: import com.icesoft.jasper.Constants;
004: import org.apache.commons.logging.Log;
005: import org.apache.commons.logging.LogFactory;
006:
007: import javax.portlet.ActionRequest;
008: import javax.portlet.ActionResponse;
009: import javax.portlet.Portlet;
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.RenderRequest;
016: import javax.portlet.RenderResponse;
017: import java.io.IOException;
018:
019: /**
020: * The MainPortlet is the entry point for ICEfaces-based portlets. The goal is
021: * we set up the environment as required and then dispatch the request to the
022: * MainServlet and let the framework do all the normal processing. It's
023: * basically only the initial page load that we care about. The rest of the
024: * processing is handled between the ICEfaces JavaScript bridge and the
025: * MainServlet via AJAX mechanisms. The main activities we do on the first page
026: * load are:
027: * <p/>
028: * - Get the initial view from the portlet config and set it as a request
029: * attribute. We use the key "javax.servlet.include.request_uri" as portlets
030: * are fragrments and so to the framework, they are treated much like includes.
031: * <p/>
032: * - Get a request dispatcher for the view (typically an .iface resource) and
033: * call the include() method of the dispatcher. By checking for the include
034: * attribute on the request, the framework should process it correctly.
035: */
036: public class MainPortlet implements Portlet {
037:
038: private static Log log = LogFactory.getLog(MainPortlet.class);
039:
040: private static final String PORTLET_MARKER = "portlet";
041: private PortletConfig portletConfig;
042:
043: public void init(PortletConfig portletConfig)
044: throws PortletException {
045:
046: this .portletConfig = portletConfig;
047:
048: if (log.isTraceEnabled()) {
049: log.trace("portlet config: " + portletConfig);
050: }
051: }
052:
053: public void destroy() {
054:
055: if (log == null) {
056: return;
057: }
058:
059: if (log.isTraceEnabled()) {
060: log.trace("portlet config: " + portletConfig);
061: }
062: }
063:
064: public void processAction(ActionRequest actionRequest,
065: ActionResponse actionResponse) throws IOException,
066: PortletException {
067: }
068:
069: public void render(RenderRequest renderRequest,
070: RenderResponse renderResponse) throws IOException,
071: PortletException {
072:
073: //Portlets are provided in a namespace which is used to uniquely
074: //identify aspects (e.g. JavaScript, JSF component IDs) of the portlet.
075: //JSF uses the ExternalContext.encodeNamespace() method to do this.
076: //Because we are dispatching, we have to ensure that we make this
077: //setting available to the ICEfaces framework.
078: addAttribute(renderRequest, Constants.NAMESPACE_KEY,
079: renderResponse.getNamespace());
080:
081: //General marker attribute that shows that this request originated from
082: //a portlet environment.
083: addAttribute(renderRequest, Constants.PORTLET_KEY,
084: PORTLET_MARKER);
085:
086: //Get the inital view that is configured in the portlet.xml file
087: PortletMode portletMode = portletMode = renderRequest
088: .getPortletMode();
089: String viewId = null;
090: if (portletMode == PortletMode.VIEW) {
091: viewId = portletConfig.getInitParameter(Constants.VIEW_KEY);
092: if (viewId == null) {
093: if (log.isErrorEnabled()) {
094: log.error(Constants.VIEW_KEY
095: + " is not properly configured");
096: }
097: throw new PortletException(Constants.VIEW_KEY
098: + " is not properly configured");
099: }
100: } else if (portletMode == PortletMode.EDIT) {
101: viewId = portletConfig.getInitParameter(Constants.EDIT_KEY);
102: if (viewId == null) {
103: if (log.isErrorEnabled()) {
104: log.error(Constants.EDIT_KEY
105: + " is not properly configured");
106: }
107: throw new PortletException(Constants.EDIT_KEY
108: + " is not properly configured");
109: }
110: } else if (portletMode == PortletMode.HELP) {
111: viewId = portletConfig.getInitParameter(Constants.HELP_KEY);
112: if (viewId == null) {
113: if (log.isErrorEnabled()) {
114: log.error(Constants.HELP_KEY
115: + " is not properly configured");
116: }
117: throw new PortletException(Constants.HELP_KEY
118: + " is not properly configured");
119: }
120: }
121:
122: //We request a dispatcher for the actual resource which is typically
123: //an .iface. This maps to the proper handler, typically the ICEfaces
124: //MainServlet which takes over the processing.
125: PortletContext ctxt = portletConfig.getPortletContext();
126: PortletRequestDispatcher disp = ctxt
127: .getRequestDispatcher(viewId);
128:
129: if (disp == null) {
130: throw new PortletException("could not find dispatcher for "
131: + viewId);
132: }
133:
134: //IMPORTANT: See the JavaDoc for this class
135: PortletArtifactWrapper wrapper = new PortletArtifactWrapper(
136: portletConfig, renderRequest, renderResponse);
137: addAttribute(renderRequest,
138: PortletArtifactWrapper.PORTLET_ARTIFACT_KEY, wrapper);
139:
140: // Jack: This is a temporary fix for JBoss Portal. We should come up
141: // with a better fix in our framework that makes sure the
142: // Content-Type is set either before or when the
143: // ServletExternalContext.getWriter(String encoding) method is
144: // invoked.
145: renderResponse.setContentType("text/html");
146: disp.include(renderRequest, renderResponse);
147:
148: }
149:
150: private static void addAttribute(RenderRequest req, String key,
151: Object value) {
152: if (key != null && value != null) {
153: req.setAttribute(key, value);
154: }
155: if (log.isTraceEnabled()) {
156: log.trace(key + ": " + value);
157: }
158: }
159:
160: }
|