Example usage for io.netty.channel ChannelHandlerContext attr

List of usage examples for io.netty.channel ChannelHandlerContext attr

Introduction

In this page you can find the example usage for io.netty.channel ChannelHandlerContext attr.

Prototype

@Deprecated
@Override
<T> Attribute<T> attr(AttributeKey<T> key);

Source Link

Usage

From source file:org.ebayopensource.scc.filter.NettyResponseProxyFilter.java

License:Apache License

@Override
public HttpObject filterResponse(HttpObject orignal, ChannelHandlerContext ctx) {
    HttpObject httpObject = orignal;//from   w w  w  .  j  a v a 2 s .  c  o m
    if (httpObject instanceof HttpResponse) {
        String uri = ctx.attr(NettyRequestProxyFilter.REQUEST_URI).get();
        LOGGER.info("Received a response from: " + uri);
        if (((HttpResponse) httpObject).getStatus().code() >= 300) {
            ctx.attr(NettyRequestProxyFilter.IS_CACHABLE).set(false);
        }
    }
    if (!ctx.attr(NettyRequestProxyFilter.IS_CACHABLE).get()) {
        return httpObject;
    }
    final String key = ctx.attr(NettyRequestProxyFilter.CACHE_KEY).get();
    if (key == null) {
        return httpObject;
    }

    if (httpObject instanceof HttpResponse) {
        LOGGER.debug("Response code: " + ((HttpResponse) httpObject).getStatus().code());
    }
    if (httpObject instanceof FullHttpResponse) {
        m_policyManager.getCacheManager().put(key, httpObject);
    } else {
        final List<HttpObject> chunkedResponses = m_chunkedResponses.get();
        if (httpObject instanceof HttpContent) {
            HttpContent httpContent = ((HttpContent) httpObject).duplicate();
            httpContent.retain();
            httpObject = httpContent;
        }
        chunkedResponses.add(httpObject);
        if (httpObject instanceof LastHttpContent) {
            Runnable runnable = new Runnable() {

                @Override
                public void run() {
                    try {
                        m_policyManager.getCacheManager().put(key, chunkedResponses);
                    } catch (Throwable t) {
                        LOGGER.error(t.getMessage(), t);
                        return;
                    }
                    for (HttpObject obj : chunkedResponses) {
                        if (obj instanceof HttpContent) {
                            HttpContent httpContent = (HttpContent) obj;
                            httpContent.release();
                        }
                    }
                    chunkedResponses.clear();
                    LOGGER.debug("Cache response to: " + key);
                }

            };
            m_execService.submit(runnable);
        }
    }
    return orignal;
}

From source file:org.ebayopensource.scc.filter.NettyResponseProxyFilterTest.java

License:Apache License

@Test
public void testFilterFullHttpResponse() {
    PolicyManager policyManager = mock(PolicyManager.class);
    AppConfiguration appConfig = mock(AppConfiguration.class);
    when(appConfig.getBoolean(AppConfiguration.KEY_DEBUG_INFO)).thenReturn(true);

    NettyResponseProxyFilter filter = new NettyResponseProxyFilter(policyManager,
            Executors.newCachedThreadPool());

    ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
    when(ctx.attr(any(AttributeKey.class))).thenReturn(mock(Attribute.class));
    Attribute cachable = mock(Attribute.class);
    when(ctx.attr(NettyRequestProxyFilter.IS_CACHABLE)).thenReturn(cachable);
    when(cachable.get()).thenReturn(Boolean.FALSE);
    assertNull(filter.filterResponse(null, ctx));

    when(cachable.get()).thenReturn(Boolean.TRUE);
    assertNull(filter.filterResponse(null, ctx));

    HttpResponse resp = mock(HttpResponse.class);
    when(resp.getStatus()).thenReturn(HttpResponseStatus.OK);
    assertEquals(resp, filter.filterResponse(resp, ctx));

    resp = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND);
    assertEquals(resp, filter.filterResponse(resp, ctx));

    resp.setStatus(HttpResponseStatus.OK);
    assertEquals(resp, filter.filterResponse(resp, ctx));

    Attribute cacheKey = mock(Attribute.class);
    when(ctx.attr(NettyRequestProxyFilter.CACHE_KEY)).thenReturn(cacheKey);
    when(cacheKey.getAndRemove()).thenReturn("key");
    when(policyManager.getCacheManager()).thenReturn(mock(CacheManager.class));
    assertEquals(resp, filter.filterResponse(resp, ctx));

    when(cacheKey.get()).thenReturn("key");
    assertEquals(resp, filter.filterResponse(resp, ctx));
}

From source file:org.ebayopensource.scc.filter.NettyResponseProxyFilterTest.java

License:Apache License

