Search in sources :

Example 1 with ResponseHeadersBuilder

use of com.linecorp.armeria.common.ResponseHeadersBuilder in project zipkin by openzipkin.

the class ZipkinQueryApiV2 method maybeCacheNames.

/**
 * We cache names if there are more than 3 names. This helps people getting started: if we cache
 * empty results, users have more questions. We assume caching becomes a concern when zipkin is in
 * active use, and active use usually implies more than 3 services.
 */
AggregatedHttpResponse maybeCacheNames(boolean shouldCacheControl, List<String> values, ByteBufAllocator alloc) {
    Collections.sort(values);
    // Two brackets.
    int sizeEstimate = 2;
    for (String value : values) {
        sizeEstimate += value.length() + 1;
    }
    // Last element doesn't have a comma.
    sizeEstimate -= 1;
    // If the values don't require escaping, this buffer will not be resized.
    ByteBuf buf = alloc.buffer(sizeEstimate);
    try (JsonGenerator gen = JsonUtil.JSON_FACTORY.createGenerator((OutputStream) new ByteBufOutputStream(buf))) {
        gen.writeStartArray(values.size());
        for (String value : values) {
            gen.writeString(value);
        }
        gen.writeEndArray();
    } catch (IOException e) {
        buf.release();
        throw new UncheckedIOException(e);
    }
    ResponseHeadersBuilder headers = ResponseHeaders.builder(200).contentType(MediaType.JSON).setInt(HttpHeaderNames.CONTENT_LENGTH, buf.readableBytes());
    if (shouldCacheControl) {
        headers = headers.add(CACHE_CONTROL, "max-age=" + namesMaxAge + ", must-revalidate");
    }
    return AggregatedHttpResponse.of(headers.build(), HttpData.wrap(buf));
}
Also used : ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) ResponseHeadersBuilder(com.linecorp.armeria.common.ResponseHeadersBuilder) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 ResponseHeadersBuilder (com.linecorp.armeria.common.ResponseHeadersBuilder)1 ByteBuf (io.netty.buffer.ByteBuf)1 ByteBufOutputStream (io.netty.buffer.ByteBufOutputStream)1 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1