Class WSHelper

java.lang.Object
com.iizix.jetty.WSHelper

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

    • DEBUG

      public static final boolean DEBUG
      Debug flag.
  • Constructor Details

    • WSHelper

      public WSHelper()
  • Method Details

    • appendToPath

      public static URI appendToPath(URI uri, String suffix) throws URISyntaxException
      Creates a new URI with the path + suffix.
      Parameters:
      uri - The original URI.
      suffix - The suffix (e.g. file extension ".gz").
      Returns:
      The new URI.
      Throws:
      URISyntaxException - If the suffix is null, if the URI is opaque and therefore has no path to append to (e.g. a "jar:" URI), or if the resulting URI is invalid (the last should not occur when the suffix is just a normal file extension).
    • getExternalBaseServerURL

      public static String getExternalBaseServerURL(String extServerURL, String bind, int port, int securePort, boolean hasExtSSL)
      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.
      hasExtSSL - HTTP port is externally secured.
    • validateExternalHostName

      public static String validateExternalHostName(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 String getDomain(HttpServletRequest request, int check, 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 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.
    • isLoopbackOrigin

      public static boolean isLoopbackOrigin(String origin)
      Tells whether an Origin is a loopback one: http or https on localhost, 127.0.0.1 or [::1], any port or none. A development server and the Designer's internal web server grant them for CORS (CC-FIX-SB-CORS): the pages one development server serves on a developer's machine call another on another port of it.
      Parameters:
      origin - The origin, may be null.
      Returns:
      true for a loopback origin.
    • getDomainFromHostHeader

      public static String getDomainFromHostHeader(HttpServletRequest request)
      Extracts the domain name from an HttpServletRequest from the "host" header field (removing the port specification - keeping in mind that it could be an IPv6 address).

      If the header field 'host' is missing, the return value will be set to request.getServerName().

      Parameters:
      request - The HttpServletRequest request.
      Returns:
      The domain name, or null for missing "host" header field and failure to get it using request.getServerName().
    • isSecure

      public static boolean isSecure(HttpServletRequest request)
      Checks if a request arrived on a secure port: TLS is terminated either by this server (request.isSecure()) or by a reverse proxy the operator declared in the port configuration (PortConfig.isFrontEndSecured). Behind a reverse proxy (e.g. Caddy) the connection to Jetty is plain HTTP, so request.isSecure() is false although the client is on TLS; the port configuration is the only thing a client cannot forge, so no request header (X-Forwarded-Proto, Forwarded) is ever consulted.

      The connector is reached as ServletApiRequest to ServletContextRequest to its connection meta data, tested for IServerConnector. When any link does not match, the answer is request.isSecure() alone.

      Parameters:
      request - The servlet request.
      Returns:
      true if the request is secure by either rule, false otherwise.
    • isThroughFrontEnd

      public static boolean isThroughFrontEnd(HttpServletRequest request)
      Checks if a request came through the front-end proxy of a port configured as front-end secured (PortConfig.isFrontEndSecured). The port configuration alone cannot tell: the same connector may also be reached directly, e.g. http://192.168.10.100:8080 on the LAN, and such a client uses this connector's own scheme and port. The Host header tells: a request whose Host names this connector's own port came directly; a Host without a port (the proxy's default port) or with another port came through the proxy.
      Parameters:
      request - The servlet request.
      Returns:
      true if the connector is front-end secured and the request came through the proxy.
    • getPortConfig

      public static PortConfig getPortConfig(HttpServletRequest request)
      Gets the port configuration of the connector that received a request.

      The connector is reached as ServletApiRequest to ServletContextRequest to its connection meta data, tested for IServerConnector.

      Parameters:
      request - The servlet request.
      Returns:
      The port configuration, or null when any link does not match or the connector has none.
    • getPortConfig

      public static PortConfig getPortConfig(Request request)
      Gets the port configuration of the connector that received a core request (CC-FIX-APP-DEFINITIONS): the connector of its connection meta data, tested for IServerConnector.
      Parameters:
      request - The core request, may be null.
      Returns:
      The port configuration, or null when the connector is none of ours or the request is null.
    • getClientPort

      public static int getClientPort(HttpServletRequest request)
      Gets the port the client used for a request: from the port configuration of the connector (PortConfig.getClientPort(): the external port, else 443 behind a front-end proxy that terminates TLS, else the local port), never from a request header. Behind a reverse proxy (e.g. Caddy) request.getServerPort() is the port of the plain HTTP hop (80 when the Host header has none), not the client's.
      Parameters:
      request - The servlet request.
      Returns:
      The client's port, or request.getServerPort() when the connector has no port configuration.