java.lang.Object
de.iip_ecosphere.platform.support.aas.basyx1_5.basyx.BaSyxHTTPServer

public class BaSyxHTTPServer extends Object
Starter Class for Apache Tomcat HTTP server that adds the provided servlets and respective mappings on startup. Taken over from BaSyx code to enable authentication adjustments prevented by private methods there.
Author:
pschorn, espen, haque, danish
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final de.iip_ecosphere.platform.support.logging.Logger
     
    private final org.apache.catalina.startup.Tomcat
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    BaSyxHTTPServer(org.eclipse.basyx.vab.protocol.http.server.BaSyxContext context, de.iip_ecosphere.platform.support.aas.SetupSpec spec, de.iip_ecosphere.platform.support.aas.SetupSpec.AasComponent component)
    Constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    private void
    addChildContextsIfConfigured(org.eclipse.basyx.vab.protocol.http.server.BaSyxContext context)
    Adds a child context if such were configured.
    private Consumer<? super org.eclipse.basyx.vab.protocol.http.server.BaSyxChildContext>
    Adds a child context to tomcat.
    private void
    addFilterChainProxyFilterToContext(org.apache.catalina.Context context, org.springframework.security.web.FilterChainProxy filterChainProxy)
    Adds a filter chain proxy to context.
    private void
    addNewServletAndMappingToTomcatEnvironment(org.eclipse.basyx.vab.protocol.http.server.BaSyxContext context, org.apache.catalina.Context rootCtx, Map.Entry<String,javax.servlet.http.HttpServlet> entry)
    Adds new servlets and mappings.
    private void
    addSecurityFiltersToContext(org.apache.catalina.Context context, org.eclipse.basyx.vab.protocol.http.server.JwtBearerTokenAuthenticationConfiguration jwtBearerTokenAuthenticationConfiguration)
    Adds security filters to context.
    private void
    configureCorsOrigin(org.eclipse.basyx.vab.protocol.http.server.BaSyxContext context, javax.servlet.http.HttpServlet servlet)
    Configures the CORS origin.
    private void
    Configures the health endpoint.
    private void
    configureSslConnector(org.eclipse.basyx.vab.protocol.http.server.BaSyxContext context, org.apache.catalina.connector.Connector httpsConnector)
    SSL Configuration for SSL connector.
    private org.springframework.security.oauth2.server.resource.web.BearerTokenAuthenticationFilter
    createBearerTokenAuthenticationFilter(org.eclipse.basyx.vab.protocol.http.server.JwtBearerTokenAuthenticationConfiguration jwtBearerTokenAuthenticationConfiguration)
    Creates a bearer token authentication filter.
    private org.springframework.security.web.access.ExceptionTranslationFilter
    Creates an exception translation filter.
    private org.springframework.security.web.FilterChainProxy
    createFilterChainProxy(org.eclipse.basyx.vab.protocol.http.server.JwtBearerTokenAuthenticationConfiguration jwtBearerTokenAuthenticationConfiguration)
    Creates a filter chain proxy for token authentication.
    private org.apache.tomcat.util.descriptor.web.FilterDef
    createFilterChainProxyFilterDefinition(org.springframework.security.web.FilterChainProxy filterChainProxy)
    Creates a filter chain proxy filter definition.
    private org.apache.tomcat.util.descriptor.web.FilterMap
    Creates a filter chain proxy filter map.
    private org.springframework.security.web.firewall.HttpFirewall
    Creates the HTTP firewall.
    private org.springframework.security.oauth2.jwt.JwtClaimValidator<Collection<String>>
    Creates a JWT claim validator for required audience.
    private org.springframework.security.oauth2.jwt.JwtDecoder
    createJwtDecoder(String issuerUri, String jwkSetUri, String requiredAud)
    Creates a JWT decoder.
    private org.springframework.security.oauth2.core.OAuth2TokenValidator<org.springframework.security.oauth2.jwt.Jwt>
    createOAuth2TokenValidator(String issuerUri, String requiredAud)
    Creates an OAuth2 token validator.
    private org.springframework.security.web.SecurityFilterChain
    createSecurityFilterChain(org.eclipse.basyx.vab.protocol.http.server.JwtBearerTokenAuthenticationConfiguration jwtBearerTokenAuthenticationConfiguration)
    Creates a security filter chain for OAuth2.
    boolean
    Returns a value indicating whether the server is currently running.
    private boolean
    isCorsOriginDefined(org.eclipse.basyx.vab.protocol.http.server.BaSyxContext context)
    Returns whether CORS origin is defined.
    private boolean
    Returns whether the tomcat server is running.
    void
    This Method stops and destroys the tomcat instance.
    void
    Starts the server in a new thread to avoid blocking the main thread
    private void
    Stops it if the tomcat server is running.

    Methods inherited from class java.lang.Object

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

    • LOGGER

      private static final de.iip_ecosphere.platform.support.logging.Logger LOGGER
    • tomcat

      private final org.apache.catalina.startup.Tomcat tomcat
  • Constructor Details

    • BaSyxHTTPServer

      public BaSyxHTTPServer(org.eclipse.basyx.vab.protocol.http.server.BaSyxContext context, de.iip_ecosphere.platform.support.aas.SetupSpec spec, de.iip_ecosphere.platform.support.aas.SetupSpec.AasComponent component)
      Constructor. Create new Tomcat instance and add the provided servlet mappings
      Parameters:
      context - Basyx context with of url mappings to HTTPServlet
      spec - the setup specification
      component - the component to create the server for
  • Method Details

    • addChildContextsIfConfigured

      private void addChildContextsIfConfigured(org.eclipse.basyx.vab.protocol.http.server.BaSyxContext context)
      Adds a child context if such were configured.
      Parameters:
      context - the BaSyx context
    • addChildContextToTomcat

      private Consumer<? super org.eclipse.basyx.vab.protocol.http.server.BaSyxChildContext> addChildContextToTomcat()
      Adds a child context to tomcat.
      Returns:
      the child context consumer
    • configureHealthEndpoint

      private void configureHealthEndpoint()
      Configures the health endpoint.
    • addNewServletAndMappingToTomcatEnvironment

      private void addNewServletAndMappingToTomcatEnvironment(org.eclipse.basyx.vab.protocol.http.server.BaSyxContext context, org.apache.catalina.Context rootCtx, Map.Entry<String,javax.servlet.http.HttpServlet> entry)
      Adds new servlets and mappings.
      Parameters:
      context - the BaSyx context
      rootCtx - the root context
      entry - the servlet entries
    • configureCorsOrigin

      private void configureCorsOrigin(org.eclipse.basyx.vab.protocol.http.server.BaSyxContext context, javax.servlet.http.HttpServlet servlet)
      Configures the CORS origin.
      Parameters:
      context - the context
      servlet - the servlet
    • isCorsOriginDefined

      private boolean isCorsOriginDefined(org.eclipse.basyx.vab.protocol.http.server.BaSyxContext context)
      Returns whether CORS origin is defined.
      Parameters:
      context - the context to look into
      Returns:
      true for defined, false else
    • addSecurityFiltersToContext

      private void addSecurityFiltersToContext(org.apache.catalina.Context context, org.eclipse.basyx.vab.protocol.http.server.JwtBearerTokenAuthenticationConfiguration jwtBearerTokenAuthenticationConfiguration)
      Adds security filters to context.
      Parameters:
      context - the context to modify
      jwtBearerTokenAuthenticationConfiguration - the bearer token authentication setup
    • addFilterChainProxyFilterToContext

      private void addFilterChainProxyFilterToContext(org.apache.catalina.Context context, org.springframework.security.web.FilterChainProxy filterChainProxy)
      Adds a filter chain proxy to context.
      Parameters:
      context - the context to modify
      filterChainProxy - the proxy
    • createFilterChainProxyFilterMap

      private org.apache.tomcat.util.descriptor.web.FilterMap createFilterChainProxyFilterMap()
      Creates a filter chain proxy filter map.
      Returns:
      the map
    • createFilterChainProxyFilterDefinition

      private org.apache.tomcat.util.descriptor.web.FilterDef createFilterChainProxyFilterDefinition(org.springframework.security.web.FilterChainProxy filterChainProxy)
      Creates a filter chain proxy filter definition.
      Parameters:
      filterChainProxy - the proxy
      Returns:
      the filter definition
    • createFilterChainProxy

      private org.springframework.security.web.FilterChainProxy createFilterChainProxy(org.eclipse.basyx.vab.protocol.http.server.JwtBearerTokenAuthenticationConfiguration jwtBearerTokenAuthenticationConfiguration)
      Creates a filter chain proxy for token authentication.
      Parameters:
      jwtBearerTokenAuthenticationConfiguration - the authentication configuration
      Returns:
      the filter chain proxy
    • createHttpFirewall

      private org.springframework.security.web.firewall.HttpFirewall createHttpFirewall()
      Creates the HTTP firewall.
      Returns:
      the firewall instance.
    • createSecurityFilterChain

      private org.springframework.security.web.SecurityFilterChain createSecurityFilterChain(org.eclipse.basyx.vab.protocol.http.server.JwtBearerTokenAuthenticationConfiguration jwtBearerTokenAuthenticationConfiguration)
      Creates a security filter chain for OAuth2.
      Parameters:
      jwtBearerTokenAuthenticationConfiguration - the authentication configuration
      Returns:
      the filter chain
    • createExceptionTranslationFilter

      private org.springframework.security.web.access.ExceptionTranslationFilter createExceptionTranslationFilter()
      Creates an exception translation filter.
      Returns:
      the filter
    • createBearerTokenAuthenticationFilter

      private org.springframework.security.oauth2.server.resource.web.BearerTokenAuthenticationFilter createBearerTokenAuthenticationFilter(org.eclipse.basyx.vab.protocol.http.server.JwtBearerTokenAuthenticationConfiguration jwtBearerTokenAuthenticationConfiguration)
      Creates a bearer token authentication filter.
      Parameters:
      jwtBearerTokenAuthenticationConfiguration - the token authentication configuration
      Returns:
      the filter
    • createJwtDecoder

      private org.springframework.security.oauth2.jwt.JwtDecoder createJwtDecoder(String issuerUri, String jwkSetUri, @Nullable String requiredAud)
      Creates a JWT decoder.
      Parameters:
      issuerUri - the issuer URI
      jwkSetUri - the JWK Set URI
      requiredAud - the required audience for the OAuth2 token validator
      Returns:
      the decoder
    • createOAuth2TokenValidator

      private org.springframework.security.oauth2.core.OAuth2TokenValidator<org.springframework.security.oauth2.jwt.Jwt> createOAuth2TokenValidator(String issuerUri, @Nullable String requiredAud)
      Creates an OAuth2 token validator.
      Parameters:
      issuerUri - the issuer URI
      requiredAud - the required audience for the OAuth2 token validator
      Returns:
      the validator
    • createJwtClaimValidatorForRequiredAudience

      private org.springframework.security.oauth2.jwt.JwtClaimValidator<Collection<String>> createJwtClaimValidatorForRequiredAudience(String requiredAud)
      Creates a JWT claim validator for required audience.
      Parameters:
      requiredAud - the required audience for the OAuth2 token validator
      Returns:
      the claim validator
    • configureSslConnector

      private void configureSslConnector(org.eclipse.basyx.vab.protocol.http.server.BaSyxContext context, org.apache.catalina.connector.Connector httpsConnector)
      SSL Configuration for SSL connector.
      Parameters:
      context -
      httpsConnector -
    • start

      public void start()
      Starts the server in a new thread to avoid blocking the main thread

      This method blocks until the server is up and running.

      If an error occurs during server startup the process is aborted and the method returns immediately. hasEnded() returns true in this case.
      This behavior can be disabled by setting the system property org.apache.catalina.startup.EXIT_ON_INIT_FAILURE = false, for instance with the -D command line option when launching the JVM, or through System.setProperty(String, String) (before the first call to BaSyxHTTPServer). In this case the startup is finished regardless of any errors and subsequent calls to hasEnded() return false, but the server might be left in an undefined and non-functional state.

      TODO: Throw exception upon startup failure. This is a breaking change, so wait until next major version.

    • stopTomcatServerIfRunningAlready

      private void stopTomcatServerIfRunningAlready() throws org.apache.catalina.LifecycleException
      Stops it if the tomcat server is running.
      Throws:
      org.apache.catalina.LifecycleException - if stopping tomcat fails
    • isTomcatServerRunning

      private boolean isTomcatServerRunning()
      Returns whether the tomcat server is running.
      Returns:
      true for running, false else
    • shutdown

      public void shutdown()
      This Method stops and destroys the tomcat instance. This is important since Tomcat would be already bound to port 8080 when new tests are run that require a start of tomcat
    • hasEnded

      public boolean hasEnded()
      Returns a value indicating whether the server is currently running.
      Returns:
      false if the server is running, true otherwise.