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

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

Introduction

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

Prototype

AsciiString EXPIRES

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

Click Source Link

Document

"expires"

Usage

From source file:org.pidome.server.system.network.http.HttpClientHandler.java

License:Apache License

/**
 * Writes the response to the output/* w w  w.  jav a  2s. com*/
 * @param ctx The channel context
 * @param status The response status
 * @param buf The buffer containing the data to send.
 * @param fileType The file type.
 * @param streamId The Stream id (only used in http2)
 * @param cache (if cache headers should be send).
 */
@Override
public final void writeResponse(ChannelHandlerContext ctx, HttpResponseStatus status, byte[] buf,
        String fileType, String streamId, boolean cache) {

    ByteBuf content = ctx.alloc().buffer(buf.length);
    content.writeBytes(buf);
    FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, content);

    HttpUtil.setContentLength(response, response.content().readableBytes());

    // In case of SPDY protocol used.
    if (spdyId != null) {
        response.headers().set(SPDY_STREAM_ID, spdyId);
        response.headers().set(SPDY_STREAM_PRIO, 0);
        response.headers().set(HttpHeaderNames.SERVER, "PiDome integrated 0.2 SPDY");
    } else {
        response.headers().set(HttpHeaderNames.SERVER, "PiDome integrated 0.2 HTTP1.1");
    }

    response.headers().set(HttpHeaderNames.CONTENT_TYPE, HttpRequestHandler.getContentTypeHeader(fileType));
    response.headers().set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN,
            "http" + ((ssl == true) ? "s" : "") + "://" + plainIp + ((port != 80) ? ":" + port : ""));
    response.headers().set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true");

    if (cache == true) {
        DateTime dt = new DateTime();
        HttpHeaderDateFormat dateFormat = HttpHeaderDateFormat.get();
        response.headers().set(HttpHeaderNames.CACHE_CONTROL, "public, max-age=3153600");
        response.headers().set(HttpHeaderNames.EXPIRES, dateFormat.format(dt.plusMonths(12).toDate()));
    } else {
        response.headers().set(HttpHeaderNames.CACHE_CONTROL, "no-cache, must-revalidate");
        response.headers().set(HttpHeaderNames.EXPIRES, "Sat, 26 Jul 1997 05:00:00 GMT");
    }

    if (keepAlive) {
        // Add keep alive header as per:
        // - http://www.w3.org/Protocols/HTTP/1.1/draft-ietf-http-v11-spec-01.html#Connection
        response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);

        ctx.write(response);
    } else {
        // If keep-alive is off, close the connection once the content is fully written.
        ctx.write(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
    }
}

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  .ja va 2 s .  c o  m*/
 * @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())));
}