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