Example usage for io.netty.util Attribute get

List of usage examples for io.netty.util Attribute get

Introduction

In this page you can find the example usage for io.netty.util Attribute get.

Prototype

T get();

Source Link

Document

Returns the current value, which may be null

Usage

From source file:com.linecorp.armeria.client.Clients.java

License:Apache License

/**
 * Sets the specified HTTP header manipulating function in a thread-local variable so that the manipulated
 * headers are sent by the client call made from the current thread. Use the `try-with-resources` block
 * with the returned {@link SafeCloseable} to unset the thread-local variable automatically:
 * <pre>{@code//from www.  j  a  va  2  s.c  o m
 * import static com.linecorp.armeria.common.HttpHeaderNames.AUTHORIZATION;
 * import static com.linecorp.armeria.common.HttpHeaderNames.USER_AGENT;
 *
 * try (SafeCloseable ignored = withHttpHeaders(headers -> {
 *     headers.set(HttpHeaders.AUTHORIZATION, myCredential)
 *            .set(HttpHeaders.USER_AGENT, myAgent);
 * })) {
 *     client.executeSomething(..);
 * }
 * }</pre>
 * You can also nest the header manipulation:
 * <pre>{@code
 * import static com.linecorp.armeria.common.HttpHeaderNames.AUTHORIZATION;
 * import static com.linecorp.armeria.common.HttpHeaderNames.USER_AGENT;
 *
 * try (SafeCloseable ignored = withHttpHeaders(h -> h.set(USER_AGENT, myAgent))) {
 *     for (String secret : secrets) {
 *         try (SafeCloseable ignored2 = withHttpHeaders(h -> h.set(AUTHORIZATION, secret))) {
 *             // Both USER_AGENT and AUTHORIZATION will be set.
 *             client.executeSomething(..);
 *         }
 *     }
 * }
 * }</pre>
 *
 * @see #withHttpHeader(AsciiString, String)
 */
public static SafeCloseable withHttpHeaders(Function<HttpHeaders, HttpHeaders> headerManipulator) {
    requireNonNull(headerManipulator, "headerManipulator");
    return withContextCustomizer(ctx -> {
        final Attribute<HttpHeaders> attr = ctx.attr(HTTP_HEADERS);
        final HttpHeaders headers = attr.get();
        attr.set(headerManipulator.apply(headers != null ? headers : new DefaultHttpHeaders()));
    });
}

From source file:com.linecorp.armeria.common.logback.RequestContextExportingAppender.java

License:Apache License

private static State state(RequestContext ctx) {
    final Attribute<State> attr = ctx.attr(STATE);
    final State state = attr.get();
    if (state == null) {
        final State newState = new State();
        final State oldState = attr.setIfAbsent(newState);
        if (oldState != null) {
            return oldState;
        } else {//from   w  w  w . ja v  a2  s. c o m
            return newState;
        }
    }
    return state;
}

From source file:com.linecorp.armeria.internal.DefaultAttributeMap.java

License:Apache License

@Override
public String toString() {
    final ToStringHelper helper = MoreObjects.toStringHelper("");
    for (Iterator<Attribute<?>> i = attrs(); i.hasNext();) {
        final Attribute<?> a = i.next();
        helper.add(a.key().name(), a.get());
    }//from  w ww  .j  a  v a 2 s .co m
    return helper.toString();
}

From source file:com.linecorp.armeria.internal.DefaultAttributeMapTest.java

License:Apache License

@Test
public void testGetSetString() {
    AttributeKey<String> key = AttributeKey.valueOf("Nothing");
    Attribute<String> one = map.attr(key);

    assertSame(one, map.attr(key));/*  w  ww .  j  a  v a  2  s  .c  om*/

    one.setIfAbsent("Whoohoo");
    assertSame("Whoohoo", one.get());

    one.setIfAbsent("What");
    assertNotSame("What", one.get());

    one.remove();
    assertNull(one.get());
}

From source file:com.linecorp.armeria.internal.DefaultAttributeMapTest.java

License:Apache License

@Test
public void testGetSetInt() {
    AttributeKey<Integer> key = AttributeKey.valueOf("Nada");
    Attribute<Integer> one = map.attr(key);

    assertSame(one, map.attr(key));/*w ww  . j  a  va 2 s . c  o m*/

    one.setIfAbsent(3653);
    assertEquals(Integer.valueOf(3653), one.get());

    one.setIfAbsent(1);
    assertNotSame(1, one.get());

    one.remove();
    assertNull(one.get());
}

From source file:com.linecorp.armeria.internal.DefaultAttributeMapTest.java

License:Apache License

@Test
public void testSetRemove() {
    AttributeKey<Integer> key = AttributeKey.valueOf("key");

    Attribute<Integer> attr = map.attr(key);
    attr.set(1);//from   ww w.j  av  a  2s.  co m
    assertSame(1, attr.getAndRemove());

    Attribute<Integer> attr2 = map.attr(key);
    attr2.set(2);
    assertSame(2, attr2.get());
    assertNotSame(attr, attr2);
}

From source file:com.linecorp.armeria.internal.DefaultAttributeMapTest.java

License:Apache License

@Test
public void testGetAndSetWithNull() {
    AttributeKey<Integer> key = AttributeKey.valueOf("key");

    Attribute<Integer> attr = map.attr(key);
    attr.set(1);/*from  ww w . ja v  a  2  s  .  c  o  m*/
    assertSame(1, attr.getAndSet(null));

    Attribute<Integer> attr2 = map.attr(key);
    attr2.set(2);
    assertSame(2, attr2.get());
    assertSame(attr, attr2);
}

