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;
}
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()));
}
}
}
}
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;
}
Aggregations