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.HttpUtil;
28 import com.liferay.portal.kernel.util.ParamUtil;
29 import com.liferay.portal.kernel.util.StringMaker;
30 import com.liferay.portal.kernel.util.StringPool;
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<String, String[]> params = new HashMap<String, String[]>(
131 httpReq.getParameterMap());
132
133 params.remove(_APP_URL);
134
135 String queryString = HttpUtil.parameterMapToString(params, false);
136
137 String appUrl = ParamUtil.getString(req, _APP_URL, StringPool.SLASH);
138
139 if (_connector.equals(CONNECTOR_IFRAME)) {
140 httpReq.setAttribute(_APP_URL, req.getContextPath() + appUrl);
141
142 String iframeExtraHeight = GetterUtil.getString(
143 getPortletConfig().getInitParameter(
144 "wai.connector.iframe.height.extra"),
145 "40");
146
147 req.setAttribute(
148 "wai.connector.iframe.height.extra", iframeExtraHeight);
149
150 forward(httpReq, httpRes, _JSP_IFRAME);
151 }
152 else if (_connector.equals(CONNECTOR_INCLUDE)) {
153 HttpServletRequest waiHttpReq = new WAIHttpServletRequest(
154 httpReq, contextPath.toString(), pathInfo, queryString, params);
155
156 RequestDispatcher rd = httpReq.getRequestDispatcher(appUrl);
157
158 try {
159 rd.forward(waiHttpReq, httpRes);
160 }
161 catch (ServletException se) {
162 throw new PortletException(se);
163 }
164 }
165 }
166
167 protected void renderNormalWindowState(
168 RenderRequest req, RenderResponse res)
169 throws IOException, PortletException {
170
171 HttpServletRequest httpReq = (HttpServletRequest)req.getAttribute(
172 PortletServlet.PORTLET_SERVLET_REQUEST);
173 HttpServletResponse httpRes = (HttpServletResponse)req.getAttribute(
174 PortletServlet.PORTLET_SERVLET_RESPONSE);
175
176 PortletURL renderURL = res.createRenderURL();
177
178 renderURL.setWindowState(WindowState.MAXIMIZED);
179
180 req.setAttribute("renderURL", renderURL.toString());
181
182 forward(httpReq, httpRes, _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 = LogFactory.getLog(WAIPortlet.class);
197
198 private String _connector;
199
200 }