Search in sources :

Example 1 with ServletContextHandler

use of org.apache.hbase.thirdparty.org.eclipse.jetty.servlet.ServletContextHandler in project hbase by apache.

the class RESTServer method run.

/**
 * Runs the REST server.
 */
public synchronized void run() throws Exception {
    Pair<FilterHolder, Class<? extends ServletContainer>> pair = loginServerPrincipal(userProvider, conf);
    FilterHolder authFilter = pair.getFirst();
    Class<? extends ServletContainer> containerClass = pair.getSecond();
    RESTServlet servlet = RESTServlet.getInstance(conf, userProvider);
    // set up the Jersey servlet container for Jetty
    ResourceConfig application = new ResourceConfig().packages("org.apache.hadoop.hbase.rest").register(JacksonJaxbJsonProvider.class);
    // Using our custom ServletContainer is tremendously important. This is what makes sure the
    // UGI.doAs() is done for the remoteUser, and calls are not made as the REST server itself.
    ServletContainer servletContainer = ReflectionUtils.newInstance(containerClass, application);
    ServletHolder sh = new ServletHolder(servletContainer);
    // Set the default max thread number to 100 to limit
    // the number of concurrent requests so that REST server doesn't OOM easily.
    // Jetty set the default max thread number to 250, if we don't set it.
    // 
    // Our default min thread number 2 is the same as that used by Jetty.
    int maxThreads = servlet.getConfiguration().getInt(REST_THREAD_POOL_THREADS_MAX, 100);
    int minThreads = servlet.getConfiguration().getInt(REST_THREAD_POOL_THREADS_MIN, 2);
    // Use the default queue (unbounded with Jetty 9.3) if the queue size is negative, otherwise use
    // bounded {@link ArrayBlockingQueue} with the given size
    int queueSize = servlet.getConfiguration().getInt(REST_THREAD_POOL_TASK_QUEUE_SIZE, -1);
    int idleTimeout = servlet.getConfiguration().getInt(REST_THREAD_POOL_THREAD_IDLE_TIMEOUT, 60000);
    QueuedThreadPool threadPool = queueSize > 0 ? new QueuedThreadPool(maxThreads, minThreads, idleTimeout, new ArrayBlockingQueue<>(queueSize)) : new QueuedThreadPool(maxThreads, minThreads, idleTimeout);
    this.server = new Server(threadPool);
    // Setup JMX
    MBeanContainer mbContainer = new MBeanContainer(ManagementFactory.getPlatformMBeanServer());
    server.addEventListener(mbContainer);
    server.addBean(mbContainer);
    String host = servlet.getConfiguration().get("hbase.rest.host", "0.0.0.0");
    int servicePort = servlet.getConfiguration().getInt("hbase.rest.port", 8080);
    int httpHeaderCacheSize = servlet.getConfiguration().getInt(HTTP_HEADER_CACHE_SIZE, DEFAULT_HTTP_HEADER_CACHE_SIZE);
    HttpConfiguration httpConfig = new HttpConfiguration();
    httpConfig.setSecureScheme("https");
    httpConfig.setSecurePort(servicePort);
    httpConfig.setHeaderCacheSize(httpHeaderCacheSize);
    httpConfig.setRequestHeaderSize(DEFAULT_HTTP_MAX_HEADER_SIZE);
    httpConfig.setResponseHeaderSize(DEFAULT_HTTP_MAX_HEADER_SIZE);
    httpConfig.setSendServerVersion(false);
    httpConfig.setSendDateHeader(false);
    ServerConnector serverConnector;
    if (conf.getBoolean(REST_SSL_ENABLED, false)) {
        HttpConfiguration httpsConfig = new HttpConfiguration(httpConfig);
        httpsConfig.addCustomizer(new SecureRequestCustomizer());
        SslContextFactory sslCtxFactory = new SslContextFactory();
        String keystore = conf.get(REST_SSL_KEYSTORE_STORE);
        String keystoreType = conf.get(REST_SSL_KEYSTORE_TYPE);
        String password = HBaseConfiguration.getPassword(conf, REST_SSL_KEYSTORE_PASSWORD, null);
        String keyPassword = HBaseConfiguration.getPassword(conf, REST_SSL_KEYSTORE_KEYPASSWORD, password);
        sslCtxFactory.setKeyStorePath(keystore);
        if (StringUtils.isNotBlank(keystoreType)) {
            sslCtxFactory.setKeyStoreType(keystoreType);
        }
        sslCtxFactory.setKeyStorePassword(password);
        sslCtxFactory.setKeyManagerPassword(keyPassword);
        String trustStore = conf.get(REST_SSL_TRUSTSTORE_STORE);
        if (StringUtils.isNotBlank(trustStore)) {
            sslCtxFactory.setTrustStorePath(trustStore);
        }
        String trustStorePassword = HBaseConfiguration.getPassword(conf, REST_SSL_TRUSTSTORE_PASSWORD, null);
        if (StringUtils.isNotBlank(trustStorePassword)) {
            sslCtxFactory.setTrustStorePassword(trustStorePassword);
        }
        String trustStoreType = conf.get(REST_SSL_TRUSTSTORE_TYPE);
        if (StringUtils.isNotBlank(trustStoreType)) {
            sslCtxFactory.setTrustStoreType(trustStoreType);
        }
        String[] excludeCiphers = servlet.getConfiguration().getStrings(REST_SSL_EXCLUDE_CIPHER_SUITES, ArrayUtils.EMPTY_STRING_ARRAY);
        if (excludeCiphers.length != 0) {
            sslCtxFactory.setExcludeCipherSuites(excludeCiphers);
        }
        String[] includeCiphers = servlet.getConfiguration().getStrings(REST_SSL_INCLUDE_CIPHER_SUITES, ArrayUtils.EMPTY_STRING_ARRAY);
        if (includeCiphers.length != 0) {
            sslCtxFactory.setIncludeCipherSuites(includeCiphers);
        }
        String[] excludeProtocols = servlet.getConfiguration().getStrings(REST_SSL_EXCLUDE_PROTOCOLS, ArrayUtils.EMPTY_STRING_ARRAY);
        if (excludeProtocols.length != 0) {
            sslCtxFactory.setExcludeProtocols(excludeProtocols);
        }
        String[] includeProtocols = servlet.getConfiguration().getStrings(REST_SSL_INCLUDE_PROTOCOLS, ArrayUtils.EMPTY_STRING_ARRAY);
        if (includeProtocols.length != 0) {
            sslCtxFactory.setIncludeProtocols(includeProtocols);
        }
        serverConnector = new ServerConnector(server, new SslConnectionFactory(sslCtxFactory, HttpVersion.HTTP_1_1.toString()), new HttpConnectionFactory(httpsConfig));
    } else {
        serverConnector = new ServerConnector(server, new HttpConnectionFactory(httpConfig));
    }
    int acceptQueueSize = servlet.getConfiguration().getInt(REST_CONNECTOR_ACCEPT_QUEUE_SIZE, -1);
    if (acceptQueueSize >= 0) {
        serverConnector.setAcceptQueueSize(acceptQueueSize);
    }
    serverConnector.setPort(servicePort);
    serverConnector.setHost(host);
    server.addConnector(serverConnector);
    server.setStopAtShutdown(true);
    // set up context
    ServletContextHandler ctxHandler = new ServletContextHandler(server, "/", ServletContextHandler.SESSIONS);
    ctxHandler.addServlet(sh, PATH_SPEC_ANY);
    if (authFilter != null) {
        ctxHandler.addFilter(authFilter, PATH_SPEC_ANY, EnumSet.of(DispatcherType.REQUEST));
    }
    // Load filters from configuration.
    String[] filterClasses = servlet.getConfiguration().getStrings(FILTER_CLASSES, GzipFilter.class.getName());
    for (String filter : filterClasses) {
        filter = filter.trim();
        ctxHandler.addFilter(filter, PATH_SPEC_ANY, EnumSet.of(DispatcherType.REQUEST));
    }
    addCSRFFilter(ctxHandler, conf);
    addClickjackingPreventionFilter(ctxHandler, conf);
    addSecurityHeadersFilter(ctxHandler, conf);
    HttpServerUtil.constrainHttpMethods(ctxHandler, servlet.getConfiguration().getBoolean(REST_HTTP_ALLOW_OPTIONS_METHOD, REST_HTTP_ALLOW_OPTIONS_METHOD_DEFAULT));
    // Put up info server.
    int port = conf.getInt("hbase.rest.info.port", 8085);
    if (port >= 0) {
        conf.setLong("startcode", EnvironmentEdgeManager.currentTime());
        String a = conf.get("hbase.rest.info.bindAddress", "0.0.0.0");
        this.infoServer = new InfoServer("rest", a, port, false, conf);
        this.infoServer.setAttribute("hbase.conf", conf);
        this.infoServer.start();
    }
    // start server
    server.start();
}
Also used : FilterHolder(org.apache.hbase.thirdparty.org.eclipse.jetty.servlet.FilterHolder) SecureRequestCustomizer(org.apache.hbase.thirdparty.org.eclipse.jetty.server.SecureRequestCustomizer) Server(org.apache.hbase.thirdparty.org.eclipse.jetty.server.Server) InfoServer(org.apache.hadoop.hbase.http.InfoServer) HttpConnectionFactory(org.apache.hbase.thirdparty.org.eclipse.jetty.server.HttpConnectionFactory) ServletHolder(org.apache.hbase.thirdparty.org.eclipse.jetty.servlet.ServletHolder) HttpConfiguration(org.apache.hbase.thirdparty.org.eclipse.jetty.server.HttpConfiguration) SslConnectionFactory(org.apache.hbase.thirdparty.org.eclipse.jetty.server.SslConnectionFactory) GzipFilter(org.apache.hadoop.hbase.rest.filter.GzipFilter) ServerConnector(org.apache.hbase.thirdparty.org.eclipse.jetty.server.ServerConnector) SslContextFactory(org.apache.hbase.thirdparty.org.eclipse.jetty.util.ssl.SslContextFactory) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) QueuedThreadPool(org.apache.hbase.thirdparty.org.eclipse.jetty.util.thread.QueuedThreadPool) ServletContainer(org.apache.hbase.thirdparty.org.glassfish.jersey.servlet.ServletContainer) MBeanContainer(org.apache.hbase.thirdparty.org.eclipse.jetty.jmx.MBeanContainer) InfoServer(org.apache.hadoop.hbase.http.InfoServer) ResourceConfig(org.apache.hbase.thirdparty.org.glassfish.jersey.server.ResourceConfig) ServletContextHandler(org.apache.hbase.thirdparty.org.eclipse.jetty.servlet.ServletContextHandler)

