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