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, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.rest;
20  
21  import java.io.IOException;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.apache.hadoop.conf.Configuration;
26  import org.apache.hadoop.hbase.classification.InterfaceAudience;
27  import org.apache.hadoop.hbase.client.Admin;
28  import org.apache.hadoop.hbase.client.Table;
29  import org.apache.hadoop.hbase.filter.ParseFilter;
30  import org.apache.hadoop.hbase.security.UserProvider;
31  import org.apache.hadoop.hbase.util.ConnectionCache;
32  import org.apache.hadoop.security.UserGroupInformation;
33  import org.apache.hadoop.security.authorize.ProxyUsers;
34  
35  /**
36   * Singleton class encapsulating global REST servlet state and functions.
37   */
38  @InterfaceAudience.Private
39  public class RESTServlet implements Constants {
40    private static final Log LOG = LogFactory.getLog(RESTServlet.class);
41    private static RESTServlet INSTANCE;
42    private final Configuration conf;
43    private final MetricsREST metrics = new MetricsREST();
44    private final ConnectionCache connectionCache;
45    private final UserGroupInformation realUser;
46  
47    static final String CLEANUP_INTERVAL = "hbase.rest.connection.cleanup-interval";
48    static final String MAX_IDLETIME = "hbase.rest.connection.max-idletime";
49    static final String HBASE_REST_SUPPORT_PROXYUSER = "hbase.rest.support.proxyuser";
50  
51    UserGroupInformation getRealUser() {
52      return realUser;
53    }
54  
55    /**
56     * @return the RESTServlet singleton instance
57     */
58    public synchronized static RESTServlet getInstance() {
59      assert(INSTANCE != null);
60      return INSTANCE;
61    }
62  
63    /**
64     * @param conf Existing configuration to use in rest servlet
65     * @param userProvider the login user provider
66     * @return the RESTServlet singleton instance
67     * @throws IOException
68     */
69    public synchronized static RESTServlet getInstance(Configuration conf,
70        UserProvider userProvider) throws IOException {
71      if (INSTANCE == null) {
72        INSTANCE = new RESTServlet(conf, userProvider);
73      }
74      return INSTANCE;
75    }
76  
77    public synchronized static void stop() {
78      if (INSTANCE != null) {
79        INSTANCE.shutdown();
80        INSTANCE = null;
81      }
82    }
83  
84    /**
85     * Constructor with existing configuration
86     * @param conf existing configuration
87     * @param userProvider the login user provider
88     * @throws IOException
89     */
90    RESTServlet(final Configuration conf,
91        final UserProvider userProvider) throws IOException {
92      this.realUser = userProvider.getCurrent().getUGI();
93      this.conf = conf;
94      registerCustomFilter(conf);
95  
96      int cleanInterval = conf.getInt(CLEANUP_INTERVAL, 10 * 1000);
97      int maxIdleTime = conf.getInt(MAX_IDLETIME, 10 * 60 * 1000);
98      connectionCache = new ConnectionCache(
99        conf, userProvider, cleanInterval, maxIdleTime);
100     if (supportsProxyuser()) {
101       ProxyUsers.refreshSuperUserGroupsConfiguration(conf);
102     }
103   }
104 
105   Admin getAdmin() throws IOException {
106     return connectionCache.getAdmin();
107   }
108 
109   /**
110    * Caller closes the table afterwards.
111    */
112   Table getTable(String tableName) throws IOException {
113     return connectionCache.getTable(tableName);
114   }
115 
116   Configuration getConfiguration() {
117     return conf;
118   }
119 
120   MetricsREST getMetrics() {
121     return metrics;
122   }
123 
124   /**
125    * Helper method to determine if server should
126    * only respond to GET HTTP method requests.
127    * @return boolean for server read-only state
128    */
129   boolean isReadOnly() {
130     return getConfiguration().getBoolean("hbase.rest.readonly", false);
131   }
132 
133   void setEffectiveUser(String effectiveUser) {
134     connectionCache.setEffectiveUser(effectiveUser);
135   }
136 
137   /**
138    * Shutdown any services that need to stop
139    */
140   void shutdown() {
141     if (connectionCache != null) connectionCache.shutdown();
142   }
143 
144   boolean supportsProxyuser() {
145     return conf.getBoolean(HBASE_REST_SUPPORT_PROXYUSER, false);
146   }
147 
148   private void registerCustomFilter(Configuration conf) {
149     String[] filterList = conf.getStrings(Constants.CUSTOM_FILTERS);
150     if (filterList != null) {
151       for (String filterClass : filterList) {
152         String[] filterPart = filterClass.split(":");
153         if (filterPart.length != 2) {
154           LOG.warn(
155             "Invalid filter specification " + filterClass + " - skipping");
156         } else {
157           ParseFilter.registerFilter(filterPart[0], filterPart[1]);
158         }
159       }
160     }
161   }
162 }