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