1   /**
2    * Copyright (c) 2000-2009 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.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.servlet.PortletServlet;
28  import com.liferay.portal.kernel.util.GetterUtil;
29  import com.liferay.portal.kernel.util.HttpUtil;
30  import com.liferay.portal.kernel.util.ParamUtil;
31  import com.liferay.portal.kernel.util.StringPool;
32  
33  import java.io.IOException;
34  
35  import java.util.HashMap;
36  import java.util.Map;
37  
38  import javax.portlet.ActionRequest;
39  import javax.portlet.ActionResponse;
40  import javax.portlet.GenericPortlet;
41  import javax.portlet.PortletConfig;
42  import javax.portlet.PortletException;
43  import javax.portlet.PortletURL;
44  import javax.portlet.RenderRequest;
45  import javax.portlet.RenderResponse;
46  import javax.portlet.WindowState;
47  
48  import javax.servlet.RequestDispatcher;
49  import javax.servlet.ServletException;
50  import javax.servlet.http.HttpServletRequest;
51  import javax.servlet.http.HttpServletResponse;
52  
53  /**
54   * <a href="WAIPortlet.java.html"><b><i>View Source</i></b></a>
55   *
56   * @author Jorge Ferrer
57   *
58   */
59  public class WAIPortlet extends GenericPortlet {
60  
61      public static final String CONNECTOR_IFRAME = "iframe";
62  
63      public static final String CONNECTOR_INCLUDE = "include";
64  
65      public void init(PortletConfig portletConfig) throws PortletException {
66          super.init(portletConfig);
67  
68          _connector = GetterUtil.getString(
69              portletConfig.getInitParameter("wai.connector"), CONNECTOR_IFRAME);
70      }
71  
72      public void processAction(
73          ActionRequest actionRequest, ActionResponse actionResponse) {
74      }
75  
76      public void render(
77              RenderRequest renderRequest, RenderResponse renderResponse)
78          throws IOException, PortletException {
79  
80          if (renderRequest.getWindowState().equals(WindowState.MAXIMIZED)) {
81              invokeApplication(renderRequest, renderResponse);
82          }
83          else {
84              renderNormalWindowState(renderRequest, renderResponse);
85          }
86      }
87  
88      protected void forward(
89              HttpServletRequest request, HttpServletResponse response,
90              String path)
91          throws PortletException {
92  
93          RequestDispatcher requestDispatcher =
94              request.getRequestDispatcher(path);
95  
96          try {
97              requestDispatcher.forward(request, response);
98          }
99          catch (IOException ioe) {
100             _log.error(ioe, ioe);
101         }
102         catch (ServletException se) {
103             throw new PortletException(se);
104         }
105     }
106 
107     protected void invokeApplication(
108             RenderRequest renderRequest, RenderResponse renderResponse)
109         throws IOException, PortletException {
110 
111         HttpServletRequest request =
112             (HttpServletRequest)renderRequest.getAttribute(
113                 PortletServlet.PORTLET_SERVLET_REQUEST);
114         HttpServletResponse response =
115             (HttpServletResponse)renderRequest.getAttribute(
116                 PortletServlet.PORTLET_SERVLET_RESPONSE);
117 
118         String portletName = getPortletConfig().getPortletName();
119 
120         String friendlyURL = (String)request.getAttribute("FRIENDLY_URL");
121 
122         int pos = friendlyURL.indexOf(_MAPPING);
123 
124         StringBuilder contextPath = new StringBuilder();
125 
126         contextPath.append(friendlyURL.substring(0, pos + _MAPPING.length()));
127         contextPath.append(StringPool.SLASH);
128         contextPath.append(portletName);
129 
130         pos = friendlyURL.indexOf(portletName);
131 
132         String pathInfo = friendlyURL.substring(pos + portletName.length());
133 
134         Map<String, String[]> params = new HashMap<String, String[]>(
135             request.getParameterMap());
136 
137         params.remove(_APP_URL);
138 
139         String queryString = HttpUtil.parameterMapToString(params, false);
140 
141         String appUrl = ParamUtil.getString(
142             renderRequest, _APP_URL, StringPool.SLASH);
143 
144         if (_connector.equals(CONNECTOR_IFRAME)) {
145             request.setAttribute(
146                 _APP_URL, renderRequest.getContextPath() + appUrl);
147 
148             String iframeExtraHeight = GetterUtil.getString(
149                 getPortletConfig().getInitParameter(
150                     "wai.connector.iframe.height.extra"),
151                 "40");
152 
153             renderRequest.setAttribute(
154                 "wai.connector.iframe.height.extra", iframeExtraHeight);
155 
156             forward(request, response, _JSP_IFRAME);
157         }
158         else if (_connector.equals(CONNECTOR_INCLUDE)) {
159             HttpServletRequest waiRequest = new WAIHttpServletRequest(
160                 request, contextPath.toString(), pathInfo, queryString, params);
161 
162             RequestDispatcher requestDispatcher = request.getRequestDispatcher(
163                 appUrl);
164 
165             try {
166                 requestDispatcher.forward(waiRequest, response);
167             }
168             catch (ServletException se) {
169                 throw new PortletException(se);
170             }
171         }
172     }
173 
174     protected void renderNormalWindowState(
175             RenderRequest renderRequest, RenderResponse renderResponse)
176         throws PortletException {
177 
178         HttpServletRequest request =
179             (HttpServletRequest)renderRequest.getAttribute(
180                 PortletServlet.PORTLET_SERVLET_REQUEST);
181         HttpServletResponse response =
182             (HttpServletResponse)renderRequest.getAttribute(
183                 PortletServlet.PORTLET_SERVLET_RESPONSE);
184 
185         PortletURL renderURL = renderResponse.createRenderURL();
186 
187         renderURL.setWindowState(WindowState.MAXIMIZED);
188 
189         renderRequest.setAttribute("renderURL", renderURL.toString());
190 
191         forward(request, response, _JSP_NORMAL_WINDOW_STATE);
192     }
193 
194     private static final String _MAPPING = "waiapp";
195 
196     private static final String _APP_URL = "appURL";
197 
198     private static final String _JSP_DIR = "/WEB-INF/jsp/liferay/wai";
199 
200     private static final String _JSP_IFRAME = _JSP_DIR + "/iframe.jsp";
201 
202     private static final String _JSP_NORMAL_WINDOW_STATE =
203         _JSP_DIR + "/normal_window_state.jsp";
204 
205     private static Log _log = LogFactoryUtil.getLog(WAIPortlet.class);
206 
207     private String _connector;
208 
209 }