001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.util.portlet;
022:
023: import com.liferay.portal.kernel.portlet.LiferayWindowState;
024: import com.liferay.portal.kernel.util.StringPool;
025: import com.liferay.portal.kernel.util.Validator;
026: import com.liferay.util.xml.DocUtil;
027: import com.liferay.util.xml.XMLFormatter;
028:
029: import java.io.IOException;
030:
031: import java.util.Collection;
032: import java.util.Enumeration;
033: import java.util.Map;
034:
035: import javax.portlet.ActionRequest;
036: import javax.portlet.PortletRequest;
037: import javax.portlet.PortletResponse;
038: import javax.portlet.PortletSession;
039: import javax.portlet.PortletURL;
040: import javax.portlet.RenderRequest;
041: import javax.portlet.RenderResponse;
042: import javax.portlet.WindowStateException;
043:
044: import org.dom4j.Document;
045: import org.dom4j.DocumentHelper;
046: import org.dom4j.Element;
047:
048: /**
049: * <a href="PortletRequestUtil.java.html"><b><i>View Source</i></b></a>
050: *
051: * @author Brian Wing Shun Chan
052: * @author Raymond Augé
053: *
054: */
055: public class PortletRequestUtil {
056:
057: public static String toXML(PortletRequest req, PortletResponse res) {
058: String xml = null;
059:
060: Document doc = DocumentHelper.createDocument();
061:
062: Element reqEl = doc.addElement("request");
063:
064: DocUtil.add(reqEl, "container-type", "portlet");
065: DocUtil.add(reqEl, "container-namespace", req.getContextPath());
066: DocUtil
067: .add(reqEl, "content-type", req
068: .getResponseContentType());
069: DocUtil.add(reqEl, "server-name", req.getServerName());
070: DocUtil.add(reqEl, "server-port", req.getServerPort());
071: DocUtil.add(reqEl, "secure", req.isSecure());
072: DocUtil.add(reqEl, "auth-type", req.getAuthType());
073: DocUtil.add(reqEl, "remote-user", req.getRemoteUser());
074: DocUtil.add(reqEl, "context-path", req.getContextPath());
075: DocUtil.add(reqEl, "locale", req.getLocale());
076: DocUtil.add(reqEl, "portlet-mode", req.getPortletMode());
077: DocUtil.add(reqEl, "portlet-session-id", req
078: .getRequestedSessionId());
079: DocUtil.add(reqEl, "scheme", req.getScheme());
080: DocUtil.add(reqEl, "window-state", req.getWindowState());
081:
082: if (req instanceof RenderRequest) {
083: DocUtil.add(reqEl, "action", Boolean.FALSE);
084: } else if (req instanceof ActionRequest) {
085: DocUtil.add(reqEl, "action", Boolean.TRUE);
086: }
087:
088: if (res instanceof RenderResponse) {
089: _renderResponseToXML((RenderResponse) res, reqEl);
090: }
091:
092: Element parametersEl = reqEl.addElement("parameters");
093:
094: Enumeration enu = req.getParameterNames();
095:
096: while (enu.hasMoreElements()) {
097: String name = (String) enu.nextElement();
098:
099: 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
136: .addElement("portlet-attributes");
137:
138: PortletSession ses = req.getPortletSession();
139:
140: enu = ses.getAttributeNames(PortletSession.PORTLET_SCOPE);
141:
142: while (enu.hasMoreElements()) {
143: String name = (String) enu.nextElement();
144:
145: if (!_isValidAttributeName(name)) {
146: continue;
147: }
148:
149: Object value = ses.getAttribute(name,
150: PortletSession.PORTLET_SCOPE);
151:
152: if (!_isValidAttributeValue(value)) {
153: continue;
154: }
155:
156: Element attributeEl = attributesEl.addElement("attribute");
157:
158: DocUtil.add(attributeEl, "name", name);
159: DocUtil.add(attributeEl, "value", String.valueOf(value));
160: }
161:
162: attributesEl = portletSessionEl
163: .addElement("application-attributes");
164:
165: enu = ses.getAttributeNames(PortletSession.APPLICATION_SCOPE);
166:
167: while (enu.hasMoreElements()) {
168: String name = (String) enu.nextElement();
169:
170: if (!_isValidAttributeName(name)) {
171: continue;
172: }
173:
174: Object value = ses.getAttribute(name,
175: PortletSession.APPLICATION_SCOPE);
176:
177: if (!_isValidAttributeValue(value)) {
178: continue;
179: }
180:
181: Element attributeEl = attributesEl.addElement("attribute");
182:
183: DocUtil.add(attributeEl, "name", name);
184: DocUtil.add(attributeEl, "value", String.valueOf(value));
185: }
186:
187: try {
188: xml = XMLFormatter.toString(doc);
189: } catch (IOException ioe) {
190: }
191:
192: return xml;
193: }
194:
195: private static void _renderResponseToXML(RenderResponse res,
196: Element reqEl) {
197:
198: DocUtil.add(reqEl, "portlet-namespace", res.getNamespace());
199:
200: PortletURL url = res.createRenderURL();
201:
202: DocUtil.add(reqEl, "render-url", url);
203:
204: try {
205: url.setWindowState(LiferayWindowState.EXCLUSIVE);
206:
207: DocUtil.add(reqEl, "render-url-exclusive", url);
208: } catch (WindowStateException wse) {
209: }
210:
211: try {
212: url.setWindowState(LiferayWindowState.MAXIMIZED);
213:
214: DocUtil.add(reqEl, "render-url-maximized", url);
215: } catch (WindowStateException wse) {
216: }
217:
218: try {
219: url.setWindowState(LiferayWindowState.MINIMIZED);
220:
221: DocUtil.add(reqEl, "render-url-minimized", url);
222: } catch (WindowStateException wse) {
223: }
224:
225: try {
226: url.setWindowState(LiferayWindowState.NORMAL);
227:
228: DocUtil.add(reqEl, "render-url-normal", url);
229: } catch (WindowStateException wse) {
230: }
231:
232: try {
233: url.setWindowState(LiferayWindowState.POP_UP);
234:
235: DocUtil.add(reqEl, "render-url-pop-up", url);
236: } catch (WindowStateException wse) {
237: }
238: }
239:
240: private static boolean _isValidAttributeName(String name) {
241: if (name.equalsIgnoreCase("j_password")
242: || name.equalsIgnoreCase("LAYOUT_CONTENT")
243: || name.equalsIgnoreCase("LAYOUTS")
244: || name.equalsIgnoreCase("PORTLET_RENDER_PARAMETERS")
245: || name.equalsIgnoreCase("USER_PASSWORD")
246: || name.startsWith("javax.")
247: || name.startsWith("liferay-ui:")) {
248:
249: return false;
250: } else {
251: return true;
252: }
253: }
254:
255: private static boolean _isValidAttributeValue(Object obj) {
256: if (obj == null) {
257: return false;
258: } else if (obj instanceof Collection) {
259: Collection col = (Collection) obj;
260:
261: if (col.size() == 0) {
262: return false;
263: } else {
264: return true;
265: }
266: } else if (obj instanceof Map) {
267: Map map = (Map) obj;
268:
269: if (map.size() == 0) {
270: return false;
271: } else {
272: return true;
273: }
274: } else {
275: String objString = String.valueOf(obj);
276:
277: if (Validator.isNull(objString)) {
278: return false;
279: }
280:
281: String hashCode = StringPool.AT
282: + Integer.toHexString(obj.hashCode());
283:
284: if (objString.endsWith(hashCode)) {
285: return false;
286: }
287:
288: return true;
289: }
290: }
291:
292: }
|