use of com.nike.riposte.server.hooks.PipelineCreateHook in project riposte by Nike-Inc.
the class HttpChannelInitializerTest method constructor_works_with_valid_args.
@Test
public void constructor_works_with_valid_args() {
// given
SslContext sslCtx = mock(SslContext.class);
int maxRequestSizeInBytes = 42;
Collection<Endpoint<?>> endpoints = Arrays.asList(getMockEndpoint("/some/path", HttpMethod.GET));
RequestAndResponseFilter beforeSecurityRequestFilter = mock(RequestAndResponseFilter.class);
doReturn(true).when(beforeSecurityRequestFilter).shouldExecuteBeforeSecurityValidation();
RequestAndResponseFilter afterSecurityRequestFilter = mock(RequestAndResponseFilter.class);
doReturn(false).when(afterSecurityRequestFilter).shouldExecuteBeforeSecurityValidation();
List<RequestAndResponseFilter> reqResFilters = Arrays.asList(beforeSecurityRequestFilter, afterSecurityRequestFilter);
Executor longRunningTaskExecutor = mock(Executor.class);
RiposteErrorHandler riposteErrorHandler = mock(RiposteErrorHandler.class);
RiposteUnhandledErrorHandler riposteUnhandledErrorHandler = mock(RiposteUnhandledErrorHandler.class);
RequestValidator validationService = mock(RequestValidator.class);
ObjectMapper requestContentDeserializer = mock(ObjectMapper.class);
ResponseSender responseSender = mock(ResponseSender.class);
@SuppressWarnings("unchecked") MetricsListener metricsListener = mock(MetricsListener.class);
long defaultCompletableFutureTimeoutMillis = 4242L;
AccessLogger accessLogger = mock(AccessLogger.class);
List<PipelineCreateHook> pipelineCreateHooks = mock(List.class);
RequestSecurityValidator requestSecurityValidator = mock(RequestSecurityValidator.class);
long workerChannelIdleTimeoutMillis = 121000;
long proxyRouterConnectTimeoutMillis = 4200;
long incompleteHttpCallTimeoutMillis = 1234;
int maxOpenChannelsThreshold = 1000;
boolean debugChannelLifecycleLoggingEnabled = true;
List<String> userIdHeaderKeys = mock(List.class);
int responseCompressionThresholdBytes = 5678;
HttpRequestDecoderConfig httpRequestDecoderConfig = new HttpRequestDecoderConfig() {
};
DistributedTracingConfig<Span> distributedTracingConfig = mock(DistributedTracingConfig.class);
ProxyRouterSpanNamingAndTaggingStrategy<Span> proxySpanTaggingStrategyMock = mock(ProxyRouterSpanNamingAndTaggingStrategy.class);
doReturn(proxySpanTaggingStrategyMock).when(distributedTracingConfig).getProxyRouterSpanNamingAndTaggingStrategy();
// when
HttpChannelInitializer hci = new HttpChannelInitializer(sslCtx, maxRequestSizeInBytes, endpoints, reqResFilters, longRunningTaskExecutor, riposteErrorHandler, riposteUnhandledErrorHandler, validationService, requestContentDeserializer, responseSender, metricsListener, defaultCompletableFutureTimeoutMillis, accessLogger, pipelineCreateHooks, requestSecurityValidator, workerChannelIdleTimeoutMillis, proxyRouterConnectTimeoutMillis, incompleteHttpCallTimeoutMillis, maxOpenChannelsThreshold, debugChannelLifecycleLoggingEnabled, userIdHeaderKeys, responseCompressionThresholdBytes, httpRequestDecoderConfig, distributedTracingConfig);
// then
assertThat(extractField(hci, "sslCtx"), is(sslCtx));
assertThat(extractField(hci, "maxRequestSizeInBytes"), is(maxRequestSizeInBytes));
assertThat(extractField(hci, "endpoints"), is(endpoints));
assertThat(extractField(hci, "longRunningTaskExecutor"), is(longRunningTaskExecutor));
assertThat(extractField(hci, "riposteErrorHandler"), is(riposteErrorHandler));
assertThat(extractField(hci, "riposteUnhandledErrorHandler"), is(riposteUnhandledErrorHandler));
assertThat(extractField(hci, "validationService"), is(validationService));
assertThat(extractField(hci, "requestContentDeserializer"), is(requestContentDeserializer));
assertThat(extractField(hci, "responseSender"), is(responseSender));
assertThat(extractField(hci, "metricsListener"), is(metricsListener));
assertThat(extractField(hci, "defaultCompletableFutureTimeoutMillis"), is(defaultCompletableFutureTimeoutMillis));
assertThat(extractField(hci, "accessLogger"), is(accessLogger));
assertThat(extractField(hci, "pipelineCreateHooks"), is(pipelineCreateHooks));
assertThat(extractField(hci, "requestSecurityValidator"), is(requestSecurityValidator));
assertThat(extractField(hci, "workerChannelIdleTimeoutMillis"), is(workerChannelIdleTimeoutMillis));
assertThat(extractField(hci, "incompleteHttpCallTimeoutMillis"), is(incompleteHttpCallTimeoutMillis));
assertThat(extractField(hci, "maxOpenChannelsThreshold"), is(maxOpenChannelsThreshold));
assertThat(extractField(hci, "debugChannelLifecycleLoggingEnabled"), is(debugChannelLifecycleLoggingEnabled));
assertThat(extractField(hci, "userIdHeaderKeys"), is(userIdHeaderKeys));
assertThat(extractField(hci, "responseCompressionThresholdBytes"), is(responseCompressionThresholdBytes));
assertThat(extractField(hci, "httpRequestDecoderConfig"), is(httpRequestDecoderConfig));
assertThat(extractField(hci, "distributedTracingConfig"), is(distributedTracingConfig));
StreamingAsyncHttpClient sahc = extractField(hci, "streamingAsyncHttpClientForProxyRouterEndpoints");
assertThat(extractField(sahc, "idleChannelTimeoutMillis"), is(workerChannelIdleTimeoutMillis));
assertThat(extractField(sahc, "downstreamConnectionTimeoutMillis"), is((int) proxyRouterConnectTimeoutMillis));
assertThat(extractField(sahc, "debugChannelLifecycleLoggingEnabled"), is(debugChannelLifecycleLoggingEnabled));
assertThat(extractField(sahc, "proxySpanTaggingStrategy"), is(proxySpanTaggingStrategyMock));
RequestFilterHandler beforeSecReqFH = extractField(hci, "beforeSecurityRequestFilterHandler");
assertThat(extractField(beforeSecReqFH, "filters"), is(Collections.singletonList(beforeSecurityRequestFilter)));
RequestFilterHandler afterSecReqFH = extractField(hci, "afterSecurityRequestFilterHandler");
assertThat(extractField(afterSecReqFH, "filters"), is(Collections.singletonList(afterSecurityRequestFilter)));
ResponseFilterHandler resFH = extractField(hci, "cachedResponseFilterHandler");
List<RequestAndResponseFilter> reversedFilters = new ArrayList<>(reqResFilters);
Collections.reverse(reversedFilters);
assertThat(extractField(resFH, "filtersInResponseProcessingOrder"), is(reversedFilters));
}
use of com.nike.riposte.server.hooks.PipelineCreateHook in project riposte by Nike-Inc.
the class HttpChannelInitializer method initChannel.
@Override
public void initChannel(SocketChannel ch) {
ChannelPipeline p = ch.pipeline();
// request/response payloads, etc.
if (debugChannelLifecycleLoggingEnabled) {
p.addLast(SERVER_WORKER_CHANNEL_DEBUG_LOGGING_HANDLER_NAME, new LoggingHandler(SERVER_WORKER_CHANNEL_DEBUG_SLF4J_LOGGER_NAME, LogLevel.DEBUG));
}
// order).
if (sslCtx != null)
p.addLast(SSL_HANDLER_NAME, sslCtx.newHandler(ch.alloc()));
// IN/OUT - Add the HttpServerCodec to decode requests into the appropriate HttpObjects and encode responses
// from HttpObjects into bytes. This MUST be the earliest "outbound" handler after the SSL handler
// since outbound handlers are processed in reverse order.
p.addLast(HTTP_SERVER_CODEC_HANDLER_NAME, new HttpServerCodec(httpRequestDecoderConfig.maxInitialLineLength(), httpRequestDecoderConfig.maxHeaderSize(), httpRequestDecoderConfig.maxChunkSize()));
// OUTBOUND - Add ProcessFinalResponseOutputHandler to get the final response headers, calculate the final
// content length (after compression/gzip and/or any other modifications), etc, and set those values
// on the channel's HttpProcessingState.
p.addLast(PROCESS_FINAL_RESPONSE_OUTPUT_HANDLER_NAME, new ProcessFinalResponseOutputHandler());
// INBOUND - Now that the message is translated into HttpObjects we can add RequestStateCleanerHandler to
// setup/clean state for the rest of the pipeline.
p.addLast(REQUEST_STATE_CLEANER_HANDLER_NAME, new RequestStateCleanerHandler(metricsListener, incompleteHttpCallTimeoutMillis, distributedTracingConfig));
// INBOUND - Add DTraceStartHandler to start the distributed tracing for this request
p.addLast(DTRACE_START_HANDLER_NAME, new DTraceStartHandler(userIdHeaderKeys, distributedTracingConfig));
// INBOUND - Access log start
p.addLast(ACCESS_LOG_START_HANDLER_NAME, new AccessLogStartHandler());
// IN/OUT - Add SmartHttpContentCompressor for automatic content compression (if appropriate for the
// request/response/size threshold). This must be added after HttpServerCodec so that it can process
// after the request on the incoming pipeline and before the response on the outbound pipeline.
p.addLast(SMART_HTTP_CONTENT_COMPRESSOR_HANDLER_NAME, new SmartHttpContentCompressor(responseCompressionThresholdBytes));
// before RoutingHandler throws 404s/405s.
if (beforeSecurityRequestFilterHandler != null)
p.addLast(REQUEST_FILTER_BEFORE_SECURITY_HANDLER_NAME, beforeSecurityRequestFilterHandler);
// INBOUND - Add RoutingHandler to figure out which endpoint should handle the request and set it on our request
// state for later execution
p.addLast(ROUTING_HANDLER_NAME, new RoutingHandler(endpoints, maxRequestSizeInBytes, distributedTracingConfig));
// INBOUND - Add SmartHttpContentDecompressor for automatic content decompression if the request indicates it
// is compressed *and* the target endpoint (determined by the previous RoutingHandler) is one that
// is eligible for auto-decompression.
p.addLast(SMART_HTTP_CONTENT_DECOMPRESSOR_HANDLER_NAME, new SmartHttpContentDecompressor());
// INBOUND - Add RequestInfoSetterHandler to populate our RequestInfo's content.
p.addLast(REQUEST_INFO_SETTER_HANDLER_NAME, new RequestInfoSetterHandler(maxRequestSizeInBytes));
// maxOpenChannelsThreshold is not -1.
if (maxOpenChannelsThreshold != -1) {
p.addLast(OPEN_CHANNEL_LIMIT_HANDLER_NAME, new OpenChannelLimitHandler(openChannelsGroup, maxOpenChannelsThreshold));
}
// INBOUND - Add SecurityValidationHandler to validate the RequestInfo object for the matching endpoint
p.addLast(SECURITY_VALIDATION_HANDLER_NAME, new SecurityValidationHandler(requestSecurityValidator));
// INBOUND - Add the RequestFilterHandler for after security (if we have any filters to apply).
if (afterSecurityRequestFilterHandler != null)
p.addLast(REQUEST_FILTER_AFTER_SECURITY_HANDLER_NAME, afterSecurityRequestFilterHandler);
// INBOUND - Now that the request state knows which endpoint will be called we can try to deserialize the
// request content (if desired by the endpoint)
p.addLast(REQUEST_CONTENT_DESERIALIZER_HANDLER_NAME, new RequestContentDeserializerHandler(requestContentDeserializer));
// deserialized content (if desired by the endpoint and if we have a non-null validator)
if (validationService != null)
p.addLast(REQUEST_CONTENT_VALIDATION_HANDLER_NAME, new RequestContentValidationHandler(validationService));
// INBOUND - Add NonblockingEndpointExecutionHandler to perform execution of async/nonblocking endpoints
p.addLast(NONBLOCKING_ENDPOINT_EXECUTION_HANDLER_NAME, new NonblockingEndpointExecutionHandler(longRunningTaskExecutor, defaultCompletableFutureTimeoutMillis, distributedTracingConfig));
// INBOUND - Add ProxyRouterEndpointExecutionHandler to perform execution of proxy routing endpoints
p.addLast(PROXY_ROUTER_ENDPOINT_EXECUTION_HANDLER_NAME, new ProxyRouterEndpointExecutionHandler(longRunningTaskExecutor, streamingAsyncHttpClientForProxyRouterEndpoints, defaultCompletableFutureTimeoutMillis, distributedTracingConfig));
// INBOUND - Add RequestHasBeenHandledVerificationHandler to verify that one of the endpoint handlers took care
// of the request. This makes sure that the messages coming into channelRead are correctly typed for
// the rest of the pipeline.
p.addLast(REQUEST_HAS_BEEN_HANDLED_VERIFICATION_HANDLER_NAME, new RequestHasBeenHandledVerificationHandler());
// INBOUND - Add ExceptionHandlingHandler to catch and deal with any exceptions or requests that fell through
// the cracks.
ExceptionHandlingHandler exceptionHandlingHandler = new ExceptionHandlingHandler(riposteErrorHandler, riposteUnhandledErrorHandler, distributedTracingConfig);
p.addLast(EXCEPTION_HANDLING_HANDLER_NAME, exceptionHandlingHandler);
// INBOUND - Add the ResponseFilterHandler (if we have any filters to apply).
if (cachedResponseFilterHandler != null)
p.addLast(RESPONSE_FILTER_HANDLER_NAME, cachedResponseFilterHandler);
// INBOUND - Add ResponseSenderHandler to send the response that got put into the request state
p.addLast(RESPONSE_SENDER_HANDLER_NAME, new ResponseSenderHandler(responseSender));
// INBOUND - Access log end
p.addLast(ACCESS_LOG_END_HANDLER_NAME, new AccessLogEndHandler(accessLogger));
// INBOUND - Add DTraceEndHandler to finish up our distributed trace for this request.
p.addLast(DTRACE_END_HANDLER_NAME, new DTraceEndHandler());
// INBOUND - Add ChannelPipelineFinalizerHandler to stop the request processing.
p.addLast(CHANNEL_PIPELINE_FINALIZER_HANDLER_NAME, new ChannelPipelineFinalizerHandler(exceptionHandlingHandler, responseSender, metricsListener, accessLogger, workerChannelIdleTimeoutMillis));
// pipeline create hooks
if (pipelineCreateHooks != null) {
for (PipelineCreateHook hook : pipelineCreateHooks) {
hook.executePipelineCreateHook(p);
}
}
}
Aggregations