001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.pluto.driver;
018:
019: import java.io.IOException;
020:
021: import javax.portlet.PortletException;
022: import javax.servlet.RequestDispatcher;
023: import javax.servlet.ServletContext;
024: import javax.servlet.ServletException;
025: import javax.servlet.http.HttpServlet;
026: import javax.servlet.http.HttpServletRequest;
027: import javax.servlet.http.HttpServletResponse;
028:
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031: import org.apache.pluto.PortletContainer;
032: import org.apache.pluto.PortletContainerException;
033: import org.apache.pluto.driver.config.DriverConfiguration;
034: import org.apache.pluto.driver.core.PortalRequestContext;
035: import org.apache.pluto.driver.core.PortletWindowImpl;
036: import org.apache.pluto.driver.services.portal.PageConfig;
037: import org.apache.pluto.driver.services.portal.PortletWindowConfig;
038: import org.apache.pluto.driver.url.PortalURL;
039:
040: /**
041: * The controller servlet used to drive the Portal Driver. All requests mapped
042: * to this servlet will be processed as Portal Requests.
043: *
044: * @version 1.0
045: * @since Sep 22, 2004
046: */
047: public class PortalDriverServlet extends HttpServlet {
048:
049: /** Internal Logger. */
050: private static final Log LOG = LogFactory
051: .getLog(PortalDriverServlet.class);
052:
053: /** The Portal Driver sServlet Context */
054: private ServletContext servletContext;
055:
056: public static final String DEFAULT_PAGE_URI = "/WEB-INF/themes/pluto-default-theme.jsp";
057:
058: /** The portlet container to which we will forward all portlet requests. */
059: protected PortletContainer container;
060:
061: // HttpServlet Impl --------------------------------------------------------
062:
063: public String getServletInfo() {
064: return "Pluto Portal Driver Servlet";
065: }
066:
067: /**
068: * Initialize the Portal Driver. This method retrieves the portlet container
069: * instance from the servlet context scope.
070: * @see PortletContainer
071: */
072: public void init() {
073: servletContext = getServletContext();
074: container = (PortletContainer) servletContext
075: .getAttribute(AttributeKeys.PORTLET_CONTAINER);
076: }
077:
078: /**
079: * Handle all requests. All POST requests are passed to this method.
080: * @param request the incoming HttpServletRequest.
081: * @param response the incoming HttpServletResponse.
082: * @throws ServletException if an internal error occurs.
083: * @throws IOException if an error occurs writing to the response.
084: */
085: public void doGet(HttpServletRequest request,
086: HttpServletResponse response) throws ServletException,
087: IOException {
088:
089: PortalRequestContext portalRequestContext = new PortalRequestContext(
090: getServletContext(), request, response);
091:
092: PortalURL portalURL = portalRequestContext
093: .getRequestedPortalURL();
094: String actionWindowId = portalURL.getActionWindow();
095:
096: PortletWindowConfig actionWindowConfig = actionWindowId == null ? null
097: : PortletWindowConfig.fromId(actionWindowId);
098:
099: // Action window config will only exist if there is an action request.
100: if (actionWindowConfig != null) {
101: PortletWindowImpl portletWindow = new PortletWindowImpl(
102: actionWindowConfig, portalURL);
103: if (LOG.isDebugEnabled()) {
104: LOG.debug("Processing action request for window: "
105: + portletWindow.getId().getStringId());
106: }
107: try {
108: container.doAction(portletWindow, request, response);
109: } catch (PortletContainerException ex) {
110: throw new ServletException(ex);
111: } catch (PortletException ex) {
112: throw new ServletException(ex);
113: }
114: if (LOG.isDebugEnabled()) {
115: LOG.debug("Action request processed.\n\n");
116: }
117: }
118:
119: // Otherwise (actionWindowConfig == null), handle the render request.
120: else {
121: if (LOG.isDebugEnabled()) {
122: LOG.debug("Processing render request.");
123: }
124: PageConfig pageConfig = getPageConfig(portalURL);
125: if (pageConfig == null) {
126: // TODO Shouldn't we throw an exception here?
127: LOG.error("PageConfig for render path ["
128: + portalURL.getRenderPath()
129: + "] could not be found.");
130: }
131:
132: request
133: .setAttribute(AttributeKeys.CURRENT_PAGE,
134: pageConfig);
135: String uri = (pageConfig.getUri() != null) ? pageConfig
136: .getUri() : DEFAULT_PAGE_URI;
137: if (LOG.isDebugEnabled()) {
138: LOG.debug("Dispatching to: " + uri);
139: }
140: RequestDispatcher dispatcher = request
141: .getRequestDispatcher(uri);
142: dispatcher.forward(request, response);
143: if (LOG.isDebugEnabled()) {
144: LOG.debug("Render request processed.\n\n");
145: }
146: }
147: }
148:
149: /**
150: * Pass all POST requests to {@link #doGet(HttpServletRequest, HttpServletResponse)}.
151: * @param request the incoming servlet request.
152: * @param response the incoming servlet response.
153: * @throws ServletException if an exception occurs.
154: * @throws IOException if an exception occurs writing to the response.
155: */
156: public void doPost(HttpServletRequest request,
157: HttpServletResponse response) throws ServletException,
158: IOException {
159: doGet(request, response);
160: }
161:
162: // Private Methods ---------------------------------------------------------
163:
164: /**
165: * Returns the config of the portal page to be rendered.
166: * @param currentURL the current portal URL.
167: * @return the config of the portal page to be rendered.
168: */
169: private PageConfig getPageConfig(PortalURL currentURL) {
170: String requestedPageId = currentURL.getRenderPath();
171: if (LOG.isDebugEnabled()) {
172: LOG.debug("Rendering Portal: Requested Page: "
173: + requestedPageId);
174: }
175: return getDriverConfiguration().getPageConfig(requestedPageId);
176: }
177:
178: /**
179: * Returns the portal driver configuration object.
180: * @return the portal driver configuration object.
181: */
182: private DriverConfiguration getDriverConfiguration() {
183: return (DriverConfiguration) getServletContext().getAttribute(
184: AttributeKeys.DRIVER_CONFIG);
185: }
186:
187: }
|