Example usage for io.netty.handler.codec.http HttpHeaderNames DATE

List of usage examples for io.netty.handler.codec.http HttpHeaderNames DATE

Introduction

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

Prototype

AsciiString DATE

To view the source code for io.netty.handler.codec.http HttpHeaderNames DATE.

Click Source Link

Document

"date"

Usage

From source file:com.cmz.http.file.HttpStaticFileServerHandler.java

License:Apache License

/**
 * Sets the Date header for the HTTP response
 *
 * @param response//from   w w  w  .j a  v  a  2 s.c  o m
 *            HTTP response
 */
private static void setDateHeader(FullHttpResponse response) {
    SimpleDateFormat dateFormatter = new SimpleDateFormat(HTTP_DATE_FORMAT, Locale.US);
    dateFormatter.setTimeZone(TimeZone.getTimeZone(HTTP_DATE_GMT_TIMEZONE));

    Calendar time = new GregorianCalendar();
    response.headers().set(HttpHeaderNames.DATE, dateFormatter.format(time.getTime()));
}

From source file:com.cmz.http.file.HttpStaticFileServerHandler.java

License:Apache License

/**
 * Sets the Date and Cache headers for the HTTP Response
 *
 * @param response//from  ww w  .j  a v a  2 s.co m
 *            HTTP response
 * @param fileToCache
 *            file to extract content type
 */
private static void setDateAndCacheHeaders(HttpResponse response, File fileToCache) {
    SimpleDateFormat dateFormatter = new SimpleDateFormat(HTTP_DATE_FORMAT, Locale.US);
    dateFormatter.setTimeZone(TimeZone.getTimeZone(HTTP_DATE_GMT_TIMEZONE));

    // Date header
    Calendar time = new GregorianCalendar();
    response.headers().set(HttpHeaderNames.DATE, dateFormatter.format(time.getTime()));

    // Add cache headers
    time.add(Calendar.SECOND, HTTP_CACHE_SECONDS);
    response.headers().set(HttpHeaderNames.EXPIRES, dateFormatter.format(time.getTime()));
    response.headers().set(HttpHeaderNames.CACHE_CONTROL, "private, max-age=" + HTTP_CACHE_SECONDS);
    response.headers().set(HttpHeaderNames.LAST_MODIFIED,
            dateFormatter.format(new Date(fileToCache.lastModified())));
}

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

License:Apache License

/**
 * Writes a HTTP response.//from w  ww. j  ava 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:com.ociweb.pronghorn.adapter.netty.impl.HttpStaticFileServerHandler.java

License:Apache License

/**
 * Sets the Date and Cache headers for the HTTP Response
 *
 * @param response/* w  ww.j a  va2  s .c o  m*/
 *            HTTP response
 * @param fileToCache
 *            file to extract content type
 */
private static void setDateAndCacheHeaders(HttpResponse response, long lastModified) {
    SimpleDateFormat dateFormatter = new SimpleDateFormat(HTTP_DATE_FORMAT, Locale.US);
    dateFormatter.setTimeZone(TimeZone.getTimeZone(HTTP_DATE_GMT_TIMEZONE));

    // Date header
    Calendar time = new GregorianCalendar();
    response.headers().set(HttpHeaderNames.DATE, dateFormatter.format(time.getTime()));

    // Add cache headers
    time.add(Calendar.SECOND, HTTP_CACHE_SECONDS);
    response.headers().set(HttpHeaderNames.EXPIRES, dateFormatter.format(time.getTime()));
    response.headers().set(HttpHeaderNames.CACHE_CONTROL, "private, max-age=" + HTTP_CACHE_SECONDS);
    response.headers().set(HttpHeaderNames.LAST_MODIFIED, dateFormatter.format(new Date(lastModified)));
}

From source file:dpfmanager.shell.modules.server.get.HttpGetHandler.java

License:Open Source License

