1   /*
2    * Copyright 2018 LINE Corporation
3    *
4    * LINE Corporation licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://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, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package com.linecorp.centraldogma.server.internal.admin.auth;
17  
18  import static java.util.Objects.requireNonNull;
19  
20  import java.util.concurrent.CompletableFuture;
21  
22  import com.linecorp.centraldogma.internal.Util;
23  import com.linecorp.centraldogma.server.auth.Session;
24  import com.linecorp.centraldogma.server.auth.SessionManager;
25  
26  /**
27   * A {@link SessionManager} which forwards all its method calls to another {@link SessionManager}.
28   */
29  public class ForwardingSessionManager implements SessionManager {
30  
31      private final SessionManager delegate;
32  
33      /**
34       * Creates a new {@link SessionManager} instance which forwards all its method calls to the specified
35       * {@code delegate}.
36       */
37      protected ForwardingSessionManager(SessionManager delegate) {
38          this.delegate = requireNonNull(delegate, "delegate");
39      }
40  
41      protected final <T extends SessionManager> T delegate() {
42          return Util.unsafeCast(delegate);
43      }
44  
45      @Override
46      public String generateSessionId() {
47          return delegate().generateSessionId();
48      }
49  
50      @Override
51      public CompletableFuture<Boolean> exists(String sessionId) {
52          return delegate().exists(sessionId);
53      }
54  
55      @Override
56      public CompletableFuture<Session> get(String sessionId) {
57          return delegate().get(sessionId);
58      }
59  
60      @Override
61      public CompletableFuture<Void> create(Session session) {
62          return delegate().create(session);
63      }
64  
65      @Override
66      public CompletableFuture<Void> update(Session session) {
67          return delegate().update(session);
68      }
69  
70      @Override
71      public CompletableFuture<Void> delete(String sessionId) {
72          return delegate().delete(sessionId);
73      }
74  
75      @Override
76      public void close() throws Exception {
77          delegate().close();
78      }
79  
80      @Override
81      public String toString() {
82          return getClass().getSimpleName() + '(' + delegate() + ')';
83      }
84  }