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

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

Introduction

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

Prototype

AsciiString CACHE_CONTROL

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

Click Source Link

Document

"cache-control"

Usage

From source file:com.bunjlabs.fuga.network.netty.NettyHttpServerHandler.java

License:Apache License

private void writeResponse(ChannelHandlerContext ctx, Request request, Response response) {
    HttpResponse httpresponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1,
            HttpResponseStatus.valueOf(response.status()));

    httpresponse.headers().set(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
    httpresponse.headers().set(HttpHeaderNames.CONTENT_TYPE, response.contentType());

    // Disable cache by default
    httpresponse.headers().set(HttpHeaderNames.CACHE_CONTROL, "no-cache, no-store, must-revalidate, max-age=0");
    httpresponse.headers().set(HttpHeaderNames.PRAGMA, "no-cache");
    httpresponse.headers().set(HttpHeaderNames.EXPIRES, "0");

    response.headers().entrySet().stream().forEach((e) -> httpresponse.headers().set(e.getKey(), e.getValue()));

    httpresponse.headers().set(HttpHeaderNames.SERVER, "Fuga Netty Web Server/" + serverVersion);

    // Set cookies
    httpresponse.headers().set(HttpHeaderNames.SET_COOKIE,
            ServerCookieEncoder.STRICT.encode(NettyCookieConverter.convertListToNetty(response.cookies())));

    if (response.length() >= 0) {
        httpresponse.headers().set(HttpHeaderNames.CONTENT_LENGTH, response.length());
    }//ww  w  .j a  v a 2s. c  o m

    if (HttpUtil.isKeepAlive(httprequest)) {
        httpresponse.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
    } else {
        httpresponse.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
    }

    ctx.write(httpresponse);

    if (response.stream() != null) {
        ctx.write(new HttpChunkedInput(new ChunkedStream(response.stream())));
    }

    LastHttpContent fs = new DefaultLastHttpContent();
    ChannelFuture sendContentFuture = ctx.writeAndFlush(fs);
    if (!HttpUtil.isKeepAlive(httprequest)) {
        sendContentFuture.addListener(ChannelFutureListener.CLOSE);
    }
}

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  w w  w .j  ava  2s  .  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.groupon.vertx.utils.HealthcheckHandler.java

License:Apache License

private void setCommonHttpResponse(HttpServerRequest request, HttpResponseStatus status) {
    request.response().putHeader(HttpHeaderNames.CONTENT_TYPE, CONTENT_TYPE);
    request.response().putHeader(HttpHeaderNames.CACHE_CONTROL, CACHE_CONTROL);
    request.response().setStatusCode(status.code());
    request.response().setStatusMessage(status.reasonPhrase());
}

From source file:com.groupon.vertx.utils.HealthcheckHandlerTest.java

License:Apache License

@Test
public void testHandle() {
    stub(existsResult.result()).toReturn(true);
    handler.handle(request);//from   w ww. j a  v a  2s  . c o  m

    verify(vertx, times(1)).fileSystem();

    verify(fileSystem, times(1)).exists(eq("filepath"), existCaptor.capture());

    existCaptor.getValue().handle(existsResult);

    verify(response, times(1)).putHeader(HttpHeaderNames.CONTENT_TYPE, CONTENT_TYPE);
    verify(response, times(1)).putHeader(HttpHeaderNames.CACHE_CONTROL, CACHE_CONTROL);
    verify(response, times(1)).setStatusCode(OK.code());
    verify(response, times(1)).setStatusMessage(OK.reasonPhrase());
    verify(response, times(1)).end(OK.reasonPhrase());
}

From source file:com.groupon.vertx.utils.HealthcheckHandlerTest.java

License:Apache License

@Test
public void testSyncHandle() {
    handler = new SyncHealthcheckHandler(vertx, "filepath");
    stub(fileSystem.existsBlocking(eq("filepath"))).toReturn(true);
    stub(vertx.fileSystem()).toReturn(fileSystem);

    handler.handle(request);//from  w  ww  . j a v  a 2s  .  c om
    verify(vertx, times(1)).fileSystem();
    verify(fileSystem, times(1)).existsBlocking(eq("filepath"));

    verify(response, times(1)).putHeader(HttpHeaderNames.CONTENT_TYPE, CONTENT_TYPE);
    verify(response, times(1)).putHeader(HttpHeaderNames.CACHE_CONTROL, CACHE_CONTROL);
    verify(response, times(1)).setStatusCode(OK.code());
    verify(response, times(1)).setStatusMessage(OK.reasonPhrase());
    verify(response, times(1)).end(OK.reasonPhrase());
}

From source file:com.groupon.vertx.utils.HealthcheckHandlerTest.java

License:Apache License

