Package org.ametys.core.util
Class HttpUtils
java.lang.Object
org.ametys.core.util.HttpUtils
- All Implemented Interfaces:
Disposable,Initializable,Component
Utility class for HTTP urls.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumThe status of HTTP checkstatic final recordRepresent the result of a check and a potential message -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic org.apache.hc.core5.http.ClassicHttpRequestbuildRequest(URI uri, String userAgent, String method, int timeout, int readTimeOut, boolean followRedirects, int maxRedirects, Map<String, String> requestHeaders) build a request to the remote URIcheckHttpUrl(String httpUrl) Method to check a HTTP url from client side.static HttpUtils.HttpCheckReportcheckHttpUrl(String httpUrl, String userAgent, String method, int timeout, int readTimeOut, boolean followRedirects) Check the HTTP urlstatic HttpUtils.HttpCheckReportcheckHttpUrl(String httpUrl, String userAgent, String method, int timeout, int readTimeOut, boolean followRedirects, Map<String, String> requestHeaders) Check the HTTP urlstatic HttpUtils.HttpCheckReportcheckHttpUrl(URL url, String userAgent, String method, int timeout, int readTimeOut, boolean followRedirects, Map<String, String> requestHeaders) Check the HTTP urlstatic org.apache.hc.client5.http.impl.classic.CloseableHttpClientcreateHttpClient(int maxConnections, int timeout) Create ands return a configured, ready to use,HttpClient.
This method is well suited to create a client dedicated to a single route.static org.apache.hc.client5.http.impl.classic.CloseableHttpClientcreateHttpClient(int maxConnections, int timeout, boolean blockInternal) Create ands return a configured, ready to use,HttpClient.
This method is well suited to create a client dedicated to a single route.voiddispose()static <T> TexecuteRequest(String httpUrl, String userAgent, String method, int timeout, int readTimeOut, boolean followRedirects, Map<String, String> requestHeaders, boolean ignoreRestriction, org.apache.hc.core5.http.io.HttpClientResponseHandler<? extends T> responseHandler) Prepare the connection to the remote URLstatic <T> TexecuteRequest(String httpUrl, String userAgent, String method, int timeout, int readTimeOut, boolean followRedirects, Map<String, String> requestHeaders, org.apache.hc.core5.http.io.HttpClientResponseHandler<? extends T> responseHandler) Prepare the connection to the remote URLvoidstatic StringStrips the end of an uri to remove "*.html" ends and "/" ending characters
-
Field Details
-
ROLE
The Avalon role -
HTTP_URL_VALIDATOR
Regexp for HTTP url -
HTTP_REDIRECT_TEMP
HTTP Status-Code 307: Temporay Redirect- See Also:
-
HTTP_REDIRECT_PERM
HTTP Status-Code 308: Temporay Redirect- See Also:
-
-
Constructor Details
-
HttpUtils
public HttpUtils()
-
-
Method Details
-
initialize
- Specified by:
initializein interfaceInitializable- Throws:
Exception
-
dispose
- Specified by:
disposein interfaceDisposable
-
executeRequest
public static <T> T executeRequest(String httpUrl, String userAgent, String method, int timeout, int readTimeOut, boolean followRedirects, Map<String, String> requestHeaders, org.apache.hc.core5.http.io.HttpClientResponseHandler<? extends T> responseHandler) throws IOExceptionPrepare the connection to the remote URL- Type Parameters:
T- The type of the value returned by the execution- Parameters:
httpUrl- The HTTP URLuserAgent- The user agent. Can be null.method- The method for the URL request. Can be null.timeout- The connection timeout in milliseconds. Set to -1 to not set a timeout.readTimeOut- The read timeout in milliseconds. Set to -1 to not set a timeout.followRedirects- Sets to true to follow HTTP redirectsrequestHeaders- The request headers.responseHandler- The handler that transform the HTTP response into the execution response- Returns:
- The result obtained by the response handler
- Throws:
IOException- if an I/O exception occurs or the URL is invalid
-
executeRequest
public static <T> T executeRequest(String httpUrl, String userAgent, String method, int timeout, int readTimeOut, boolean followRedirects, Map<String, String> requestHeaders, boolean ignoreRestriction, org.apache.hc.core5.http.io.HttpClientResponseHandler<? extends T> responseHandler) throws IOExceptionPrepare the connection to the remote URL- Type Parameters:
T- The type of the value returned by the execution- Parameters:
httpUrl- The HTTP URLuserAgent- The user agent. Can be null.method- The method for the URL request. Can be null.timeout- The connection timeout in milliseconds. Set to -1 to not set a timeout.readTimeOut- The read timeout in milliseconds. Set to -1 to not set a timeout.followRedirects- Sets to true to follow HTTP redirectsrequestHeaders- The request headers.ignoreRestriction- true to bypass security restriction on HTTP clientresponseHandler- The handler that transform the HTTP response into the execution response- Returns:
- The result obtained by the response handler
- Throws:
IOException- if an I/O exception occurs or the URL is invalid
-
buildRequest
public static org.apache.hc.core5.http.ClassicHttpRequest buildRequest(URI uri, String userAgent, String method, int timeout, int readTimeOut, boolean followRedirects, int maxRedirects, Map<String, String> requestHeaders) throws IOExceptionbuild a request to the remote URI- Parameters:
uri- The URI to requestuserAgent- The user agent. Can be null.method- The method for the URL request. Can be null.timeout- The connection timeout in milliseconds. Set to -1 to not set a timeout.readTimeOut- The read timeout in milliseconds. Set to -1 to not set a timeout.followRedirects- Sets to true to follow HTTP redirectsmaxRedirects- The maximum number of redirects to followrequestHeaders- The request headers.- Returns:
- The HTTP request
- Throws:
IOException- if an I/O exception occurs
-
checkHttpUrl
public static HttpUtils.HttpCheckReport checkHttpUrl(String httpUrl, String userAgent, String method, int timeout, int readTimeOut, boolean followRedirects) Check the HTTP url- Parameters:
httpUrl- The HTTP url to testuserAgent- The user agent. Can be null.method- The method for teh URL request. Can be null.timeout- The connection timeout in milliseconds. Set to -1 to not set a timeout.readTimeOut- The read timeout in milliseconds. Set to -1 to not set a timeout.followRedirects- Sets to true to follow HTTP redirects- Returns:
- The URL connection
-
checkHttpUrl
public static HttpUtils.HttpCheckReport checkHttpUrl(String httpUrl, String userAgent, String method, int timeout, int readTimeOut, boolean followRedirects, Map<String, String> requestHeaders) Check the HTTP url- Parameters:
httpUrl- The url to testuserAgent- The user agent. Can be null.method- The method for teh URL request. Can be null.timeout- The connection timeout in milliseconds. Set to -1 to not set a timeout.readTimeOut- The read timeout in milliseconds. Set to -1 to not set a timeout.followRedirects- Sets to true to follow HTTP redirectsrequestHeaders- The request headers.- Returns:
- The URL connection
-
checkHttpUrl
public static HttpUtils.HttpCheckReport checkHttpUrl(URL url, String userAgent, String method, int timeout, int readTimeOut, boolean followRedirects, Map<String, String> requestHeaders) Check the HTTP url- Parameters:
url- The url to testuserAgent- The user agent. Can be null.method- The method for teh URL request. Can be null.timeout- The connection timeout in milliseconds. Set to -1 to not set a timeout.readTimeOut- The read timeout in milliseconds. Set to -1 to not set a timeout.followRedirects- Sets to true to follow HTTP redirectsrequestHeaders- The request headers.- Returns:
- The URL connection
-
checkHttpUrl
Method to check a HTTP url from client side. The HTTP redirects will be followed.- Parameters:
httpUrl- the http url to check- Returns:
- the result of the check
-
createHttpClient
public static org.apache.hc.client5.http.impl.classic.CloseableHttpClient createHttpClient(int maxConnections, int timeout) Create ands return a configured, ready to use,HttpClient.
This method is well suited to create a client dedicated to a single route.- Parameters:
maxConnections- the maximum simultaneous connections. If zero or negative, defaults to HttpClient's default values.timeout- the socket and connect timeout, in seconds. If zero or negative, defaults to HttpClient's default values.- Returns:
- a configured
HttpClient
-
createHttpClient
public static org.apache.hc.client5.http.impl.classic.CloseableHttpClient createHttpClient(int maxConnections, int timeout, boolean blockInternal) Create ands return a configured, ready to use,HttpClient.
This method is well suited to create a client dedicated to a single route.- Parameters:
maxConnections- the maximum simultaneous connections. If zero or negative, defaults to HttpClient's default values.timeout- the socket and connect timeout, in seconds. If zero or negative, defaults to HttpClient's default values.blockInternal- true to prevent request to restricted IP address (as defined in configuration)- Returns:
- a configured
HttpClient
-
sanitize
Strips the end of an uri to remove "*.html" ends and "/" ending characters- Parameters:
uri- The uri to edit- Returns:
- The edited uri
-