View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.engine.servlet;
18  
19  import java.util.Collections;
20  import java.util.Enumeration;
21  import java.util.HashSet;
22  
23  import javax.servlet.http.HttpSession;
24  
25  import org.apache.jetspeed.Jetspeed;
26  import org.apache.jetspeed.container.namespace.JetspeedNamespaceMapper;
27  import org.apache.jetspeed.container.namespace.JetspeedNamespaceMapperFactory;
28  import org.apache.pluto.om.common.ObjectID;
29  
30  /***
31   * @author Scott T Weaver
32   *  
33   */
34  public class NamespaceEncodedSession extends HttpSessionWrapper
35  {
36  
37      private JetspeedNamespaceMapper nameSpaceMapper;
38  
39      private ObjectID webAppId;
40  
41      private HashSet mappedNames = new HashSet();
42  
43      /***
44       * @param session
45       */
46      public NamespaceEncodedSession(HttpSession session, ObjectID webAppId)
47      {
48          super(session);
49          this.nameSpaceMapper = ((JetspeedNamespaceMapperFactory) Jetspeed.getComponentManager().getComponent(
50                  org.apache.pluto.util.NamespaceMapper.class)).getJetspeedNamespaceMapper();
51          this.webAppId = webAppId;
52      }
53  
54      /***
55       * <p>
56       * setAttribute
57       * </p>
58       * 
59       * @see javax.servlet.ServletRequest#setAttribute(java.lang.String,
60       *      java.lang.Object)
61       * @param arg0
62       * @param arg1
63       */
64      public void setAttribute(String name, Object value)
65      {
66  
67          if (name == null)
68          {
69              throw new IllegalArgumentException("Attribute name == null");
70          }
71  
72          if (skipEncode(name))
73          {
74              super.setAttribute(name, value);
75          }
76          else
77          {
78              String encodedKey = nameSpaceMapper.encode(webAppId, name);
79              mappedNames.add(name);
80              super.setAttribute(encodedKey, value);
81          }
82  
83      }
84  
85      /***
86       * @see javax.servlet.http.HttpServletRequest#getAttribute(java.lang.String)
87       */
88      public Object getAttribute(String name)
89      {
90          if (skipEncode(name))
91          {
92              return super.getAttribute(name);
93          }
94          else
95          {
96              return super.getAttribute(nameSpaceMapper.encode(webAppId, name));
97          }
98      }
99  
100     private boolean skipEncode(String name)
101     {
102         return name.startsWith(nameSpaceMapper.getPrefix()) || name.startsWith("javax.portlet") || name.startsWith("javax.servlet") || name.startsWith("org.apache.jetspeed");
103     }
104 
105     /*
106      * (non-Javadoc)
107      * 
108      * @see javax.servlet.http.HttpSession#getAttributeNames()
109      */
110     public Enumeration getAttributeNames()
111     {
112         Enumeration names = super.getAttributeNames();
113         while (names.hasMoreElements())
114         {
115             String name = (String) names.nextElement();
116             if (skipEncode(name))
117             {
118                 mappedNames.add(name);
119             }
120         }
121 
122         return Collections.enumeration(mappedNames);
123     }
124 
125     /*
126      * (non-Javadoc)
127      * 
128      * @see javax.servlet.http.HttpSession#removeAttribute(java.lang.String)
129      */
130     public void removeAttribute(String name)
131     {
132         if (skipEncode(name))
133         {
134             super.removeAttribute(name);
135         }
136         else
137         {
138             mappedNames.add(name);
139             super.removeAttribute(nameSpaceMapper.encode(webAppId, name));
140         }
141     }
142 }