Package io.goobi.viewer.controller
Class NetTools
java.lang.Object
io.goobi.viewer.controller.NetTools
Utility methods for HTTP operations, mail, etc.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
ConstantADDRESS_LOCALHOST_IPV4="127.0.0.1"
static final String
ConstantADDRESS_LOCALHOST_IPV6="0:0:0:0:0:0:0:1"
static final String
static final String
static final String
static final String
static final String
static final String
static final String
static final String
-
Method Summary
Modifier and TypeMethodDescriptionstatic String
addQueryParameters
(String uriString, List<List<String>> queryParams) Append one or more query parameters to an existing URIstatic URI
addQueryParameters
(URI uri, List<List<String>> queryParams) Append one or more query parameters to an existing URIstatic String
buildClearCacheUrl
(String mode, String pi, String rootUrl, String webApiToken) static String[]
callUrlGET
(String url) callUrlGET.static String
getIpAddress
(javax.servlet.http.HttpServletRequest request) Returns the remote IP address of the given HttpServletRequest.static String
getWebContentDELETE
(String url, Map<String, String> headers, Map<String, String> params, Map<String, String> cookies, String stringBody) getWebContentDELETE.static String
getWebContentGET
(String url) static String
getWebContentGET
(String url, int timeout) getWebContentGET.static String
getWebContentPOST
(String url, Map<String, String> headers, Map<String, String> params, Map<String, String> cookies, String contentType, String stringBody, File file) getWebContentPOST.static boolean
isCrawlerBotRequest
(javax.servlet.http.HttpServletRequest request) Check if the request Contains a 'User-Agent' header matching the regex configured inConfiguration.getCrawlerDetectionRegex()
.static boolean
isIpAddressLocalhost
(String address) static boolean
isStatusOk
(String string) return true if the given string is a whole number between 200 and 399 (inclusive)static boolean
isValidSubnetMask
(String subnetMask) protected static String
parseMultipleIpAddresses
(String address) parseMultipleIpAddresses.static boolean
Sends an email to with the given subject and body to the given recipient list.static String
scrambleEmailAddress
(String email) Replaces most of the given email address with asterisks.static String
scrambleIpAddress
(String address) Replaces most the last two segments of the given IPv4 address with placeholders.
-
Field Details
-
ADDRESS_LOCALHOST_IPV4
ConstantADDRESS_LOCALHOST_IPV4="127.0.0.1"
- See Also:
-
ADDRESS_LOCALHOST_IPV6
ConstantADDRESS_LOCALHOST_IPV6="0:0:0:0:0:0:0:1"
- See Also:
-
HTTP_HEADER_CONTENT_DISPOSITION
- See Also:
-
HTTP_HEADER_CONTENT_TYPE
- See Also:
-
HTTP_HEADER_CONTENT_LENGTH
- See Also:
-
HTTP_HEADER_VALUE_ATTACHMENT_FILENAME
- See Also:
-
PARAM_CLEAR_CACHE_ALL
- See Also:
-
PARAM_CLEAR_CACHE_CONTENT
- See Also:
-
PARAM_CLEAR_CACHE_THUMBS
- See Also:
-
PARAM_CLEAR_CACHE_PDF
- See Also:
-
-
Method Details
-
callUrlGET
callUrlGET.
- Parameters:
url
- aString
object.- Returns:
- A String array with two elements. The first contains the HTTP status code, the second either the requested data (if status code is 200) or the error message.
-
getWebContentGET
- Parameters:
url
- URL to call- Returns:
String
content fetched from the given url- Throws:
IOException
HTTPException
-
getWebContentGET
getWebContentGET.
- Parameters:
url
- URL to calltimeout
- Custom timeout- Returns:
- a
String
object. - Throws:
org.apache.http.client.ClientProtocolException
- if any.IOException
- if any.HTTPException
- if any.
-
getWebContentPOST
public static String getWebContentPOST(String url, Map<String, String> headers, Map<String, throws IOExceptionString> params, Map<String, String> cookies, String contentType, String stringBody, File file) getWebContentPOST.
- Parameters:
url
- aString
object.headers
-params
- aMap
object.cookies
- aMap
object.contentType
-stringBody
- Optional entity content.file
-- Returns:
- a
String
object. - Throws:
org.apache.http.client.ClientProtocolException
- if any.IOException
- if any.
-
getWebContentDELETE
public static String getWebContentDELETE(String url, Map<String, String> headers, Map<String, throws IOExceptionString> params, Map<String, String> cookies, String stringBody) getWebContentDELETE.
- Parameters:
url
- aString
object.headers
-params
- aMap
object.cookies
- aMap
object.stringBody
- Optional entity content.- Returns:
- a
String
object. - Throws:
org.apache.http.client.ClientProtocolException
- if any.IOException
- if any.HTTPException
- if return code is not 200
-
postMail
public static boolean postMail(List<String> recipients, List<String> cc, List<String> bcc, String subject, String body) throws UnsupportedEncodingException, jakarta.mail.MessagingException Sends an email to with the given subject and body to the given recipient list.- Parameters:
recipients
- aList
object.cc
-bcc
-subject
- aString
object.body
- aString
object.- Returns:
- true if mail sent successfully; false otherwise
- Throws:
UnsupportedEncodingException
- if any.jakarta.mail.MessagingException
- if any.
-
getIpAddress
Returns the remote IP address of the given HttpServletRequest. If multiple addresses are found in x-forwarded-for, the last in the list is returned.- Parameters:
request
- aHttpServletRequest
object.- Returns:
- a
String
object.
-
parseMultipleIpAddresses
parseMultipleIpAddresses.
- Parameters:
address
- IP address- Returns:
- a
String
object.
-
scrambleEmailAddress
Replaces most of the given email address with asterisks.- Parameters:
email
-- Returns:
- Scrambled email address
-
scrambleIpAddress
Replaces most the last two segments of the given IPv4 address with placeholders.- Parameters:
address
- IP address- Returns:
- Scrambled IP address
-
isIpAddressLocalhost
- Parameters:
address
- IP address- Returns:
- true if given address is a localhost address; false otherwise
-
buildClearCacheUrl
- Parameters:
mode
-pi
-rootUrl
-webApiToken
-- Returns:
- Generated URL
-
isStatusOk
return true if the given string is a whole number between 200 and 399 (inclusive)- Parameters:
string
- HTTP status asString
- Returns:
- true if HTTP code is in the 200-399 range; false otherwise
-
isValidSubnetMask
- Parameters:
subnetMask
-- Returns:
- true if subnetMask valid; false otherwise
-
isCrawlerBotRequest
public static boolean isCrawlerBotRequest(javax.servlet.http.HttpServletRequest request) Check if the request Contains a 'User-Agent' header matching the regex configured inConfiguration.getCrawlerDetectionRegex()
. If it matches, the request is assumed be be from a web-crawler bot and not from a human. Identifying a web-crawler request via the CrawlerSessionManagerValve session attribute does not work for this purpose since it is only applied to the session after the first request- Parameters:
request
-- Returns:
- true if the request is made by a web crawler
-
addQueryParameters
public static String addQueryParameters(String uriString, List<List<String>> queryParams) throws URISyntaxException Append one or more query parameters to an existing URI- Parameters:
uriString
- the URI as a stringqueryParams
- A list of parameters. Each element of the list is assumed to be a list of size 2, whith the first element being the parameter name and the second the parameter value. If the list has only one item, it is assumed to be a parameter name without value, any elements after the second will be ignored- Returns:
- The URI with query params appended
- Throws:
URISyntaxException
-
addQueryParameters
Append one or more query parameters to an existing URI- Parameters:
uri
- the URIqueryParams
- A list of parameters. Each element of the list is assumed to be a list of size 2, whith the first element being the parameter name and the second the parameter value. If the list has only one item, it is assumed to be a parameter name without value, any elements after the second will be ignored- Returns:
- The URI with query params appended
- Throws:
URISyntaxException
-