1 | /* |
2 | * Copyright 2004 The Apache Software Foundation. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | */ |
16 | package org.apache.pluto.driver; |
17 | |
18 | import org.apache.commons.logging.Log; |
19 | import org.apache.commons.logging.LogFactory; |
20 | import org.apache.pluto.PortletContainer; |
21 | import org.apache.pluto.PortletContainerException; |
22 | import org.apache.pluto.driver.config.DriverConfiguration; |
23 | import org.apache.pluto.driver.core.PortalEnvironment; |
24 | import org.apache.pluto.driver.core.PortletWindowImpl; |
25 | import org.apache.pluto.driver.services.portal.PageConfig; |
26 | import org.apache.pluto.driver.services.portal.PortletWindowConfig; |
27 | import org.apache.pluto.driver.url.PortalURL; |
28 | |
29 | import javax.portlet.PortletException; |
30 | import javax.servlet.RequestDispatcher; |
31 | import javax.servlet.ServletContext; |
32 | import javax.servlet.ServletException; |
33 | import javax.servlet.http.HttpServlet; |
34 | import javax.servlet.http.HttpServletRequest; |
35 | import javax.servlet.http.HttpServletResponse; |
36 | import java.io.IOException; |
37 | |
38 | /** |
39 | * The controller servlet used to drive the Portal Driver. All requests mapped |
40 | * to this servlet will be processed as Portal Requests. |
41 | * |
42 | * @author <a href="mailto:ddewolf@apache.org">David H. DeWolf</a> |
43 | * @author <a href="mailto:zheng@apache.org">ZHENG Zhong</a> |
44 | * @version 1.0 |
45 | * @since Sep 22, 2004 |
46 | */ |
47 | public class PortalDriverServlet extends HttpServlet { |
48 | |
49 | /** Internal Logger. */ |
50 | private static final Log LOG = LogFactory.getLog(PortalDriverServlet.class); |
51 | |
52 | protected static final String DEFAULT_PAGE_URI = |
53 | "/WEB-INF/themes/pluto-default-theme.jsp"; |
54 | |
55 | /** The portlet container to which we will forward all portlet requests. */ |
56 | protected PortletContainer container = null; |
57 | |
58 | |
59 | // HttpServlet Impl -------------------------------------------------------- |
60 | |
61 | public String getServletInfo() { |
62 | return "Pluto Portal Driver Servlet"; |
63 | } |
64 | |
65 | /** |
66 | * Initialize the Portal Driver. This method retrieves the portlet container |
67 | * instance from the servlet context scope. |
68 | * @see PortletContainer |
69 | */ |
70 | public void init() { |
71 | ServletContext servletContext = getServletContext(); |
72 | container = (PortletContainer) servletContext.getAttribute( |
73 | AttributeKeys.PORTLET_CONTAINER); |
74 | } |
75 | |
76 | |
77 | /** |
78 | * Handle all requests. All POST requests are passed to this method. |
79 | * @param request the incoming HttpServletRequest. |
80 | * @param response the incoming HttpServletResponse. |
81 | * @throws ServletException if an internal error occurs. |
82 | * @throws IOException if an error occurs writing to the response. |
83 | */ |
84 | public void doGet(HttpServletRequest request, HttpServletResponse response) |
85 | throws ServletException, IOException { |
86 | |
87 | PortalEnvironment portalEnvironment = new PortalEnvironment( |
88 | request, response); |
89 | PortalURL portalURL = portalEnvironment.getRequestedPortalURL(); |
90 | String actionWindowId = portalURL.getActionWindow(); |
91 | PortletWindowConfig actionWindowConfig = getDriverConfiguration() |
92 | .getPortletWindowConfig(actionWindowId); |
93 | |
94 | // Action window config will only exist if there is an action request. |
95 | if (actionWindowConfig != null) { |
96 | PortletWindowImpl portletWindow = new PortletWindowImpl( |
97 | actionWindowConfig, portalURL); |
98 | if (LOG.isDebugEnabled()) { |
99 | LOG.debug("Processing action request for window: " |
100 | + portletWindow.getId().getStringId()); |
101 | } |
102 | try { |
103 | container.doAction(portletWindow, request, response); |
104 | } catch (PortletContainerException ex) { |
105 | throw new ServletException(ex); |
106 | } catch (PortletException ex) { |
107 | throw new ServletException(ex); |
108 | } |
109 | if (LOG.isDebugEnabled()) { |
110 | LOG.debug("Action request processed.\n\n"); |
111 | } |
112 | } |
113 | |
114 | // Otherwise (actionWindowConfig == null), handle the render request. |
115 | else { |
116 | if (LOG.isDebugEnabled()) { |
117 | LOG.debug("Processing render request."); |
118 | } |
119 | PageConfig pageConfig = getPageConfig(portalURL); |
120 | request.setAttribute(AttributeKeys.CURRENT_PAGE, pageConfig); |
121 | String uri = (pageConfig.getUri() != null) |
122 | ? pageConfig.getUri() : DEFAULT_PAGE_URI; |
123 | if (LOG.isDebugEnabled()) { |
124 | LOG.debug("Dispatching to: " + uri); |
125 | } |
126 | RequestDispatcher dispatcher = request.getRequestDispatcher(uri); |
127 | dispatcher.forward(request, response); |
128 | if (LOG.isDebugEnabled()) { |
129 | LOG.debug("Render request processed.\n\n"); |
130 | } |
131 | } |
132 | } |
133 | |
134 | /** |
135 | * Pass all POST requests to {@link #doGet(HttpServletRequest, HttpServletResponse)}. |
136 | * @param request the incoming servlet request. |
137 | * @param response the incoming servlet response. |
138 | * @throws ServletException if an exception occurs. |
139 | * @throws IOException if an exception occurs writing to the response. |
140 | */ |
141 | public void doPost(HttpServletRequest request, HttpServletResponse response) |
142 | throws ServletException, IOException { |
143 | doGet(request, response); |
144 | } |
145 | |
146 | |
147 | // Private Methods --------------------------------------------------------- |
148 | |
149 | /** |
150 | * Returns the config of the portal page to be rendered. |
151 | * @param currentURL the current portal URL. |
152 | * @return the config of the portal page to be rendered. |
153 | */ |
154 | private PageConfig getPageConfig(PortalURL currentURL) { |
155 | String requestedPageId = currentURL.getRenderPath(); |
156 | if (LOG.isDebugEnabled()) { |
157 | LOG.debug("Rendering Portal: Requested Page: " + requestedPageId); |
158 | } |
159 | return getDriverConfiguration().getPageConfig(requestedPageId); |
160 | } |
161 | |
162 | /** |
163 | * Returns the portal driver configuration object. |
164 | * @return the portal driver configuration object. |
165 | */ |
166 | private DriverConfiguration getDriverConfiguration() { |
167 | return (DriverConfiguration) getServletContext().getAttribute( |
168 | AttributeKeys.DRIVER_CONFIG); |
169 | } |
170 | |
171 | |
172 | |
173 | } |
174 | |