Search in sources :

Example 1 with AttributeKey

use of io.netty.util.AttributeKey in project netty by netty.

the class AbstractBootstrapConfig method toString.

@Override
public String toString() {
    StringBuilder buf = new StringBuilder().append(StringUtil.simpleClassName(this)).append('(');
    EventLoopGroup group = group();
    if (group != null) {
        buf.append("group: ").append(StringUtil.simpleClassName(group)).append(", ");
    }
    @SuppressWarnings("deprecation") ChannelFactory<? extends C> factory = channelFactory();
    if (factory != null) {
        buf.append("channelFactory: ").append(factory).append(", ");
    }
    SocketAddress localAddress = localAddress();
    if (localAddress != null) {
        buf.append("localAddress: ").append(localAddress).append(", ");
    }
    Map<ChannelOption<?>, Object> options = options();
    if (!options.isEmpty()) {
        buf.append("options: ").append(options).append(", ");
    }
    Map<AttributeKey<?>, Object> attrs = attrs();
    if (!attrs.isEmpty()) {
        buf.append("attrs: ").append(attrs).append(", ");
    }
    ChannelHandler handler = handler();
    if (handler != null) {
        buf.append("handler: ").append(handler).append(", ");
    }
    if (buf.charAt(buf.length() - 1) == '(') {
        buf.append(')');
    } else {
        buf.setCharAt(buf.length() - 2, ')');
        buf.setLength(buf.length() - 1);
    }
    return buf.toString();
}
Also used : AttributeKey(io.netty.util.AttributeKey) EventLoopGroup(io.netty.channel.EventLoopGroup) ChannelOption(io.netty.channel.ChannelOption) ChannelHandler(io.netty.channel.ChannelHandler) SocketAddress(java.net.SocketAddress)

Example 2 with AttributeKey

use of io.netty.util.AttributeKey in project riposte by Nike-Inc.

the class RequestStateCleanerHandler method channelRead.

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg instanceof HttpRequest) {
        // New request incoming - setup/clear *all* state objects for new requests
        for (ProcessingStateClassAndKeyPair<? extends ProcessingState> stateClassAndKeyPair : PROCESSING_STATE_ATTRIBUTE_KEYS) {
            // See if we have an existing state object for this channel for the given state type.
            @SuppressWarnings("unchecked") AttributeKey<ProcessingState> attrKey = (AttributeKey<ProcessingState>) stateClassAndKeyPair.getRight();
            Attribute<ProcessingState> processingStateAttr = ctx.channel().attr(attrKey);
            ProcessingState processingState = processingStateAttr.get();
            if (processingState == null) {
                // We don't already have one for this channel, so create one and register it.
                processingState = stateClassAndKeyPair.getLeft().newInstance();
                processingStateAttr.set(processingState);
            }
            // Clean the state for the new request.
            processingState.cleanStateForNewRequest();
        }
        HttpProcessingState httpProcessingState = ChannelAttributes.getHttpProcessingStateForChannel(ctx).get();
        // Set the DistributedTracingConfig on the HttpProcessingState.
        // noinspection deprecation - This is the only place that should actually be calling this method.
        httpProcessingState.setDistributedTracingConfig(distributedTracingConfig);
        // Send a request received event to the metricsListener.
        if (metricsListener != null) {
            metricsListener.onEvent(ServerMetricsEvent.REQUEST_RECEIVED, httpProcessingState);
        }
        // Remove the idle channel timeout handler (if there is one) so that it doesn't kill this new request if the
        // endpoint takes longer to complete than the idle timeout value - the idle channel timeout is only for
        // timing out channels that are idle *in-between* requests.
        ChannelPipeline pipeline = ctx.pipeline();
        ChannelHandler idleChannelTimeoutHandler = pipeline.get(HttpChannelInitializer.IDLE_CHANNEL_TIMEOUT_HANDLER_NAME);
        if (idleChannelTimeoutHandler != null)
            pipeline.remove(idleChannelTimeoutHandler);
        // last chunk when the timeout hits.
        if (incompleteHttpCallTimeoutMillis > 0 && !(msg instanceof LastHttpContent)) {
            IncompleteHttpCallTimeoutHandler newHandler = new IncompleteHttpCallTimeoutHandler(incompleteHttpCallTimeoutMillis);
            ChannelHandler existingHandler = pipeline.get(INCOMPLETE_HTTP_CALL_TIMEOUT_HANDLER_NAME);
            if (existingHandler == null) {
                pipeline.addFirst(INCOMPLETE_HTTP_CALL_TIMEOUT_HANDLER_NAME, newHandler);
            } else {
                logger.error("Handling HttpRequest for new request and found an IncompleteHttpCallTimeoutHandler " + "already in the pipeline. This should not be possible. A new " + "IncompleteHttpCallTimeoutHandler will replace the old one. worker_channel_id={}", ctx.channel().toString());
                pipeline.replace(existingHandler, INCOMPLETE_HTTP_CALL_TIMEOUT_HANDLER_NAME, newHandler);
            }
        }
        ProxyRouterProcessingState proxyRouterProcessingState = ChannelAttributes.getProxyRouterProcessingStateForChannel(ctx).get();
        // Set the DistributedTracingConfig on the ProxyRouterProcessingState.
        // noinspection deprecation - This is the only place that should actually be calling this method.
        proxyRouterProcessingState.setDistributedTracingConfig(distributedTracingConfig);
    } else if (msg instanceof LastHttpContent) {
        // The HTTP call is complete, so we can remove the IncompleteHttpCallTimeoutHandler.
        ChannelPipeline pipeline = ctx.pipeline();
        ChannelHandler existingHandler = pipeline.get(INCOMPLETE_HTTP_CALL_TIMEOUT_HANDLER_NAME);
        if (existingHandler != null)
            pipeline.remove(INCOMPLETE_HTTP_CALL_TIMEOUT_HANDLER_NAME);
    }
    // Continue on the pipeline processing.
    super.channelRead(ctx, msg);
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) HttpProcessingState(com.nike.riposte.server.http.HttpProcessingState) ProxyRouterProcessingState(com.nike.riposte.server.http.ProxyRouterProcessingState) ChannelHandler(io.netty.channel.ChannelHandler) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) ChannelPipeline(io.netty.channel.ChannelPipeline) AttributeKey(io.netty.util.AttributeKey) HttpProcessingState(com.nike.riposte.server.http.HttpProcessingState) ProcessingState(com.nike.riposte.server.http.ProcessingState) ProxyRouterProcessingState(com.nike.riposte.server.http.ProxyRouterProcessingState)

