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.yetus.audience.InterfaceAudience;
021import org.apache.yetus.audience.InterfaceStability;
022import org.apache.hadoop.conf.Configuration;
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  public enum Policy {
032    HTTP_ONLY,
033    HTTPS_ONLY,
034    HTTP_AND_HTTPS;
035
036    public Policy fromString(String value) {
037      if (HTTPS_ONLY.name().equalsIgnoreCase(value)) {
038        return HTTPS_ONLY;
039      } else if (HTTP_AND_HTTPS.name().equalsIgnoreCase(value)) {
040        return HTTP_AND_HTTPS;
041      }
042      return HTTP_ONLY;
043    }
044
045    public boolean isHttpEnabled() {
046      return this == HTTP_ONLY || this == HTTP_AND_HTTPS;
047    }
048
049    public boolean isHttpsEnabled() {
050      return this == HTTPS_ONLY || this == HTTP_AND_HTTPS;
051    }
052  }
053
054   public HttpConfig(final Configuration conf) {
055    boolean sslEnabled = conf.getBoolean(
056      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}