View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.shiro.session.mgt;
20  
21  import org.apache.shiro.session.InvalidSessionException;
22  import org.apache.shiro.session.ProxiedSession;
23  import org.apache.shiro.session.Session;
24  
25  
26  /**
27   * Implementation of the {@link Session Session} interface that proxies another <code>Session</code>, but does not
28   * allow any 'write' operations to the underlying session. It allows 'read' operations only.
29   * <p/>
30   * The <code>Session</code> write operations are defined as follows.  A call to any of these methods on this
31   * proxy will immediately result in an {@link InvalidSessionException} being thrown:
32   * <ul>
33   * <li>{@link Session#setTimeout(long) Session.setTimeout(long)}</li>
34   * <li>{@link Session#touch() Session.touch()}</li>
35   * <li>{@link Session#stop() Session.stop()}</li>
36   * <li>{@link Session#setAttribute(Object, Object) Session.setAttribute(key,value)}</li>
37   * <li>{@link Session#removeAttribute(Object) Session.removeAttribute(key)}</li>
38   * </ul>
39   * Any other method invocation not listed above will result in a corresponding call to the underlying <code>Session</code>.
40   *
41   * @since 0.9
42   */
43  public class ImmutableProxiedSession extends ProxiedSession {
44  
45      /**
46       * Constructs a new instance of this class proxying the specified <code>Session</code>.
47       *
48       * @param target the target <code>Session</code> to proxy.
49       */
50      public ImmutableProxiedSession(Session target) {
51          super(target);
52      }
53  
54      /**
55       * Simply throws an <code>InvalidSessionException</code> indicating that this proxy is immutable.  Used
56       * only in the Session's 'write' methods documented in the top class-level JavaDoc.
57       *
58       * @throws InvalidSessionException in all cases - used by the Session 'write' method implementations.
59       */
60      protected void throwImmutableException() throws InvalidSessionException {
61          String msg = "This session is immutable and read-only - it cannot be altered.  This is usually because " +
62                  "the session has been stopped or expired already.";
63          throw new InvalidSessionException(msg);
64      }
65  
66      /**
67       * Immediately {@link #throwImmutableException() throws} an <code>InvalidSessionException</code> in all
68       * cases because this proxy is immutable.
69       */
70      public void setTimeout(long maxIdleTimeInMillis) throws InvalidSessionException {
71          throwImmutableException();
72      }
73  
74      /**
75       * Immediately {@link #throwImmutableException() throws} an <code>InvalidSessionException</code> in all
76       * cases because this proxy is immutable.
77       */
78      public void touch() throws InvalidSessionException {
79          throwImmutableException();
80      }
81  
82      /**
83       * Immediately {@link #throwImmutableException() throws} an <code>InvalidSessionException</code> in all
84       * cases because this proxy is immutable.
85       */
86      public void stop() throws InvalidSessionException {
87          throwImmutableException();
88      }
89  
90      /**
91       * Immediately {@link #throwImmutableException() throws} an <code>InvalidSessionException</code> in all
92       * cases because this proxy is immutable.
93       */
94      public void setAttribute(Object key, Object value) throws InvalidSessionException {
95          throwImmutableException();
96      }
97  
98      /**
99       * 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 }