All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zodiac.netty.http.headers.Headers Maven / Gradle / Ivy

package org.zodiac.netty.http.headers;

import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpMessage;
import io.netty.handler.codec.http.cookie.Cookie;
import io.netty.util.AsciiString;
import java.net.URI;
import java.net.URL;
import java.nio.charset.Charset;
import java.time.Duration;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.SignStyle;
import java.time.format.TextStyle;
import java.time.temporal.ChronoField;
import java.util.Locale;
import java.util.Objects;

import org.zodiac.commons.http.entities.BasicCredentials;
import org.zodiac.commons.http.entities.CacheControl;
import org.zodiac.commons.http.entities.ConnectionHeaderData;
import org.zodiac.commons.http.entities.FrameOptions;
import org.zodiac.commons.http.entities.StrictTransportSecurity;
import org.zodiac.netty.http.base.Realm;
import org.zodiac.sdk.mime.MimeType;

/**
 * A collection of standard HTTP headers and objects that convert between them
 * and actual header strings and vice versa. Typical usage would be something
 * like:
 * 
 * response.setHeader(Headers.LAST_MODIFIED, new ZonedDateTime());
 * 
This handles conversion between header strings and Java objects * without creating a class which dicatates what headers should be or what they * should look like. * */ public final class Headers { private Headers() { } public static final DateTimeFormatter ISO2822DateFormat = new DateTimeFormatterBuilder() .appendText(ChronoField.DAY_OF_WEEK, TextStyle.SHORT_STANDALONE).appendLiteral(", ") .appendText(ChronoField.DAY_OF_MONTH, TextStyle.FULL).appendLiteral(" ") .appendText(ChronoField.MONTH_OF_YEAR, TextStyle.SHORT).appendLiteral(" ") .appendText(ChronoField.YEAR, TextStyle.FULL).appendLiteral(" ") .appendValue(ChronoField.HOUR_OF_DAY, 2).appendLiteral(":") .appendValue(ChronoField.MINUTE_OF_HOUR, 2).appendLiteral(":") .appendValue(ChronoField.SECOND_OF_MINUTE, 2).appendLiteral(" ") .appendZoneOrOffsetId().toFormatter(); public static final DateTimeFormatter ISO2822DateFormatForOutput = new DateTimeFormatterBuilder() .appendText(ChronoField.DAY_OF_WEEK, TextStyle.SHORT_STANDALONE).appendLiteral(", ") .appendText(ChronoField.DAY_OF_MONTH, TextStyle.FULL).appendLiteral(" ") .appendText(ChronoField.MONTH_OF_YEAR, TextStyle.SHORT).appendLiteral(" ") .appendText(ChronoField.YEAR, TextStyle.FULL).appendLiteral(" ") .appendValue(ChronoField.HOUR_OF_DAY, 2).appendLiteral(":") .appendValue(ChronoField.MINUTE_OF_HOUR, 2).appendLiteral(":") .appendValue(ChronoField.SECOND_OF_MINUTE, 2).appendLiteral(" ") .appendOffsetId().toFormatter(); static final DateTimeFormatter TWO_DIGIT_YEAR = new DateTimeFormatterBuilder() // .appendText(ChronoField.DAY_OF_WEEK, TextStyle.SHORT_STANDALONE).appendLiteral(", ") .appendText(ChronoField.DAY_OF_MONTH, TextStyle.FULL).appendLiteral(" ") .appendText(ChronoField.MONTH_OF_YEAR, TextStyle.SHORT).appendLiteral(" ") .appendValue(ChronoField.YEAR, 2, 4, SignStyle.NEVER).appendLiteral(" ") .appendValue(ChronoField.HOUR_OF_DAY, 2).appendLiteral(":") .appendValue(ChronoField.MINUTE_OF_HOUR, 2).appendLiteral(":") .appendValue(ChronoField.SECOND_OF_MINUTE, 2).appendLiteral(" ") .appendZoneOrOffsetId().toFormatter(); static final ZoneId UTC = ZoneId.of("GMT"); public static final HeaderValueType EXPECT = header(HttpHeaderNames.EXPECT); public static final TimestampHeader DATE = new DateTimeHeader(HttpHeaderNames.DATE); public static final TimestampHeader LAST_MODIFIED = new DateTimeHeader(HttpHeaderNames.LAST_MODIFIED); public static final TimestampHeader EXPIRES = new DateTimeHeader(HttpHeaderNames.EXPIRES); public static final TimestampHeader IF_MODIFIED_SINCE = new DateTimeHeader(HttpHeaderNames.IF_MODIFIED_SINCE); public static final TimestampHeader IF_UNMODIFIED_SINCE = new DateTimeHeader(HttpHeaderNames.IF_UNMODIFIED_SINCE); public static final TimestampHeader RETRY_AFTER_DATE = new DateTimeHeader(HttpHeaderNames.RETRY_AFTER); public static final HeaderValueType RETRY_AFTER_DURATION = new DurationHeader(HttpHeaderNames.RETRY_AFTER); public static final HeaderValueType HOST = header(HttpHeaderNames.HOST); public static final HeaderValueType CONTENT_TYPE = new MimeTypeHeader(); public static final HeaderValueType SERVER = header(HttpHeaderNames.SERVER); public static final HeaderValueType VARY = new VaryHeader(); public static final HeaderValueType RANGE = new ByteRangeHeader(HttpHeaderNames.RANGE); public static final HeaderValueType CONTENT_RANGE = new ContentRangeHeader(HttpHeaderNames.CONTENT_RANGE); public static final HeaderValueType ACCEPT = header(HttpHeaderNames.ACCEPT); public static final HeaderValueType ACCEPT_ENCODING = header(HttpHeaderNames.ACCEPT_ENCODING); public static final HeaderValueType ACCEPT_RANGES = header(HttpHeaderNames.ACCEPT_RANGES); public static final HeaderValueType CONTENT_ENCODING = header(HttpHeaderNames.CONTENT_ENCODING); public static final HeaderValueType USER_AGENT = header(HttpHeaderNames.USER_AGENT); public static final HeaderValueType CONNECTION = new ConnectionHeader(); public static final HeaderValueType CONTENT_LENGTH = new NumberHeader(HttpHeaderNames.CONTENT_LENGTH); public static final HeaderValueType CONTENT_LOCATION = new UriHeader(HttpHeaderNames.CONTENT_LOCATION); public static final HeaderValueType LOCATION = new UriHeader(HttpHeaderNames.LOCATION); public static final HeaderValueType ACCEPT_CHARSET = new CharsetHeader(HttpHeaderNames.ACCEPT_CHARSET); public static final HeaderValueType CONTENT_LANGUAGE = new LocaleHeader(HttpHeaderNames.CONTENT_LANGUAGE); public static final HeaderValueType ETAG = new ETagHeader(HttpHeaderNames.ETAG); public static final HeaderValueType IF_NONE_MATCH = new ETagHeader(HttpHeaderNames.IF_NONE_MATCH); public static final HeaderValueType IF_MATCH = new ETagHeader(HttpHeaderNames.IF_MATCH); public static final HeaderValueType AGE = new DurationHeader(HttpHeaderNames.AGE); public static final HeaderValueType RETRY_AFTER = new DurationHeader(HttpHeaderNames.RETRY_AFTER); public static final HeaderValueType AUTHORIZATION = new BasicCredentialsHeader(); public static final HeaderValueType CACHE_CONTROL = new CacheControlHeader(); public static final HeaderValueType PRAGMA = new PragmaHeader(); public static final HeaderValueType WWW_AUTHENTICATE = new AuthHeader(); public static final HeaderValueType ALLOW = new AllowHeader(false); public static final HeaderValueType ACCESS_CONTROL_ALLOW = new AllowHeader(true); public static final HeaderValueType ACCESS_CONTROL_ALLOW_ORIGIN = new StringArrayHeader(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN); public static final HeaderValueType ACCESS_CONTROL_ALLOW_MAX_AGE = new NumberHeader(HttpHeaderNames.ACCESS_CONTROL_MAX_AGE); public static final HeaderValueType[]> ACCESS_CONTROL_ALLOW_HEADERS = new HeaderNamesHeader(HttpHeaderNames.ACCESS_CONTROL_ALLOW_HEADERS); public static final HeaderValueType[]> ACCESS_CONTROL_EXPOSE_HEADERS = new HeaderNamesHeader(HttpHeaderNames.ACCESS_CONTROL_EXPOSE_HEADERS); public static final HeaderValueType ACCESS_CONTROL_ALLOW_CREDENTIALS = new BooleanHeader(HttpHeaderNames.ACCESS_CONTROL_ALLOW_CREDENTIALS); public static final HeaderValueType X_REQUESTED_WITH = header(new AsciiString("x-requested-with")); public static final HeaderValueType X_FORWARDED_PROTO = header(new AsciiString("x-forwarded-proto")); @Deprecated public static final HeaderValueType SET_COOKIE = new SetCookieHeader(); public static final HeaderValueType SET_COOKIE_B = new SetCookieHeaderNetty428(HttpHeaderNames.SET_COOKIE, false); public static final HeaderValueType SET_COOKIE_B_STRICT = new SetCookieHeaderNetty428(HttpHeaderNames.SET_COOKIE, false); @Deprecated public static final HeaderValueType COOKIE = new CookieHeader(); public static final HeaderValueType COOKIE_B = new CookieHeaderNetty428(false); public static final HeaderValueType COOKIE_B_STRICT = new CookieHeaderNetty428(true); public static final HeaderValueType WEBSOCKET_PROTOCOLS = new WebSocketProtocolsHeader(); public static final HeaderValueType WEBSOCKET_PROTOCOL = new StringHeader(HttpHeaderNames.WEBSOCKET_PROTOCOL); public static final HeaderValueType WEBSOCKET_LOCATION = new WebSocketLocationHeader(); public static final HeaderValueType UPGRADE = header(HttpHeaderNames.UPGRADE); public static final HeaderValueType CONTENT_MD5 = header(HttpHeaderNames.CONTENT_MD5); public static final HeaderValueType REFERRER = header(HttpHeaderNames.REFERER); public static final HeaderValueType TRANSFER_ENCODING = header(HttpHeaderNames.TRANSFER_ENCODING); public static final HeaderValueType ACCESS_CONTROL_ALLOW_METHODS = header(HttpHeaderNames.ACCESS_CONTROL_ALLOW_METHODS); public static final HeaderValueType ACCESS_CONTROL_MAX_AGE = new DurationHeader(HttpHeaderNames.ACCESS_CONTROL_MAX_AGE); public static final HeaderValueType X_ACCEL_BUFFERING = new AccelBufferingHeader(); public static final HeaderValueType KEEP_ALIVE = new KeepAliveHeader(); public static final HeaderValueType CONTENT_DISPOSITION = header(HttpHeaderNames.CONTENT_DISPOSITION); public static final HeaderValueType UPGRADE_INSECURE_REQUESTS = new BooleanHeader(AsciiString.of("upgrade-insecure-requests")); public static final HeaderValueType X_FRAME_OPTIONS = new FrameOptionsHeader(); public static final HeaderValueType STRICT_TRANSPORT_SECURITY = new StrictTransportSecurityHeader(); public static HeaderValueType stringHeader(CharSequence key) { return new StringHeader(key); } public static HeaderValueType header(CharSequence key) { return new CharSequenceHeader(key); } public static T read(HeaderValueType type, HttpMessage msg) { String val = msg.headers().get(type.name()); return val == null ? null : type.toValue(val); } public static CharSequence writeIfNotNull(HeaderValueType type, T value, HttpMessage msg) { if (value != null) { return write(type, value, msg); } return null; } public static CharSequence write(HeaderValueType type, T value, HttpMessage msg) { Objects.requireNonNull(type, "type"); Objects.requireNonNull(msg, "msg"); Objects.requireNonNull(value, "value " + type); CharSequence val = type.toCharSequence(value); msg.headers().add(type.name(), val); return val; } public static CharSequence write(HeaderValueType type, T value, HttpHeaders headers) { Objects.requireNonNull(type, "type"); Objects.requireNonNull(headers, "headers"); Objects.requireNonNull(value, "value " + type); CharSequence val = type.toCharSequence(value); headers.add(type.name(), val); return val; } public static String toISO2822Date(ZonedDateTime dt) { dt = dt.withZoneSameInstant(UTC); return dt.format(ISO2822DateFormatForOutput); // return dt.toDateTime(DateTimeZone.UTC).toDateTimeISO().toString( // ISO2822DateFormat); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy