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 8641 2012-09-27 06:45:17Z schulte $
37   *
38   * @see ServletFilter
39   */
40  public class HttpSessionContext implements Context
41  {
42      //--Context-----------------------------------------------------------------
43  
44      public final Collection getObjectKeys()
45      {
46          return Collections.list( this.getSession().getAttributeNames() );
47      }
48  
49      public final Object getObject( final String key )
50      {
51          if ( key == null )
52          {
53              throw new NullPointerException( "key" );
54          }
55  
56          return this.getSession().getAttribute( key );
57      }
58  
59      public final Object setObject( final String key, final Object o )
60      {
61          if ( key == null )
62          {
63              throw new NullPointerException( "key" );
64          }
65  
66          final Object old = this.getSession().getAttribute( key );
67          this.getSession().setAttribute( key, o );
68  
69          return old;
70      }
71  
72      public final Object removeObject( final String key )
73      {
74          if ( key == null )
75          {
76              throw new NullPointerException( "key" );
77          }
78  
79          final Object old = this.getSession().getAttribute( key );
80          this.getSession().removeAttribute( key );
81  
82          return old;
83      }
84  
85      public final Object getAttribute( final String key )
86      {
87          return this.getObject( key );
88      }
89  
90      public final Object setAttribute( final String key, final Serializable o )
91      {
92          return this.setObject( key, o );
93      }
94  
95      public final Object removeAttribute( String key )
96      {
97          return this.removeObject( key );
98      }
99  
100     //-----------------------------------------------------------------Context--
101     //--HttpSessionContext------------------------------------------------------
102 
103     /** @see ServletFilter#getHttpSession() */
104     public HttpSession getSession()
105     {
106         return ServletFilter.getHttpSession();
107     }
108 
109     //------------------------------------------------------HttpSessionContext--
110 }