private void setDateAndCacheHeaders(HttpResponse response, File fileToCache) {
    SimpleDateFormat dateFormatter = new SimpleDateFormat(HTTP_DATE_FORMAT, Locale.US);
    dateFormatter.setTimeZone(TimeZone.getTimeZone(HTTP_DATE_GMT_TIMEZONE));

    // Date header
    Calendar time = new GregorianCalendar();
    response.headers().set(HttpHeaderNames.DATE, dateFormatter.format(time.getTime()));

    // Add cache headers
    time.add(Calendar.SECOND, HTTP_CACHE_SECONDS);
    response.headers().set(HttpHeaderNames.EXPIRES, dateFormatter.format(time.getTime()));
    response.headers().set(HttpHeaderNames.CACHE_CONTROL, "private, max-age=" + HTTP_CACHE_SECONDS);
    response.headers().set(HttpHeaderNames.LAST_MODIFIED,
            dateFormatter.format(new Date(fileToCache.lastModified())));
}

From source file:fr.kissy.zergling_push.infrastructure.HttpStaticFileServerHandler.java

License:Apache License

/**
* Sets the Date and Cache headers for the HTTP Response
*
* @param response//from   ww w  . j  a v a 2  s . c o  m
*            HTTP response
* @param fileToCache
*            file to extract content type
*/
private static void setDateAndCacheHeaders(HttpResponse response, File fileToCache) {
    SimpleDateFormat dateFormatter = new SimpleDateFormat(HTTP_DATE_FORMAT, Locale.US);
    dateFormatter.setTimeZone(TimeZone.getTimeZone(HTTP_DATE_GMT_TIMEZONE));

    // Date header
    Calendar time = new GregorianCalendar();
    response.headers().set(HttpHeaderNames.DATE, dateFormatter.format(time.getTime()));

    // Add cache headers
    response.headers().set(HttpHeaderNames.EXPIRES, dateFormatter.format(time.getTime()));
    response.headers().set(HttpHeaderNames.CACHE_CONTROL, "no-store");
    response.headers().set(HttpHeaderNames.LAST_MODIFIED,
            dateFormatter.format(new Date(fileToCache.lastModified())));
}

From source file:io.vertx.benchmarks.HttpServerHandlerBenchmark.java

License:Open Source License

@Setup
public void setup() {
    vertx = (VertxInternal) Vertx.vertx();
    HttpServerOptions options = new HttpServerOptions();
    vertxChannel = new EmbeddedChannel(
            new HttpRequestDecoder(options.getMaxInitialLineLength(), options.getMaxHeaderSize(),
                    options.getMaxChunkSize(), false, options.getDecoderInitialBufferSize()),
            new HttpResponseEncoder());
    vertxChannel.config().setAllocator(new Alloc());

    ContextInternal context = new EventLoopContext(vertx, vertxChannel.eventLoop(), null, null, null,
            new JsonObject(), Thread.currentThread().getContextClassLoader());
    Handler<HttpServerRequest> app = request -> {
        HttpServerResponse response = request.response();
        MultiMap headers = response.headers();
        headers.add(HEADER_CONTENT_TYPE, RESPONSE_TYPE_PLAIN).add(HEADER_SERVER, SERVER)
                .add(HEADER_DATE, DATE_STRING).add(HEADER_CONTENT_LENGTH, HELLO_WORLD_LENGTH);
        response.end(HELLO_WORLD_BUFFER);
    };// www.  j av  a  2 s.com
    HandlerHolder<HttpHandlers> holder = new HandlerHolder<>(context, new HttpHandlers(app, null, null, null));
    Http1xServerHandler handler = new Http1xServerHandler(null, new HttpServerOptions(), "localhost", holder,
            null);
    vertxChannel.pipeline().addLast("handler", handler);

    nettyChannel = new EmbeddedChannel(
            new HttpRequestDecoder(options.getMaxInitialLineLength(), options.getMaxHeaderSize(),
                    options.getMaxChunkSize(), false, options.getDecoderInitialBufferSize()),
            new HttpResponseEncoder(), new SimpleChannelInboundHandler<HttpRequest>() {

                private final byte[] STATIC_PLAINTEXT = "Hello, World!".getBytes(CharsetUtil.UTF_8);
                private final int STATIC_PLAINTEXT_LEN = STATIC_PLAINTEXT.length;
                private final ByteBuf PLAINTEXT_CONTENT_BUFFER = Unpooled
                        .unreleasableBuffer(Unpooled.directBuffer().writeBytes(STATIC_PLAINTEXT));
                private final CharSequence PLAINTEXT_CLHEADER_VALUE = new AsciiString(
                        String.valueOf(STATIC_PLAINTEXT_LEN));

                private final CharSequence TYPE_PLAIN = new AsciiString("text/plain");
                private final CharSequence SERVER_NAME = new AsciiString("Netty");
                private final CharSequence CONTENT_TYPE_ENTITY = HttpHeaderNames.CONTENT_TYPE;
                private final CharSequence DATE_ENTITY = HttpHeaderNames.DATE;
                private final CharSequence CONTENT_LENGTH_ENTITY = HttpHeaderNames.CONTENT_LENGTH;
                private final CharSequence SERVER_ENTITY = HttpHeaderNames.SERVER;

                private final DateFormat FORMAT = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z");
                private final CharSequence date = new AsciiString(FORMAT.format(new Date()));

                @Override
                protected void channelRead0(ChannelHandlerContext ctx, HttpRequest msg) throws Exception {
                    writeResponse(ctx, msg, PLAINTEXT_CONTENT_BUFFER.duplicate(), TYPE_PLAIN,
                            PLAINTEXT_CLHEADER_VALUE);
                }

                @Override
                public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
                    ctx.flush();
                }

                private void writeResponse(ChannelHandlerContext ctx, HttpRequest request, ByteBuf buf,
                        CharSequence contentType, CharSequence contentLength) {

                    // Build the response object.
                    FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
                            HttpResponseStatus.OK, buf, false);
                    HttpHeaders headers = response.headers();
                    headers.set(CONTENT_TYPE_ENTITY, contentType);
                    headers.set(SERVER_ENTITY, SERVER_NAME);
                    headers.set(DATE_ENTITY, date);
                    headers.set(CONTENT_LENGTH_ENTITY, contentLength);

                    // Close the non-keep-alive connection after the write operation is done.
                    ctx.write(response, ctx.voidPromise());
                }
            });
    nettyChannel.config().setAllocator(new Alloc());

    GET = Unpooled.unreleasableBuffer(Unpooled.copiedBuffer(("GET / HTTP/1.1\r\n" + "\r\n").getBytes()));
    readerIndex = GET.readerIndex();
    writeIndex = GET.writerIndex();
}

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

