Example usage for io.netty.handler.codec.http DefaultHttpHeaders set

List of usage examples for io.netty.handler.codec.http DefaultHttpHeaders set

Introduction

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

Prototype

@Override
    public HttpHeaders set(CharSequence name, Iterable<?> values) 

Source Link

Usage

From source file:com.github.thinker0.mesos.MesosHealthCheckerServer.java

License:Apache License

/**
 * Writes a HTTP response.//from  ww w .  j av a  2 s.co m
 *
 * @param ctx           The channel context.
 * @param request       The HTTP request.
 * @param status        The HTTP status code.
 * @param buf           The response content buffer.
 * @param contentType   The response content type.
 * @param contentLength The response content length;
 */
private void writeResponse(final ChannelHandlerContext ctx, final FullHttpRequest request,
        final HttpResponseStatus status, final ByteBuf buf, final CharSequence contentType,
        final int contentLength) {

    // Decide whether to close the connection or not.
    final boolean keepAlive = isKeepAlive(request);

    // Build the response object.
    final FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, buf, false);

    final ZonedDateTime dateTime = ZonedDateTime.now();
    final DateTimeFormatter formatter = DateTimeFormatter.RFC_1123_DATE_TIME;

    final DefaultHttpHeaders headers = (DefaultHttpHeaders) response.headers();
    headers.set(HttpHeaderNames.SERVER, SERVER_NAME);
    headers.set(HttpHeaderNames.DATE, dateTime.format(formatter));
    headers.set(HttpHeaderNames.CONTENT_TYPE, contentType);
    headers.set(HttpHeaderNames.CONTENT_LENGTH, Integer.toString(contentLength));

    // Close the non-keep-alive connection after the write operation is done.
    if (!keepAlive) {
        ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
    } else {
        ctx.writeAndFlush(response, ctx.voidPromise());
    }
}

From source file:org.ballerinalang.test.agent.server.WebServer.java

License:Open Source License

/**
 * Writes a HTTP response.//from  w w  w.  ja va 2 s  .  co  m
 *
 * @param ctx The channel context.
 * @param status The HTTP status code.
 * @param buf The response content buffer.
 * @param contentType The response content type.
 * @param contentLength The response content length;
 */
private static void writeResponse(ChannelHandlerContext ctx, HttpResponseStatus status, ByteBuf buf,
        CharSequence contentType, int contentLength) {
    // Build the response object.
    final FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, buf, false);

    final ZonedDateTime dateTime = ZonedDateTime.now();
    final DateTimeFormatter formatter = DateTimeFormatter.RFC_1123_DATE_TIME;

    final DefaultHttpHeaders headers = (DefaultHttpHeaders) response.headers();
    headers.set(HttpHeaderNames.SERVER, SERVER_NAME);
    headers.set(HttpHeaderNames.DATE, dateTime.format(formatter));
    headers.set(HttpHeaderNames.CONTENT_TYPE, contentType);
    headers.set(HttpHeaderNames.CONTENT_LENGTH, Integer.toString(contentLength));

    // Close the non-keep-alive connection after the write operation is done.
    ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}

From source file:org.jooby.internal.netty.NettySse.java

License:Apache License

@Override
protected void handshake(final Runnable handler) throws Exception {
    DefaultHttpHeaders headers = new DefaultHttpHeaders();
    headers.set(HttpHeaderNames.CONNECTION, "Close");
    headers.set(HttpHeaderNames.CONTENT_TYPE, "text/event-stream; charset=utf-8");
    ctx.writeAndFlush(new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, headers));
    ctx.executor().execute(handler);/*  w ww .j  av a2 s.c o  m*/
}