Example 2 with ServletContextHandler

use of org.apache.hbase.thirdparty.org.eclipse.jetty.servlet.ServletContextHandler in project hbase by apache.

the class HttpServer method addDefaultApps.

/**
 * Add default apps.
 * @param appDir The application directory
 */
protected void addDefaultApps(ContextHandlerCollection parent, final String appDir, Configuration conf) {
    // set up the context for "/logs/" if "hadoop.log.dir" property is defined.
    String logDir = this.logDir;
    if (logDir == null) {
        logDir = System.getProperty("hadoop.log.dir");
    }
    if (logDir != null) {
        ServletContextHandler logContext = new ServletContextHandler(parent, "/logs");
        logContext.addServlet(AdminAuthorizedServlet.class, "/*");
        logContext.setResourceBase(logDir);
        if (conf.getBoolean(ServerConfigurationKeys.HBASE_JETTY_LOGS_SERVE_ALIASES, ServerConfigurationKeys.DEFAULT_HBASE_JETTY_LOGS_SERVE_ALIASES)) {
            Map<String, String> params = logContext.getInitParams();
            params.put("org.mortbay.jetty.servlet.Default.aliases", "true");
        }
        logContext.setDisplayName("logs");
        setContextAttributes(logContext, conf);
        defaultContexts.put(logContext, true);
    }
    // set up the context for "/static/*"
    ServletContextHandler staticContext = new ServletContextHandler(parent, "/static");
    staticContext.setResourceBase(appDir + "/static");
    staticContext.addServlet(DefaultServlet.class, "/*");
    staticContext.setDisplayName("static");
    setContextAttributes(staticContext, conf);
    defaultContexts.put(staticContext, true);
}
Also used : ServletContextHandler(org.apache.hbase.thirdparty.org.eclipse.jetty.servlet.ServletContextHandler)

