    1   /*
    2    * Copyright 2002-2004 the original author or authors.
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *      http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16
    17  package org.huihoo.jfox.soaf.util.filter;
    18
    19  import java.util.Enumeration;
    20  import java.util.Hashtable;
    21
    22  import javax.servlet.ServletContext;
    23  import javax.servlet.http.HttpSession;
    24  import javax.servlet.http.HttpSessionContext;
    25
    26  /***
    27   * Mock implementation of the HttpSession interface.
    28   *
    29   * <p>
    30   * Used for testing the web framework; also useful for testing application
    31   * controllers.
    32   *
    33   * @author Rod Johnson
    34   * @author Juergen Hoeller
    35   * @since 1.0.2
    36   */
    37  public class MockHttpSession implements HttpSession {
    38
    39  	public static final String SESSION_COOKIE_NAME = "JSESSION";
    40
    41  	private static int nextId = 1;
    42
    43  	private final String id = Integer.toString(nextId++);
    44
    45  	private final long creationTime = System.currentTimeMillis();
    46
    47  	private int maxInactiveInterval;
    48
    49  	private long lastAccessedTime = System.currentTimeMillis();
    50
    51  	private final ServletContext servletContext;
    52
    53  	private final Hashtable attributes = new Hashtable();
    54
    55  	private boolean invalid = false;
    56
    57  	private boolean isNew = true;
    58
    59  	/***
    60  	 * Create a new MockHttpSession.
    61  	 *
    62  	 * @param servletContext
    63  	 *            the ServletContext that the session runs in
    64  	 */
    65  	public MockHttpSession(ServletContext servletContext) {
    66  		this.servletContext = servletContext;
    67  	}
    68
    69  	/***
    70  	 * Create a new MockHttpSession with a MockServletContext.
    71  	 *
    72  	 * @see MockServletContext
    73  	 */
    74  	public MockHttpSession() {
    75  		this(new MockServletContext());
    76  	}
    77
    78  	public long getCreationTime() {
    79  		return creationTime;
    80  	}
    81
    82  	public String getId() {
    83  		return id;
    84  	}
    85
    86  	public void access() {
    87  		this.lastAccessedTime = System.currentTimeMillis();
    88  		this.isNew = false;
    89  	}
    90
    91  	public long getLastAccessedTime() {
    92  		return lastAccessedTime;
    93  	}
    94
    95  	public ServletContext getServletContext() {
    96  		return servletContext;
    97  	}
    98
    99  	public void setMaxInactiveInterval(int interval) {
    100 		maxInactiveInterval = interval;
    101 	}
    102
    103 	public int getMaxInactiveInterval() {
    104 		return maxInactiveInterval;
    105 	}
    106
    107 	/***
    108 	 * @deprecated
    109 	 */
    110 	public HttpSessionContext getSessionContext() {
    111 		throw new UnsupportedOperationException("getSessionContext");
    112 	}
    113
    114 	public Object getAttribute(String name) {
    115 		return this.attributes.get(name);
    116 	}
    117
    118 	public Object getValue(String name) {
    119 		return getAttribute(name);
    120 	}
    121
    122 	public Enumeration getAttributeNames() {
    123 		return this.attributes.keys();
    124 	}
    125
    126 	public String[] getValueNames() {
    127 		return (String[]) this.attributes.keySet().toArray(
    128 				new String[this.attributes.size()]);
    129 	}
    130
    131 	public void setAttribute(String name, Object value) {
    132 		if (value != null) {
    133 			this.attributes.put(name, value);
    134 		} else {
    135 			this.attributes.remove(name);
    136 		}
    137 	}
    138
    139 	public void putValue(String name, Object value) {
    140 		setAttribute(name, value);
    141 	}
    142
    143 	public void removeAttribute(String name) {
    144 		this.attributes.remove(name);
    145 	}
    146
    147 	public void removeValue(String name) {
    148 		removeAttribute(name);
    149 	}
    150
    151 	public void invalidate() {
    152 		this.invalid = true;
    153 		this.attributes.clear();
    154 	}
    155
    156 	public boolean isInvalid() {
    157 		return invalid;
    158 	}
    159
    160 	public void setNew(boolean value) {
    161 		isNew = value;
    162 	}
    163
    164 	public boolean isNew() {
    165 		return isNew;
    166 	}
    167
    168 }