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

    • callUrlGET

      public static String[] callUrlGET(String url)

      callUrlGET.

      Parameters:
      url - a String 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

      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:
      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,String> params, Map<String,String> cookies, String contentType, String stringBody, File file) throws IOException

      getWebContentPOST.

      Parameters:
      url - a String object.
      headers -
      params - a Map object.
      cookies - a Map 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,String> params, Map<String,String> cookies, String stringBody) throws IOException

      getWebContentDELETE.

      Parameters:
      url - a String object.
      headers -
      params - a Map object.
      cookies - a Map 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 - a List object.
      cc -
      bcc -
      subject - a String object.
      body - a String object.
      Returns:
      true if mail sent successfully; false otherwise
      Throws:
      UnsupportedEncodingException - if any.
      javax.mail.MessagingException - if any.
      jakarta.mail.MessagingException
    • getIpAddress

      public static String getIpAddress(javax.servlet.http.HttpServletRequest request)
      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 - a HttpServletRequest object.
      Returns:
      a String object.
    • parseMultipleIpAddresses

      protected static String parseMultipleIpAddresses(String address)

      parseMultipleIpAddresses.

      Parameters:
      address - IP address
      Returns:
      a String object.
    • scrambleEmailAddress

      public static String scrambleEmailAddress(String email)
      Replaces most of the given email address with asterisks.
      Parameters:
      email -
      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 -
      pi -
      rootUrl -
      webApiToken -
      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 -
      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 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 org.apache.catalina.valves.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 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)
      Append one or more query parameters to an existing URI
      Parameters:
      uri - the URI
      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