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 module hunt.shiro.session.mgt.ImmutableProxiedSession;
20 
21 import hunt.shiro.Exceptions;
22 import hunt.shiro.session.ProxiedSession;
23 import hunt.shiro.session.Session;
24 
25 import hunt.Exceptions;
26 
27 /**
28  * Implementation of the {@link Session Session} interface that proxies another <code>Session</code>, but does not
29  * allow any 'write' operations to the underlying session. It allows 'read' operations only.
30  * <p/>
31  * The <code>Session</code> write operations are defined as follows.  A call to any of these methods on this
32  * proxy will immediately result in an {@link InvalidSessionException} being thrown:
33  * <ul>
34  * <li>{@link Session#setTimeout(long) Session.setTimeout(long)}</li>
35  * <li>{@link Session#touch() Session.touch()}</li>
36  * <li>{@link Session#stop() Session.stop()}</li>
37  * <li>{@link Session#setAttribute(Object, Object) Session.setAttribute(key,value)}</li>
38  * <li>{@link Session#removeAttribute(Object) Session.removeAttribute(key)}</li>
39  * </ul>
40  * Any other method invocation not listed above will result in a corresponding call to the underlying <code>Session</code>.
41  *
42  */
43 class ImmutableProxiedSession : 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     this(Session target) {
51         super(target);
52     }
53 
54     /**
55      * Simply<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(){
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     override void setTimeout(long maxIdleTimeInMillis){
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     override void touch(){
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     override void stop(){
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     override void setAttribute(Object key, Object value){
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     override Object removeAttribute(Object key){
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 }