Package de.aristaflow.adept2.util.net
Class HttpTools
- java.lang.Object
-
- de.aristaflow.adept2.util.net.HttpTools
-
public class HttpTools extends Object
Tool methods for simplifying life with HTTP.
-
-
Field Summary
Fields Modifier and Type Field Description protected static PatterncharsetPatternThe pattern determining the character set from the Content-Type.static StringHEADER_FORWARDEDThe header name for forward information for HTTP requests through a proxy.static StringHEADER_X_FORWARDED_HOSTThe header name for the forward host for HTTP requests through a proxy.static StringHEADER_X_FORWARDED_PORTThe header name for the forward port for HTTP requests through a proxy.static StringHEADER_X_FORWARDED_PROTOThe header name for the forward protocol for HTTP requests through a proxy.protected static PatternhostPatternThe pattern determining the host and port from the Forwarded header.protected static PatternprotoPatternThe pattern determining the protocol from the Forwarded header.
-
Constructor Summary
Constructors Constructor Description HttpTools()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static StringadaptUrl(String url, Function<String,String> headers, Logger logger)Adapts the designated URL by transforming the string to a URL, applyingadaptUrl(URL, Function, Logger)and converting the URL back to String again.static URLadaptUrl(URL url, Function<String,String> headers, Logger logger)Adapts 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 Detail
-
HEADER_FORWARDED
public static final String HEADER_FORWARDED
The header name for forward information for HTTP requests through a proxy.- See Also:
- Forwarded, Constant Field Values
-
HEADER_X_FORWARDED_HOST
public static final String HEADER_X_FORWARDED_HOST
The header name for the forward host for HTTP requests through a proxy.- See Also:
- X-Forwarded-Host, Constant Field Values
-
HEADER_X_FORWARDED_PORT
public static final String HEADER_X_FORWARDED_PORT
The header name for the forward port for HTTP requests through a proxy.- See Also:
- Constant Field Values
-
HEADER_X_FORWARDED_PROTO
public static final String HEADER_X_FORWARDED_PROTO
The header name for the forward protocol for HTTP requests through a proxy.- See Also:
- X-Forwarded-Proto, Constant Field Values
-
hostPattern
protected static final Pattern hostPattern
The pattern determining the host and port from the Forwarded header.
-
protoPattern
protected static final Pattern protoPattern
The pattern determining the protocol from the Forwarded header.
-
charsetPattern
protected static final Pattern charsetPattern
The pattern determining the character set from the Content-Type.
-
-
Method Detail
-
adaptUrl
public static String adaptUrl(String url, Function<String,String> headers, Logger logger)
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(URL, Function, Logger)
-
adaptUrl
public static URL adaptUrl(URL url, Function<String,String> headers, Logger logger)
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
public static Charset getCharset(URLConnection conn)
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.
-
-