Search in sources :

Example 6 with CommonNettyHttpServiceBuilder

use of co.cask.cdap.common.http.CommonNettyHttpServiceBuilder in project cdap by caskdata.

the class MessagingHttpService method startUp.

@Override
protected void startUp() throws Exception {
    httpService = new CommonNettyHttpServiceBuilder(cConf, Constants.Service.MESSAGING_SERVICE).setHost(cConf.get(Constants.MessagingSystem.HTTP_SERVER_BIND_ADDRESS)).setHandlerHooks(ImmutableList.of(new MetricsReporterHook(metricsCollectionService, Constants.Service.MESSAGING_SERVICE))).setWorkerThreadPoolSize(cConf.getInt(Constants.MessagingSystem.HTTP_SERVER_WORKER_THREADS)).setExecThreadPoolSize(cConf.getInt(Constants.MessagingSystem.HTTP_SERVER_EXECUTOR_THREADS)).setHttpChunkLimit(cConf.getInt(Constants.MessagingSystem.HTTP_SERVER_MAX_REQUEST_SIZE_MB) * 1024 * 1024).setExceptionHandler(new HttpExceptionHandler() {

        @Override
        public void handle(Throwable t, HttpRequest request, HttpResponder responder) {
            // TODO: CDAP-7688. Override the handling to return 400 on IllegalArgumentException
            if (t instanceof IllegalArgumentException) {
                logWithTrace(request, t);
                responder.sendString(HttpResponseStatus.BAD_REQUEST, t.getMessage());
            } else {
                super.handle(t, request, responder);
            }
        }

        private void logWithTrace(HttpRequest request, Throwable t) {
            LOG.trace("Error in handling request={} {} for user={}:", request.getMethod().getName(), request.getUri(), Objects.firstNonNull(SecurityRequestContext.getUserId(), "<null>"), t);
        }
    }).addHttpHandlers(handlers).build();
    httpService.startAndWait();
    cancelDiscovery = discoveryService.register(new Discoverable(Constants.Service.MESSAGING_SERVICE, httpService.getBindAddress()));
    LOG.info("Messaging HTTP server started on {}", httpService.getBindAddress());
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) HttpResponder(co.cask.http.HttpResponder) Discoverable(org.apache.twill.discovery.Discoverable) MetricsReporterHook(co.cask.cdap.common.metrics.MetricsReporterHook) CommonNettyHttpServiceBuilder(co.cask.cdap.common.http.CommonNettyHttpServiceBuilder) HttpExceptionHandler(co.cask.cdap.common.HttpExceptionHandler)

Aggregations

CommonNettyHttpServiceBuilder (co.cask.cdap.common.http.CommonNettyHttpServiceBuilder)6 Discoverable (org.apache.twill.discovery.Discoverable)4 ResolvingDiscoverable (co.cask.cdap.common.discovery.ResolvingDiscoverable)3 MetricsReporterHook (co.cask.cdap.common.metrics.MetricsReporterHook)3 NettyHttpService (co.cask.http.NettyHttpService)3 InetSocketAddress (java.net.InetSocketAddress)3 Cancellable (org.apache.twill.common.Cancellable)3 AuthorizationClient (co.cask.cdap.client.AuthorizationClient)2 MasterAuthenticationContext (co.cask.cdap.security.auth.context.MasterAuthenticationContext)2 AuthorizerInstantiator (co.cask.cdap.security.authorization.AuthorizerInstantiator)2 InMemoryAuthorizer (co.cask.cdap.security.authorization.InMemoryAuthorizer)2 ServiceListenerAdapter (org.apache.twill.internal.ServiceListenerAdapter)2 ChannelPipeline (org.jboss.netty.channel.ChannelPipeline)2 FeatureDisabledException (co.cask.cdap.common.FeatureDisabledException)1 HttpExceptionHandler (co.cask.cdap.common.HttpExceptionHandler)1 NotFoundException (co.cask.cdap.common.NotFoundException)1 UnauthenticatedException (co.cask.cdap.common.UnauthenticatedException)1 CConfiguration (co.cask.cdap.common.conf.CConfiguration)1 AuthenticationChannelHandler (co.cask.cdap.common.http.AuthenticationChannelHandler)1 ServiceLoggingContext (co.cask.cdap.common.logging.ServiceLoggingContext)1