1
22
23 package com.liferay.util.portlet;
24
25 import com.liferay.portal.kernel.portlet.LiferayWindowState;
26 import com.liferay.portal.kernel.util.HttpUtil;
27 import com.liferay.portal.kernel.util.StringPool;
28 import com.liferay.portal.kernel.util.Validator;
29 import com.liferay.portal.kernel.util.WebKeys;
30 import com.liferay.portal.kernel.xml.Document;
31 import com.liferay.portal.kernel.xml.Element;
32 import com.liferay.portal.kernel.xml.SAXReaderUtil;
33 import com.liferay.portal.theme.ThemeDisplay;
34 import com.liferay.util.xml.DocUtil;
35
36 import java.io.IOException;
37
38 import java.util.Collection;
39 import java.util.Enumeration;
40 import java.util.Map;
41
42 import javax.portlet.ActionRequest;
43 import javax.portlet.PortletRequest;
44 import javax.portlet.PortletResponse;
45 import javax.portlet.PortletSession;
46 import javax.portlet.PortletURL;
47 import javax.portlet.RenderRequest;
48 import javax.portlet.RenderResponse;
49 import javax.portlet.WindowStateException;
50
51
58 public class PortletRequestUtil {
59
60 public static String toXML(
61 PortletRequest portletRequest, PortletResponse portletResponse) {
62
63 String xml = null;
64
65 Document doc = SAXReaderUtil.createDocument();
66
67 Element reqEl = doc.addElement("request");
68
69 DocUtil.add(reqEl, "container-type", "portlet");
70 DocUtil.add(
71 reqEl, "container-namespace", portletRequest.getContextPath());
72 DocUtil.add(
73 reqEl, "content-type", portletRequest.getResponseContentType());
74 DocUtil.add(reqEl, "server-name", portletRequest.getServerName());
75 DocUtil.add(reqEl, "server-port", portletRequest.getServerPort());
76 DocUtil.add(reqEl, "secure", portletRequest.isSecure());
77 DocUtil.add(reqEl, "auth-type", portletRequest.getAuthType());
78 DocUtil.add(reqEl, "remote-user", portletRequest.getRemoteUser());
79 DocUtil.add(reqEl, "context-path", portletRequest.getContextPath());
80 DocUtil.add(reqEl, "locale", portletRequest.getLocale());
81 DocUtil.add(reqEl, "portlet-mode", portletRequest.getPortletMode());
82 DocUtil.add(
83 reqEl, "portlet-session-id",
84 portletRequest.getRequestedSessionId());
85 DocUtil.add(reqEl, "scheme", portletRequest.getScheme());
86 DocUtil.add(reqEl, "window-state", portletRequest.getWindowState());
87
88 if (portletRequest instanceof RenderRequest) {
89 DocUtil.add(reqEl, "action", Boolean.FALSE);
90 }
91 else if (portletRequest instanceof ActionRequest) {
92 DocUtil.add(reqEl, "action", Boolean.TRUE);
93 }
94
95 if (portletResponse instanceof RenderResponse) {
96 _renderResponseToXML((RenderResponse)portletResponse, reqEl);
97 }
98
99 ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
100 WebKeys.THEME_DISPLAY);
101
102 if (themeDisplay != null) {
103 Element themeDisplayEl = reqEl.addElement("theme-display");
104
105 _themeDisplayToXML(themeDisplay, themeDisplayEl);
106 }
107
108 Element parametersEl = reqEl.addElement("parameters");
109
110 Enumeration<String> enu = portletRequest.getParameterNames();
111
112 while (enu.hasMoreElements()) {
113 String name = enu.nextElement();
114
115 Element parameterEl = parametersEl.addElement("parameter");
116
117 DocUtil.add(parameterEl, "name", name);
118
119 String[] values = portletRequest.getParameterValues(name);
120
121 for (int i = 0; i < values.length; i++) {
122 DocUtil.add(parameterEl, "value", values[i]);
123 }
124 }
125
126 Element attributesEl = reqEl.addElement("attributes");
127
128 enu = portletRequest.getAttributeNames();
129
130 while (enu.hasMoreElements()) {
131 String name = enu.nextElement();
132
133 if (!_isValidAttributeName(name)) {
134 continue;
135 }
136
137 Object value = portletRequest.getAttribute(name);
138
139 if (!_isValidAttributeValue(value)) {
140 continue;
141 }
142
143 Element attributeEl = attributesEl.addElement("attribute");
144
145 DocUtil.add(attributeEl, "name", name);
146 DocUtil.add(attributeEl, "value", String.valueOf(value));
147 }
148
149 Element portletSessionEl = reqEl.addElement("portlet-session");
150
151 attributesEl = portletSessionEl.addElement("portlet-attributes");
152
153 PortletSession portletSession = portletRequest.getPortletSession();
154
155 enu = portletSession.getAttributeNames(PortletSession.PORTLET_SCOPE);
156
157 while (enu.hasMoreElements()) {
158 String name = enu.nextElement();
159
160 if (!_isValidAttributeName(name)) {
161 continue;
162 }
163
164 Object value = portletSession.getAttribute(
165 name, PortletSession.PORTLET_SCOPE);
166
167 if (!_isValidAttributeValue(value)) {
168 continue;
169 }
170
171 Element attributeEl = attributesEl.addElement("attribute");
172
173 DocUtil.add(attributeEl, "name", name);
174 DocUtil.add(attributeEl, "value", String.valueOf(value));
175 }
176
177 attributesEl = portletSessionEl.addElement("application-attributes");
178
179 enu = portletSession.getAttributeNames(
180 PortletSession.APPLICATION_SCOPE);
181
182 while (enu.hasMoreElements()) {
183 String name = enu.nextElement();
184
185 if (!_isValidAttributeName(name)) {
186 continue;
187 }
188
189 Object value = portletSession.getAttribute(
190 name, PortletSession.APPLICATION_SCOPE);
191
192 if (!_isValidAttributeValue(value)) {
193 continue;
194 }
195
196 Element attributeEl = attributesEl.addElement("attribute");
197
198 DocUtil.add(attributeEl, "name", name);
199 DocUtil.add(attributeEl, "value", String.valueOf(value));
200 }
201
202 try {
203 xml = doc.formattedString();
204 }
205 catch (IOException ioe) {
206 }
207
208 return xml;
209 }
210
211 private static void _renderResponseToXML(
212 RenderResponse renderResponse, Element reqEl) {
213
214 DocUtil.add(reqEl, "portlet-namespace", renderResponse.getNamespace());
215
216 PortletURL url = renderResponse.createRenderURL();
217
218 DocUtil.add(reqEl, "render-url", url);
219
220 try {
221 url.setWindowState(LiferayWindowState.EXCLUSIVE);
222
223 DocUtil.add(reqEl, "render-url-exclusive", url);
224 }
225 catch (WindowStateException wse) {
226 }
227
228 try {
229 url.setWindowState(LiferayWindowState.MAXIMIZED);
230
231 DocUtil.add(reqEl, "render-url-maximized", url);
232 }
233 catch (WindowStateException wse) {
234 }
235
236 try {
237 url.setWindowState(LiferayWindowState.MINIMIZED);
238
239 DocUtil.add(reqEl, "render-url-minimized", url);
240 }
241 catch (WindowStateException wse) {
242 }
243
244 try {
245 url.setWindowState(LiferayWindowState.NORMAL);
246
247 DocUtil.add(reqEl, "render-url-normal", url);
248 }
249 catch (WindowStateException wse) {
250 }
251
252 try {
253 url.setWindowState(LiferayWindowState.POP_UP);
254
255 DocUtil.add(reqEl, "render-url-pop-up", url);
256 }
257 catch (WindowStateException wse) {
258 }
259 }
260
261 private static void _themeDisplayToXML(
262 ThemeDisplay themeDisplay, Element themeDisplayEl) {
263
264 DocUtil.add(themeDisplayEl, "cdn-host", themeDisplay.getCDNHost());
265 DocUtil.add(themeDisplayEl, "company-id", themeDisplay.getCompanyId());
266 DocUtil.add(
267 themeDisplayEl, "path-context", themeDisplay.getPathContext());
268 DocUtil.add(
269 themeDisplayEl, "path-friendly-url-private-group",
270 themeDisplay.getPathFriendlyURLPrivateGroup());
271 DocUtil.add(
272 themeDisplayEl, "path-friendly-url-private-user",
273 themeDisplay.getPathFriendlyURLPrivateUser());
274 DocUtil.add(
275 themeDisplayEl, "path-friendly-url-public",
276 themeDisplay.getPathFriendlyURLPublic());
277 DocUtil.add(themeDisplayEl, "path-image", themeDisplay.getPathImage());
278 DocUtil.add(themeDisplayEl, "path-main", themeDisplay.getPathMain());
279 DocUtil.add(
280 themeDisplayEl, "path-theme-images",
281 themeDisplay.getPathThemeImages());
282 DocUtil.add(themeDisplayEl, "plid", themeDisplay.getPlid());
283 DocUtil.add(
284 themeDisplayEl, "url-portal",
285 HttpUtil.removeProtocol(themeDisplay.getURLPortal()));
286 }
287
288 private static boolean _isValidAttributeName(String name) {
289 if (name.equalsIgnoreCase("j_password") ||
290 name.equalsIgnoreCase("LAYOUT_CONTENT") ||
291 name.equalsIgnoreCase("LAYOUTS") ||
292 name.equalsIgnoreCase("PORTLET_RENDER_PARAMETERS") ||
293 name.equalsIgnoreCase("USER_PASSWORD") ||
294 name.startsWith("javax.") ||
295 name.startsWith("liferay-ui:")) {
296
297 return false;
298 }
299 else {
300 return true;
301 }
302 }
303
304 private static boolean _isValidAttributeValue(Object obj) {
305 if (obj == null) {
306 return false;
307 }
308 else if (obj instanceof Collection) {
309 Collection<?> col = (Collection<?>)obj;
310
311 if (col.size() == 0) {
312 return false;
313 }
314 else {
315 return true;
316 }
317 }
318 else if (obj instanceof Map) {
319 Map<?, ?> map = (Map<?, ?>)obj;
320
321 if (map.size() == 0) {
322 return false;
323 }
324 else {
325 return true;
326 }
327 }
328 else {
329 String objString = String.valueOf(obj);
330
331 if (Validator.isNull(objString)) {
332 return false;
333 }
334
335 String hashCode =
336 StringPool.AT + Integer.toHexString(obj.hashCode());
337
338 if (objString.endsWith(hashCode)) {
339 return false;
340 }
341
342 return true;
343 }
344 }
345
346 }