1717package co .cask .http ;
1818
1919import com .google .common .base .Function ;
20+ import com .google .common .base .Preconditions ;
2021import com .google .common .collect .ImmutableList ;
2122import com .google .common .collect .ImmutableMap ;
2223import com .google .common .collect .Maps ;
4243import org .slf4j .Logger ;
4344import org .slf4j .LoggerFactory ;
4445
45- import java .io .File ;
4646import java .net .InetSocketAddress ;
4747import java .util .Map ;
4848import java .util .concurrent .Executor ;
@@ -93,6 +93,7 @@ public final class NettyHttpService extends AbstractIdleService {
9393 * @param urlRewriter URLRewriter to rewrite incoming URLs.
9494 * @param httpHandlers HttpHandlers to handle the calls.
9595 * @param handlerHooks Hooks to be called before/after request processing by httpHandlers.
96+ *
9697 * @deprecated Use {@link NettyHttpService.Builder} instead.
9798 */
9899 @ Deprecated
@@ -102,19 +103,9 @@ public NettyHttpService(InetSocketAddress bindAddress, int bossThreadPoolSize, i
102103 RejectedExecutionHandler rejectedExecutionHandler , URLRewriter urlRewriter ,
103104 Iterable <? extends HttpHandler > httpHandlers ,
104105 Iterable <? extends HandlerHook > handlerHooks , int httpChunkLimit ) {
105- this .bindAddress = bindAddress ;
106- this .bossThreadPoolSize = bossThreadPoolSize ;
107- this .workerThreadPoolSize = workerThreadPoolSize ;
108- this .execThreadPoolSize = execThreadPoolSize ;
109- this .execThreadKeepAliveSecs = execThreadKeepAliveSecs ;
110- this .channelConfigs = ImmutableMap .copyOf (channelConfigs );
111- this .rejectedExecutionHandler = rejectedExecutionHandler ;
112- this .channelGroup = new DefaultChannelGroup ();
113- this .resourceHandler = new HttpResourceHandler (httpHandlers , handlerHooks , urlRewriter );
114- this .handlerContext = new BasicHandlerContext (this .resourceHandler );
115- this .httpChunkLimit = httpChunkLimit ;
116- this .pipelineModifier = null ;
117- this .sslHandlerFactory = null ;
106+ this (bindAddress , bossThreadPoolSize , workerThreadPoolSize , execThreadPoolSize , execThreadKeepAliveSecs ,
107+ channelConfigs , rejectedExecutionHandler , urlRewriter , httpHandlers , handlerHooks , httpChunkLimit ,
108+ null , null , new ExceptionHandler ());
118109 }
119110
120111 /**
@@ -131,6 +122,7 @@ public NettyHttpService(InetSocketAddress bindAddress, int bossThreadPoolSize, i
131122 * @param handlerHooks Hooks to be called before/after request processing by httpHandlers.
132123 * @param pipelineModifier Function used to modify the pipeline.
133124 * @param sslHandlerFactory Object used to share SSL certificate details
125+ * @param exceptionHandler Handles exceptions from calling handler methods
134126 */
135127 private NettyHttpService (InetSocketAddress bindAddress , int bossThreadPoolSize , int workerThreadPoolSize ,
136128 int execThreadPoolSize , long execThreadKeepAliveSecs ,
@@ -139,7 +131,7 @@ private NettyHttpService(InetSocketAddress bindAddress, int bossThreadPoolSize,
139131 Iterable <? extends HttpHandler > httpHandlers ,
140132 Iterable <? extends HandlerHook > handlerHooks , int httpChunkLimit ,
141133 Function <ChannelPipeline , ChannelPipeline > pipelineModifier ,
142- SSLHandlerFactory sslHandlerFactory ) {
134+ SSLHandlerFactory sslHandlerFactory , ExceptionHandler exceptionHandler ) {
143135 this .bindAddress = bindAddress ;
144136 this .bossThreadPoolSize = bossThreadPoolSize ;
145137 this .workerThreadPoolSize = workerThreadPoolSize ;
@@ -148,7 +140,7 @@ private NettyHttpService(InetSocketAddress bindAddress, int bossThreadPoolSize,
148140 this .channelConfigs = ImmutableMap .copyOf (channelConfigs );
149141 this .rejectedExecutionHandler = rejectedExecutionHandler ;
150142 this .channelGroup = new DefaultChannelGroup ();
151- this .resourceHandler = new HttpResourceHandler (httpHandlers , handlerHooks , urlRewriter );
143+ this .resourceHandler = new HttpResourceHandler (httpHandlers , handlerHooks , urlRewriter , exceptionHandler );
152144 this .handlerContext = new BasicHandlerContext (this .resourceHandler );
153145 this .httpChunkLimit = httpChunkLimit ;
154146 this .pipelineModifier = pipelineModifier ;
@@ -322,6 +314,7 @@ public static class Builder {
322314 private int httpChunkLimit ;
323315 private SSLHandlerFactory sslHandlerFactory ;
324316 private Function <ChannelPipeline , ChannelPipeline > pipelineModifier ;
317+ private ExceptionHandler exceptionHandler ;
325318
326319 // Protected constructor to prevent instantiating Builder instance directly.
327320 protected Builder () {
@@ -335,6 +328,7 @@ protected Builder() {
335328 channelConfigs = Maps .newHashMap ();
336329 channelConfigs .put ("backlog" , DEFAULT_CONNECTION_BACKLOG );
337330 sslHandlerFactory = null ;
331+ exceptionHandler = new ExceptionHandler ();
338332 }
339333
340334 /**
@@ -501,6 +495,12 @@ public Builder enableSSL(SSLConfig sslConfig) {
501495 return this ;
502496 }
503497
498+ public Builder setExceptionHandler (ExceptionHandler exceptionHandler ) {
499+ Preconditions .checkNotNull (exceptionHandler , "exceptionHandler cannot be null" );
500+ this .exceptionHandler = exceptionHandler ;
501+ return this ;
502+ }
503+
504504 /**
505505 * @return instance of {@code NettyHttpService}
506506 */
@@ -515,7 +515,7 @@ public NettyHttpService build() {
515515 return new NettyHttpService (bindAddress , bossThreadPoolSize , workerThreadPoolSize ,
516516 execThreadPoolSize , execThreadKeepAliveSecs , channelConfigs , rejectedExecutionHandler ,
517517 urlRewriter , handlers , handlerHooks , httpChunkLimit , pipelineModifier ,
518- sslHandlerFactory );
518+ sslHandlerFactory , exceptionHandler );
519519 }
520520 }
521521}
0 commit comments