View Javadoc

1   /*
2    *  jDTAUS Core RI Servlet Container
3    *  Copyright (C) 2005 Christian Schulte
4    *  <cs@schulte.it>
5    *
6    *  This library is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public
8    *  License as published by the Free Software Foundation; either
9    *  version 2.1 of the License, or any later version.
10   *
11   *  This library is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this library; if not, write to the Free Software
18   *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19   *
20   */
21  package org.jdtaus.core.container.ri.servlet;
22  
23  import java.io.Serializable;
24  import java.util.Collection;
25  import java.util.Collections;
26  import javax.servlet.http.HttpSession;
27  import org.jdtaus.core.container.Context;
28  
29  /**
30   * HTTP session {@code Context} implementation.
31   * <p>This implementation needs to be configured manually for use by
32   * {@link org.jdtaus.core.container.ContextFactory#newContext()}. See the
33   * factory for how to configure this class to be used.</p>
34   *
35   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
36   * @version $JDTAUS: HttpSessionContext.java 8743 2012-10-07 03:06:20Z schulte $
37   *
38   * @see ServletFilter
39   */
40  public class HttpSessionContext implements Context
41  {
42      //--Context-----------------------------------------------------------------
43  
44      /** Creates a new {@code HttpSessionContext} instance. */
45      public HttpSessionContext()
46      {
47          super();
48      }
49  
50      public final Collection getObjectKeys()
51      {
52          return Collections.list( this.getSession().getAttributeNames() );
53      }
54  
55      public final Object getObject( final String key )
56      {
57          if ( key == null )
58          {
59              throw new NullPointerException( "key" );
60          }
61  
62          return this.getSession().getAttribute( key );
63      }
64  
65      public final Object setObject( final String key, final Object o )
66      {
67          if ( key == null )
68          {
69              throw new NullPointerException( "key" );
70          }
71  
72          final Object old = this.getSession().getAttribute( key );
73          this.getSession().setAttribute( key, o );
74  
75          return old;
76      }
77  
78      public final Object removeObject( final String key )
79      {
80          if ( key == null )
81          {
82              throw new NullPointerException( "key" );
83          }
84  
85          final Object old = this.getSession().getAttribute( key );
86          this.getSession().removeAttribute( key );
87  
88          return old;
89      }
90  
91      public final Object getAttribute( final String key )
92      {
93          return this.getObject( key );
94      }
95  
96      public final Object setAttribute( final String key, final Serializable o )
97      {
98          return this.setObject( key, o );
99      }
100 
101     public final Object removeAttribute( final String key )
102     {
103         return this.removeObject( key );
104     }
105 
106     //-----------------------------------------------------------------Context--
107     //--HttpSessionContext------------------------------------------------------
108 
109     /** @see ServletFilter#getHttpSession() */
110     public HttpSession getSession()
111     {
112         return ServletFilter.getHttpSession();
113     }
114 
115     //------------------------------------------------------HttpSessionContext--
116 }