Example usage for io.netty.handler.codec.http HttpHeaderDateFormat get

List of usage examples for io.netty.handler.codec.http HttpHeaderDateFormat get

Introduction

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

Prototype

public static HttpHeaderDateFormat get() 

Source Link

Usage

From source file:com.linecorp.armeria.server.http.file.HttpFileServiceTest.java

License:Apache License

private static String assert200Ok(CloseableHttpResponse res, String expectedContentType, String expectedContent)
        throws Exception {

    assertStatusLine(res, "HTTP/1.1 200 OK");

    // Ensure that the 'Last-Modified' header exists and is well-formed.
    final String lastModified;
    assertThat(res.containsHeader(HttpHeaders.LAST_MODIFIED), is(true));
    lastModified = res.getFirstHeader(HttpHeaders.LAST_MODIFIED).getValue();
    HttpHeaderDateFormat.get().parse(lastModified);

    // Ensure the content and its type are correct.
    assertThat(EntityUtils.toString(res.getEntity()), is(expectedContent));

    assertThat(res.containsHeader(HttpHeaders.CONTENT_TYPE), is(true));
    assertThat(res.getFirstHeader(HttpHeaders.CONTENT_TYPE).getValue(), startsWith(expectedContentType));

    return lastModified;
}

From source file:com.linecorp.armeria.server.http.file.HttpFileServiceTest.java

License:Apache License

private static String currentHttpDate() {
    return HttpHeaderDateFormat.get().format(new Date());
}

From source file:org.asynchttpclient.cookie.CookieDecoderTest.java

License:Open Source License

@Test(groups = "standalone")
public void testDecodingSingleCookieV0() {
    String cookieString = "myCookie=myValue;expires=XXX;path=/apathsomewhere;domain=.adomainsomewhere;secure;";
    cookieString = cookieString.replace("XXX",
            HttpHeaderDateFormat.get().format(new Date(System.currentTimeMillis() + 50000)));

    Cookie cookie = CookieDecoder.decode(cookieString);
    assertNotNull(cookie);//from   w ww.j a  v  a2 s .c  o m
    assertEquals("myValue", cookie.getValue());
    assertEquals(".adomainsomewhere", cookie.getDomain());

    boolean fail = true;
    for (int i = 40; i <= 60; i++) {
        if (cookie.getMaxAge() == i) {
            fail = false;
            break;
        }
    }
    if (fail) {
        fail("expected: 50, actual: " + cookie.getMaxAge());
    }

    assertEquals(cookie.getPath(), "/apathsomewhere");
    assertTrue(cookie.isSecure());
}

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

@Override
public void writeResponse(ChannelHandlerContext ctx, HttpResponseStatus status, byte[] buf, String fileType,
        String streamId, boolean cache) {

    String plainIp = HttpRequestHandler.getPlainIp(ctx.channel().localAddress());

    ByteBuf content = ctx.alloc().buffer(buf.length);
    content.writeBytes(buf);//from   w w  w .  j a v  a2  s  .co m
    FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status, content);
    HttpUtil.setContentLength(response, response.content().readableBytes());
    streamId(response, streamId);

    response.headers().set(HttpHeaderNames.CONTENT_TYPE, fileType);

    response.headers().set(HttpHeaderNames.CONTENT_TYPE, HttpRequestHandler.getContentTypeHeader(fileType));
    response.headers().set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN,
            "https://" + plainIp + ((port != 80) ? ":" + port : ""));
    response.headers().set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true");
    response.headers().set(HttpHeaderNames.SERVER, "PiDome integrated 0.2 HTTP2");

    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");
    }

    ctx.writeAndFlush(response);
}

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

License:Apache License

/**
 * Writes the response to the output//from w ww  . j  a v  a 2  s  . c  om
 * @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);
    }
}