1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.util.bridges.wai;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.servlet.PortletServlet;
20  import com.liferay.portal.kernel.util.GetterUtil;
21  import com.liferay.portal.kernel.util.HttpUtil;
22  import com.liferay.portal.kernel.util.ParamUtil;
23  import com.liferay.portal.kernel.util.StringPool;
24  
25  import java.io.IOException;
26  
27  import java.util.HashMap;
28  import java.util.Map;
29  
30  import javax.portlet.ActionRequest;
31  import javax.portlet.ActionResponse;
32  import javax.portlet.GenericPortlet;
33  import javax.portlet.PortletConfig;
34  import javax.portlet.PortletException;
35  import javax.portlet.PortletURL;
36  import javax.portlet.RenderRequest;
37  import javax.portlet.RenderResponse;
38  import javax.portlet.WindowState;
39  
40  import javax.servlet.RequestDispatcher;
41  import javax.servlet.ServletException;
42  import javax.servlet.http.HttpServletRequest;
43  import javax.servlet.http.HttpServletResponse;
44  
45  /**
46   * <a href="WAIPortlet.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Jorge Ferrer
49   */
50  public class WAIPortlet extends GenericPortlet {
51  
52      public static final String CONNECTOR_IFRAME = "iframe";
53  
54      public static final String CONNECTOR_INCLUDE = "include";
55  
56      public void init(PortletConfig portletConfig) throws PortletException {
57          super.init(portletConfig);
58  
59          _connector = GetterUtil.getString(
60              portletConfig.getInitParameter("wai.connector"), CONNECTOR_IFRAME);
61      }
62  
63      public void processAction(
64          ActionRequest actionRequest, ActionResponse actionResponse) {
65      }
66  
67      public void render(
68              RenderRequest renderRequest, RenderResponse renderResponse)
69          throws IOException, PortletException {
70  
71          if (_connector.equals(CONNECTOR_IFRAME) ||
72              renderRequest.getWindowState().equals(WindowState.MAXIMIZED)) {
73  
74              invokeApplication(renderRequest, renderResponse);
75          }
76          else {
77              renderNormalWindowState(renderRequest, renderResponse);
78          }
79      }
80  
81      protected void forward(
82              HttpServletRequest request, HttpServletResponse response,
83              String path)
84          throws PortletException {
85  
86          RequestDispatcher requestDispatcher =
87              request.getRequestDispatcher(path);
88  
89          try {
90              requestDispatcher.forward(request, response);
91          }
92          catch (IOException ioe) {
93              _log.error(ioe, ioe);
94          }
95          catch (ServletException se) {
96              throw new PortletException(se);
97          }
98      }
99  
100     protected void invokeApplication(
101             RenderRequest renderRequest, RenderResponse renderResponse)
102         throws IOException, PortletException {
103 
104         HttpServletRequest request =
105             (HttpServletRequest)renderRequest.getAttribute(
106                 PortletServlet.PORTLET_SERVLET_REQUEST);
107         HttpServletResponse response =
108             (HttpServletResponse)renderRequest.getAttribute(
109                 PortletServlet.PORTLET_SERVLET_RESPONSE);
110 
111         String portletName = getPortletConfig().getPortletName();
112 
113         String friendlyURL = (String)request.getAttribute("FRIENDLY_URL");
114 
115         int pos = friendlyURL.indexOf(_MAPPING);
116 
117         String contextPath =
118             friendlyURL.substring(0, pos + _MAPPING.length()).concat(
119                 StringPool.SLASH).concat(portletName);
120 
121         pos = friendlyURL.indexOf(portletName);
122 
123         String pathInfo = friendlyURL.substring(pos + portletName.length());
124 
125         Map<String, String[]> params = new HashMap<String, String[]>(
126             request.getParameterMap());
127 
128         params.remove(_APP_URL);
129 
130         String queryString = HttpUtil.parameterMapToString(params, false);
131 
132         String appUrl = ParamUtil.getString(
133             renderRequest, _APP_URL, StringPool.SLASH);
134 
135         if (_connector.equals(CONNECTOR_IFRAME)) {
136             request.setAttribute(
137                 _APP_URL, renderRequest.getContextPath() + appUrl);
138 
139             String iframeExtraHeight = GetterUtil.getString(
140                 getPortletConfig().getInitParameter(
141                     "wai.connector.iframe.height.extra"),
142                 "40");
143 
144             renderRequest.setAttribute(
145                 "wai.connector.iframe.height.extra", iframeExtraHeight);
146 
147             forward(request, response, _JSP_IFRAME);
148         }
149         else if (_connector.equals(CONNECTOR_INCLUDE)) {
150             HttpServletRequest waiRequest = new WAIHttpServletRequest(
151                 request, contextPath, pathInfo, queryString, params);
152 
153             RequestDispatcher requestDispatcher = request.getRequestDispatcher(
154                 appUrl);
155 
156             try {
157                 requestDispatcher.forward(waiRequest, response);
158             }
159             catch (ServletException se) {
160                 throw new PortletException(se);
161             }
162         }
163     }
164 
165     protected void renderNormalWindowState(
166             RenderRequest renderRequest, RenderResponse renderResponse)
167         throws PortletException {
168 
169         HttpServletRequest request =
170             (HttpServletRequest)renderRequest.getAttribute(
171                 PortletServlet.PORTLET_SERVLET_REQUEST);
172         HttpServletResponse response =
173             (HttpServletResponse)renderRequest.getAttribute(
174                 PortletServlet.PORTLET_SERVLET_RESPONSE);
175 
176         PortletURL renderURL = renderResponse.createRenderURL();
177 
178         renderURL.setWindowState(WindowState.MAXIMIZED);
179 
180         renderRequest.setAttribute("renderURL", renderURL.toString());
181 
182         forward(request, response, _JSP_NORMAL_WINDOW_STATE);
183     }
184 
185     private static final String _MAPPING = "waiapp";
186 
187     private static final String _APP_URL = "appURL";
188 
189     private static final String _JSP_DIR = "/WEB-INF/jsp/liferay/wai";
190 
191     private static final String _JSP_IFRAME = _JSP_DIR + "/iframe.jsp";
192 
193     private static final String _JSP_NORMAL_WINDOW_STATE =
194         _JSP_DIR + "/normal_window_state.jsp";
195 
196     private static Log _log = LogFactoryUtil.getLog(WAIPortlet.class);
197 
198     private String _connector;
199 
200 }