Package io.goobi.viewer.controller
Class NetTools
java.lang.Object
io.goobi.viewer.controller.NetTools
Utility methods for HTTP operations, mail, etc.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringConstantADDRESS_LOCALHOST_IPV4="127.0.0.1"static final StringConstantADDRESS_LOCALHOST_IPV6="0:0:0:0:0:0:0:1"static final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final String -
Method Summary
Modifier and TypeMethodDescriptionstatic StringaddQueryParameters(String uriString, List<List<String>> queryParams) Append one or more query parameters to an existing URIstatic URIaddQueryParameters(URI uri, List<List<String>> queryParams) Append one or more query parameters to an existing URIstatic StringbuildClearCacheUrl(String mode, String pi, String rootUrl, String webApiToken) static String[]callUrlGET(String url) callUrlGET.static StringgetIpAddress(jakarta.servlet.http.HttpServletRequest request) Returns the remote IP address of the given HttpServletRequest.static StringgetWebContentDELETE(String url, Map<String, String> headers, Map<String, String> params, Map<String, String> cookies, String stringBody) getWebContentDELETE.static StringgetWebContentGET(String url) static StringgetWebContentGET(String url, int timeout) getWebContentGET.static StringgetWebContentPOST(String url, Map<String, String> headers, Map<String, String> params, Map<String, String> cookies, String contentType, String stringBody, File file) getWebContentPOST.static booleanisCrawlerBotRequest(jakarta.servlet.http.HttpServletRequest request) Check if the request Contains a 'User-Agent' header matching the regex configured inConfiguration.getCrawlerDetectionRegex().static booleanisIpAddressLocalhost(String address) static booleanisStatusOk(String string) return true if the given string is a whole number between 200 and 399 (inclusive)static booleanisValidSubnetMask(String subnetMask) protected static StringparseMultipleIpAddresses(String address) parseMultipleIpAddresses.static booleanSends an email to with the given subject and body to the given recipient list.static StringscrambleEmailAddress(String email) Replaces most of the given email address with asterisks.static StringscrambleIpAddress(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- aStringobject.- 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:
Stringcontent fetched from the given url- Throws:
IOExceptionHTTPException
-
getWebContentGET
getWebContentGET.
- Parameters:
url- URL to calltimeout- Custom timeout- Returns:
- a
Stringobject. - 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- aStringobject.headers-params- aMapobject.cookies- aMapobject.contentType-stringBody- Optional entity content.file-- Returns:
- a
Stringobject. - 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- aStringobject.headers-params- aMapobject.cookies- aMapobject.stringBody- Optional entity content.- Returns:
- a
Stringobject. - 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- aListobject.cc-bcc-subject- aStringobject.body- aStringobject.- 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 first in the list is returned.- Parameters:
request- aHttpServletRequestobject.- Returns:
- a
Stringobject.
-
parseMultipleIpAddresses
parseMultipleIpAddresses. If the given string contains more than one address, return the first one, otherwise the entire string
- Parameters:
address- IP address- Returns:
- a
Stringobject.
-
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(jakarta.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
-