Class SpincastUndertowServer

java.lang.Object
org.spincast.plugins.undertow.SpincastUndertowServer
All Implemented Interfaces:
Server

public class SpincastUndertowServer extends Object implements Server
Server implementation using Undertow.
  • Field Details

    • logger

      protected static final org.slf4j.Logger logger
    • UNDERTOW_EXCEPTION_CODE_REQUEST_TOO_LARGE

      public static final String UNDERTOW_EXCEPTION_CODE_REQUEST_TOO_LARGE
      See Also:
    • EXCHANGE_VARIABLE_EXCHANGE_COMPLETED

      public static final String EXCHANGE_VARIABLE_EXCHANGE_COMPLETED
  • Constructor Details

  • Method Details

    • getSpincastUndertowUtils

      protected SpincastUndertowUtils getSpincastUndertowUtils()
    • getConfig

      protected SpincastConfig getConfig()
    • getSpincastUndertowConfig

      protected SpincastUndertowConfig getSpincastUndertowConfig()
    • getFrontController

      protected FrontController getFrontController()
    • getSpincastUtils

      protected SpincastUtils getSpincastUtils()
    • getCookieFactory

      protected CookieFactory getCookieFactory()
    • getCorsHandlerFactory

      protected CorsHandlerFactory getCorsHandlerFactory()
    • getGzipCheckerHandlerFactory

      protected GzipCheckerHandlerFactory getGzipCheckerHandlerFactory()
    • getSkipResourceOnQueryStringHandlerFactory

      protected SkipResourceOnQueryStringHandlerFactory getSkipResourceOnQueryStringHandlerFactory()
    • getSpincastResourceHandlerFactory

      protected SpincastResourceHandlerFactory getSpincastResourceHandlerFactory()
    • getCacheBusterRemovalHandlerFactory

      protected CacheBusterRemovalHandlerFactory getCacheBusterRemovalHandlerFactory()
    • getFileClassPathResourceManagerFactory

      protected SpincastClassPathResourceManagerFactory getFileClassPathResourceManagerFactory()
    • getSpincastHttpAuthIdentityManagerFactory

      protected SpincastHttpAuthIdentityManagerFactory getSpincastHttpAuthIdentityManagerFactory()
    • getSpincastWebsocketEndpointFactory

      protected WebsocketEndpointFactory getSpincastWebsocketEndpointFactory()
    • getStaticResourcesServedByUrlPath

      protected Map<String,StaticResource<?>> getStaticResourcesServedByUrlPath()
    • getHttpAuthIdentityManagersByRealmName

      protected Map<String,SpincastHttpAuthIdentityManager> getHttpAuthIdentityManagersByRealmName()
    • getWebsocketEndpointsMap

      protected Map<String,WebsocketEndpoint> getWebsocketEndpointsMap()
    • getHttpAuthActiveRealms

      protected Map<String,String> getHttpAuthActiveRealms()
    • getSslContextFactory

      protected SSLContextFactory getSslContextFactory()
    • getHttpAuthenticationRealms

      public Map<String,String> getHttpAuthenticationRealms()
      Description copied from interface: Server
      Returns the existing HTTP authentication realms, the key being the realm's name and the value being the prefix path associated to this realm.

      The map is immutable.

      Specified by:
      getHttpAuthenticationRealms in interface Server
    • getFormParserFactory

      protected io.undertow.server.handlers.form.FormParserFactory getFormParserFactory()
    • createUndertowTempDir

      protected Path createUndertowTempDir()
    • isRunning

      public boolean isRunning()
      Description copied from interface: Server
      Is this server running (started)?
      Specified by:
      isRunning in interface Server
    • start

      public void start()
      Description copied from interface: Server
      Starts the server.
      Specified by:
      start in interface Server
    • getServerStartTryNbr

      protected int getServerStartTryNbr()
    • getStartServerSleepMilliseconds

      protected long getStartServerSleepMilliseconds()
    • getServerBuilder

      protected io.undertow.Undertow.Builder getServerBuilder()
    • addHttpListener

      protected void addHttpListener(io.undertow.Undertow.Builder builder, String serverHost, int httpServerPort)
    • addHttpsListener

      protected void addHttpsListener(io.undertow.Undertow.Builder builder, String serverHost, int httpsServerPort)
    • addBuilderOptions

      protected io.undertow.Undertow.Builder addBuilderOptions(io.undertow.Undertow.Builder builder)
    • addMaxEntitySize

      protected void addMaxEntitySize(io.undertow.Undertow.Builder builder)
    • enableHttp2

      protected void enableHttp2(io.undertow.Undertow.Builder builder)
    • getFinalHandler

      protected io.undertow.server.HttpHandler getFinalHandler()
      The very first handler considered by Undertow.
    • getCacheBusterRemovalHandler

      protected CacheBusterRemovalHandler getCacheBusterRemovalHandler()
      Handler to remove cache busters from the request's URL.
    • getHttpAuthenticationHandler

      protected io.undertow.server.handlers.PathHandler getHttpAuthenticationHandler()
      Handler to check for HTTP authentication requirement.
    • getHttpAuthHandlerNextHandler

      protected io.undertow.server.HttpHandler getHttpAuthHandlerNextHandler()
    • getLearningPushHandler

      protected io.undertow.server.HttpHandler getLearningPushHandler()
    • createLearningPushHandler

      protected io.undertow.server.handlers.LearningPushHandler createLearningPushHandler()
    • createHttpAuthenticationRealm

      public void createHttpAuthenticationRealm(String pathPrefix, String realmName)
      Description copied from interface: Server
      Creates HTTP authentication protection (realm) for the specified path prefix.
      Specified by:
      createHttpAuthenticationRealm in interface Server
      realmName - The name of the realm. Must be unique on this server, otherwise an exception is thrown. This allows the application to add user to the realm using its name.
    • getRealmNameToDisplay

      protected String getRealmNameToDisplay(String pathPrefix, String realmName)
      The realm name to display.
    • getOrCreateHttpAuthIdentityManagersByRealmName

      protected SpincastHttpAuthIdentityManager getOrCreateHttpAuthIdentityManagersByRealmName(String realmName)
    • addHttpAuthentication

      public void addHttpAuthentication(String realmName, String username, String password)
      Description copied from interface: Server
      Adds a user to an HTTP protected realm.
      Specified by:
      addHttpAuthentication in interface Server
    • removeHttpAuthentication

      public void removeHttpAuthentication(String username, String realmName)
      Description copied from interface: Server
      Removes a user to an HTTP protected realm.
      Specified by:
      removeHttpAuthentication in interface Server
    • removeHttpAuthentication

      public void removeHttpAuthentication(String username)
      Description copied from interface: Server
      Removes a user from all HTTP protected realms.
      Specified by:
      removeHttpAuthentication in interface Server
    • getSpincastFrontControllerHandler

      protected io.undertow.server.HttpHandler getSpincastFrontControllerHandler()
    • stop

      public void stop()
      Description copied from interface: Server
      Stops the server

      Will try to send a "closing" message to any WebSocket peer before closing their connections.

      Specified by:
      stop in interface Server
    • stop

      public void stop(boolean sendClosingMessageToPeers)
      Description copied from interface: Server
      Stops the server
      Specified by:
      stop in interface Server
      Parameters:
      sendClosingMessageToPeers - if true, Spincast will try to send a "closing" message to any WebSocket peer before closing their connections.
    • getSecondsToWaitForWebSocketEndpointsToBeProperlyClosedBeforeKillingTheServer

      protected int getSecondsToWaitForWebSocketEndpointsToBeProperlyClosedBeforeKillingTheServer()
    • getMilliSecondsIncrementWhenWaitingForWebSocketEndpointsToBeProperlyClosedBeforeKillingTheServer

      protected int getMilliSecondsIncrementWhenWaitingForWebSocketEndpointsToBeProperlyClosedBeforeKillingTheServer()
    • sendWebsocketEnpointsClosedWhenServerStops

      protected void sendWebsocketEnpointsClosedWhenServerStops()
    • getStaticResourcesPathHandler

      protected io.undertow.server.handlers.PathHandler getStaticResourcesPathHandler()
    • addStaticResourceToServe

      public void addStaticResourceToServe(StaticResource<?> staticResource)
      Description copied from interface: Server
      Adds a static resource to serve directly by the server.
      Specified by:
      addStaticResourceToServe in interface Server
    • removeStaticResourcesServed

      public void removeStaticResourcesServed(StaticResourceType staticResourceType, String urlPath)
      Description copied from interface: Server
      Removes a static resource served directly by the server.
      Specified by:
      removeStaticResourcesServed in interface Server
    • removeAllStaticResourcesServed

      public void removeAllStaticResourcesServed()
      Description copied from interface: Server
      Removes all static resources served directly by the server.
      Specified by:
      removeAllStaticResourcesServed in interface Server
    • removeStaticResource

      protected void removeStaticResource(StaticResourceType staticResourceType, String urlPath)
    • getStaticResourceServed

      public StaticResource<?> getStaticResourceServed(String urlPath)
      Description copied from interface: Server
      Gets a static resource served directly by the server, using its path.
      Specified by:
      getStaticResourceServed in interface Server
    • getStaticResourcesServed

      public Set<StaticResource<?>> getStaticResourcesServed()
      Description copied from interface: Server
      Gets all static resource served directly by the server.
      Specified by:
      getStaticResourcesServed in interface Server
    • getHttpMethod

      public HttpMethod getHttpMethod(Object exchange)
      Description copied from interface: Server
      Gets the HTTP method associated with the request.
      Specified by:
      getHttpMethod in interface Server
    • castExchange

      protected io.undertow.server.HttpServerExchange castExchange(Object exchange)
    • getContentTypeBestMatch

      public ContentTypeDefaults getContentTypeBestMatch(Object exchangeObj)
      Description copied from interface: Server
      Gets the best Content-Type to use for the current request.
      Specified by:
      getContentTypeBestMatch in interface Server
    • getFullUrlProxied

      public String getFullUrlProxied(Object exchangeObj)
      Description copied from interface: Server
      The full encoded URL of the potentially proxied request, including the queryString. Cache buster codes are removed, if there were any.

      Is a reverse proxy is used (such as Nginx or Apache), this is going to be the proxied URL, as forwarded by the reverse proxy. If no reverse proxy is used, this is going to be the original URL, as seen by the user.

      Even if the request is forwarded elsewhere in the framework, this URL won't change, it will still be the original one.

      Specified by:
      getFullUrlProxied in interface Server
    • getFullUrlProxied

      public String getFullUrlProxied(Object exchangeObj, boolean keepCacheBusters)
      Description copied from interface: Server
      The full encoded URL of the potentially proxied request, including the queryString.

      Is a reverse proxy is used (such as Nginx or Apache), this is going to be the proxied URL, as forwarded by the reverse proxy. If no reverse proxy is used, this is going to be the original URL, as seen by the user.

      Even if the request is forwarded elsewhere in the framework, this URL won't change, it will still be the original one.

      Specified by:
      getFullUrlProxied in interface Server
      keepCacheBusters - if true, the returned URL will contain the cache buster codes, if there were any. The default behavior is to automatically remove them.
    • getFullUrlOriginal

      public String getFullUrlOriginal(Object exchangeObj)
      Description copied from interface: Server
      The full encoded URL of the original, non proxied, request, including the queryString. Cache buster codes are removed, if there were any.

      This is going to be the *original* URL, as seen by the user, even if a reverse proxy is used (such as Nginx or Apache).

      Even if the request is forwarded elsewhere in the framework, this URL won't change, it will still be the original one.

      Specified by:
      getFullUrlOriginal in interface Server
    • getFullUrlOriginal

      public String getFullUrlOriginal(Object exchangeObj, boolean keepCacheBusters)
      Description copied from interface: Server
      The full encoded URL of the original, non proxied, request, including the queryString.

      This is going to be the *original* URL, as seen by the user, even if a reverse proxy is used (such as Nginx or Apache).

      Even if the request is forwarded elsewhere in the framework, this URL won't change, it will still be the original one.

      Specified by:
      getFullUrlOriginal in interface Server
      keepCacheBusters - if true, the returned URL will contain the cache buster codes, if there were any. The default behavior is to automatically remove them.
    • setResponseHeader

      public void setResponseHeader(Object exchangeObj, String name, List<String> values)
      Description copied from interface: Server
      Sets a response header. Override any existing one with the same name.
      Specified by:
      setResponseHeader in interface Server
    • setResponseHeaders

      public void setResponseHeaders(Object exchange, Map<String,List<String>> headers)
      Description copied from interface: Server
      Sets the response headers. Override any existing ones.
      Specified by:
      setResponseHeaders in interface Server
    • getResponseHeaders

      public Map<String,List<String>> getResponseHeaders(Object exchangeObj)
      Description copied from interface: Server
      Gets the response headers.
      Specified by:
      getResponseHeaders in interface Server
    • removeResponseHeader

      public void removeResponseHeader(Object exchange, String name)
      Description copied from interface: Server
      Removes a response header.
      Specified by:
      removeResponseHeader in interface Server
    • setResponseStatusCode

      public void setResponseStatusCode(Object exchange, int statusCode)
      Description copied from interface: Server
      Sets the response status code.
      Specified by:
      setResponseStatusCode in interface Server
    • getDoNothingCallback

      protected io.undertow.io.IoCallback getDoNothingCallback()
    • getCloseExchangeCallback

      protected io.undertow.io.IoCallback getCloseExchangeCallback()
    • flushBytes

      public void flushBytes(Object exchange, byte[] bytes, boolean end)
      Description copied from interface: Server
      Flushes some bytes to the response.
      Specified by:
      flushBytes in interface Server
      end - if true, the exchange will be closed and nothing more can be send.
    • end

      public void end(Object exchange)
      Description copied from interface: Server
      Ends the exchange. Nothing more can be send.
      Specified by:
      end in interface Server
    • isResponseClosed

      public boolean isResponseClosed(Object exchange)
      Description copied from interface: Server
      Is the response closed?
      Specified by:
      isResponseClosed in interface Server
    • isResponseHeadersSent

      public boolean isResponseHeadersSent(Object exchange)
      Description copied from interface: Server
      Are the response headers sent?
      Specified by:
      isResponseHeadersSent in interface Server
    • getRequestScheme

      public String getRequestScheme(Object exchange)
      Description copied from interface: Server
      Gets the request scheme, "http" for example.
      Specified by:
      getRequestScheme in interface Server
    • addCookies

      public void addCookies(Object exchange, Map<String,Cookie> cookies)
      Description copied from interface: Server
      Adds cookies.
      Specified by:
      addCookies in interface Server
    • getCookies

      public Map<String,String> getCookies(Object exchange)
      Description copied from interface: Server
      Gets the request cookies values.
      Specified by:
      getCookies in interface Server
    • getCookieEncoding

      protected String getCookieEncoding()
    • getQueryStringParams

      public Map<String,List<String>> getQueryStringParams(Object exchange)
      Description copied from interface: Server
      Gets the queryString parameters.
      Specified by:
      getQueryStringParams in interface Server
    • getRawInputStream

      public InputStream getRawInputStream(Object exchangeObj)
      Description copied from interface: Server
      The raw InputStream of the current request.
      Specified by:
      getRawInputStream in interface Server
    • getFormData

      protected io.undertow.server.handlers.form.FormData getFormData(io.undertow.server.HttpServerExchange exchange)
      May return NULL.
    • getFormData

      public Map<String,List<String>> getFormData(Object exchangeObj)
      Description copied from interface: Server
      The form data, if any.
      Specified by:
      getFormData in interface Server
    • getUploadedFiles

      public Map<String,List<UploadedFile>> getUploadedFiles(Object exchangeObj)
      Description copied from interface: Server
      The uploaded files, if any. The key of the map if the HTML's name attribute.
      Specified by:
      getUploadedFiles in interface Server
    • forceRequestSizeValidation

      public boolean forceRequestSizeValidation(Object exchangeObj)
      Description copied from interface: Server
      Is the request size valid?
      Specified by:
      forceRequestSizeValidation in interface Server
    • getRequestHeaders

      public Map<String,List<String>> getRequestHeaders(Object exchangeObj)
      The names of the headers are case insensitive.
      Specified by:
      getRequestHeaders in interface Server
    • getWebsocketEndpointCreationLock

      protected Object getWebsocketEndpointCreationLock(String endpointId)
      Gets the creation/close lock for a specific Websocket endpoint.
    • websocketCreateEndpoint

      public WebsocketEndpointManager websocketCreateEndpoint(String endpointId, WebsocketEndpointHandler appEndpointHandler)
      Description copied from interface: Server
      Creates a new Websocket endpoint.
      Specified by:
      websocketCreateEndpoint in interface Server
      Returns:
      the manager for this endpoint.
    • createUndertowWebsocketEndpointHandler

      protected WebsocketEndpointHandler createUndertowWebsocketEndpointHandler(String endpointId, WebsocketEndpointHandler appHandler)
    • websocketCloseEndpoint

      public void websocketCloseEndpoint(String endpointId)
      Description copied from interface: Server
      Closes a Websocket endpoint. No more connections will be accepter
      Specified by:
      websocketCloseEndpoint in interface Server
    • websocketCloseEndpoint

      public void websocketCloseEndpoint(String endpointId, int closingCode, String closingReason)
      Description copied from interface: Server
      Closes the entire Websocket endpoint. All peer connections of this endpoint will be closed.
      Specified by:
      websocketCloseEndpoint in interface Server
      closingCode - The closing code.
      closingReason - The closing reason.
    • websocketConnection

      public void websocketConnection(Object exchangeObj, String endpointId, String peerId)
      Description copied from interface: Server
      Transforms the request to a peer Websocket connection on the endpoint 'endpointId'.
      Specified by:
      websocketConnection in interface Server
    • getWebsocketEndpointManagers

      public List<WebsocketEndpointManager> getWebsocketEndpointManagers()
      Description copied from interface: Server
      Returns the managers of the existing Websockets endpoints.
      Specified by:
      getWebsocketEndpointManagers in interface Server
    • getWebsocketEndpointManager

      public WebsocketEndpointManager getWebsocketEndpointManager(String endpointId)
      Description copied from interface: Server
      Returns the manager for a Websockets endpoint.
      Specified by:
      getWebsocketEndpointManager in interface Server
      Returns:
      the manager or null if not found.
    • getIp

      public String getIp(Object exchangeObj)
      Description copied from interface: Server
      Gets the IP of the current request.
      Specified by:
      getIp in interface Server
    • convertHttpMethodToUndertowHttpString

      public io.undertow.util.HttpString convertHttpMethodToUndertowHttpString(HttpMethod httpMethod)
    • convertHeadersToUndertowHeaderMap

      public io.undertow.util.HeaderMap convertHeadersToUndertowHeaderMap(Map<String,List<String>> headers)
    • push

      public void push(Object exchangeObj, Set<ResourceToPush> resourcesToPush)
      Description copied from interface: Server
      If HTTP/2 is used, you can push extra resources at the same time you response to a request.

      If the embedded server deals with a HTTTP/2 request, it will push the extra resources by itself. If it deals with an HTTP/1.X request (for example if it is behind a reverse-proxy) it will send Link headers to the potential proxy in front of it and it is the proxy that will be in charge of doing the actual push.

      Beware that pushing resources does not always result in an increase of performance and may lead to wasted bandwidth (the client may decide to not use the pushed resources).

      Specified by:
      push in interface Server
    • isPushSupported

      protected boolean isPushSupported(io.undertow.server.ServerConnection connection)
    • pushResource

      protected void pushResource(io.undertow.server.ServerConnection connection, String path, io.undertow.util.HttpString httpMethod, io.undertow.util.HeaderMap headerMap)
    • sendPushHeaders

      protected void sendPushHeaders(io.undertow.server.HttpServerExchange exchange, ResourceToPush resourceToPush)
      In case the server is behind a reverse-proxy such as Nginx: it is this proxy that will do the actual push of the resource. We tell it to do so using special response headers.