All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.nike.riposte.server.channelpipeline.ChannelAttributes Maven / Gradle / Ivy

There is a newer version: 0.20.0
Show newest version
package com.nike.riposte.server.channelpipeline;

import com.nike.internal.util.Pair;
import com.nike.riposte.server.http.HttpProcessingState;
import com.nike.riposte.server.http.ProcessingState;
import com.nike.riposte.server.http.ProxyRouterProcessingState;

import java.util.Arrays;
import java.util.Collection;

import io.netty.channel.ChannelHandlerContext;
import io.netty.util.Attribute;
import io.netty.util.AttributeKey;

/**
 * Class for holding attribute keys for use with {@link io.netty.channel.Channel#attr(AttributeKey)}.
 *
 * @author Nic Munroe
 */
public class ChannelAttributes {

    // Intentionally private - all access should come through the static helper methods.
    private ChannelAttributes() { /* do nothing */ }

    /**
     * Attr key for the {@link HttpProcessingState} associated with a channel.
     */
    public static final AttributeKey HTTP_PROCESSING_STATE_ATTRIBUTE_KEY =
        AttributeKey.valueOf("HTTP_PROCESSING_STATE");
    /**
     * Attr key for the {@link ProxyRouterProcessingState} associated with a channel. Generally only used with
     * proxy/edge/domain routing servers.
     */
    public static final AttributeKey PROXY_ROUTER_PROCESSING_STATE_ATTRIBUTE_KEY =
        AttributeKey.valueOf("PROXY_ROUTER_PROCESSING_STATE");

    /**
     * Collection of all attribute keys that represent {@link ProcessingState} objects.
     */
    public static final Collection>
        PROCESSING_STATE_ATTRIBUTE_KEYS = Arrays.asList(
            ProcessingStateClassAndKeyPair.of(HttpProcessingState.class, HTTP_PROCESSING_STATE_ATTRIBUTE_KEY),
            ProcessingStateClassAndKeyPair.of(ProxyRouterProcessingState.class,
                                              PROXY_ROUTER_PROCESSING_STATE_ATTRIBUTE_KEY)
    );

    /**
     * Helper method for retrieving the {@link HttpProcessingState} for the given {@link ChannelHandlerContext}'s {@link
     * io.netty.channel.Channel}. This will never return null, however the {@link Attribute} it returns may not be set,
     * and therefore {@link Attribute#get()} (and related methods) may return null.
     */
    public static Attribute getHttpProcessingStateForChannel(ChannelHandlerContext ctx) {
        return ctx.channel().attr(HTTP_PROCESSING_STATE_ATTRIBUTE_KEY);
    }

    /**
     * Helper method for retrieving the {@link ProxyRouterProcessingState} for the given {@link ChannelHandlerContext}'s
     * {@link io.netty.channel.Channel}. This will never return null, however the {@link Attribute} it returns may not
     * be set, and therefore {@link Attribute#get()} (and related methods) may return null. This is generally only used
     * with proxy/edge/domain routing endpoints and will therefore not be populated for other endpoint types.
     */
    public static Attribute getProxyRouterProcessingStateForChannel(
        ChannelHandlerContext ctx) {
        return ctx.channel().attr(PROXY_ROUTER_PROCESSING_STATE_ATTRIBUTE_KEY);
    }

    /**
     * Custom extension of {@link Pair} that guarantees the left and right fields have correctly matching generics.
     */
    public static class ProcessingStateClassAndKeyPair
        extends Pair, AttributeKey> {

        public final Class left;
        public final AttributeKey right;

        public static  ProcessingStateClassAndKeyPair of(final Class left,
                                                                                       final AttributeKey right) {
            return new ProcessingStateClassAndKeyPair<>(left, right);
        }

        /**
         * Create a new pair instance.
         */
        ProcessingStateClassAndKeyPair(final Class left, final AttributeKey right) {
            super();
            this.left = left;
            this.right = right;
        }

        //-----------------------------------------------------------------------

        /**
         * {@inheritDoc}
         */
        @Override
        public Class getLeft() {
            return left;
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public AttributeKey getRight() {
            return right;
        }

        /**
         * 

Throws {@code UnsupportedOperationException}.

* *

This pair is immutable, so this operation is not supported.

* * @param value * the value to set * * @return never * * @throws UnsupportedOperationException * as this operation is not supported */ @Override public AttributeKey setValue(final AttributeKey value) { throw new UnsupportedOperationException(); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy