001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019 package org.apache.shiro.session.mgt;
020
021 import org.apache.shiro.session.InvalidSessionException;
022 import org.apache.shiro.session.ProxiedSession;
023 import org.apache.shiro.session.Session;
024
025
026 /**
027 * Implementation of the {@link Session Session} interface that proxies another <code>Session</code>, but does not
028 * allow any 'write' operations to the underlying session. It allows 'read' operations only.
029 * <p/>
030 * The <code>Session</code> write operations are defined as follows. A call to any of these methods on this
031 * proxy will immediately result in an {@link InvalidSessionException} being thrown:
032 * <ul>
033 * <li>{@link Session#setTimeout(long) Session.setTimeout(long)}</li>
034 * <li>{@link Session#touch() Session.touch()}</li>
035 * <li>{@link Session#stop() Session.stop()}</li>
036 * <li>{@link Session#setAttribute(Object, Object) Session.setAttribute(key,value)}</li>
037 * <li>{@link Session#removeAttribute(Object) Session.removeAttribute(key)}</li>
038 * </ul>
039 * Any other method invocation not listed above will result in a corresponding call to the underlying <code>Session</code>.
040 *
041 * @author Les Hazlewood
042 * @since 0.9
043 */
044 public class ImmutableProxiedSession extends ProxiedSession {
045
046 /**
047 * Constructs a new instance of this class proxying the specified <code>Session</code>.
048 *
049 * @param target the target <code>Session</code> to proxy.
050 */
051 public ImmutableProxiedSession(Session target) {
052 super(target);
053 }
054
055 /**
056 * Simply throws an <code>InvalidSessionException</code> indicating that this proxy is immutable. Used
057 * only in the Session's 'write' methods documented in the top class-level JavaDoc.
058 *
059 * @throws InvalidSessionException in all cases - used by the Session 'write' method implementations.
060 */
061 protected void throwImmutableException() throws InvalidSessionException {
062 String msg = "This session is immutable and read-only - it cannot be altered. This is usually because " +
063 "the session has been stopped or expired already.";
064 throw new InvalidSessionException(msg);
065 }
066
067 /**
068 * Immediately {@link #throwImmutableException() throws} an <code>InvalidSessionException</code> in all
069 * cases because this proxy is immutable.
070 */
071 public void setTimeout(long maxIdleTimeInMillis) throws InvalidSessionException {
072 throwImmutableException();
073 }
074
075 /**
076 * Immediately {@link #throwImmutableException() throws} an <code>InvalidSessionException</code> in all
077 * cases because this proxy is immutable.
078 */
079 public void touch() throws InvalidSessionException {
080 throwImmutableException();
081 }
082
083 /**
084 * Immediately {@link #throwImmutableException() throws} an <code>InvalidSessionException</code> in all
085 * cases because this proxy is immutable.
086 */
087 public void stop() throws InvalidSessionException {
088 throwImmutableException();
089 }
090
091 /**
092 * Immediately {@link #throwImmutableException() throws} an <code>InvalidSessionException</code> in all
093 * cases because this proxy is immutable.
094 */
095 public void setAttribute(Object key, Object value) throws InvalidSessionException {
096 throwImmutableException();
097 }
098
099 /**
100 * Immediately {@link #throwImmutableException() throws} an <code>InvalidSessionException</code> in all
101 * cases because this proxy is immutable.
102 */
103 public Object removeAttribute(Object key) throws InvalidSessionException {
104 throwImmutableException();
105 //we should never ever reach this point due to the exception being thrown.
106 throw new InternalError("This code should never execute - please report this as a bug!");
107 }
108 }