Package com.helger.servlet.response
Class UnifiedResponseDefaultSettings
- java.lang.Object
-
- com.helger.servlet.response.UnifiedResponseDefaultSettings
-
@ThreadSafe public final class UnifiedResponseDefaultSettings extends Object
This class encapsulates default settings to be applied to allUnifiedResponseobjects.- Since:
- 6.0.5
- Author:
- Philip Helger
-
-
Field Summary
Fields Modifier and Type Field Description static booleanDEFAULT_HTTP_HEADER_VALUES_QUOTE_IF_NECESSARYBy default HTTP header value unification is disabledstatic booleanDEFAULT_HTTP_HEADER_VALUES_UNIFIEDBy default HTTP header value unification is enabled
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidaddCookie(jakarta.servlet.http.Cookie aCookie)Add the passed cookie.static voidaddResponseHeader(String sName, String sValue)Adds a response header to the response according to the passed name and value.static com.helger.commons.collection.impl.ICommonsOrderedMap<String,jakarta.servlet.http.Cookie>getAllCookies()static com.helger.commons.http.HttpHeaderMapgetResponseHeaderMap()static booleanhasCookies()static booleanisHttpHeaderValuesQuoteIfNecessary()static booleanisHttpHeaderValuesUnified()static com.helger.commons.state.EChangeremoveAllCookies()Remove all cookies.static com.helger.commons.state.EChangeremoveAllResponseHeaders()Remove all response headers currently present.static com.helger.commons.state.EChangeremoveCookie(String sName)Remove the cookie with the specified name.static com.helger.commons.state.EChangeremoveResponseHeaders(String sName)Removes the response headers matching the passed name from the response.static voidremoveStrictTransportSecurity()Remove the `Strict-Transport-Security` headers from the default settings.static voidsetAllowMimeSniffing(boolean bAllow)When specifyingfalse, this method uses a special response header to prevent certain browsers from MIME-sniffing a response away from the declared content-type.static voidsetEnableXSSFilter(boolean bEnable)This header enables the Cross-site scripting (XSS) filter built into most recent web browsers.static voidsetHttpHeaderValuesQuoteIfNecessary(boolean bHttpHeaderValuesQuoteIfNecessary)Enable or disable the automatic quoting of HTTP header values.static voidsetHttpHeaderValuesUnified(boolean bHttpHeaderValuesUnified)Enable or disable the unification of HTTP header values.static voidsetReferrerPolicy(com.helger.http.EHttpReferrerPolicy eReferrerPolicy)Set the default referrer policy to use.static voidsetResponseHeader(String sName, String sValue)Sets a response header to the response according to the passed name and value.static voidsetStrictTransportSecurity(int nMaxAgeSeconds, boolean bIncludeSubdomains)HTTP Strict Transport Security (HSTS) is an opt-in security enhancement that is specified by a web application through the use of a special response header.static voidsetXFrameOptions(EXFrameOptionType eType, com.helger.commons.url.ISimpleURL aDomain)The X-Frame-Options HTTP response header can be used to indicate whether or not a browser should be allowed to render a page in a <frame>, <iframe> or <object> .
-
-
-
Field Detail
-
DEFAULT_HTTP_HEADER_VALUES_UNIFIED
public static final boolean DEFAULT_HTTP_HEADER_VALUES_UNIFIED
By default HTTP header value unification is enabled- See Also:
- Constant Field Values
-
DEFAULT_HTTP_HEADER_VALUES_QUOTE_IF_NECESSARY
public static final boolean DEFAULT_HTTP_HEADER_VALUES_QUOTE_IF_NECESSARY
By default HTTP header value unification is disabled- See Also:
- Constant Field Values
-
-
Method Detail
-
getResponseHeaderMap
@Nonnull @ReturnsMutableCopy public static com.helger.commons.http.HttpHeaderMap getResponseHeaderMap()
- Returns:
- The non-
nullheader map.
-
setAllowMimeSniffing
public static void setAllowMimeSniffing(boolean bAllow)
When specifyingfalse, this method uses a special response header to prevent certain browsers from MIME-sniffing a response away from the declared content-type. When passingtrue, that header is removed.- Parameters:
bAllow- Whether or not sniffing should be allowed (default istrue).
-
setEnableXSSFilter
public static void setEnableXSSFilter(boolean bEnable)
This header enables the Cross-site scripting (XSS) filter built into most recent web browsers. It's usually enabled by default anyway, so the role of this header is to re-enable the filter for this particular website if it was disabled by the user. This header is supported in IE 8+, and in Chrome (not sure which versions). The anti-XSS filter was added in Chrome 4. Its unknown if that version honored this header.- Parameters:
bEnable-trueto enable the header,falseto disable it.
-
setStrictTransportSecurity
public static void setStrictTransportSecurity(@Nonnegative int nMaxAgeSeconds, boolean bIncludeSubdomains)
HTTP Strict Transport Security (HSTS) is an opt-in security enhancement that is specified by a web application through the use of a special response header. Once a supported browser receives this header that browser will prevent any communications from being sent over HTTP to the specified domain and will instead send all communications over HTTPS. It also prevents HTTPS click through prompts on browsers. The specification has been released and published end of 2012 as RFC 6797 (HTTP Strict Transport Security (HSTS)) by the IETF.- Parameters:
nMaxAgeSeconds- number of seconds, after the reception of the STS header field, during which the UA regards the host (from whom the message was received) as a Known HSTS Host.bIncludeSubdomains- if enabled, this signals the UA that the HSTS Policy applies to this HSTS Host as well as any sub-domains of the host's domain name.
-
removeStrictTransportSecurity
public static void removeStrictTransportSecurity()
Remove the `Strict-Transport-Security` headers from the default settings.- Since:
- 9.1.1
-
setXFrameOptions
public static void setXFrameOptions(@Nullable EXFrameOptionType eType, @Nullable com.helger.commons.url.ISimpleURL aDomain)
The X-Frame-Options HTTP response header can be used to indicate whether or not a browser should be allowed to render a page in a <frame>, <iframe> or <object> . Sites can use this to avoid clickjacking attacks, by ensuring that their content is not embedded into other sites. Example:X-Frame-Options: DENY X-Frame-Options: SAMEORIGIN X-Frame-Options: ALLOW-FROM https://example.com/
- Parameters:
eType- The X-Frame-Options type to be set. May not benull.aDomain- The domain URL to be used in "ALLOW-FROM". May benullfor the other cases.
-
setReferrerPolicy
public static void setReferrerPolicy(@Nullable com.helger.http.EHttpReferrerPolicy eReferrerPolicy)
Set the default referrer policy to use. See https://scotthelme.co.uk/a-new-security-header-referrer-policy/- Parameters:
eReferrerPolicy- Policy to use. May benull.
-
setResponseHeader
public static void setResponseHeader(@Nonnull @Nonempty String sName, @Nonnull @Nonempty String sValue)
Sets a response header to the response according to the passed name and value. An existing header entry with the same name is overridden.- Parameters:
sName- Name of the header. May neither benullnor empty.sValue- Value of the header. May neither benullnor empty.
-
addResponseHeader
public static void addResponseHeader(@Nonnull @Nonempty String sName, @Nonnull @Nonempty String sValue)
Adds a response header to the response according to the passed name and value. If an existing header with the same is present, the value is added to the list so that the header is emitted more than once.- Parameters:
sName- Name of the header. May neither benullnor empty.sValue- Value of the header. May neither benullnor empty.
-
removeResponseHeaders
@Nonnull public static com.helger.commons.state.EChange removeResponseHeaders(@Nonnull @Nonempty String sName)
Removes the response headers matching the passed name from the response.
ATTENTION: You should only use the APIs thatUnifiedResponseDefaultSettingsdirectly offers. Use this method only in emergency and make sure you validate the header field and allowed value!- Parameters:
sName- Name of the header to be removed. May neither benullnor empty.- Returns:
EChange.CHANGEDin header was removed.
-
removeAllResponseHeaders
@Nonnull public static com.helger.commons.state.EChange removeAllResponseHeaders()
Remove all response headers currently present.- Returns:
EChange
-
hasCookies
public static boolean hasCookies()
- Returns:
trueif at least one cookie is present.
-
getAllCookies
@Nonnull @ReturnsMutableCopy public static com.helger.commons.collection.impl.ICommonsOrderedMap<String,jakarta.servlet.http.Cookie> getAllCookies()
- Returns:
- A copy of all contained cookies. Never
nullbut maybe empty.
-
addCookie
public static void addCookie(@Nonnull jakarta.servlet.http.Cookie aCookie)
Add the passed cookie.- Parameters:
aCookie- The cookie to be added. May not benull.
-
removeCookie
@Nonnull public static com.helger.commons.state.EChange removeCookie(@Nullable String sName)
Remove the cookie with the specified name.- Parameters:
sName- The name of the cookie to be removed. May benull.- Returns:
EChange
-
removeAllCookies
@Nonnull public static com.helger.commons.state.EChange removeAllCookies()
Remove all cookies.- Returns:
EChange.CHANGEDif at least one cookie was removed.
-
isHttpHeaderValuesUnified
public static boolean isHttpHeaderValuesUnified()
- Returns:
trueif HTTP header values will be unified,falseif not.- Since:
- 9.1.4
- See Also:
DEFAULT_HTTP_HEADER_VALUES_UNIFIED
-
setHttpHeaderValuesUnified
public static void setHttpHeaderValuesUnified(boolean bHttpHeaderValuesUnified)
Enable or disable the unification of HTTP header values.- Parameters:
bHttpHeaderValuesUnified-trueto enable it,falseto disable it.- Since:
- 9.1.4
-
isHttpHeaderValuesQuoteIfNecessary
public static boolean isHttpHeaderValuesQuoteIfNecessary()
- Returns:
trueif HTTP header values will be unified and quoted if necessary,falseif not.- Since:
- 9.1.4
- See Also:
DEFAULT_HTTP_HEADER_VALUES_QUOTE_IF_NECESSARY
-
setHttpHeaderValuesQuoteIfNecessary
public static void setHttpHeaderValuesQuoteIfNecessary(boolean bHttpHeaderValuesQuoteIfNecessary)
Enable or disable the automatic quoting of HTTP header values. This only takes effect, if the unification is enabled.- Parameters:
bHttpHeaderValuesQuoteIfNecessary-trueto enable it,falseto disable it.- Since:
- 9.1.4
- See Also:
setHttpHeaderValuesUnified(boolean)
-
-