Example 3 with ServletContextHandler

use of org.apache.hbase.thirdparty.org.eclipse.jetty.servlet.ServletContextHandler in project hbase by apache.

the class HttpServer method addDefaultServlets.

/**
 * Add default servlets.
 */
protected void addDefaultServlets(ContextHandlerCollection contexts, Configuration conf) throws IOException {
    // set up default servlets
    addPrivilegedServlet("stacks", "/stacks", StackServlet.class);
    addPrivilegedServlet("logLevel", "/logLevel", LogLevel.Servlet.class);
    // Remove when we drop support for hbase on hadoop2.x.
    try {
        Class<?> clz = Class.forName("org.apache.hadoop.metrics.MetricsServlet");
        addPrivilegedServlet("metrics", "/metrics", clz.asSubclass(HttpServlet.class));
    } catch (Exception e) {
    // do nothing
    }
    addPrivilegedServlet("jmx", "/jmx", JMXJsonServlet.class);
    // might. Give them an option to not expose the service configuration to all users.
    if (conf.getBoolean(HTTP_PRIVILEGED_CONF_KEY, HTTP_PRIVILEGED_CONF_DEFAULT)) {
        addPrivilegedServlet("conf", "/conf", ConfServlet.class);
    } else {
        addUnprivilegedServlet("conf", "/conf", ConfServlet.class);
    }
    final String asyncProfilerHome = ProfileServlet.getAsyncProfilerHome();
    if (asyncProfilerHome != null && !asyncProfilerHome.trim().isEmpty()) {
        addPrivilegedServlet("prof", "/prof", ProfileServlet.class);
        Path tmpDir = Paths.get(ProfileServlet.OUTPUT_DIR);
        if (Files.notExists(tmpDir)) {
            Files.createDirectories(tmpDir);
        }
        ServletContextHandler genCtx = new ServletContextHandler(contexts, "/prof-output-hbase");
        genCtx.addServlet(ProfileOutputServlet.class, "/*");
        genCtx.setResourceBase(tmpDir.toAbsolutePath().toString());
        genCtx.setDisplayName("prof-output-hbase");
    } else {
        addUnprivilegedServlet("prof", "/prof", ProfileServlet.DisabledServlet.class);
        LOG.info("ASYNC_PROFILER_HOME environment variable and async.profiler.home system property " + "not specified. Disabling /prof endpoint.");
    }
}
Also used : Path(java.nio.file.Path) HttpServlet(javax.servlet.http.HttpServlet) ServletContextHandler(org.apache.hbase.thirdparty.org.eclipse.jetty.servlet.ServletContextHandler) LogLevel(org.apache.hadoop.hbase.http.log.LogLevel) ServletException(javax.servlet.ServletException) URISyntaxException(java.net.URISyntaxException) MultiException(org.apache.hbase.thirdparty.org.eclipse.jetty.util.MultiException) FileNotFoundException(java.io.FileNotFoundException) BindException(java.net.BindException) InterruptedIOException(java.io.InterruptedIOException) HadoopIllegalArgumentException(org.apache.hadoop.HadoopIllegalArgumentException) IOException(java.io.IOException)

