Java Code Examples for io.netty.handler.codec.DateFormatter#format()

The following examples show how to use io.netty.handler.codec.DateFormatter#format() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ClientCookieDecoderTest.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Test
public void testDecodingSingleCookieV0() {
    String cookieString = "myCookie=myValue;expires="
            + DateFormatter.format(new Date(System.currentTimeMillis() + 50000))
            + ";path=/apathsomewhere;domain=.adomainsomewhere;secure;";

    Cookie cookie = ClientCookieDecoder.STRICT.decode(cookieString);
    assertNotNull(cookie);
    assertEquals("myValue", cookie.value());
    assertEquals(".adomainsomewhere", cookie.domain());
    assertNotEquals("maxAge should be defined when parsing cookie " + cookieString,
            Long.MIN_VALUE, cookie.maxAge());
    assertTrue("maxAge should be about 50ms when parsing cookie " + cookieString,
            cookie.maxAge() >= 40 && cookie.maxAge() <= 60);
    assertEquals("/apathsomewhere", cookie.path());
    assertTrue(cookie.isSecure());
}
 
Example 2
Source File: ClientCookieDecoderTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Test
void testDecodingSingleCookieV0() {
    final String cookieString = "myCookie=myValue;expires=" +
                                DateFormatter.format(new Date(System.currentTimeMillis() + 50000)) +
                                ";path=/apathsomewhere;domain=.adomainsomewhere;secure;";

    final Cookie cookie = Cookie.fromSetCookieHeader(cookieString);
    assertThat(cookie).isNotNull();
    assertThat(cookie.value()).isEqualTo("myValue");
    assertThat(cookie.domain()).isEqualTo(".adomainsomewhere");
    assertThat(cookie.maxAge()).withFailMessage("maxAge should be defined when parsing cookie: " +
                                                cookieString)
                               .isNotEqualTo(Cookie.UNDEFINED_MAX_AGE);
    assertThat(cookie.maxAge()).withFailMessage("maxAge should be about 50ms when parsing cookie: " +
                                                cookieString)
                               .isGreaterThanOrEqualTo(40)
                               .isLessThanOrEqualTo(60);
    assertThat(cookie.path()).isEqualTo("/apathsomewhere");
    assertThat(cookie.isSecure()).isTrue();
}
 
Example 3
Source File: DefaultHttpHeaders.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
public CharSequence convertObject(Object value) {
    if (value instanceof CharSequence) {
        return (CharSequence) value;
    }
    if (value instanceof Date) {
        return DateFormatter.format((Date) value);
    }
    if (value instanceof Calendar) {
        return DateFormatter.format(((Calendar) value).getTime());
    }
    return value.toString();
}
 
Example 4
Source File: StringValueConverter.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
@SuppressWarnings("UseOfObsoleteDateTimeApi")
public String convertObject(@Nullable Object value) {
    if (value == null) {
        return null;
    }

    // Try the types that appears more often first.
    if (value instanceof CharSequence ||
        value instanceof Number ||
        value instanceof MediaType) {
        return value.toString();
    }

    if (value instanceof Instant) {
        return DateFormatter.format(new Date(((Instant) value).toEpochMilli()));
    }

    if (value instanceof TemporalAccessor) {
        return DateFormatter.format(new Date(Instant.from((TemporalAccessor) value).toEpochMilli()));
    }

    if (value instanceof CacheControl) {
        return ((CacheControl) value).asHeaderValue();
    }

    // Obsolete types.
    if (value instanceof Date) {
        return DateFormatter.format((Date) value);
    }

    if (value instanceof Calendar) {
        return DateFormatter.format(((Calendar) value).getTime());
    }

    return value.toString();
}
 
Example 5
Source File: StringValueConverter.java    From armeria with Apache License 2.0 4 votes vote down vote up
@Override
public String convertTimeMillis(long value) {
    return DateFormatter.format(new Date(value));
}