View Javadoc

1   /*
2    * Copyright The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.ipc;
22  
23  import org.apache.hadoop.classification.InterfaceAudience;
24  import org.apache.hadoop.hbase.IpcProtocol;
25  import org.apache.hadoop.hbase.security.User;
26  
27  import java.net.InetAddress;
28  
29  /**
30   * Represents client information (authenticated username, remote address, protocol)
31   * for the currently executing request within a RPC server handler thread.  If
32   * called outside the context of a RPC request, all values will be
33   * <code>null</code>.
34   */
35  @InterfaceAudience.Private
36  public class RequestContext {
37    private static ThreadLocal<RequestContext> instance =
38        new ThreadLocal<RequestContext>() {
39          protected RequestContext initialValue() {
40            return new RequestContext(null, null, null);
41          }
42        };
43  
44    public static RequestContext get() {
45      return instance.get();
46    }
47  
48  
49    /**
50     * Returns the user credentials associated with the current RPC request or
51     * <code>null</code> if no credentials were provided.
52     * @return A User
53     */
54    public static User getRequestUser() {
55      RequestContext ctx = instance.get();
56      if (ctx != null) {
57        return ctx.getUser();
58      }
59      return null;
60    }
61  
62    /**
63     * Returns the username for any user associated with the current RPC
64     * request or <code>null</code> if no user is set.
65     */
66    public static String getRequestUserName() {
67      User user = getRequestUser();
68      if (user != null) {
69        return user.getShortName();
70      }
71      return null;
72    }
73  
74    /**
75     * Indicates whether or not the current thread is within scope of executing
76     * an RPC request.
77     */
78    public static boolean isInRequestContext() {
79      RequestContext ctx = instance.get();
80      if (ctx != null) {
81        return ctx.isInRequest();
82      }
83      return false;
84    }
85  
86    /**
87     * Initializes the client credentials for the current request.
88     * @param user
89     * @param remoteAddress
90     * @param protocol
91     */
92    public static void set(User user,
93        InetAddress remoteAddress,
94        Class<? extends IpcProtocol> protocol) {
95      RequestContext ctx = instance.get();
96      ctx.user = user;
97      ctx.remoteAddress = remoteAddress;
98      ctx.protocol = protocol;
99      ctx.inRequest = true;
100   }
101 
102   /**
103    * Clears out the client credentials for a given request.
104    */
105   public static void clear() {
106     RequestContext ctx = instance.get();
107     ctx.user = null;
108     ctx.remoteAddress = null;
109     ctx.protocol = null;
110     ctx.inRequest = false;
111   }
112 
113   private User user;
114   private InetAddress remoteAddress;
115   private Class<? extends IpcProtocol> protocol;
116   // indicates we're within a RPC request invocation
117   private boolean inRequest;
118 
119   private RequestContext(User user, InetAddress remoteAddr,
120       Class<? extends IpcProtocol> protocol) {
121     this.user = user;
122     this.remoteAddress = remoteAddr;
123     this.protocol = protocol;
124   }
125 
126   public User getUser() {
127     return user;
128   }
129 
130   public InetAddress getRemoteAddress() {
131     return remoteAddress;
132   }
133 
134   public Class<? extends IpcProtocol> getProtocol() {
135     return protocol;
136   }
137 
138   public boolean isInRequest() {
139     return inRequest;
140   }
141 }