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  package org.apache.hadoop.hbase.http;
19  
20  import org.apache.hadoop.hbase.classification.InterfaceAudience;
21  import org.apache.hadoop.hbase.classification.InterfaceStability;
22  import org.apache.hadoop.conf.Configuration;
23  
24  /**
25   * Statics to get access to Http related configuration.
26   */
27  @InterfaceAudience.Private
28  @InterfaceStability.Unstable
29  public class HttpConfig {
30    private Policy policy;
31    public enum Policy {
32      HTTP_ONLY,
33      HTTPS_ONLY,
34      HTTP_AND_HTTPS;
35  
36      public Policy fromString(String value) {
37        if (HTTPS_ONLY.name().equalsIgnoreCase(value)) {
38          return HTTPS_ONLY;
39        } else if (HTTP_AND_HTTPS.name().equalsIgnoreCase(value)) {
40          return HTTP_AND_HTTPS;
41        }
42        return HTTP_ONLY;
43      }
44  
45      public boolean isHttpEnabled() {
46        return this == HTTP_ONLY || this == HTTP_AND_HTTPS;
47      }
48  
49      public boolean isHttpsEnabled() {
50        return this == HTTPS_ONLY || this == HTTP_AND_HTTPS;
51      }
52    }
53  
54     public HttpConfig(final Configuration conf) {
55      boolean sslEnabled = conf.getBoolean(
56        ServerConfigurationKeys.HBASE_SSL_ENABLED_KEY,
57        ServerConfigurationKeys.HBASE_SSL_ENABLED_DEFAULT);
58      policy = sslEnabled ? Policy.HTTPS_ONLY : Policy.HTTP_ONLY;
59      if (sslEnabled) {
60        conf.addResource("ssl-server.xml");
61        conf.addResource("ssl-client.xml");
62      }
63    }
64  
65    public void setPolicy(Policy policy) {
66      this.policy = policy;
67    }
68  
69    public boolean isSecure() {
70      return policy == Policy.HTTPS_ONLY;
71    }
72  
73    public String getSchemePrefix() {
74      return (isSecure()) ? "https://" : "http://";
75    }
76  
77    public String getScheme(Policy policy) {
78      return policy == Policy.HTTPS_ONLY ? "https://" : "http://";
79    }
80  }