001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.engine.servlet;
018:
019: import java.util.Collections;
020: import java.util.Enumeration;
021: import java.util.HashSet;
022:
023: import javax.servlet.http.HttpSession;
024:
025: import org.apache.jetspeed.Jetspeed;
026: import org.apache.jetspeed.container.namespace.JetspeedNamespaceMapper;
027: import org.apache.jetspeed.container.namespace.JetspeedNamespaceMapperFactory;
028: import org.apache.pluto.om.common.ObjectID;
029:
030: /**
031: * @author Scott T Weaver
032: *
033: */
034: public class NamespaceEncodedSession extends HttpSessionWrapper {
035:
036: private JetspeedNamespaceMapper nameSpaceMapper;
037:
038: private ObjectID webAppId;
039:
040: private HashSet mappedNames = new HashSet();
041:
042: /**
043: * @param session
044: */
045: public NamespaceEncodedSession(HttpSession session,
046: ObjectID webAppId) {
047: super (session);
048: this .nameSpaceMapper = ((JetspeedNamespaceMapperFactory) Jetspeed
049: .getComponentManager().getComponent(
050: org.apache.pluto.util.NamespaceMapper.class))
051: .getJetspeedNamespaceMapper();
052: this .webAppId = webAppId;
053: }
054:
055: /**
056: * <p>
057: * setAttribute
058: * </p>
059: *
060: * @see javax.servlet.ServletRequest#setAttribute(java.lang.String,
061: * java.lang.Object)
062: * @param arg0
063: * @param arg1
064: */
065: public void setAttribute(String name, Object value) {
066:
067: if (name == null) {
068: throw new IllegalArgumentException("Attribute name == null");
069: }
070:
071: if (skipEncode(name)) {
072: super .setAttribute(name, value);
073: } else {
074: String encodedKey = nameSpaceMapper.encode(webAppId, name);
075: mappedNames.add(name);
076: super .setAttribute(encodedKey, value);
077: }
078:
079: }
080:
081: /**
082: * @see javax.servlet.http.HttpServletRequest#getAttribute(java.lang.String)
083: */
084: public Object getAttribute(String name) {
085: if (skipEncode(name)) {
086: return super .getAttribute(name);
087: } else {
088: return super .getAttribute(nameSpaceMapper.encode(webAppId,
089: name));
090: }
091: }
092:
093: private boolean skipEncode(String name) {
094: return name.startsWith(nameSpaceMapper.getPrefix())
095: || name.startsWith("javax.portlet")
096: || name.startsWith("javax.servlet")
097: || name.startsWith("org.apache.jetspeed");
098: }
099:
100: /*
101: * (non-Javadoc)
102: *
103: * @see javax.servlet.http.HttpSession#getAttributeNames()
104: */
105: public Enumeration getAttributeNames() {
106: Enumeration names = super .getAttributeNames();
107: while (names.hasMoreElements()) {
108: String name = (String) names.nextElement();
109: if (skipEncode(name)) {
110: mappedNames.add(name);
111: }
112: }
113:
114: return Collections.enumeration(mappedNames);
115: }
116:
117: /*
118: * (non-Javadoc)
119: *
120: * @see javax.servlet.http.HttpSession#removeAttribute(java.lang.String)
121: */
122: public void removeAttribute(String name) {
123: if (skipEncode(name)) {
124: super.removeAttribute(name);
125: } else {
126: mappedNames.add(name);
127: super.removeAttribute(nameSpaceMapper
128: .encode(webAppId, name));
129: }
130: }
131: }
|