001/**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020package org.apache.hadoop.hbase.http;
021
022import java.io.IOException;
023import java.net.URI;
024
025import javax.servlet.http.HttpServlet;
026
027import org.apache.hadoop.hbase.HBaseConfiguration;
028import org.apache.yetus.audience.InterfaceAudience;
029import org.apache.hadoop.conf.Configuration;
030
031/**
032 * Create a Jetty embedded server to answer http requests. The primary goal
033 * is to serve up status information for the server.
034 * There are three contexts:
035 *   "/stacks/" -> points to stack trace
036 *   "/static/" -> points to common static files (src/hbase-webapps/static)
037 *   "/" -> the jsp server code from (src/hbase-webapps/<name>)
038 */
039@InterfaceAudience.Private
040public class InfoServer {
041
042  private static final String HBASE_APP_DIR = "hbase-webapps";
043  private final org.apache.hadoop.hbase.http.HttpServer httpServer;
044
045  /**
046   * Create a status server on the given port.
047   * The jsp scripts are taken from src/hbase-webapps/<code>name</code>.
048   * @param name The name of the server
049   * @param bindAddress address to bind to
050   * @param port The port to use on the server
051   * @param findPort whether the server should start at the given port and
052   * increment by 1 until it finds a free port.
053   * @throws IOException e
054   */
055  public InfoServer(String name, String bindAddress, int port, boolean findPort,
056      final Configuration c)
057  throws IOException {
058    HttpConfig httpConfig = new HttpConfig(c);
059    HttpServer.Builder builder =
060      new org.apache.hadoop.hbase.http.HttpServer.Builder();
061
062      builder.setName(name).addEndpoint(URI.create(httpConfig.getSchemePrefix() +
063        bindAddress + ":" +
064        port)).setAppDir(HBASE_APP_DIR).setFindPort(findPort).setConf(c);
065      String logDir = System.getProperty("hbase.log.dir");
066      if (logDir != null) {
067        builder.setLogDir(logDir);
068      }
069    if (httpConfig.isSecure()) {
070    builder.keyPassword(HBaseConfiguration.getPassword(c, "ssl.server.keystore.keypassword", null))
071      .keyStore(c.get("ssl.server.keystore.location"),
072        HBaseConfiguration.getPassword(c,"ssl.server.keystore.password", null),
073        c.get("ssl.server.keystore.type", "jks"))
074      .trustStore(c.get("ssl.server.truststore.location"),
075        HBaseConfiguration.getPassword(c, "ssl.server.truststore.password", null),
076        c.get("ssl.server.truststore.type", "jks"));
077    }
078    // Enable SPNEGO authentication
079    if ("kerberos".equalsIgnoreCase(c.get(HttpServer.HTTP_UI_AUTHENTICATION, null))) {
080      builder.setUsernameConfKey(HttpServer.HTTP_SPNEGO_AUTHENTICATION_PRINCIPAL_KEY)
081        .setKeytabConfKey(HttpServer.HTTP_SPNEGO_AUTHENTICATION_KEYTAB_KEY)
082        .setKerberosNameRulesKey(HttpServer.HTTP_SPNEGO_AUTHENTICATION_KRB_NAME_KEY)
083        .setSignatureSecretFileKey(
084            HttpServer.HTTP_AUTHENTICATION_SIGNATURE_SECRET_FILE_KEY)
085        .setSecurityEnabled(true);
086    }
087    this.httpServer = builder.build();
088  }
089
090  public void addServlet(String name, String pathSpec,
091          Class<? extends HttpServlet> clazz) {
092      this.httpServer.addServlet(name, pathSpec, clazz);
093  }
094
095  public void setAttribute(String name, Object value) {
096    this.httpServer.setAttribute(name, value);
097  }
098
099  public void start() throws IOException {
100    this.httpServer.start();
101  }
102
103  @Deprecated
104  public int getPort() {
105    return this.httpServer.getPort();
106  }
107
108  public void stop() throws Exception {
109    this.httpServer.stop();
110  }
111
112}