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