Search in sources :

Example 1 with HttpHandlerFactory

use of co.cask.cdap.internal.app.runtime.service.http.HttpHandlerFactory in project cdap by caskdata.

the class ServiceHttpServer method createNettyHttpService.

/**
   * Creates a {@link NettyHttpService} from the given host, and list of {@link HandlerDelegatorContext}s
   *
   * @param program Program that contains the handler
   * @param host the host which the service will run on
   * @param delegatorContexts the list {@link HandlerDelegatorContext}
   * @param metricsContext a {@link MetricsContext} for metrics collection
   *
   * @return a NettyHttpService which delegates to the {@link HttpServiceHandler}s to handle the HTTP requests
   */
private NettyHttpService createNettyHttpService(Program program, String host, Iterable<HandlerDelegatorContext> delegatorContexts, MetricsContext metricsContext) {
    // The service URI is always prefixed for routing purpose
    String pathPrefix = String.format("%s/namespaces/%s/apps/%s/services/%s/methods", Constants.Gateway.API_VERSION_3, program.getNamespaceId(), program.getApplicationId(), program.getName());
    String versionId = program.getId().getVersion();
    String versionedPathPrefix = String.format("%s/namespaces/%s/apps/%s/versions/%s/services/%s/methods", Constants.Gateway.API_VERSION_3, program.getNamespaceId(), program.getApplicationId(), versionId, program.getName());
    // Create HttpHandlers which delegate to the HttpServiceHandlers
    HttpHandlerFactory factory = new HttpHandlerFactory(pathPrefix, metricsContext);
    HttpHandlerFactory versionedFactory = new HttpHandlerFactory(versionedPathPrefix, metricsContext);
    List<HttpHandler> nettyHttpHandlers = Lists.newArrayList();
    // get the runtime args from the twill context
    for (HandlerDelegatorContext context : delegatorContexts) {
        nettyHttpHandlers.add(factory.createHttpHandler(context.getHandlerType(), context));
        nettyHttpHandlers.add(versionedFactory.createHttpHandler(context.getHandlerType(), context));
    }
    NettyHttpService.Builder builder = NettyHttpService.builder(program.getName() + "-http").setHost(host).setPort(0).addHttpHandlers(nettyHttpHandlers);
    // These properties are for unit-test only. Currently they are not controllable by the user program
    String threadPoolSize = System.getProperty(THREAD_POOL_SIZE);
    if (threadPoolSize != null) {
        builder.setExecThreadPoolSize(Integer.parseInt(threadPoolSize));
    }
    String threadAliveSec = System.getProperty(THREAD_KEEP_ALIVE_SECONDS);
    if (threadAliveSec != null) {
        builder.setExecThreadKeepAliveSeconds(Long.parseLong(threadAliveSec));
    }
    return builder.build();
}
Also used : HttpHandler(co.cask.http.HttpHandler) HttpHandlerFactory(co.cask.cdap.internal.app.runtime.service.http.HttpHandlerFactory) NettyHttpService(co.cask.http.NettyHttpService)

Example 2 with HttpHandlerFactory

use of co.cask.cdap.internal.app.runtime.service.http.HttpHandlerFactory in project cdap by caskdata.

the class DefaultServiceConfigurer method verifyHandlers.

private void verifyHandlers(List<? extends HttpServiceHandler> handlers) {
    Preconditions.checkArgument(!handlers.isEmpty(), "Service %s should have at least one handler", name);
    new HttpHandlerFactory("", TransactionControl.IMPLICIT).validateHttpHandler(handlers);
}
Also used : HttpHandlerFactory(co.cask.cdap.internal.app.runtime.service.http.HttpHandlerFactory)

Example 3 with HttpHandlerFactory

use of co.cask.cdap.internal.app.runtime.service.http.HttpHandlerFactory in project cdap by caskdata.

the class DefaultServiceConfigurer method createHttpHandler.

private <T extends HttpServiceHandler> HttpHandler createHttpHandler(T handler) {
    MetricsContext noOpsMetricsContext = new NoOpMetricsCollectionService().getContext(new HashMap<String, String>());
    HttpHandlerFactory factory = new HttpHandlerFactory("", noOpsMetricsContext);
    @SuppressWarnings("unchecked") TypeToken<T> type = (TypeToken<T>) TypeToken.of(handler.getClass());
    return factory.createHttpHandler(type, new VerificationDelegateContext<>(handler));
}
Also used : HttpHandlerFactory(co.cask.cdap.internal.app.runtime.service.http.HttpHandlerFactory) TypeToken(com.google.common.reflect.TypeToken) MetricsContext(co.cask.cdap.api.metrics.MetricsContext) NoOpMetricsCollectionService(co.cask.cdap.common.metrics.NoOpMetricsCollectionService)

Aggregations

HttpHandlerFactory (co.cask.cdap.internal.app.runtime.service.http.HttpHandlerFactory)3 MetricsContext (co.cask.cdap.api.metrics.MetricsContext)1 NoOpMetricsCollectionService (co.cask.cdap.common.metrics.NoOpMetricsCollectionService)1 HttpHandler (co.cask.http.HttpHandler)1 NettyHttpService (co.cask.http.NettyHttpService)1 TypeToken (com.google.common.reflect.TypeToken)1