Search in sources :

Example 1 with HttpServiceHandler

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

the class ServiceHttpServer method createDelegatorContexts.

@Override
protected List<HandlerDelegatorContext> createDelegatorContexts() throws Exception {
    // Constructs all handler delegator. It is for bridging ServiceHttpHandler and HttpHandler (in netty-http).
    List<HandlerDelegatorContext> delegatorContexts = new ArrayList<>();
    InstantiatorFactory instantiatorFactory = new InstantiatorFactory(false);
    for (HttpServiceHandlerSpecification handlerSpec : serviceSpecification.getHandlers().values()) {
        Class<?> handlerClass = getProgram().getClassLoader().loadClass(handlerSpec.getClassName());
        @SuppressWarnings("unchecked") TypeToken<HttpServiceHandler> type = TypeToken.of((Class<HttpServiceHandler>) handlerClass);
        MetricsContext metrics = httpServiceContext.getProgramMetrics().childContext(BasicHttpServiceContext.createMetricsTags(handlerSpec, getInstanceId()));
        delegatorContexts.add(new HandlerDelegatorContext(type, instantiatorFactory, handlerSpec, contextFactory, metrics));
    }
    return delegatorContexts;
}
Also used : InstantiatorFactory(io.cdap.cdap.common.lang.InstantiatorFactory) HttpServiceHandler(io.cdap.cdap.api.service.http.HttpServiceHandler) AbstractSystemHttpServiceHandler(io.cdap.cdap.api.service.http.AbstractSystemHttpServiceHandler) MetricsContext(io.cdap.cdap.api.metrics.MetricsContext) ArrayList(java.util.ArrayList) HttpServiceHandlerSpecification(io.cdap.cdap.api.service.http.HttpServiceHandlerSpecification)

Example 2 with HttpServiceHandler

use of io.cdap.cdap.api.service.http.HttpServiceHandler 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);
    for (HttpServiceHandler handler : handlers) {
        // check that a system service handler is only used in system namespace
        if (!deployNamespace.equals(Id.Namespace.fromEntityId(NamespaceId.SYSTEM))) {
            TypeToken<?> contextType = TypeToken.of(handler.getClass()).resolveType(HttpServiceHandler.class.getTypeParameters()[0]);
            if (SystemHttpServiceContext.class.isAssignableFrom(contextType.getRawType())) {
                throw new IllegalArgumentException(String.format("Invalid service handler '%s'. Service handlers can only use a SystemHttpServiceContext if the " + "application is deployed in the system namespace.", handler.getClass().getSimpleName()));
            }
            TypeToken<?> configurerType = TypeToken.of(handler.getClass()).resolveType(HttpServiceHandler.class.getTypeParameters()[1]);
            if (SystemHttpServiceConfigurer.class.isAssignableFrom(configurerType.getRawType())) {
                throw new IllegalArgumentException(String.format("Invalid service handler '%s'. Service handlers can only use a SystemHttpServiceConfigurer if the " + "application is deployed in the system namespace.", handler.getClass().getSimpleName()));
            }
        }
    }
}
Also used : HttpHandlerFactory(io.cdap.cdap.internal.app.runtime.service.http.HttpHandlerFactory) HttpServiceHandler(io.cdap.cdap.api.service.http.HttpServiceHandler)

Example 3 with HttpServiceHandler

use of io.cdap.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, pluginFinder, pluginInstantiator, systemTableConfigurer, runtimeInfo, getFeatureFlagsProvider());
        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);
        addDatasetModules(configurer.getDatasetModules());
        addDatasetSpecs(configurer.getDatasetSpecs());
        addPlugins(configurer.getPlugins());
    }
    return handleSpecs;
}
Also used : HttpServiceHandler(io.cdap.cdap.api.service.http.HttpServiceHandler) HttpServiceHandlerSpecification(io.cdap.cdap.api.service.http.HttpServiceHandlerSpecification)

Aggregations

HttpServiceHandler (io.cdap.cdap.api.service.http.HttpServiceHandler)3 HttpServiceHandlerSpecification (io.cdap.cdap.api.service.http.HttpServiceHandlerSpecification)2 MetricsContext (io.cdap.cdap.api.metrics.MetricsContext)1 AbstractSystemHttpServiceHandler (io.cdap.cdap.api.service.http.AbstractSystemHttpServiceHandler)1 InstantiatorFactory (io.cdap.cdap.common.lang.InstantiatorFactory)1 HttpHandlerFactory (io.cdap.cdap.internal.app.runtime.service.http.HttpHandlerFactory)1 ArrayList (java.util.ArrayList)1