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) Appends one or more query parameters to an existing URI.static URIaddQueryParameters(URI uri, List<List<String>> queryParams) Appends one or more query parameters to an existing URI.static StringbuildClearCacheUrl(String mode, String pi, String rootUrl, String webApiToken) static String[]callUrlGET(String url) callUrlGET.static StringgetIpAddress(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(HttpServletRequest request) Check if the request Contains a 'User-Agent' header matching the regex configured inConfiguration.getCrawlerDetectionRegex().static booleanisIpAddressLocalhost(String address) static booleanisRedirectUrlAllowed(String redirectUrl, String applicationUrl) Checks whether the given redirect URL is allowed.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
-
isRedirectUrlAllowed
Checks whether the given redirect URL is allowed. A URL is allowed if it starts with the given application base URL or if its host is in the configured redirect whitelist.- Parameters:
redirectUrl- URL to checkapplicationUrl- Application base URL (may be null)- Returns:
- true if allowed; false otherwise
-
callUrlGET
callUrlGET.- Parameters:
url- URL to call via HTTP GET- 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:
- the HTTP response body as a string
- 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- URL to call via HTTP POSTheaders- HTTP request headers as name-value pairsparams- form parameters sent in the request bodycookies- cookies to include with the requestcontentType- MIME type for the request bodystringBody- Optional entity content.file- optional file to upload as multipart body- Returns:
- the HTTP response body as a string
- 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- URL to call via HTTP DELETEheaders- HTTP request headers as name-value pairsparams- form parameters sent in the request bodycookies- cookies to include with the requeststringBody- Optional entity content.- Returns:
- the HTTP response body as a string
- 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, MessagingException Sends an email to with the given subject and body to the given recipient list.- Parameters:
recipients- list of primary recipient email addressescc- list of CC recipient email addressesbcc- list of BCC recipient email addressessubject- email subject linebody- email body text (HTML)- Returns:
- true if mail sent successfully; false otherwise
- Throws:
UnsupportedEncodingException- if any.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- incoming HTTP servlet request to inspect- Returns:
- the resolved remote IP address of the client
-
parseMultipleIpAddresses
parseMultipleIpAddresses. If the given string contains more than one address, return the first one, otherwise the entire string- Parameters:
address- IP address- Returns:
- the first IP address from a comma-separated list, or the entire string if it contains only one address
-
scrambleEmailAddress
Replaces most of the given email address with asterisks.- Parameters:
email- email address to scramble- 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- cache clear mode (all, content, thumbs, pdf)pi- persistent identifier of the recordrootUrl- base URL of the viewer applicationwebApiToken- authentication token for the web API- 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- subnet mask in CIDR notation to validate- Returns:
- true if subnetMask valid; false otherwise
-
isCrawlerBotRequest
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- incoming HTTP servlet request to inspect- Returns:
- true if the request is made by a web crawler
-
addQueryParameters
public static String addQueryParameters(String uriString, List<List<String>> queryParams) throws URISyntaxException Appends 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
Appends one or more query parameters to an existing URI.- Parameters:
uri- the base URI to which query parameters are appendedqueryParams- 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
-