Search in sources :

Example 1 with HttpServiceHandler

use of co.cask.cdap.api.service.http.HttpServiceHandler in project cdap by caskdata.

the class DefaultServiceConfigurer method createHandlerSpecs.

/**
   * Constructs HttpServiceSpecifications for each of the handlers in the {@param handlers} list.
   * Also performs verifications on these handlers (that a NettyHttpService can be constructed from them).
   */
private Map<String, HttpServiceHandlerSpecification> createHandlerSpecs(List<? extends HttpServiceHandler> handlers) {
    verifyHandlers(handlers);
    Map<String, HttpServiceHandlerSpecification> handleSpecs = Maps.newHashMap();
    for (HttpServiceHandler handler : handlers) {
        DefaultHttpServiceHandlerConfigurer configurer = new DefaultHttpServiceHandlerConfigurer(handler, deployNamespace, artifactId, artifactRepository, pluginInstantiator);
        handler.configure(configurer);
        HttpServiceHandlerSpecification spec = configurer.createSpecification();
        Preconditions.checkArgument(!handleSpecs.containsKey(spec.getName()), "Handler with name %s already existed.", spec.getName());
        handleSpecs.put(spec.getName(), spec);
        addStreams(configurer.getStreams());
        addDatasetModules(configurer.getDatasetModules());
        addDatasetSpecs(configurer.getDatasetSpecs());
        addPlugins(configurer.getPlugins());
    }
    return handleSpecs;
}
Also used : HttpServiceHandler(co.cask.cdap.api.service.http.HttpServiceHandler) HttpServiceHandlerSpecification(co.cask.cdap.api.service.http.HttpServiceHandlerSpecification)

Example 2 with HttpServiceHandler

use of co.cask.cdap.api.service.http.HttpServiceHandler in project cdap by caskdata.

the class DefaultServiceConfigurer method verifyHandlers.

private void verifyHandlers(List<? extends HttpServiceHandler> handlers) {
    Preconditions.checkArgument(!Iterables.isEmpty(handlers), "Service %s should have at least one handler", name);
    try {
        List<HttpHandler> httpHandlers = Lists.newArrayList();
        for (HttpServiceHandler handler : handlers) {
            httpHandlers.add(createHttpHandler(handler));
        }
        // Constructs a NettyHttpService, to verify that the handlers passed in by the user are valid.
        NettyHttpService.builder().addHttpHandlers(httpHandlers).build();
    } catch (Throwable t) {
        String errMessage = String.format("Invalid handlers in service: %s.", name);
        LOG.error(errMessage, t);
        throw new IllegalArgumentException(errMessage, t);
    }
}
Also used : HttpHandler(co.cask.http.HttpHandler) HttpServiceHandler(co.cask.cdap.api.service.http.HttpServiceHandler)

Example 3 with HttpServiceHandler

use of co.cask.cdap.api.service.http.HttpServiceHandler in project cdap by caskdata.

the class ServiceHttpServer method createHandlerDelegatorContexts.

private List<HandlerDelegatorContext> createHandlerDelegatorContexts(Program program, ServiceSpecification spec, BasicHttpServiceContextFactory contextFactory) {
    // Constructs all handler delegator. It is for bridging ServiceHttpHandler and HttpHandler (in netty-http).
    List<HandlerDelegatorContext> delegatorContexts = Lists.newArrayList();
    InstantiatorFactory instantiatorFactory = new InstantiatorFactory(false);
    for (Map.Entry<String, HttpServiceHandlerSpecification> entry : spec.getHandlers().entrySet()) {
        try {
            Class<?> handlerClass = program.getClassLoader().loadClass(entry.getValue().getClassName());
            @SuppressWarnings("unchecked") TypeToken<HttpServiceHandler> type = TypeToken.of((Class<HttpServiceHandler>) handlerClass);
            delegatorContexts.add(new HandlerDelegatorContext(type, instantiatorFactory, entry.getValue(), contextFactory));
        } catch (Exception e) {
            LOG.error("Could not initialize HTTP Service");
            throw Throwables.propagate(e);
        }
    }
    return delegatorContexts;
}
Also used : InstantiatorFactory(co.cask.cdap.common.lang.InstantiatorFactory) HttpServiceHandler(co.cask.cdap.api.service.http.HttpServiceHandler) HttpServiceHandlerSpecification(co.cask.cdap.api.service.http.HttpServiceHandlerSpecification) Map(java.util.Map)

Aggregations

HttpServiceHandler (co.cask.cdap.api.service.http.HttpServiceHandler)3 HttpServiceHandlerSpecification (co.cask.cdap.api.service.http.HttpServiceHandlerSpecification)2 InstantiatorFactory (co.cask.cdap.common.lang.InstantiatorFactory)1 HttpHandler (co.cask.http.HttpHandler)1 Map (java.util.Map)1