Class NetTools

java.lang.Object
io.goobi.viewer.controller.NetTools

public final class NetTools extends Object
Utility methods for HTTP operations, mail, etc.
  • Field Details

  • Method Details

    • isRedirectUrlAllowed

      public static boolean isRedirectUrlAllowed(String redirectUrl, String applicationUrl)
      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 check
      applicationUrl - Application base URL (may be null)
      Returns:
      true if allowed; false otherwise
    • callUrlGET

      public static String[] callUrlGET(String url)
      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

      public static String getWebContentGET(String url) throws IOException, HTTPException
      Parameters:
      url - URL to call
      Returns:
      String content fetched from the given url
      Throws:
      IOException
      HTTPException
    • getWebContentGET

      public static String getWebContentGET(String url, int timeout) throws IOException, HTTPException
      getWebContentGET.
      Parameters:
      url - URL to call
      timeout - 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,String> params, Map<String,String> cookies, String contentType, String stringBody, File file) throws IOException
      getWebContentPOST.
      Parameters:
      url - URL to call via HTTP POST
      headers - HTTP request headers as name-value pairs
      params - form parameters sent in the request body
      cookies - cookies to include with the request
      contentType - MIME type for the request body
      stringBody - 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,String> params, Map<String,String> cookies, String stringBody) throws IOException
      getWebContentDELETE.
      Parameters:
      url - URL to call via HTTP DELETE
      headers - HTTP request headers as name-value pairs
      params - form parameters sent in the request body
      cookies - cookies to include with the request
      stringBody - 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 addresses
      cc - list of CC recipient email addresses
      bcc - list of BCC recipient email addresses
      subject - email subject line
      body - email body text (HTML)
      Returns:
      true if mail sent successfully; false otherwise
      Throws:
      UnsupportedEncodingException - if any.
      MessagingException - if any.
    • getIpAddress

      public static String getIpAddress(HttpServletRequest request)
      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

      protected static String parseMultipleIpAddresses(String address)
      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

      public static String scrambleEmailAddress(String email)
      Replaces most of the given email address with asterisks.
      Parameters:
      email - email address to scramble
      Returns:
      Scrambled email address
    • scrambleIpAddress

      public static String scrambleIpAddress(String address)
      Replaces most the last two segments of the given IPv4 address with placeholders.
      Parameters:
      address - IP address
      Returns:
      Scrambled IP address
    • isIpAddressLocalhost

      public static boolean isIpAddressLocalhost(String address)
      Parameters:
      address - IP address
      Returns:
      true if given address is a localhost address; false otherwise
    • buildClearCacheUrl

      public static String buildClearCacheUrl(String mode, String pi, String rootUrl, String webApiToken)
      Parameters:
      mode - cache clear mode (all, content, thumbs, pdf)
      pi - persistent identifier of the record
      rootUrl - base URL of the viewer application
      webApiToken - authentication token for the web API
      Returns:
      Generated URL
    • isStatusOk

      public static boolean isStatusOk(String string)
      Return true if the given string is a whole number between 200 and 399 (inclusive).
      Parameters:
      string - HTTP status as String
      Returns:
      true if HTTP code is in the 200-399 range; false otherwise
    • isValidSubnetMask

      public static boolean isValidSubnetMask(String subnetMask)
      Parameters:
      subnetMask - subnet mask in CIDR notation to validate
      Returns:
      true if subnetMask valid; false otherwise
    • isCrawlerBotRequest

      public static boolean isCrawlerBotRequest(HttpServletRequest request)
      Check if the request Contains a 'User-Agent' header matching the regex configured in Configuration.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 string
      queryParams - 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

      public static URI addQueryParameters(URI uri, List<List<String>> queryParams)
      Appends one or more query parameters to an existing URI.
      Parameters:
      uri - the base URI to which query parameters are appended
      queryParams - 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