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;
021import org.apache.yetus.audience.InterfaceAudience;
022import org.apache.yetus.audience.InterfaceStability;
023
024/**
025 * Statics to get access to Http related configuration.
026 */
027@InterfaceAudience.Private
028@InterfaceStability.Unstable
029public class HttpConfig {
030  private Policy policy;
031
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(ServerConfigurationKeys.HBASE_SSL_ENABLED_KEY,
057      ServerConfigurationKeys.HBASE_SSL_ENABLED_DEFAULT);
058    policy = sslEnabled ? Policy.HTTPS_ONLY : Policy.HTTP_ONLY;
059    if (sslEnabled) {
060      conf.addResource("ssl-server.xml");
061      conf.addResource("ssl-client.xml");
062    }
063  }
064
065  public void setPolicy(Policy policy) {
066    this.policy = policy;
067  }
068
069  public boolean isSecure() {
070    return policy == Policy.HTTPS_ONLY;
071  }
072
073  public String getSchemePrefix() {
074    return isSecure() ? "https://" : "http://";
075  }
076
077  public String getScheme(Policy policy) {
078    return policy == Policy.HTTPS_ONLY ? "https://" : "http://";
079  }
080}