org.zodiac.netty.http.headers.CookieHeader Maven / Gradle / Ivy
package org.zodiac.netty.http.headers;
import java.util.Objects;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.cookie.ClientCookieDecoder;
import io.netty.handler.codec.http.cookie.ClientCookieEncoder;
import io.netty.handler.codec.http.cookie.Cookie;
final class CookieHeader extends AbstractHeader {
private final boolean strict;
private final ClientCookieEncoder encoder;
private final ClientCookieDecoder decoder;
CookieHeader() {
this(true);
}
CookieHeader(boolean strict) {
super(Cookie[].class, HttpHeaderNames.COOKIE);
this.strict = strict;
this.encoder = this.strict ? ClientCookieEncoder.STRICT : ClientCookieEncoder.LAX;
this.decoder = this.strict ? ClientCookieDecoder.STRICT : ClientCookieDecoder.LAX;
}
@Override
public String toString(Cookie[] value) {
return this.encoder.encode(Objects.requireNonNull(value, "value"));
}
@Override
public Cookie[] toValue(CharSequence value) {
/*Having multiple cookies in a single Set-Cookie header is deprecated, modern browsers only parse the first one.*/
return new Cookie[] {this.decoder.decode(Objects.requireNonNull(value, "value").toString())};
}
}
//@SuppressWarnings("deprecation")
//final class CookieHeader extends AbstractHeader {
//
// CookieHeader() {
// super(io.netty.handler.codec.http.Cookie[].class, HttpHeaderNames.COOKIE);
// }
//
// @Override
// public String toString(io.netty.handler.codec.http.Cookie[] value) {
// return io.netty.handler.codec.http.ClientCookieEncoder.encode(Objects.requireNonNull(value, "value"));
// }
//
// @Override
// public io.netty.handler.codec.http.Cookie[] toValue(CharSequence value) {
// return io.netty.handler.codec.http.CookieDecoder.decode(Objects.requireNonNull(value, "value").toString()).toArray(new io.netty.handler.codec.http.Cookie[0]);
// }
//
//}