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 */
019package org.apache.shiro.session.mgt;
020
021import org.apache.shiro.session.InvalidSessionException;
022import org.apache.shiro.session.ProxiedSession;
023import 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 * @since 0.9
042 */
043public class ImmutableProxiedSession extends ProxiedSession {
044
045    /**
046     * Constructs a new instance of this class proxying the specified <code>Session</code>.
047     *
048     * @param target the target <code>Session</code> to proxy.
049     */
050    public ImmutableProxiedSession(Session target) {
051        super(target);
052    }
053
054    /**
055     * Simply throws an <code>InvalidSessionException</code> indicating that this proxy is immutable.  Used
056     * only in the Session's 'write' methods documented in the top class-level JavaDoc.
057     *
058     * @throws InvalidSessionException in all cases - used by the Session 'write' method implementations.
059     */
060    protected void throwImmutableException() throws InvalidSessionException {
061        String msg = "This session is immutable and read-only - it cannot be altered.  This is usually because " +
062                "the session has been stopped or expired already.";
063        throw new InvalidSessionException(msg);
064    }
065
066    /**
067     * Immediately {@link #throwImmutableException() throws} an <code>InvalidSessionException</code> in all
068     * cases because this proxy is immutable.
069     */
070    public void setTimeout(long maxIdleTimeInMillis) throws InvalidSessionException {
071        throwImmutableException();
072    }
073
074    /**
075     * Immediately {@link #throwImmutableException() throws} an <code>InvalidSessionException</code> in all
076     * cases because this proxy is immutable.
077     */
078    public void touch() throws InvalidSessionException {
079        throwImmutableException();
080    }
081
082    /**
083     * Immediately {@link #throwImmutableException() throws} an <code>InvalidSessionException</code> in all
084     * cases because this proxy is immutable.
085     */
086    public void stop() throws InvalidSessionException {
087        throwImmutableException();
088    }
089
090    /**
091     * Immediately {@link #throwImmutableException() throws} an <code>InvalidSessionException</code> in all
092     * cases because this proxy is immutable.
093     */
094    public void setAttribute(Object key, Object value) throws InvalidSessionException {
095        throwImmutableException();
096    }
097
098    /**
099     * Immediately {@link #throwImmutableException() throws} an <code>InvalidSessionException</code> in all
100     * cases because this proxy is immutable.
101     */
102    public Object removeAttribute(Object key) throws InvalidSessionException {
103        throwImmutableException();
104        //we should never ever reach this point due to the exception being thrown.
105        throw new InternalError("This code should never execute - please report this as a bug!");
106    }
107}