org.zodiac.sdk.nio.http.common.HttpHeaderCacheControl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of zodiac-sdk-nio Show documentation
Show all versions of zodiac-sdk-nio Show documentation
Zodiac SDK NIO2(New Non-Blocking IO)
package org.zodiac.sdk.nio.http.common;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
import org.zodiac.sdk.util.StringUtil;
public class HttpHeaderCacheControl {
private Duration maxAge;
private boolean noCache = false;
private boolean noStore = false;
private boolean mustRevalidate = false;
private boolean noTransform = false;
private boolean cachePublic = false;
private boolean cachePrivate = false;
private boolean proxyRevalidate = false;
private Duration staleWhileRevalidate;
private Duration staleIfError;
private Duration sMaxAge;
protected HttpHeaderCacheControl() {
}
public static HttpHeaderCacheControl empty() {
return new HttpHeaderCacheControl();
}
public static HttpHeaderCacheControl maxAge(long maxAge, TimeUnit unit) {
return maxAge(Duration.ofSeconds(unit.toSeconds(maxAge)));
}
public static HttpHeaderCacheControl maxAge(Duration maxAge) {
HttpHeaderCacheControl cc = new HttpHeaderCacheControl();
cc.maxAge = maxAge;
return cc;
}
public static HttpHeaderCacheControl noCache() {
HttpHeaderCacheControl cc = new HttpHeaderCacheControl();
cc.noCache = true;
return cc;
}
public static HttpHeaderCacheControl noStore() {
HttpHeaderCacheControl cc = new HttpHeaderCacheControl();
cc.noStore = true;
return cc;
}
public HttpHeaderCacheControl mustRevalidate() {
this.mustRevalidate = true;
return this;
}
public HttpHeaderCacheControl noTransform() {
this.noTransform = true;
return this;
}
public HttpHeaderCacheControl cachePublic() {
this.cachePublic = true;
return this;
}
public HttpHeaderCacheControl cachePrivate() {
this.cachePrivate = true;
return this;
}
public HttpHeaderCacheControl proxyRevalidate() {
this.proxyRevalidate = true;
return this;
}
public HttpHeaderCacheControl sMaxAge(long sMaxAge, TimeUnit unit) {
return sMaxAge(Duration.ofSeconds(unit.toSeconds(sMaxAge)));
}
public HttpHeaderCacheControl sMaxAge(Duration sMaxAge) {
this.sMaxAge = sMaxAge;
return this;
}
public HttpHeaderCacheControl staleWhileRevalidate(long staleWhileRevalidate, TimeUnit unit) {
return staleWhileRevalidate(Duration.ofSeconds(unit.toSeconds(staleWhileRevalidate)));
}
public HttpHeaderCacheControl staleWhileRevalidate(Duration staleWhileRevalidate) {
this.staleWhileRevalidate = staleWhileRevalidate;
return this;
}
public HttpHeaderCacheControl staleIfError(long staleIfError, TimeUnit unit) {
return staleIfError(Duration.ofSeconds(unit.toSeconds(staleIfError)));
}
public HttpHeaderCacheControl staleIfError(Duration staleIfError) {
this.staleIfError = staleIfError;
return this;
}
public String getHeaderValue() {
String headerValue = toHeaderValue();
return (StringUtil.isNotBlank(headerValue) ? headerValue : null);
}
private String toHeaderValue() {
StringBuilder headerValue = new StringBuilder();
if (this.maxAge != null) {
appendDirective(headerValue, "max-age=" + this.maxAge.getSeconds());
}
if (this.noCache) {
appendDirective(headerValue, "no-cache");
}
if (this.noStore) {
appendDirective(headerValue, "no-store");
}
if (this.mustRevalidate) {
appendDirective(headerValue, "must-revalidate");
}
if (this.noTransform) {
appendDirective(headerValue, "no-transform");
}
if (this.cachePublic) {
appendDirective(headerValue, "public");
}
if (this.cachePrivate) {
appendDirective(headerValue, "private");
}
if (this.proxyRevalidate) {
appendDirective(headerValue, "proxy-revalidate");
}
if (this.sMaxAge != null) {
appendDirective(headerValue, "s-maxage=" + this.sMaxAge.getSeconds());
}
if (this.staleIfError != null) {
appendDirective(headerValue, "stale-if-error=" + this.staleIfError.getSeconds());
}
if (this.staleWhileRevalidate != null) {
appendDirective(headerValue, "stale-while-revalidate=" + this.staleWhileRevalidate.getSeconds());
}
return headerValue.toString();
}
private void appendDirective(StringBuilder builder, String value) {
if (builder.length() > 0) {
builder.append(", ");
}
builder.append(value);
}
@Override
public String toString() {
return "CacheControl [" + toHeaderValue() + "]";
}
}