Package de.aristaflow.adept2.util.net
Class HttpTools
java.lang.Object
de.aristaflow.adept2.util.net.HttpTools
Tool methods for simplifying life with HTTP.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final PatternThe pattern determining the character set from the Content-Type.static final StringThe header name for forward information for HTTP requests through a proxy.static final StringThe header name for the forward host for HTTP requests through a proxy.static final StringThe header name for the forward port for HTTP requests through a proxy.static final StringThe header name for the forward protocol for HTTP requests through a proxy.protected static final PatternThe pattern determining the host and port from the Forwarded header.protected static final PatternThe pattern determining the protocol from the Forwarded header. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringAdapts the designated URL by transforming the string to a URL, applyingadaptUrl(URL, Function, Logger)and converting the URL back to String again.static URLAdapts the designated URL by setting protocol, host and port from the headers provided by the designated function.static CharsetgetCharset(URLConnection conn) Gets the character set used for text content of the designatedURLConnection.
-
Field Details
-
HEADER_FORWARDED
The header name for forward information for HTTP requests through a proxy.- See Also:
-
HEADER_X_FORWARDED_HOST
The header name for the forward host for HTTP requests through a proxy.- See Also:
-
HEADER_X_FORWARDED_PORT
The header name for the forward port for HTTP requests through a proxy.- See Also:
-
HEADER_X_FORWARDED_PROTO
The header name for the forward protocol for HTTP requests through a proxy.- See Also:
-
hostPattern
The pattern determining the host and port from the Forwarded header. -
protoPattern
The pattern determining the protocol from the Forwarded header. -
charsetPattern
The pattern determining the character set from the Content-Type.
-
-
Constructor Details
-
HttpTools
public HttpTools()
-
-
Method Details
-
adaptUrl
Adapts the designated URL by transforming the string to a URL, applyingadaptUrl(URL, Function, Logger)and converting the URL back to String again.- Parameters:
url- The URL which may need to be adapted to a different protocol, host and/or port. This must not benull.headers- The function providing HTTP request headers. This must not benullbut it may returnnull.logger- The logger for logging problems, e. g. the port cannot be converted toint. This must not benull.- Returns:
- The URL with protocol, host and/or port changed to the values provided by the
designated
headersfunction for the corresponding HTTP headers. - See Also:
-
adaptUrl
Adapts the designated URL by setting protocol, host and port from the headers provided by the designated function. This allows to change the known values to the ones provided by the HTTP request which may be routed through a proxy. That is, the URL working for the requestor may need to be different from the URL known by our service.This supports (in this order, i. e. if a value is provided, the corresponding alternative header will be ignored):
- Forwarded
- X-Forwarded-Host
- X-Forwarded-Port
- X-Forwarded-Proto
- Parameters:
url- The URL which may need to be adapted to a different protocol, host and/or port. This must not benull.headers- The function providing HTTP request headers. This must not benullbut it may returnnull.logger- The logger for logging problems, e. g. the port cannot be converted toint. This must not benull.- Returns:
- The URL with protocol, host and/or port changed to the values provided by the
designated
headersfunction for the corresponding HTTP headers.
-
getCharset
Gets the character set used for text content of the designatedURLConnection. If the content is no text content or the character set is not specified,nullwill be returned.- Parameters:
conn- The URL connection for which to determine the character set of the text content.- Returns:
- The character set used for text content of the designated
URLConnectionornullin case of no text content or no specified character set.
-