1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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.PortletDisplay;
34  import com.liferay.portal.theme.ThemeDisplay;
35  import com.liferay.util.xml.DocUtil;
36  
37  import java.io.IOException;
38  
39  import java.util.Collection;
40  import java.util.Enumeration;
41  import java.util.Map;
42  
43  import javax.portlet.ActionRequest;
44  import javax.portlet.MimeResponse;
45  import javax.portlet.PortletRequest;
46  import javax.portlet.PortletResponse;
47  import javax.portlet.PortletSession;
48  import javax.portlet.PortletURL;
49  import javax.portlet.RenderRequest;
50  import javax.portlet.ResourceRequest;
51  import javax.portlet.ResourceURL;
52  import javax.portlet.WindowStateException;
53  
54  /**
55   * <a href="PortletRequestUtil.java.html"><b><i>View Source</i></b></a>
56   *
57   * @author Brian Wing Shun Chan
58   * @author Raymond Augé
59   *
60   */
61  public class PortletRequestUtil {
62  
63      public static String toXML(
64          PortletRequest portletRequest, PortletResponse portletResponse) {
65  
66          String xml = null;
67  
68          Document doc = SAXReaderUtil.createDocument();
69  
70          Element reqEl = doc.addElement("request");
71  
72          DocUtil.add(reqEl, "container-type", "portlet");
73          DocUtil.add(
74              reqEl, "container-namespace", portletRequest.getContextPath());
75          DocUtil.add(
76              reqEl, "content-type", portletRequest.getResponseContentType());
77          DocUtil.add(reqEl, "server-name", portletRequest.getServerName());
78          DocUtil.add(reqEl, "server-port", portletRequest.getServerPort());
79          DocUtil.add(reqEl, "secure", portletRequest.isSecure());
80          DocUtil.add(reqEl, "auth-type", portletRequest.getAuthType());
81          DocUtil.add(reqEl, "remote-user", portletRequest.getRemoteUser());
82          DocUtil.add(reqEl, "context-path", portletRequest.getContextPath());
83          DocUtil.add(reqEl, "locale", portletRequest.getLocale());
84          DocUtil.add(reqEl, "portlet-mode", portletRequest.getPortletMode());
85          DocUtil.add(
86              reqEl, "portlet-session-id",
87              portletRequest.getRequestedSessionId());
88          DocUtil.add(reqEl, "scheme", portletRequest.getScheme());
89          DocUtil.add(reqEl, "window-state", portletRequest.getWindowState());
90  
91          if (portletRequest instanceof ActionRequest) {
92              DocUtil.add(reqEl, "lifecycle", RenderRequest.ACTION_PHASE);
93          }
94          else if (portletRequest instanceof RenderRequest) {
95              DocUtil.add(reqEl, "lifecycle", RenderRequest.RENDER_PHASE);
96          }
97          else if (portletRequest instanceof ResourceRequest) {
98              DocUtil.add(reqEl, "lifecycle", RenderRequest.RESOURCE_PHASE);
99          }
100 
101         if (portletResponse instanceof MimeResponse) {
102             _mimeResponseToXML((MimeResponse)portletResponse, reqEl);
103         }
104 
105         ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
106             WebKeys.THEME_DISPLAY);
107 
108         if (themeDisplay != null) {
109             Element themeDisplayEl = reqEl.addElement("theme-display");
110 
111             _themeDisplayToXML(themeDisplay, themeDisplayEl);
112         }
113 
114         Element parametersEl = reqEl.addElement("parameters");
115 
116         Enumeration<String> enu = portletRequest.getParameterNames();
117 
118         while (enu.hasMoreElements()) {
119             String name = enu.nextElement();
120 
121             Element parameterEl = parametersEl.addElement("parameter");
122 
123             DocUtil.add(parameterEl, "name", name);
124 
125             String[] values = portletRequest.getParameterValues(name);
126 
127             for (int i = 0; i < values.length; i++) {
128                 DocUtil.add(parameterEl, "value", values[i]);
129             }
130         }
131 
132         Element attributesEl = reqEl.addElement("attributes");
133 
134         enu = portletRequest.getAttributeNames();
135 
136         while (enu.hasMoreElements()) {
137             String name = enu.nextElement();
138 
139             if (!_isValidAttributeName(name)) {
140                 continue;
141             }
142 
143             Object value = portletRequest.getAttribute(name);
144 
145             if (!_isValidAttributeValue(value)) {
146                 continue;
147             }
148 
149             Element attributeEl = attributesEl.addElement("attribute");
150 
151             DocUtil.add(attributeEl, "name", name);
152             DocUtil.add(attributeEl, "value", String.valueOf(value));
153         }
154 
155         Element portletSessionEl = reqEl.addElement("portlet-session");
156 
157         attributesEl = portletSessionEl.addElement("portlet-attributes");
158 
159         PortletSession portletSession = portletRequest.getPortletSession();
160 
161         try {
162             enu = portletSession.getAttributeNames(
163                 PortletSession.PORTLET_SCOPE);
164 
165             while (enu.hasMoreElements()) {
166                 String name = enu.nextElement();
167 
168                 if (!_isValidAttributeName(name)) {
169                     continue;
170                 }
171 
172                 Object value = portletSession.getAttribute(
173                     name, PortletSession.PORTLET_SCOPE);
174 
175                 if (!_isValidAttributeValue(value)) {
176                     continue;
177                 }
178 
179                 Element attributeEl = attributesEl.addElement("attribute");
180 
181                 DocUtil.add(attributeEl, "name", name);
182                 DocUtil.add(attributeEl, "value", String.valueOf(value));
183             }
184 
185             attributesEl = portletSessionEl.addElement(
186                 "application-attributes");
187 
188             enu = portletSession.getAttributeNames(
189                 PortletSession.APPLICATION_SCOPE);
190 
191             while (enu.hasMoreElements()) {
192                 String name = enu.nextElement();
193 
194                 if (!_isValidAttributeName(name)) {
195                     continue;
196                 }
197 
198                 Object value = portletSession.getAttribute(
199                     name, PortletSession.APPLICATION_SCOPE);
200 
201                 if (!_isValidAttributeValue(value)) {
202                     continue;
203                 }
204 
205                 Element attributeEl = attributesEl.addElement("attribute");
206 
207                 DocUtil.add(attributeEl, "name", name);
208                 DocUtil.add(attributeEl, "value", String.valueOf(value));
209             }
210         }
211         catch (IllegalStateException ise) {
212         }
213 
214         try {
215             xml = doc.formattedString();
216         }
217         catch (IOException ioe) {
218         }
219 
220         return xml;
221     }
222 
223     private static void _mimeResponseToXML(
224         MimeResponse mimeResponse, Element reqEl) {
225 
226         String namespace = mimeResponse.getNamespace();
227 
228         DocUtil.add(reqEl, "portlet-namespace", namespace);
229 
230         PortletURL actionUrl = mimeResponse.createActionURL();
231 
232         DocUtil.add(reqEl, "action-url", actionUrl);
233 
234         PortletURL renderUrl = mimeResponse.createRenderURL();
235 
236         DocUtil.add(reqEl, "render-url", renderUrl);
237 
238         try {
239             renderUrl.setWindowState(LiferayWindowState.EXCLUSIVE);
240 
241             DocUtil.add(reqEl, "render-url-exclusive", renderUrl);
242         }
243         catch (WindowStateException wse) {
244         }
245 
246         try {
247             renderUrl.setWindowState(LiferayWindowState.MAXIMIZED);
248 
249             DocUtil.add(reqEl, "render-url-maximized", renderUrl);
250         }
251         catch (WindowStateException wse) {
252         }
253 
254         try {
255             renderUrl.setWindowState(LiferayWindowState.MINIMIZED);
256 
257             DocUtil.add(reqEl, "render-url-minimized", renderUrl);
258         }
259         catch (WindowStateException wse) {
260         }
261 
262         try {
263             renderUrl.setWindowState(LiferayWindowState.NORMAL);
264 
265             DocUtil.add(reqEl, "render-url-normal", renderUrl);
266         }
267         catch (WindowStateException wse) {
268         }
269 
270         try {
271             renderUrl.setWindowState(LiferayWindowState.POP_UP);
272 
273             DocUtil.add(reqEl, "render-url-pop-up", renderUrl);
274         }
275         catch (WindowStateException wse) {
276         }
277 
278         ResourceURL resourceURL = mimeResponse.createResourceURL();
279 
280         String resourceURLString = HttpUtil.removeParameter(
281             resourceURL.toString(), namespace + "struts_action");
282 
283         resourceURLString = HttpUtil.removeParameter(
284             resourceURLString, namespace + "redirect");
285 
286         DocUtil.add(reqEl, "resource-url", resourceURLString);
287     }
288 
289     private static void _themeDisplayToXML(
290         ThemeDisplay themeDisplay, Element themeDisplayEl) {
291 
292         DocUtil.add(themeDisplayEl, "cdn-host", themeDisplay.getCDNHost());
293         DocUtil.add(themeDisplayEl, "company-id", themeDisplay.getCompanyId());
294         DocUtil.add(
295             themeDisplayEl, "do-as-user-id", themeDisplay.getDoAsUserId());
296         DocUtil.add(
297             themeDisplayEl, "i18n-language-id",
298             themeDisplay.getI18nLanguageId());
299         DocUtil.add(
300             themeDisplayEl, "language-id", themeDisplay.getLanguageId());
301         DocUtil.add(themeDisplayEl, "locale", themeDisplay.getLocale());
302         DocUtil.add(
303             themeDisplayEl, "path-context", themeDisplay.getPathContext());
304         DocUtil.add(
305             themeDisplayEl, "path-friendly-url-private-group",
306             themeDisplay.getPathFriendlyURLPrivateGroup());
307         DocUtil.add(
308             themeDisplayEl, "path-friendly-url-private-user",
309             themeDisplay.getPathFriendlyURLPrivateUser());
310         DocUtil.add(
311             themeDisplayEl, "path-friendly-url-public",
312             themeDisplay.getPathFriendlyURLPublic());
313         DocUtil.add(themeDisplayEl, "path-image", themeDisplay.getPathImage());
314         DocUtil.add(themeDisplayEl, "path-main", themeDisplay.getPathMain());
315         DocUtil.add(
316             themeDisplayEl, "path-theme-images",
317             themeDisplay.getPathThemeImages());
318         DocUtil.add(themeDisplayEl, "plid", themeDisplay.getPlid());
319         DocUtil.add(
320             themeDisplayEl, "portal-url",
321             HttpUtil.removeProtocol(themeDisplay.getPortalURL()));
322         DocUtil.add(
323             themeDisplayEl, "real-user-id", themeDisplay.getRealUserId());
324         DocUtil.add(
325             themeDisplayEl, "scope-group-id", themeDisplay.getScopeGroupId());
326         DocUtil.add(themeDisplayEl, "secure", themeDisplay.isSecure());
327         DocUtil.add(
328             themeDisplayEl, "server-name", themeDisplay.getServerName());
329         DocUtil.add(
330             themeDisplayEl, "server-port", themeDisplay.getServerPort());
331         DocUtil.add(
332             themeDisplayEl, "time-zone", themeDisplay.getTimeZone().getID());
333         DocUtil.add(
334             themeDisplayEl, "url-portal",
335             HttpUtil.removeProtocol(themeDisplay.getURLPortal()));
336         DocUtil.add(themeDisplayEl, "user-id", themeDisplay.getUserId());
337 
338         if (themeDisplay.getPortletDisplay() != null) {
339             Element portletDisplayEl = themeDisplayEl.addElement(
340                 "portlet-display");
341 
342             _portletDisplayToXML(
343                 themeDisplay.getPortletDisplay(), portletDisplayEl);
344         }
345     }
346 
347     private static boolean _isValidAttributeName(String name) {
348         if (name.equalsIgnoreCase("j_password") ||
349             name.equalsIgnoreCase("LAYOUT_CONTENT") ||
350             name.equalsIgnoreCase("LAYOUTS") ||
351             name.equalsIgnoreCase("PORTLET_RENDER_PARAMETERS") ||
352             name.equalsIgnoreCase("USER_PASSWORD") ||
353             name.startsWith("javax.") ||
354             name.startsWith("liferay-ui:")) {
355 
356             return false;
357         }
358         else {
359             return true;
360         }
361     }
362 
363     private static boolean _isValidAttributeValue(Object obj) {
364         if (obj == null) {
365             return false;
366         }
367         else if (obj instanceof Collection) {
368             Collection<?> col = (Collection<?>)obj;
369 
370             if (col.size() == 0) {
371                 return false;
372             }
373             else {
374                 return true;
375             }
376         }
377         else if (obj instanceof Map) {
378             Map<?, ?> map = (Map<?, ?>)obj;
379 
380             if (map.size() == 0) {
381                 return false;
382             }
383             else {
384                 return true;
385             }
386         }
387         else {
388             String objString = String.valueOf(obj);
389 
390             if (Validator.isNull(objString)) {
391                 return false;
392             }
393 
394             String hashCode =
395                 StringPool.AT + Integer.toHexString(obj.hashCode());
396 
397             if (objString.endsWith(hashCode)) {
398                 return false;
399             }
400 
401             return true;
402         }
403     }
404 
405     private static void _portletDisplayToXML(
406         PortletDisplay portletDisplay, Element portletDisplayEl) {
407 
408         DocUtil.add(portletDisplayEl, "id", portletDisplay.getId());
409         DocUtil.add(
410             portletDisplayEl, "instance-id", portletDisplay.getInstanceId());
411         DocUtil.add(
412             portletDisplayEl, "portlet-name", portletDisplay.getPortletName());
413         DocUtil.add(
414             portletDisplayEl, "resource-pk", portletDisplay.getResourcePK());
415         DocUtil.add(
416             portletDisplayEl, "root-portlet-id",
417             portletDisplay.getRootPortletId());
418         DocUtil.add(
419             portletDisplayEl, "title", portletDisplay.getTitle());
420     }
421 
422 }