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