001/* 002 * jDTAUS Core RI Servlet Container 003 * Copyright (C) 2005 Christian Schulte 004 * <cs@schulte.it> 005 * 006 * This library is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 2.1 of the License, or any later version. 010 * 011 * This library is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public 017 * License along with this library; if not, write to the Free Software 018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 019 * 020 */ 021package org.jdtaus.core.container.ri.servlet; 022 023import java.io.Serializable; 024import java.util.Collection; 025import java.util.Collections; 026import javax.servlet.http.HttpSession; 027import org.jdtaus.core.container.Context; 028 029/** 030 * HTTP session {@code Context} implementation. 031 * <p>This implementation needs to be configured manually for use by 032 * {@link org.jdtaus.core.container.ContextFactory#newContext()}. See the 033 * factory for how to configure this class to be used.</p> 034 * 035 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> 036 * @version $JDTAUS: HttpSessionContext.java 8743 2012-10-07 03:06:20Z schulte $ 037 * 038 * @see ServletFilter 039 */ 040public class HttpSessionContext implements Context 041{ 042 //--Context----------------------------------------------------------------- 043 044 /** Creates a new {@code HttpSessionContext} instance. */ 045 public HttpSessionContext() 046 { 047 super(); 048 } 049 050 public final Collection getObjectKeys() 051 { 052 return Collections.list( this.getSession().getAttributeNames() ); 053 } 054 055 public final Object getObject( final String key ) 056 { 057 if ( key == null ) 058 { 059 throw new NullPointerException( "key" ); 060 } 061 062 return this.getSession().getAttribute( key ); 063 } 064 065 public final Object setObject( final String key, final Object o ) 066 { 067 if ( key == null ) 068 { 069 throw new NullPointerException( "key" ); 070 } 071 072 final Object old = this.getSession().getAttribute( key ); 073 this.getSession().setAttribute( key, o ); 074 075 return old; 076 } 077 078 public final Object removeObject( final String key ) 079 { 080 if ( key == null ) 081 { 082 throw new NullPointerException( "key" ); 083 } 084 085 final Object old = this.getSession().getAttribute( key ); 086 this.getSession().removeAttribute( key ); 087 088 return old; 089 } 090 091 public final Object getAttribute( final String key ) 092 { 093 return this.getObject( key ); 094 } 095 096 public final Object setAttribute( final String key, final Serializable o ) 097 { 098 return this.setObject( key, o ); 099 } 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}