License:Open Source License

/**
 * Writes a HTTP response.//  www . ja v a 2  s . c o  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.thingsplode.synapse.endpoint.handlers.FileRequestHandler.java

License:Apache License

/**
 * Sets the Date header for the HTTP response
 *
 * @param response HTTP response//from  w ww  .  j av  a  2  s .co m
 */
public void setDateHeader(FullHttpResponse response) {
    SimpleDateFormat dateFormatter = new SimpleDateFormat(HTTP_DATE_FORMAT, Locale.US);
    dateFormatter.setTimeZone(TimeZone.getTimeZone(HTTP_DATE_GMT_TIMEZONE));

    Calendar time = new GregorianCalendar();
    response.headers().set(HttpHeaderNames.DATE, dateFormatter.format(time.getTime()));
}

From source file:org.thingsplode.synapse.endpoint.handlers.FileRequestHandler.java

License:Apache License

/**
 * Sets the Date and Cache headers for the HTTP Response
 *
 * @param response HTTP response//from  w  w w .  j a  va 2  s  .  c om
 * @param fileToCache file to extract content type
 */
public void setDateAndCacheHeaders(HttpResponse response, File fileToCache) {
    SimpleDateFormat dateFormatter = new SimpleDateFormat(HTTP_DATE_FORMAT, Locale.US);
    dateFormatter.setTimeZone(TimeZone.getTimeZone(HTTP_DATE_GMT_TIMEZONE));

    // Date header
    Calendar time = new GregorianCalendar();
    response.headers().set(HttpHeaderNames.DATE, dateFormatter.format(time.getTime()));

    // Add cache headers
    time.add(Calendar.SECOND, HTTP_CACHE_SECONDS);
    response.headers().set(HttpHeaderNames.EXPIRES, dateFormatter.format(time.getTime()));
    response.headers().set(HttpHeaderNames.CACHE_CONTROL, "private, max-age=" + HTTP_CACHE_SECONDS);
    response.headers().set(HttpHeaderNames.LAST_MODIFIED,
            dateFormatter.format(new Date(fileToCache.lastModified())));
}