001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.util.bridges.wai;
022:
023: import com.liferay.portal.kernel.servlet.PortletServlet;
024: import com.liferay.portal.kernel.util.GetterUtil;
025: import com.liferay.portal.kernel.util.ParamUtil;
026: import com.liferay.portal.kernel.util.StringMaker;
027: import com.liferay.portal.kernel.util.StringPool;
028: import com.liferay.util.HttpUtil;
029:
030: import java.io.IOException;
031:
032: import java.util.HashMap;
033: import java.util.Map;
034:
035: import javax.portlet.ActionRequest;
036: import javax.portlet.ActionResponse;
037: import javax.portlet.GenericPortlet;
038: import javax.portlet.PortletConfig;
039: import javax.portlet.PortletException;
040: import javax.portlet.PortletURL;
041: import javax.portlet.RenderRequest;
042: import javax.portlet.RenderResponse;
043: import javax.portlet.WindowState;
044:
045: import javax.servlet.RequestDispatcher;
046: import javax.servlet.ServletException;
047: import javax.servlet.http.HttpServletRequest;
048: import javax.servlet.http.HttpServletResponse;
049:
050: import org.apache.commons.logging.Log;
051: import org.apache.commons.logging.LogFactory;
052:
053: /**
054: * <a href="WAIPortlet.java.html"><b><i>View Source</i></b></a>
055: *
056: * @author Jorge Ferrer
057: *
058: */
059: public class WAIPortlet extends GenericPortlet {
060:
061: public static final String CONNECTOR_IFRAME = "iframe";
062:
063: public static final String CONNECTOR_INCLUDE = "include";
064:
065: public void init(PortletConfig portletConfig)
066: throws PortletException {
067: super .init(portletConfig);
068:
069: _connector = GetterUtil.getString(portletConfig
070: .getInitParameter("wai.connector"), CONNECTOR_IFRAME);
071: }
072:
073: public void processAction(ActionRequest req, ActionResponse res)
074: throws IOException, PortletException {
075: }
076:
077: public void render(RenderRequest req, RenderResponse res)
078: throws IOException, PortletException {
079:
080: if (req.getWindowState().equals(WindowState.MAXIMIZED)) {
081: invokeApplication(req, res);
082: } else {
083: renderNormalWindowState(req, res);
084: }
085: }
086:
087: protected void forward(HttpServletRequest req,
088: HttpServletResponse res, String path)
089: throws PortletException {
090:
091: RequestDispatcher rd = req.getRequestDispatcher(path);
092:
093: try {
094: rd.forward(req, res);
095: } catch (IOException ioe) {
096: _log.error(ioe, ioe);
097: } catch (ServletException se) {
098: throw new PortletException(se);
099: }
100: }
101:
102: protected void invokeApplication(RenderRequest req,
103: RenderResponse res) throws IOException, PortletException {
104:
105: HttpServletRequest httpReq = (HttpServletRequest) req
106: .getAttribute(PortletServlet.PORTLET_SERVLET_REQUEST);
107: HttpServletResponse httpRes = (HttpServletResponse) req
108: .getAttribute(PortletServlet.PORTLET_SERVLET_RESPONSE);
109:
110: String portletName = getPortletConfig().getPortletName();
111:
112: String friendlyURL = (String) httpReq
113: .getAttribute("FRIENDLY_URL");
114:
115: int pos = friendlyURL.indexOf(_MAPPING);
116:
117: StringMaker contextPath = new StringMaker();
118:
119: contextPath.append(friendlyURL.substring(0, pos
120: + _MAPPING.length()));
121: contextPath.append(StringPool.SLASH);
122: contextPath.append(portletName);
123:
124: pos = friendlyURL.indexOf(portletName);
125:
126: String pathInfo = friendlyURL.substring(pos
127: + portletName.length());
128:
129: Map params = new HashMap(httpReq.getParameterMap());
130:
131: params.remove(_APP_URL);
132:
133: String queryString = HttpUtil.parameterMapToString(params,
134: false);
135:
136: String appUrl = ParamUtil.getString(req, _APP_URL,
137: StringPool.SLASH);
138:
139: if (_connector.equals(CONNECTOR_IFRAME)) {
140: httpReq.setAttribute(_APP_URL, req.getContextPath()
141: + appUrl);
142:
143: String iframeExtraHeight = GetterUtil.getString(
144: getPortletConfig().getInitParameter(
145: "wai.connector.iframe.height.extra"), "40");
146:
147: req.setAttribute("wai.connector.iframe.height.extra",
148: iframeExtraHeight);
149:
150: forward(httpReq, httpRes, _JSP_IFRAME);
151: } else if (_connector.equals(CONNECTOR_INCLUDE)) {
152: HttpServletRequest waiHttpReq = new WAIHttpServletRequest(
153: httpReq, contextPath.toString(), pathInfo,
154: queryString, params);
155:
156: RequestDispatcher rd = httpReq.getRequestDispatcher(appUrl);
157:
158: try {
159: rd.forward(waiHttpReq, httpRes);
160: } catch (ServletException se) {
161: throw new PortletException(se);
162: }
163: }
164: }
165:
166: protected void renderNormalWindowState(RenderRequest req,
167: RenderResponse res) throws IOException, PortletException {
168:
169: HttpServletRequest httpReq = (HttpServletRequest) req
170: .getAttribute(PortletServlet.PORTLET_SERVLET_REQUEST);
171: HttpServletResponse httpRes = (HttpServletResponse) req
172: .getAttribute(PortletServlet.PORTLET_SERVLET_RESPONSE);
173:
174: PortletURL renderURL = res.createRenderURL();
175:
176: renderURL.setWindowState(WindowState.MAXIMIZED);
177:
178: req.setAttribute("renderURL", renderURL.toString());
179:
180: forward(httpReq, httpRes, _JSP_NORMAL_WINDOW_STATE);
181: }
182:
183: private static final String _MAPPING = "waiapp";
184:
185: private static final String _APP_URL = "appURL";
186:
187: private static final String _JSP_DIR = "/WEB-INF/jsp/liferay/wai";
188:
189: private static final String _JSP_IFRAME = _JSP_DIR + "/iframe.jsp";
190:
191: private static final String _JSP_NORMAL_WINDOW_STATE = _JSP_DIR
192: + "/normal_window_state.jsp";
193:
194: private static Log _log = LogFactory.getLog(WAIPortlet.class);
195:
196: private String _connector;
197:
198: }
|