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.portal.servlet;
022:
023: import com.liferay.portal.kernel.util.JavaConstants;
024: import com.liferay.portal.util.PropsUtil;
025: import com.liferay.util.CollectionFactory;
026: import com.liferay.util.servlet.DynamicServletRequest;
027:
028: import java.util.ArrayList;
029: import java.util.Collections;
030: import java.util.Enumeration;
031: import java.util.List;
032: import java.util.Set;
033:
034: import javax.servlet.http.HttpServletRequest;
035:
036: /**
037: * <a href="NamespaceServletRequest.java.html"><b><i>View Source</i></b></a>
038: *
039: * <p>
040: * This class ensures that portlet attributes and parameters are private to the
041: * portlet.
042: * </p>
043: *
044: * @author Brian Myunghun Kim
045: *
046: */
047: public class NamespaceServletRequest extends DynamicServletRequest {
048:
049: static Set reservedAttrs = CollectionFactory.getHashSet();
050:
051: static {
052: reservedAttrs.add(JavaConstants.JAVAX_PORTLET_CONFIG);
053: reservedAttrs.add(JavaConstants.JAVAX_PORTLET_PORTLET);
054: reservedAttrs.add(JavaConstants.JAVAX_PORTLET_REQUEST);
055: reservedAttrs.add(JavaConstants.JAVAX_PORTLET_RESPONSE);
056: }
057:
058: public static final String[] CUSTOM_RESERVED_ATTRS = PropsUtil
059: .getArray(PropsUtil.REQUEST_SHARED_ATTRIBUTES);
060:
061: public NamespaceServletRequest(HttpServletRequest req,
062: String attrNamespace, String paramNamespace) {
063:
064: this (req, attrNamespace, paramNamespace, true);
065: }
066:
067: public NamespaceServletRequest(HttpServletRequest req,
068: String attrNamespace, String paramNamespace, boolean inherit) {
069:
070: super (req, inherit);
071:
072: _attrNamespace = attrNamespace;
073: _paramNamespace = paramNamespace;
074: }
075:
076: public Object getAttribute(String name) {
077: Object value = super .getAttribute(_attrNamespace + name);
078:
079: if (value == null) {
080: value = super .getAttribute(name);
081: }
082:
083: return value;
084: }
085:
086: public Enumeration getAttributeNames() {
087: List names = new ArrayList();
088:
089: Enumeration enu = super .getAttributeNames();
090:
091: while (enu.hasMoreElements()) {
092: String name = (String) enu.nextElement();
093:
094: if (name.startsWith(_attrNamespace)) {
095: names.add(name.substring(_attrNamespace.length(), name
096: .length()));
097: }
098: }
099:
100: return Collections.enumeration(names);
101: }
102:
103: public void setAttribute(String name, Object value) {
104: if (_isReservedParam(name)) {
105: super .setAttribute(name, value);
106: } else {
107: super .setAttribute(_attrNamespace + name, value);
108: }
109: }
110:
111: public void setAttribute(String name, Object value,
112: boolean privateRequestAttribute) {
113:
114: if (!privateRequestAttribute) {
115: super .setAttribute(name, value);
116: } else {
117: setAttribute(name, value);
118: }
119: }
120:
121: public void removeAttribute(String name) {
122: if (_isReservedParam(name)) {
123: super .removeAttribute(name);
124: } else {
125: super .removeAttribute(_attrNamespace + name);
126: }
127: }
128:
129: public String getParameter(String name) {
130: if (name == null) {
131: throw new IllegalArgumentException();
132: }
133:
134: String value = super .getParameter(name);
135:
136: if (value == null) {
137: value = super .getParameter(_paramNamespace + name);
138: }
139:
140: return value;
141: }
142:
143: private boolean _isReservedParam(String name) {
144: if (reservedAttrs.contains(name)) {
145: return true;
146: } else {
147: for (int i = 0; i < CUSTOM_RESERVED_ATTRS.length; i++) {
148: if (name.startsWith(CUSTOM_RESERVED_ATTRS[i])) {
149: return true;
150: }
151: }
152: }
153:
154: return false;
155: }
156:
157: private String _attrNamespace;
158: private String _paramNamespace;
159:
160: }
|