Example usage for io.netty.handler.codec.http HttpHeaderValues APPLICATION_JSON

List of usage examples for io.netty.handler.codec.http HttpHeaderValues APPLICATION_JSON

Introduction

In this page you can find the example usage for io.netty.handler.codec.http HttpHeaderValues APPLICATION_JSON.

Prototype

AsciiString APPLICATION_JSON

To view the source code for io.netty.handler.codec.http HttpHeaderValues APPLICATION_JSON.

Click Source Link

Document

"application/json"

Usage

From source file:com.heliosapm.streams.forwarder.HttpJsonOutboundHandler.java

License:Apache License

/**
 * {@inheritDoc}//from  ww  w .j a  va 2  s  .c  om
 * @see io.netty.handler.codec.MessageToMessageEncoder#encode(io.netty.channel.ChannelHandlerContext, java.lang.Object, java.util.List)
 */
@Override
protected void encode(final ChannelHandlerContext ctx, final ConsumerRecords<String, StreamedMetricValue> msg,
        final List<Object> out) throws Exception {
    final StreamedMetricValue[] smvs = StreamSupport.stream(msg.spliterator(), true)
            .map(new Function<ConsumerRecord<String, StreamedMetricValue>, StreamedMetricValue>() {
                @Override
                public StreamedMetricValue apply(ConsumerRecord<String, StreamedMetricValue> t) {
                    return t.value();
                }

            }).toArray(s -> new StreamedMetricValue[s]);
    final int size = smvs.length;
    final ByteBuf buff = buffManager.buffer(size * 200);

    JSONOps.serializeAndGzip(smvs, buff);
    final int sz = buff.readableBytes();
    final HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, postUri,
            buff);
    request.headers().set(HttpHeaderNames.HOST, host);
    request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
    request.headers().set(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON);
    request.headers().set(HttpHeaderNames.CONTENT_ENCODING, HttpHeaderValues.GZIP);
    request.headers().set(HttpHeaderNames.CONTENT_LENGTH, buff.readableBytes());
    out.add(request);

    ctx.executor().execute(new Runnable() {
        public void run() {
            final NonBlockingHashSet<String> hosts = new NonBlockingHashSet<String>();
            StreamSupport.stream(msg.spliterator(), true)
                    .map(new Function<ConsumerRecord<String, StreamedMetricValue>, String>() {
                        @Override
                        public String apply(ConsumerRecord<String, StreamedMetricValue> t) {
                            return t.value().getTags().get("host");
                        }
                    }).forEach(h -> hosts.add(h));
            log.info("Hosts:{}, Size: {}", hosts, sz);
        }
    });
}

From source file:com.heliosapm.streams.metrichub.HubManager.java

License:Apache License

protected HttpRequest buildHttpRequest(final ByteBuf jsonRequest) {
    final String[] endpoints = tsdbEndpoint.getUpServers();
    final URL postUrl = URLHelper.toURL(endpoints[0] + "/query/");
    log.debug("Http Post to [{}]", postUrl);
    final DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST,
            postUrl.getPath(), jsonRequest);
    request.headers().set(HttpHeaderNames.HOST, postUrl.getHost());
    request.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
    request.headers().set(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.GZIP);
    //      request.headers().set(HttpHeaderNames.CONTENT_ENCODING, HttpHeaderValues.GZIP);
    request.headers().set(HttpHeaderNames.CONTENT_LENGTH, jsonRequest.readableBytes());
    request.headers().set(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON);
    return request;
}

From source file:org.cloudfoundry.reactor.util.JsonCodec.java

License:Apache License

public static HttpClientRequest addDecodeHeaders(HttpClientRequest request) {
    return request.header(HttpHeaderNames.ACCEPT, HttpHeaderValues.APPLICATION_JSON);
}

From source file:org.cloudfoundry.reactor.util.JsonCodec.java

License:Apache License

static Function<Mono<HttpClientRequest>, Publisher<Void>> encode(ObjectMapper objectMapper,
        Object requestPayload) {// ww  w . j  av a 2  s . c o  m
    if (!AnnotationUtils.findAnnotation(requestPayload.getClass(), JsonSerialize.class).isPresent()) {
        return outbound -> outbound.flatMap(HttpClientRequest::send);
    }

    return outbound -> outbound.flatMapMany(request -> {
        try {
            byte[] bytes = objectMapper.writeValueAsBytes(requestPayload);

            return request.header(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON)
                    .header(HttpHeaderNames.CONTENT_LENGTH, String.valueOf(bytes.length))
                    .sendByteArray(Mono.just(bytes));
        } catch (JsonProcessingException e) {
            throw Exceptions.propagate(e);
        }
    });
}

From source file:org.graylog2.inputs.transports.netty.HttpHandlerTest.java

License:Open Source License

@Test
public void withJSONContentType() {
    final FullHttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST,
            "/gelf");
    httpRequest.headers().add(HttpHeaderNames.HOST, "localhost");
    httpRequest.headers().add(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON);
    httpRequest.headers().add(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);

    httpRequest.content().writeBytes(GELF_MESSAGE);

    channel.writeInbound(httpRequest);/*  www.jav  a2  s.c om*/
    channel.finish();

    final HttpResponse httpResponse = channel.readOutbound();
    assertThat(httpResponse.status()).isEqualTo(HttpResponseStatus.ACCEPTED);
    final HttpHeaders headers = httpResponse.headers();
    assertThat(headers.get(HttpHeaderNames.CONTENT_LENGTH)).isEqualTo("0");
    assertThat(headers.get(HttpHeaderNames.CONNECTION)).isEqualTo(HttpHeaderValues.CLOSE.toString());
}