@Test
public void testFilterChunkeResponses() throws InterruptedException {
    PolicyManager policyManager = mock(PolicyManager.class);
    AppConfiguration appConfig = mock(AppConfiguration.class);
    when(appConfig.getBoolean(AppConfiguration.KEY_DEBUG_INFO)).thenReturn(true);

    ExecutorService tp = Executors.newCachedThreadPool();
    NettyResponseProxyFilter filter = new NettyResponseProxyFilter(policyManager, tp);

    ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
    when(ctx.attr(any(AttributeKey.class))).thenReturn(mock(Attribute.class));
    Attribute cachable = mock(Attribute.class);
    when(ctx.attr(NettyRequestProxyFilter.IS_CACHABLE)).thenReturn(cachable);
    when(cachable.get()).thenReturn(Boolean.TRUE);

    Attribute cacheKey = mock(Attribute.class);
    when(ctx.attr(NettyRequestProxyFilter.CACHE_KEY)).thenReturn(cacheKey);
    when(cacheKey.get()).thenReturn("key");

    HttpResponse resp = mock(HttpResponse.class);
    when(resp.getStatus()).thenReturn(HttpResponseStatus.OK);
    filter.filterResponse(resp, ctx);/*  w w  w  . j a v a 2  s .c  o  m*/
    HttpContent httpContent = mock(HttpContent.class);
    when(httpContent.duplicate()).thenReturn(mock(HttpContent.class));
    filter.filterResponse(httpContent, ctx);
    LastHttpContent lastHttpContent = mock(LastHttpContent.class);
    when(lastHttpContent.duplicate()).thenReturn(mock(LastHttpContent.class));
    filter.filterResponse(lastHttpContent, ctx);

    filter.filterResponse(resp, ctx);
    filter.filterResponse(lastHttpContent, ctx);

    tp.awaitTermination(10, TimeUnit.SECONDS);
}

From source file:org.eclipse.moquette.server.netty.metrics.BytesMetricsHandler.java

License:Open Source License

@Override
public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
    BytesMetrics metrics = ctx.attr(ATTR_KEY_METRICS).get();
    m_collector.addMetrics(metrics);/*from w  w w  . j av a  2s .  c o m*/
    super.close(ctx, promise);
}

From source file:org.eclipse.moquette.server.netty.metrics.MessageMetricsHandler.java

License:Open Source License

@Override
public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
    MessageMetrics metrics = ctx.attr(ATTR_KEY_METRICS).get();
    m_collector.addMetrics(metrics);/*from w w  w.java  2 s  . c  om*/
    super.close(ctx, promise);
}

From source file:org.eclipse.moquette.server.netty.NettyUtils.java

License:Open Source License

public static Object getAttribute(ChannelHandlerContext channel, AttributeKey<Object> key) {
    Attribute<Object> attr = channel.attr(key);
    return attr.get();
}

From source file:org.eclipse.moquette.server.netty.NettyUtils.java

License:Open Source License

public static void setAttribute(ChannelHandlerContext channel, AttributeKey<Object> key, Object value) {
    Attribute<Object> attr = channel.attr(key);
    attr.set(value);/*  w  w  w .  j a  v  a  2 s. c o m*/
}

From source file:org.eclipse.scada.protocol.relp.FrameCodec.java

License:Open Source License

@Override
public void channelActive(final ChannelHandlerContext ctx) throws Exception {
    ctx.attr(ATTR_STATE).set(State.TXNR);
    ctx.attr(ATTR_TXNR_BUFFER).set(Unpooled.buffer());
    ctx.attr(ATTR_COMMAND_BUFFER).set(Unpooled.buffer());
    ctx.attr(ATTR_LENGTH_BUFFER).set(Unpooled.buffer());
    ctx.attr(ATTR_DATA_BUFFER).set(Unpooled.buffer());
    super.channelActive(ctx);
}

From source file:org.eclipse.scada.protocol.relp.FrameCodec.java

License:Open Source License

@Override
public void channelInactive(final ChannelHandlerContext ctx) throws Exception {
    ctx.attr(ATTR_TXNR_BUFFER).getAndRemove().release();
    ctx.attr(ATTR_COMMAND_BUFFER).getAndRemove().release();
    ctx.attr(ATTR_LENGTH_BUFFER).getAndRemove().release();
    ctx.attr(ATTR_DATA_BUFFER).getAndRemove().release();
    super.channelInactive(ctx);
}

From source file:org.eclipse.scada.protocol.relp.FrameCodec.java

License:Open Source License

private void performRead(final ChannelHandlerContext ctx, final byte b, final ByteBuf msg) {
    switch (ctx.attr(ATTR_STATE).get()) {
    case TXNR:/*  www  .java  2s  .  c o m*/
        processTXNR(ctx, b);
        break;
    case COMMAND:
        processCOMMAND(ctx, b);
        break;
    case LENGTH:
        processLENGTH(ctx, b);
        break;
    case DATA:
        processDATA(ctx, b);
        break;
    case TRAILER:
        processTRAILER(ctx, b, msg);
        break;
    }
}