@NotThreadSafe public class UnifiedResponse extends Object
| Modifier and Type | Field and Description |
|---|---|
static boolean |
DEFAULT_ALLOW_CONTENT_ON_REDIRECT
By default content is not allowed on redirect
|
static boolean |
DEFAULT_ALLOW_CONTENT_ON_STATUS_CODE
By default content is not allowed for status codes
|
static EContentDispositionType |
DEFAULT_CONTENT_DISPOSITION_TYPE
Default content disposition type is Attachment
|
static boolean |
DEFAULT_WARN_ON_DUPLICATE_COOKIES
By default a warning is emitted on duplicate cookies
|
static int |
MAX_CSS_KB_FOR_IE
Maximum KB a CSS file might have in IE
|
| Constructor and Description |
|---|
UnifiedResponse(com.helger.http.EHttpVersion eHttpVersion,
com.helger.commons.http.EHttpMethod eHttpMethod,
javax.servlet.http.HttpServletRequest aHttpRequest)
Constructor
|
| Modifier and Type | Method and Description |
|---|---|
UnifiedResponse |
addCookie(javax.servlet.http.Cookie aCookie)
Add the passed cookie.
|
void |
addCustomResponseHeader(String sName,
String sValue)
Adds a response header to the response according to the passed name and
value.
|
void |
addCustomResponseHeaders(com.helger.commons.http.HttpHeaderMap aOther)
Add many custom headers at once.
|
void |
applyToResponse(javax.servlet.http.HttpServletResponse aHttpResponse) |
com.helger.http.CacheControlBuilder |
cacheControl() |
static UnifiedResponse |
createSimple(javax.servlet.http.HttpServletRequest aHttpRequest)
Factory method
|
UnifiedResponse |
disableCaching()
A utility method that disables caching for this response.
|
UnifiedResponse |
enableCaching(int nSeconds)
Enable caching of this resource for the specified number of seconds.
|
com.helger.commons.collection.impl.ICommonsOrderedMap<String,javax.servlet.http.Cookie> |
getAllCookies() |
Charset |
getCharset() |
String |
getContentDispositionFilename() |
EContentDispositionType |
getContentDispositionType() |
com.helger.commons.http.EHttpMethod |
getHttpMethod() |
com.helger.http.EHttpVersion |
getHttpVersion() |
protected String |
getLogPrefix() |
com.helger.commons.mime.IMimeType |
getMimeType() |
com.helger.useragent.browser.BrowserInfo |
getRequestBrowserInfo() |
protected String |
getRequestURL() |
int |
getStatusCode() |
boolean |
hasContent() |
boolean |
hasCookies() |
boolean |
isAllowContentOnRedirect() |
boolean |
isAllowContentOnStatusCode() |
boolean |
isRedirectDefined() |
boolean |
isStatusCodeDefined() |
boolean |
isWarnOnDuplicateCookies() |
protected void |
logError(String sMsg) |
protected void |
logInfo(String sMsg) |
protected void |
logWarn(String sMsg) |
com.helger.commons.state.EChange |
removeAllCookies()
Remove all cookies.
|
UnifiedResponse |
removeCacheControl() |
UnifiedResponse |
removeCaching()
Remove all settings and headers relevant to caching.
|
UnifiedResponse |
removeCharset() |
UnifiedResponse |
removeContent() |
UnifiedResponse |
removeContentDispositionFilename()
Remove the current content disposition filename.
|
UnifiedResponse |
removeCookie(String sName)
Remove the cookie with the specified name.
|
com.helger.commons.state.EChange |
removeCustomResponseHeaders(String sName)
Removes the response headers matching the passed name from the response.
|
UnifiedResponse |
removeETag()
Remove the ETag if present.
|
UnifiedResponse |
removeExpires() |
UnifiedResponse |
removeLastModified() |
UnifiedResponse |
removeMimeType() |
UnifiedResponse |
removeStrictTransportSecurity()
Remove the X-Frame-Options HTTP header if it is present.
|
UnifiedResponse |
removeXFrameOptions()
Remove the X-Frame-Options HTTP header if it is present.
|
protected com.helger.commons.http.HttpHeaderMap |
responseHeaderMap() |
UnifiedResponse |
setAllowContentOnRedirect(boolean bAllowContentOnRedirect) |
UnifiedResponse |
setAllowContentOnStatusCode(boolean bAllowContentOnStatusCode) |
UnifiedResponse |
setAllowMimeSniffing(boolean bAllow)
When specifying
false, this method uses a special response
header to prevent certain browsers from MIME-sniffing a response away from
the declared content-type. |
UnifiedResponse |
setCacheControl(com.helger.http.CacheControlBuilder aCacheControl) |
UnifiedResponse |
setCharset(Charset aCharset) |
UnifiedResponse |
setContent(byte[] aContent)
Set the response content.
|
UnifiedResponse |
setContent(byte[] aContent,
int nOfs,
int nLen)
Set the response content.
|
UnifiedResponse |
setContent(com.helger.commons.io.IHasInputStream aISP)
Set the response content provider.
|
UnifiedResponse |
setContentAndCharset(String sContent,
Charset aCharset)
Utility method to set content and charset at once.
|
UnifiedResponse |
setContentDispositionFilename(String sFilename)
Set the content disposition filename for attachment download.
|
UnifiedResponse |
setContentDispositionType(EContentDispositionType eContentDispositionType)
Set the content disposition type (e.g. for PDF/Excel downloads).
|
void |
setCustomResponseHeader(String sName,
String sValue)
Sets a response header to the response according to the passed name and
value.
|
void |
setCustomResponseHeaders(com.helger.commons.http.HttpHeaderMap aOther)
Set many custom headers at once.
|
UnifiedResponse |
setDownloadFilename(String sFilename)
Utility method for setting the MimeType application/force-download and set
the respective content disposition filename.
|
UnifiedResponse |
setEmptyContent()
Utility method to set an empty response content.
|
UnifiedResponse |
setEnableXSSFilter(boolean bEnable)
This header enables the Cross-site scripting (XSS) filter built into most
recent web browsers.
|
UnifiedResponse |
setETag(String sETag)
Set an ETag for the response.
|
UnifiedResponse |
setETagIfApplicable(String sETag)
Set an ETag for the response if this is an HTTP/1.1 response.
|
UnifiedResponse |
setExpires(LocalDateTime aDT) |
UnifiedResponse |
setLastModified(LocalDateTime aDT) |
UnifiedResponse |
setMimeType(com.helger.commons.mime.IMimeType aMimeType) |
UnifiedResponse |
setMimeTypeString(String sMimeType) |
UnifiedResponse |
setRedirect(com.helger.commons.url.ISimpleURL aRedirectTargetUrl) |
UnifiedResponse |
setRedirect(com.helger.commons.url.ISimpleURL aRedirectTargetUrl,
ERedirectMode eRedirectMode) |
UnifiedResponse |
setRedirect(String sRedirectTargetUrl) |
UnifiedResponse |
setRedirect(String sRedirectTargetUrl,
ERedirectMode eRedirectMode) |
UnifiedResponse |
setRequestBrowserInfo(com.helger.useragent.browser.BrowserInfo aRequestBrowserInfo) |
UnifiedResponse |
setStatus(int nStatusCode)
Set the status code to be returned from the response.
|
UnifiedResponse |
setStatusUnauthorized(String sAuthenticate)
Special handling for returning status code 401 UNAUTHORIZED.
|
UnifiedResponse |
setStrictTransportSecurity(int nMaxAgeSeconds,
boolean bIncludeSubdomains)
When specifying
false, this method uses a special response
header to prevent certain browsers from MIME-sniffing a response away from
the declared content-type. |
UnifiedResponse |
setWarnOnDuplicateCookies(boolean bWarnOnDuplicateCookies)
Enable or disable warning message on duplicated cookie names.
|
UnifiedResponse |
setXFrameOptions(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> .
|
protected void |
showRequestInfo() |
public static final boolean DEFAULT_ALLOW_CONTENT_ON_REDIRECT
public static final boolean DEFAULT_ALLOW_CONTENT_ON_STATUS_CODE
public static final EContentDispositionType DEFAULT_CONTENT_DISPOSITION_TYPE
public static final boolean DEFAULT_WARN_ON_DUPLICATE_COOKIES
public static final int MAX_CSS_KB_FOR_IE
public UnifiedResponse(@Nonnull com.helger.http.EHttpVersion eHttpVersion, @Nonnull com.helger.commons.http.EHttpMethod eHttpMethod, @Nonnull javax.servlet.http.HttpServletRequest aHttpRequest)
eHttpVersion - HTTP version of this request (1.0 or 1.1)eHttpMethod - HTTP method of this request (GET, POST, ...)aHttpRequest - The main HTTP requestprotected final void showRequestInfo()
@Nonnull public final com.helger.http.EHttpVersion getHttpVersion()
null.@Nonnull public final com.helger.commons.http.EHttpMethod getHttpMethod()
null.@Nullable public final com.helger.useragent.browser.BrowserInfo getRequestBrowserInfo()
null.@Nonnull public final UnifiedResponse setRequestBrowserInfo(@Nullable com.helger.useragent.browser.BrowserInfo aRequestBrowserInfo)
public final boolean isAllowContentOnRedirect()
true if content is allowed even if a redirect is
present.@Nonnull public final UnifiedResponse setAllowContentOnRedirect(boolean bAllowContentOnRedirect)
public final boolean isAllowContentOnStatusCode()
true if content is allowed even if a status code is
present.@Nonnull public final UnifiedResponse setAllowContentOnStatusCode(boolean bAllowContentOnStatusCode)
@Nonnull public final UnifiedResponse setCharset(@Nonnull Charset aCharset)
@Nonnull public final UnifiedResponse removeCharset()
@Nullable public final com.helger.commons.mime.IMimeType getMimeType()
@Nonnull public final UnifiedResponse setMimeType(@Nonnull com.helger.commons.mime.IMimeType aMimeType)
@Nonnull public final UnifiedResponse setMimeTypeString(@Nonnull @Nonempty String sMimeType)
@Nonnull public final UnifiedResponse removeMimeType()
public final boolean hasContent()
true if a content was already set, false if
not.@Nonnull public final UnifiedResponse setEmptyContent()
@Nonnull public final UnifiedResponse setContentAndCharset(@Nonnull String sContent, @Nonnull Charset aCharset)
sContent - The response content string. May not be null.aCharset - The charset to use. May not be null.@Nonnull public final UnifiedResponse setContent(@Nonnull byte[] aContent)
null.aContent - The content to be returned. Is not copied inside! May not be
null but maybe empty.@Nonnull public final UnifiedResponse setContent(@Nonnull byte[] aContent, @Nonnegative int nOfs, @Nonnegative int nLen)
null.aContent - The content to be returned. Is not copied inside! May not be
null but maybe empty.nOfs - The content offset to start at. Must be ≥ 0.nLen - The content length to use. Must be ≥ 0 and ≤ than the content
length!@Nonnull public final UnifiedResponse setContent(@Nonnull com.helger.commons.io.IHasInputStream aISP)
aISP - The content provider to be used. May not be null.@Nonnull public final UnifiedResponse removeContent()
@Nonnull public final UnifiedResponse setExpires(@Nonnull LocalDateTime aDT)
@Nonnull public final UnifiedResponse removeExpires()
@Nonnull public final UnifiedResponse setLastModified(@Nonnull LocalDateTime aDT)
@Nonnull public final UnifiedResponse removeLastModified()
@Nonnull public final UnifiedResponse setETag(@Nonnull @Nonempty String sETag)
sETag - The quoted ETag to be set. May neither be null nor empty.@Nonnull public final UnifiedResponse setETagIfApplicable(@Nonnull @Nonempty String sETag)
sETag - The quoted ETag to be set. May neither be null nor empty.@Nonnull public final UnifiedResponse removeETag()
@Nonnull public final UnifiedResponse setContentDispositionType(@Nonnull EContentDispositionType eContentDispositionType)
DEFAULT_CONTENT_DISPOSITION_TYPE. This value is only used if a
content disposition filename is defined.eContentDispositionType - The content disposition type to be used. May not be null.setContentDispositionFilename(String)@Nonnull public final EContentDispositionType getContentDispositionType()
null.
Default is DEFAULT_CONTENT_DISPOSITION_TYPE.@Nonnull public final UnifiedResponse setContentDispositionFilename(@Nonnull @Nonempty String sFilename)
sFilename - The filename for attachment download to use. May neither be
null nor empty.removeContentDispositionFilename()@Nullable public final String getContentDispositionFilename()
null if
not set.@Nonnull public final UnifiedResponse removeContentDispositionFilename()
@Nonnull public final UnifiedResponse setDownloadFilename(@Nonnull @Nonempty String sFilename)
sFilename - The filename to be used.@Nonnull public final UnifiedResponse setCacheControl(@Nonnull com.helger.http.CacheControlBuilder aCacheControl)
@Nullable @ReturnsMutableObject public final com.helger.http.CacheControlBuilder cacheControl()
@Nonnull public final UnifiedResponse removeCacheControl()
@Nonnull @ReturnsMutableObject protected final com.helger.commons.http.HttpHeaderMap responseHeaderMap()
null header map.@Nonnull public final UnifiedResponse removeCaching()
@Nonnull public final UnifiedResponse disableCaching()
@Nonnull public final UnifiedResponse enableCaching(@Nonnegative int nSeconds)
nSeconds - The number of seconds caching is allowed. Must be > 0.public final boolean isStatusCodeDefined()
true if a status code is defined, false if
not.public final int getStatusCode()
CGlobal.ILLEGAL_UINT if
undefined.isStatusCodeDefined()@Nonnull public final UnifiedResponse setStatus(@Nonnegative int nStatusCode)
nStatusCode - The status code to be set. Must be a valid HTTP response code.@Nonnull public final UnifiedResponse setStatusUnauthorized(@Nullable String sAuthenticate)
sAuthenticate - The string to be used for the CHttpHeader.WWW_AUTHENTICATE
response header. May be null or empty.public final boolean isRedirectDefined()
@Nonnull public final UnifiedResponse setRedirect(@Nonnull com.helger.commons.url.ISimpleURL aRedirectTargetUrl)
@Nonnull public final UnifiedResponse setRedirect(@Nonnull com.helger.commons.url.ISimpleURL aRedirectTargetUrl, @Nonnull ERedirectMode eRedirectMode)
@Nonnull public final UnifiedResponse setRedirect(@Nonnull @Nonempty String sRedirectTargetUrl)
@Nonnull public final UnifiedResponse setRedirect(@Nonnull @Nonempty String sRedirectTargetUrl, @Nonnull ERedirectMode eRedirectMode)
public final boolean isWarnOnDuplicateCookies()
true if warning on duplicated cookies is enabled,
false if it is disabled.@Nonnull public final UnifiedResponse setWarnOnDuplicateCookies(boolean bWarnOnDuplicateCookies)
bWarnOnDuplicateCookies - true to enable warnings, false to disable
them.@Nonnull public final UnifiedResponse addCookie(@Nonnull javax.servlet.http.Cookie aCookie)
aCookie - The cookie to be added. May not be null.@Nonnull public final UnifiedResponse removeCookie(@Nullable String sName)
sName - The name of the cookie to be removed. May be null.@Nonnull public final com.helger.commons.state.EChange removeAllCookies()
EChange.CHANGED if at least one cookie was removed.public final boolean hasCookies()
true if at least one cookie is present.@Nonnull @ReturnsMutableCopy public final com.helger.commons.collection.impl.ICommonsOrderedMap<String,javax.servlet.http.Cookie> getAllCookies()
null but maybe
empty.@Nonnull public final UnifiedResponse setAllowMimeSniffing(boolean bAllow)
false, this method uses a special response
header to prevent certain browsers from MIME-sniffing a response away from
the declared content-type. When passing true, that header is
removed.bAllow - Whether or not sniffing should be allowed (default is
true).@Nonnull public final UnifiedResponse setEnableXSSFilter(boolean bEnable)
bEnable - true to enable the header, false to disable
it.@Nonnull public final UnifiedResponse setStrictTransportSecurity(int nMaxAgeSeconds, boolean bIncludeSubdomains)
false, this method uses a special response
header to prevent certain browsers from MIME-sniffing a response away from
the declared content-type. When passing true, that header is
removed.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.@Nonnull public final UnifiedResponse removeStrictTransportSecurity()
@Nonnull public final UnifiedResponse setXFrameOptions(@Nonnull EXFrameOptionType eType, @Nullable com.helger.commons.url.ISimpleURL aDomain)
X-Frame-Options: DENY X-Frame-Options: SAMEORIGIN X-Frame-Options: ALLOW-FROM https://example.com/
eType - The X-Frame-Options type to be set. May not be null.aDomain - The domain URL to be used in "ALLOW-FROM". May be null
for the other cases.@Nonnull public final UnifiedResponse removeXFrameOptions()
public final void addCustomResponseHeader(@Nonnull @Nonempty String sName, @Nonnull @Nonempty String sValue)
UnifiedResponse
directly offers. Use this method only in emergency and make sure you validate
the header field and allowed value!sName - Name of the header. May neither be null nor empty.sValue - Value of the header. May neither be null nor empty.public final void addCustomResponseHeaders(@Nullable com.helger.commons.http.HttpHeaderMap aOther)
aOther - The headers to be added. May be null.public final void setCustomResponseHeader(@Nonnull @Nonempty String sName, @Nonnull @Nonempty String sValue)
UnifiedResponse
directly offers. Use this method only in emergency and make sure you validate
the header field and allowed value!sName - Name of the header. May neither be null nor empty.sValue - Value of the header. May neither be null nor empty.public final void setCustomResponseHeaders(@Nullable com.helger.commons.http.HttpHeaderMap aOther)
aOther - The headers to be set. May be null.@Nonnull public final com.helger.commons.state.EChange removeCustomResponseHeaders(@Nonnull @Nonempty String sName)
UnifiedResponse
directly offers. Use this method only in emergency and make sure you validate
the header field and allowed value!sName - Name of the header to be removed. May neither be null nor
empty.EChange.CHANGED in header was removed.public final void applyToResponse(@Nonnull javax.servlet.http.HttpServletResponse aHttpResponse) throws IOException
IOException@Nonnull public static UnifiedResponse createSimple(@Nonnull javax.servlet.http.HttpServletRequest aHttpRequest)
aHttpRequest - The main HTTP request. May not be null.UnifiedResponse. Never null.Copyright © 2016–2019 Philip Helger. All rights reserved.