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.hbase.security.User;
24  
25  import java.net.InetAddress;
26  
27  /**
28   * Represents client information (authenticated username, remote address, protocol)
29   * for the currently executing request within a RPC server handler thread.  If
30   * called outside the context of a RPC request, all values will be
31   * <code>null</code>.
32   */
33  public class RequestContext {
34    private static ThreadLocal<RequestContext> instance =
35        new ThreadLocal<RequestContext>() {
36          protected RequestContext initialValue() {
37            return new RequestContext(null, null, null);
38          }
39        };
40  
41    public static RequestContext get() {
42      return instance.get();
43    }
44  
45  
46    /**
47     * Returns the user credentials associated with the current RPC request or
48     * <code>null</code> if no credentials were provided.
49     * @return A User
50     */
51    public static User getRequestUser() {
52      RequestContext ctx = instance.get();
53      if (ctx != null) {
54        return ctx.getUser();
55      }
56      return null;
57    }
58  
59    /**
60     * Returns the username for any user associated with the current RPC
61     * request or <code>null</code> if no user is set.
62     */
63    public static String getRequestUserName() {
64      User user = getRequestUser();
65      if (user != null) {
66        return user.getShortName();
67      }
68      return null;
69    }
70  
71    /**
72     * Indicates whether or not the current thread is within scope of executing
73     * an RPC request.
74     */
75    public static boolean isInRequestContext() {
76      RequestContext ctx = instance.get();
77      if (ctx != null) {
78        return ctx.isInRequest();
79      }
80      return false;
81    }
82  
83    /**
84     * Initializes the client credentials for the current request.
85     * @param user
86     * @param remoteAddress
87     * @param protocol
88     */
89    public static void set(User user,
90        InetAddress remoteAddress,
91        Class<? extends VersionedProtocol> protocol) {
92      RequestContext ctx = instance.get();
93      ctx.user = user;
94      ctx.remoteAddress = remoteAddress;
95      ctx.protocol = protocol;
96      ctx.inRequest = true;
97    }
98  
99    /**
100    * Clears out the client credentials for a given request.
101    */
102   public static void clear() {
103     RequestContext ctx = instance.get();
104     ctx.user = null;
105     ctx.remoteAddress = null;
106     ctx.protocol = null;
107     ctx.inRequest = false;
108   }
109 
110   private User user;
111   private InetAddress remoteAddress;
112   private Class<? extends VersionedProtocol> protocol;
113   // indicates we're within a RPC request invocation
114   private boolean inRequest;
115 
116   private RequestContext(User user, InetAddress remoteAddr,
117       Class<? extends VersionedProtocol> protocol) {
118     this.user = user;
119     this.remoteAddress = remoteAddr;
120     this.protocol = protocol;
121   }
122 
123   public User getUser() {
124     return user;
125   }
126 
127   public InetAddress getRemoteAddress() {
128     return remoteAddress;
129   }
130 
131   public Class<? extends VersionedProtocol> getProtocol() {
132     return protocol;
133   }
134 
135   public boolean isInRequest() {
136     return inRequest;
137   }
138 }