1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.util.bridges.wai;
24  
25  import com.liferay.portal.kernel.servlet.PortletServlet;
26  import com.liferay.portal.kernel.util.GetterUtil;
27  import com.liferay.portal.kernel.util.HttpUtil;
28  import com.liferay.portal.kernel.util.ParamUtil;
29  import com.liferay.portal.kernel.util.StringPool;
30  
31  import java.io.IOException;
32  
33  import java.util.HashMap;
34  import java.util.Map;
35  
36  import javax.portlet.ActionRequest;
37  import javax.portlet.ActionResponse;
38  import javax.portlet.GenericPortlet;
39  import javax.portlet.PortletConfig;
40  import javax.portlet.PortletException;
41  import javax.portlet.PortletURL;
42  import javax.portlet.RenderRequest;
43  import javax.portlet.RenderResponse;
44  import javax.portlet.WindowState;
45  
46  import javax.servlet.RequestDispatcher;
47  import javax.servlet.ServletException;
48  import javax.servlet.http.HttpServletRequest;
49  import javax.servlet.http.HttpServletResponse;
50  
51  import org.apache.commons.logging.Log;
52  import org.apache.commons.logging.LogFactory;
53  
54  /**
55   * <a href="WAIPortlet.java.html"><b><i>View Source</i></b></a>
56   *
57   * @author Jorge Ferrer
58   *
59   */
60  public class WAIPortlet extends GenericPortlet {
61  
62      public static final String CONNECTOR_IFRAME = "iframe";
63  
64      public static final String CONNECTOR_INCLUDE = "include";
65  
66      public void init(PortletConfig portletConfig) throws PortletException {
67          super.init(portletConfig);
68  
69          _connector = GetterUtil.getString(
70              portletConfig.getInitParameter("wai.connector"), CONNECTOR_IFRAME);
71      }
72  
73      public void processAction(
74          ActionRequest actionRequest, ActionResponse actionResponse) {
75      }
76  
77      public void render(
78              RenderRequest renderRequest, RenderResponse renderResponse)
79          throws IOException, PortletException {
80  
81          if (renderRequest.getWindowState().equals(WindowState.MAXIMIZED)) {
82              invokeApplication(renderRequest, renderResponse);
83          }
84          else {
85              renderNormalWindowState(renderRequest, renderResponse);
86          }
87      }
88  
89      protected void forward(
90              HttpServletRequest request, HttpServletResponse response,
91              String path)
92          throws PortletException {
93  
94          RequestDispatcher requestDispatcher =
95              request.getRequestDispatcher(path);
96  
97          try {
98              requestDispatcher.forward(request, response);
99          }
100         catch (IOException ioe) {
101             _log.error(ioe, ioe);
102         }
103         catch (ServletException se) {
104             throw new PortletException(se);
105         }
106     }
107 
108     protected void invokeApplication(
109             RenderRequest renderRequest, RenderResponse renderResponse)
110         throws IOException, PortletException {
111 
112         HttpServletRequest request =
113             (HttpServletRequest)renderRequest.getAttribute(
114                 PortletServlet.PORTLET_SERVLET_REQUEST);
115         HttpServletResponse response =
116             (HttpServletResponse)renderRequest.getAttribute(
117                 PortletServlet.PORTLET_SERVLET_RESPONSE);
118 
119         String portletName = getPortletConfig().getPortletName();
120 
121         String friendlyURL = (String)request.getAttribute("FRIENDLY_URL");
122 
123         int pos = friendlyURL.indexOf(_MAPPING);
124 
125         StringBuilder contextPath = new StringBuilder();
126 
127         contextPath.append(friendlyURL.substring(0, pos + _MAPPING.length()));
128         contextPath.append(StringPool.SLASH);
129         contextPath.append(portletName);
130 
131         pos = friendlyURL.indexOf(portletName);
132 
133         String pathInfo = friendlyURL.substring(pos + portletName.length());
134 
135         Map<String, String[]> params = new HashMap<String, String[]>(
136             request.getParameterMap());
137 
138         params.remove(_APP_URL);
139 
140         String queryString = HttpUtil.parameterMapToString(params, false);
141 
142         String appUrl = ParamUtil.getString(
143             renderRequest, _APP_URL, StringPool.SLASH);
144 
145         if (_connector.equals(CONNECTOR_IFRAME)) {
146             request.setAttribute(
147                 _APP_URL, renderRequest.getContextPath() + appUrl);
148 
149             String iframeExtraHeight = GetterUtil.getString(
150                 getPortletConfig().getInitParameter(
151                     "wai.connector.iframe.height.extra"),
152                 "40");
153 
154             renderRequest.setAttribute(
155                 "wai.connector.iframe.height.extra", iframeExtraHeight);
156 
157             forward(request, response, _JSP_IFRAME);
158         }
159         else if (_connector.equals(CONNECTOR_INCLUDE)) {
160             HttpServletRequest waiRequest = new WAIHttpServletRequest(
161                 request, contextPath.toString(), pathInfo, queryString, params);
162 
163             RequestDispatcher requestDispatcher = request.getRequestDispatcher(
164                 appUrl);
165 
166             try {
167                 requestDispatcher.forward(waiRequest, response);
168             }
169             catch (ServletException se) {
170                 throw new PortletException(se);
171             }
172         }
173     }
174 
175     protected void renderNormalWindowState(
176             RenderRequest renderRequest, RenderResponse renderResponse)
177         throws PortletException {
178 
179         HttpServletRequest request =
180             (HttpServletRequest)renderRequest.getAttribute(
181                 PortletServlet.PORTLET_SERVLET_REQUEST);
182         HttpServletResponse response =
183             (HttpServletResponse)renderRequest.getAttribute(
184                 PortletServlet.PORTLET_SERVLET_RESPONSE);
185 
186         PortletURL renderURL = renderResponse.createRenderURL();
187 
188         renderURL.setWindowState(WindowState.MAXIMIZED);
189 
190         renderRequest.setAttribute("renderURL", renderURL.toString());
191 
192         forward(request, response, _JSP_NORMAL_WINDOW_STATE);
193     }
194 
195     private static final String _MAPPING = "waiapp";
196 
197     private static final String _APP_URL = "appURL";
198 
199     private static final String _JSP_DIR = "/WEB-INF/jsp/liferay/wai";
200 
201     private static final String _JSP_IFRAME = _JSP_DIR + "/iframe.jsp";
202 
203     private static final String _JSP_NORMAL_WINDOW_STATE =
204         _JSP_DIR + "/normal_window_state.jsp";
205 
206     private static Log _log = LogFactory.getLog(WAIPortlet.class);
207 
208     private String _connector;
209 
210 }