Example 4 with ServletContextHandler

use of org.apache.hbase.thirdparty.org.eclipse.jetty.servlet.ServletContextHandler in project hbase by apache.

the class HttpServer method addFilter.

@Override
public void addFilter(String name, String classname, Map<String, String> parameters) {
    final String[] USER_FACING_URLS = { "*.html", "*.jsp" };
    defineFilter(webAppContext, name, classname, parameters, USER_FACING_URLS);
    LOG.info("Added filter " + name + " (class=" + classname + ") to context " + webAppContext.getDisplayName());
    final String[] ALL_URLS = { "/*" };
    for (Map.Entry<ServletContextHandler, Boolean> e : defaultContexts.entrySet()) {
        if (e.getValue()) {
            ServletContextHandler handler = e.getKey();
            defineFilter(handler, name, classname, parameters, ALL_URLS);
            LOG.info("Added filter " + name + " (class=" + classname + ") to context " + handler.getDisplayName());
        }
    }
    filterNames.add(name);
}
Also used : ServletContextHandler(org.apache.hbase.thirdparty.org.eclipse.jetty.servlet.ServletContextHandler) Map(java.util.Map) HashMap(java.util.HashMap)

Example 5 with ServletContextHandler

use of org.apache.hbase.thirdparty.org.eclipse.jetty.servlet.ServletContextHandler in project hbase by apache.

the class HttpServer method addGlobalFilter.

@Override
public void addGlobalFilter(String name, String classname, Map<String, String> parameters) {
    final String[] ALL_URLS = { "/*" };
    defineFilter(webAppContext, name, classname, parameters, ALL_URLS);
    for (ServletContextHandler ctx : defaultContexts.keySet()) {
        defineFilter(ctx, name, classname, parameters, ALL_URLS);
    }
    LOG.info("Added global filter '" + name + "' (class=" + classname + ")");
}
Also used : ServletContextHandler(org.apache.hbase.thirdparty.org.eclipse.jetty.servlet.ServletContextHandler)

Aggregations

ServletContextHandler (org.apache.hbase.thirdparty.org.eclipse.jetty.servlet.ServletContextHandler)6 InfoServer (org.apache.hadoop.hbase.http.InfoServer)2 HttpConfiguration (org.apache.hbase.thirdparty.org.eclipse.jetty.server.HttpConfiguration)2 HttpConnectionFactory (org.apache.hbase.thirdparty.org.eclipse.jetty.server.HttpConnectionFactory)2 SecureRequestCustomizer (org.apache.hbase.thirdparty.org.eclipse.jetty.server.SecureRequestCustomizer)2 Server (org.apache.hbase.thirdparty.org.eclipse.jetty.server.Server)2 ServerConnector (org.apache.hbase.thirdparty.org.eclipse.jetty.server.ServerConnector)2 SslConnectionFactory (org.apache.hbase.thirdparty.org.eclipse.jetty.server.SslConnectionFactory)2 ServletHolder (org.apache.hbase.thirdparty.org.eclipse.jetty.servlet.ServletHolder)2 SslContextFactory (org.apache.hbase.thirdparty.org.eclipse.jetty.util.ssl.SslContextFactory)2 QueuedThreadPool (org.apache.hbase.thirdparty.org.eclipse.jetty.util.thread.QueuedThreadPool)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InterruptedIOException (java.io.InterruptedIOException)1 BindException (java.net.BindException)1 URISyntaxException (java.net.URISyntaxException)1 Path (java.nio.file.Path)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)1