001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.http;
019
020import org.apache.hadoop.conf.Configuration;
021
022import org.apache.yetus.audience.InterfaceAudience;
023import org.apache.yetus.audience.InterfaceStability;
024
025/**
026 * Statics to get access to Http related configuration.
027 */
028@InterfaceAudience.Private
029@InterfaceStability.Unstable
030public class HttpConfig {
031  private Policy policy;
032  public enum Policy {
033    HTTP_ONLY,
034    HTTPS_ONLY,
035    HTTP_AND_HTTPS;
036
037    public Policy fromString(String value) {
038      if (HTTPS_ONLY.name().equalsIgnoreCase(value)) {
039        return HTTPS_ONLY;
040      } else if (HTTP_AND_HTTPS.name().equalsIgnoreCase(value)) {
041        return HTTP_AND_HTTPS;
042      }
043      return HTTP_ONLY;
044    }
045
046    public boolean isHttpEnabled() {
047      return this == HTTP_ONLY || this == HTTP_AND_HTTPS;
048    }
049
050    public boolean isHttpsEnabled() {
051      return this == HTTPS_ONLY || this == HTTP_AND_HTTPS;
052    }
053  }
054
055  public HttpConfig(final Configuration conf) {
056    boolean sslEnabled = conf.getBoolean(
057      ServerConfigurationKeys.HBASE_SSL_ENABLED_KEY,
058      ServerConfigurationKeys.HBASE_SSL_ENABLED_DEFAULT);
059    policy = sslEnabled ? Policy.HTTPS_ONLY : Policy.HTTP_ONLY;
060    if (sslEnabled) {
061      conf.addResource("ssl-server.xml");
062      conf.addResource("ssl-client.xml");
063    }
064  }
065
066  public void setPolicy(Policy policy) {
067    this.policy = policy;
068  }
069
070  public boolean isSecure() {
071    return policy == Policy.HTTPS_ONLY;
072  }
073
074  public String getSchemePrefix() {
075    return (isSecure()) ? "https://" : "http://";
076  }
077
078  public String getScheme(Policy policy) {
079    return policy == Policy.HTTPS_ONLY ? "https://" : "http://";
080  }
081}