@Test
public void testHandleNotExists() {
    stub(existsResult.result()).toReturn(false);
    handler.handle(request);/*from  w w  w  .  j a  v a  2  s.  c o  m*/

    verify(vertx, times(1)).fileSystem();

    verify(fileSystem, times(1)).exists(eq("filepath"), existCaptor.capture());

    existCaptor.getValue().handle(existsResult);

    verify(response, times(1)).putHeader(HttpHeaderNames.CONTENT_TYPE, CONTENT_TYPE);
    verify(response, times(1)).putHeader(HttpHeaderNames.CACHE_CONTROL, CACHE_CONTROL);
    verify(response, times(1)).setStatusCode(SERVICE_UNAVAILABLE.code());
    verify(response, times(1)).setStatusMessage(SERVICE_UNAVAILABLE.reasonPhrase());
    verify(response, times(1)).end(SERVICE_UNAVAILABLE.reasonPhrase());
}

From source file:com.groupon.vertx.utils.HealthcheckHandlerTest.java

License:Apache License

@Test
public void testSyncHandleNotExists() {
    handler = new SyncHealthcheckHandler(vertx, "filepath");
    stub(fileSystem.existsBlocking(eq("filepath"))).toReturn(false);
    stub(vertx.fileSystem()).toReturn(fileSystem);

    handler.handle(request);//from  w  ww. j  a  v  a 2s .c  o m

    verify(vertx, times(1)).fileSystem();

    verify(fileSystem, times(1)).existsBlocking(eq("filepath"));

    verify(response, times(1)).putHeader(HttpHeaderNames.CONTENT_TYPE, CONTENT_TYPE);
    verify(response, times(1)).putHeader(HttpHeaderNames.CACHE_CONTROL, CACHE_CONTROL);
    verify(response, times(1)).setStatusCode(SERVICE_UNAVAILABLE.code());
    verify(response, times(1)).setStatusMessage(SERVICE_UNAVAILABLE.reasonPhrase());
    verify(response, times(1)).end(SERVICE_UNAVAILABLE.reasonPhrase());
}

From source file:com.groupon.vertx.utils.HealthcheckHandlerTest.java

License:Apache License

@Test
public void testHandleExsistsException() {
    IllegalArgumentException exception = new IllegalArgumentException("Failed");

    doThrow(exception).when(fileSystem).exists(eq("filepath"), existCaptor.capture());

    handler.handle(request);//from  w w w.j a  va  2 s.  c o m

    verify(vertx, times(1)).fileSystem();
    verify(fileSystem, times(1)).exists(eq("filepath"), existCaptor.capture());

    verify(response, times(1)).putHeader(HttpHeaderNames.CONTENT_TYPE, CONTENT_TYPE);
    verify(response, times(1)).putHeader(HttpHeaderNames.CACHE_CONTROL, CACHE_CONTROL);
    verify(response, times(1)).setStatusCode(SERVICE_UNAVAILABLE.code());
    verify(response, times(1)).setStatusMessage(SERVICE_UNAVAILABLE.reasonPhrase());
    verify(response, times(1)).end(SERVICE_UNAVAILABLE.reasonPhrase() + ": " + exception.getMessage());
}

From source file:com.groupon.vertx.utils.HealthcheckHandlerTest.java

License:Apache License

@Test
public void testSyncHandleExsistsException() {
    IllegalArgumentException exception = new IllegalArgumentException("Failed");

    doThrow(exception).when(fileSystem).existsBlocking(eq("filepath"));

    handler = new SyncHealthcheckHandler(vertx, "filepath");
    stub(vertx.fileSystem()).toReturn(fileSystem);

    handler.handle(request);/* w w  w  .ja  v  a 2  s .c o  m*/

    verify(vertx, times(1)).fileSystem();
    verify(fileSystem, times(1)).existsBlocking(eq("filepath"));

    verify(response, times(1)).putHeader(HttpHeaderNames.CONTENT_TYPE, CONTENT_TYPE);
    verify(response, times(1)).putHeader(HttpHeaderNames.CACHE_CONTROL, CACHE_CONTROL);
    verify(response, times(1)).setStatusCode(SERVICE_UNAVAILABLE.code());
    verify(response, times(1)).setStatusMessage(SERVICE_UNAVAILABLE.reasonPhrase());
    verify(response, times(1)).end(SERVICE_UNAVAILABLE.reasonPhrase() + ": " + exception.getMessage());
}

From source file:com.groupon.vertx.utils.HealthcheckHandlerTest.java

License:Apache License

@Test
public void testHandleHead() {
    stub(request.method()).toReturn(HttpMethod.HEAD);
    stub(existsResult.result()).toReturn(true);
    handler.handle(request);//from w w  w .j  a  v  a 2 s.c o  m

    verify(vertx, times(1)).fileSystem();

    verify(fileSystem, times(1)).exists(eq("filepath"), existCaptor.capture());

    existCaptor.getValue().handle(existsResult);

    verify(response, times(1)).putHeader(HttpHeaderNames.CONTENT_TYPE, CONTENT_TYPE);
    verify(response, times(1)).putHeader(HttpHeaderNames.CACHE_CONTROL, CACHE_CONTROL);
    verify(response, times(1)).putHeader(HttpHeaderNames.CONTENT_LENGTH, "" + OK.reasonPhrase().length());
    verify(response, times(1)).setStatusCode(OK.code());
    verify(response, times(1)).setStatusMessage(OK.reasonPhrase());
    verify(response, times(1)).end();
}