From source file:com.linecorp.armeria.internal.logging.DropwizardMetricConsumerTest.java

License:Apache License

@Test
public void testMetricsForHttp() {
    final MetricRegistry metricRegistry = new MetricRegistry();
    final DropwizardMetricConsumer metricConsumer = new DropwizardMetricConsumer(metricRegistry, "foo");

    final RequestContext ctx = mock(RequestContext.class);
    final RequestLog requestLog = mock(RequestLog.class);
    @SuppressWarnings("unchecked")
    final Attribute<HttpHeaders> attribute = (Attribute<HttpHeaders>) mock(Attribute.class);
    when(requestLog.hasAttr(RequestLog.HTTP_HEADERS)).thenReturn(true);
    when(requestLog.attr(RequestLog.HTTP_HEADERS)).thenReturn(attribute);
    when(attribute.get()).thenReturn(new DefaultHttpHeaders().method(HttpMethod.GET).path("/bar"));

    final DummyRequestContext requestContext = new DummyRequestContext();
    metricConsumer.onRequest(requestContext, requestLog);

    assertEquals(1L, metricRegistry.getCounters().get("foo./bar#GET.activeRequests").getCount());

    final ResponseLog responseLog = mock(ResponseLog.class);

    when(requestLog.scheme()).thenReturn(Scheme.parse("none+http"));
    when(requestLog.contentLength()).thenReturn(123L);
    when(requestLog.startTimeNanos()).thenReturn(1L);
    when(responseLog.request()).thenReturn(requestLog);
    when(responseLog.statusCode()).thenReturn(200);
    when(responseLog.contentLength()).thenReturn(456L);
    when(responseLog.endTimeNanos()).thenReturn(13L);
    metricConsumer.onResponse(requestContext, responseLog);

    assertEquals(1L, metricRegistry.getTimers().get("foo./bar#GET.requests").getCount());
    assertEquals(1L, metricRegistry.getMeters().get("foo./bar#GET.successes").getCount());
    assertEquals(0L, metricRegistry.getMeters().get("foo./bar#GET.failures").getCount());
    assertEquals(0L, metricRegistry.getCounters().get("foo./bar#GET.activeRequests").getCount());
    assertEquals(123L, metricRegistry.getMeters().get("foo./bar#GET.requestBytes").getCount());
    assertEquals(456L, metricRegistry.getMeters().get("foo./bar#GET.responseBytes").getCount());
}

From source file:com.linecorp.armeria.internal.logging.DropwizardMetricConsumerTest.java

License:Apache License

@Test
public void testMetricsForRpc() {
    final MetricRegistry metricRegistry = new MetricRegistry();
    final DropwizardMetricConsumer metricConsumer = new DropwizardMetricConsumer(metricRegistry, "foo");

    final RequestContext ctx = mock(RequestContext.class);
    final RequestLog requestLog = mock(RequestLog.class);
    @SuppressWarnings("unchecked")
    final Attribute<RpcRequest> attribute = (Attribute<RpcRequest>) mock(Attribute.class);
    when(requestLog.hasAttr(RequestLog.RPC_REQUEST)).thenReturn(true);
    when(requestLog.attr(RequestLog.RPC_REQUEST)).thenReturn(attribute);
    when(attribute.get()).thenReturn(new DummyRpcRequest("dummy"));

    final DummyRequestContext requestContext = new DummyRequestContext();
    metricConsumer.onRequest(requestContext, requestLog);

    assertEquals(1L, metricRegistry.getCounters().get("foo.dummy.activeRequests").getCount());

    final ResponseLog responseLog = mock(ResponseLog.class);

    when(requestLog.scheme()).thenReturn(Scheme.parse("tbinary+http"));
    when(requestLog.contentLength()).thenReturn(123L);
    when(requestLog.startTimeNanos()).thenReturn(1L);
    when(responseLog.request()).thenReturn(requestLog);
    when(responseLog.statusCode()).thenReturn(200);
    when(responseLog.contentLength()).thenReturn(456L);
    when(responseLog.endTimeNanos()).thenReturn(13L);
    metricConsumer.onResponse(requestContext, responseLog);

    assertEquals(1L, metricRegistry.getTimers().get("foo.dummy.requests").getCount());
    assertEquals(1L, metricRegistry.getMeters().get("foo.dummy.successes").getCount());
    assertEquals(0L, metricRegistry.getMeters().get("foo.dummy.failures").getCount());
    assertEquals(0L, metricRegistry.getCounters().get("foo.dummy.activeRequests").getCount());
    assertEquals(123L, metricRegistry.getMeters().get("foo.dummy.requestBytes").getCount());
    assertEquals(456L, metricRegistry.getMeters().get("foo.dummy.responseBytes").getCount());
}

From source file:com.mastfrog.netty.http.client.MessageHandlerImpl.java

License:Open Source License

private ResponseState state(ChannelHandlerContext ctx, RequestInfo info) {
    Attribute<ResponseState> st = ctx.channel().attr(RS);
    ResponseState rs = st.get();
    if (rs == null) {
        rs = new ResponseState(ctx, info.dontAggregate);
        st.set(rs);//from   ww w.  ja v a2 s .  c om
    }
    return rs;
}