Example 3 with AttributeKey

use of io.netty.util.AttributeKey in project netty by netty.

the class ServerBootstrapConfig method toString.

@Override
public String toString() {
    StringBuilder buf = new StringBuilder(super.toString());
    buf.setLength(buf.length() - 1);
    buf.append(", ");
    EventLoopGroup childGroup = childGroup();
    if (childGroup != null) {
        buf.append("childGroup: ");
        buf.append(StringUtil.simpleClassName(childGroup));
        buf.append(", ");
    }
    Map<ChannelOption<?>, Object> childOptions = childOptions();
    if (!childOptions.isEmpty()) {
        buf.append("childOptions: ");
        buf.append(childOptions);
        buf.append(", ");
    }
    Map<AttributeKey<?>, Object> childAttrs = childAttrs();
    if (!childAttrs.isEmpty()) {
        buf.append("childAttrs: ");
        buf.append(childAttrs);
        buf.append(", ");
    }
    ChannelHandler childHandler = childHandler();
    if (childHandler != null) {
        buf.append("childHandler: ");
        buf.append(childHandler);
        buf.append(", ");
    }
    if (buf.charAt(buf.length() - 1) == '(') {
        buf.append(')');
    } else {
        buf.setCharAt(buf.length() - 2, ')');
        buf.setLength(buf.length() - 1);
    }
    return buf.toString();
}
Also used : AttributeKey(io.netty.util.AttributeKey) EventLoopGroup(io.netty.channel.EventLoopGroup) ChannelOption(io.netty.channel.ChannelOption) ChannelHandler(io.netty.channel.ChannelHandler)

Example 4 with AttributeKey

use of io.netty.util.AttributeKey in project zuul by Netflix.

the class StripUntrustedProxyHeadersHandlerTest method before.

@Before
public void before() {
    when(channelHandlerContext.channel()).thenReturn(channel);
    DefaultAttributeMap attributeMap = new DefaultAttributeMap();
    attributeMap.attr(ATTR_SSL_INFO).set(sslHandshakeInfo);
    when(channel.attr(any())).thenAnswer(arg -> attributeMap.attr((AttributeKey) arg.getArguments()[0]));
    headers = new DefaultHttpHeaders();
    when(msg.headers()).thenReturn(headers);
    headers.add(HttpHeaderNames.HOST, "netflix.com");
}
Also used : AttributeKey(io.netty.util.AttributeKey) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) DefaultAttributeMap(io.netty.util.DefaultAttributeMap) Before(org.junit.Before)

Aggregations

AttributeKey (io.netty.util.AttributeKey)4 ChannelHandler (io.netty.channel.ChannelHandler)3 ChannelOption (io.netty.channel.ChannelOption)2 EventLoopGroup (io.netty.channel.EventLoopGroup)2 HttpProcessingState (com.nike.riposte.server.http.HttpProcessingState)1 ProcessingState (com.nike.riposte.server.http.ProcessingState)1 ProxyRouterProcessingState (com.nike.riposte.server.http.ProxyRouterProcessingState)1 ChannelPipeline (io.netty.channel.ChannelPipeline)1 DefaultHttpHeaders (io.netty.handler.codec.http.DefaultHttpHeaders)1 HttpRequest (io.netty.handler.codec.http.HttpRequest)1 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)1 DefaultAttributeMap (io.netty.util.DefaultAttributeMap)1 SocketAddress (java.net.SocketAddress)1 Before (org.junit.Before)1