Class WSHelper


  • public class WSHelper
    extends java.lang.Object
    Helper class for common functions for Jetty.
    Author:
    Christopher Mindus
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int MIN_GZIP_SIZE
      The minimum size for data before it is GZip deflate enabled: 128 bytes.
    • Constructor Summary

      Constructors 
      Constructor Description
      WSHelper()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String getDomain​(HttpServletRequest request, int check, java.lang.String originator)
      Helper to get the domain name from a servlet request and to optionally check if there is a matching "host" header for it.
      static java.lang.String getDomainFromHostHeader​(HttpServletRequest request)
      Extracts the domain name from an HttpServletRequest from the "host" header field.
      static java.lang.String getDomainFromURL​(HttpServletRequest request)
      Extracts the domain name from an HttpServletRequest from the URL.
      static java.lang.String getExternalBaseServerURL​(java.lang.String extServerURL, java.lang.String bind, int port, int securePort)
      Returns the external base server URL with protocol, host name and potentially port number.
      static java.lang.String validateExternalHostName​(java.lang.String urlString)
      Validates an external host URL.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • MIN_GZIP_SIZE

        public static int MIN_GZIP_SIZE
        The minimum size for data before it is GZip deflate enabled: 128 bytes.

        FYI: the break-even size is 23, and a common minimum is 32.

    • Constructor Detail

      • WSHelper

        public WSHelper()
    • Method Detail

      • getExternalBaseServerURL

        public static java.lang.String getExternalBaseServerURL​(java.lang.String extServerURL,
                                                                java.lang.String bind,
                                                                int port,
                                                                int securePort)
        Returns the external base server URL with protocol, host name and potentially port number.

        The name will be taken from:

        1. external host name if specified,
        2. resolved bind address if specified and possible to retrieve,
        3. bind address if specified,
        4. resolved local host name if possible to retrieve,
        5. local host name if possible to retrieve,
        6. "localhost" last.

        If "localhost" is used, a warning message is logged about it.

        The protocol to use will be secure HTTPS if possible, otherwise the HTTP, even if it is zero for none.

        Parameters:
        extServerURL - The external server URL, null or empty for none.
        bind - Bind address specified, null or empty for none.
        port - HTTP port number, zero for none.
        securePort - HTTPS port number, zero for none.
      • validateExternalHostName

        public static java.lang.String validateExternalHostName​(java.lang.String urlString)
        Validates an external host URL.
        Parameters:
        urlString - The external host URL to validate, null or empty for none.
        Returns:
        null if valid or none is specified, otherwise an error reason.
      • getDomain

        public static java.lang.String getDomain​(HttpServletRequest request,
                                                 int check,
                                                 java.lang.String originator)
        Helper to get the domain name from a servlet request and to optionally check if there is a matching "host" header for it. In case there is no matching "host" header, logging can be performed.
        Parameters:
        request - The servlet request.
        check - How to perform the check:
        0 = no warnings, no logging, just return it,
        1 = warning if "host" doesn't match, but return it,
        2 = error if "host" doesn't match, and return null.
        originator - String to mark as originator when logging error.
        Returns:
        The domain name or "host" header field, null if this fails.
      • getDomainFromURL

        public static java.lang.String getDomainFromURL​(HttpServletRequest request)
        Extracts the domain name from an HttpServletRequest from the URL.
        Parameters:
        request - The HttpServletRequest request.
        Returns:
        The domain name, or "unknown" if the request URL/URI is formatted incorrectly.
      • getDomainFromHostHeader

        public static java.lang.String getDomainFromHostHeader​(HttpServletRequest request)
        Extracts the domain name from an HttpServletRequest from the "host" header field.
        Parameters:
        request - The HttpServletRequest request.
        Returns:
        The domain name, or null for missing "host" header field.