1. Design Goals and Primary Roles
The com.aspectran.undertow.service package provides specialized service layer implementations that enable Aspectran to run directly on JBoss Undertow—an ultra-lightweight, high-performance asynchronous web server—without requiring a servlet container (Servlet-less Embedded Web Server).
The core architectural objectives of this package are:
- Servlet-Free Ultra-Fast Web Runtime: Eliminates the overhead of traditional servlet specifications (
jakarta.servlet) and container hierarchies by interacting directly with Undertow’s native I/O construct,HttpServerExchange, delivering maximum throughput and minimal memory footprint in microservice environments. - Inheritance of Pure Web Interface (
WebService): Extends the root web service interfaceWebServicefrom theaspectran-webmodule, fully reusing standard web configurations (WebConfig)—including session configuration (sessionConfig), URI decoding, and trailing slash redirection (trailingSlashRedirect)—with 100% compatibility. - Seamless Integration with Undertow Handler Chains:
DefaultTowServicecan operate as an UndertowHttpHandleror serve as the terminal endpoint of a handler chain, coordinating smoothly with static resource serving (ResourceManager), access logging (AccessLogHandler), and content compression encoding handlers. - Thread Context ClassLoader Switching: Wraps request processing with
ThreadContextHelper.overrideClassLoader()to bind theSiblingClassLoadercontext to the active thread, avoiding runtime class-loading collisions.
2. Key Classes and Interfaces In-Depth Analysis
classDiagram
class CoreService {
<<interface>>
}
class WebService {
<<interface>>
+getUriDecoding() String
+isTrailingSlashRedirect() bool
}
class TowService {
<<interface>>
+getResourceManager() ResourceManager
+isLegacyHeadHandling() bool
+service(HttpServerExchange exchange) bool
}
class AbstractTowService {
<<abstract>>
-ResourceManager resourceManager
-boolean legacyHeadHandling
}
class DefaultTowService {
+handleRequest(HttpServerExchange)
+service(HttpServerExchange exchange) bool
-perform(TowActivity)
-transletNotFound(TowActivity)
}
class DefaultTowServiceBuilder {
+build(TowServer, AspectranConfig) TowService
}
CoreService <|-- WebService
WebService <|-- TowService
TowService <|.. AbstractTowService
AbstractTowService <|-- DefaultTowService
DefaultTowServiceBuilder ..> DefaultTowService : builds
TowService (Interface)
The core specification for Undertow-based web services in Aspectran. (Tow is derived from the suffix of Undertow.)
- Core Responsibilities:
- Extends
WebServiceto share common web configurations and lifecycles. service(HttpServerExchange exchange): Declares the web entry-point method that accepts a single HTTP exchange object from Undertow’s I/O channels and executes the corresponding translet.getResourceManager(): Exposes an Undertow-specific resource manager for serving static assets (HTML, CSS, JS, images).
- Extends
AbstractTowService (Abstract Class)
An abstract base class implementing shared web infrastructure logic for TowService.
- Parses injected
WebConfigsettings to configure URI string decoding strategies, trailing slash automatic redirection policies, and static resource managers. - Oversees Undertow session management and background resource cleanup across service lifecycles (
doStart,doStop).
DefaultTowService (Concrete Implementation Class)
The primary concrete implementation of TowService, responsible for receiving Undertow’s HttpServerExchange, instantiating TowActivity, and driving the full execution pipeline.
- Core Execution Pipeline (
service(HttpServerExchange)):- Service Pause Check: If the service is in a
pausedstate, immediately returns a503 Service Unavailableresponse and halts processing. - Request Identifier and Method Resolution: Reads
exchange.getRequestURI(), safely decodes it according togetUriDecoding(), and maps the HTTP method fromexchange.getRequestMethod()to a virtualMethodType(GET,POST, etc.). - Authorization Check (
isRequestAcceptable): Verifies throughRequestAcceptorwhether the target URI represents an authorized translet eligible for execution by Aspectran. TowActivityInstantiation: Constructs a dedicatedTowActivityinjected with theexchangeobject and the resolved context path.prepare()and Trailing Slash Redirection:- If the translet is not found (
TransletNotFoundException), checks if appending a trailing slash (/) matches an existing translet rule, automatically issuing an HTTP 301/302 redirect if valid.
- If the translet is not found (
perform()Execution and ClassLoader Isolation:ClassLoader origClassLoader = ThreadContextHelper.overrideClassLoader(getServiceClassLoader()); try { activity.perform(); } finally { ThreadContextHelper.restoreClassLoader(origClassLoader); }Temporarily swaps the thread context classloader with the Aspectran service classloader, then executes the standard pipeline of AOP advice, actions, and template transformations.
- Service Pause Check: If the service is in a
DefaultTowServiceBuilder (Builder Class)
A factory builder that constructs fully initialized DefaultTowService instances from embedded Undertow server instances (TowServer) and AspectranConfig definitions.
- Assembles port bindings, listener definitions, SSL/TLS contexts, web session configurations, and Undertow handler chains into an operational service instance.
3. Interactions with Other Packages and Undertow Engine
sequenceDiagram
autonumber
participant Client as Web Browser / HTTP Client
participant UT as Undertow Native I/O Thread
participant Service as DefaultTowService
participant Act as TowActivity
participant Trans as CoreTranslet (Business Action)
Client->>UT: Send HTTP Request
UT->>Service: service(HttpServerExchange)
activate Service
Service->>Act: new TowActivity(this, exchange)
Service->>Act: prepare()
activate Act
Act->>Act: Bind TowRequestAdapter & ResponseAdapter
Service->>Act: perform()
Act->>Trans: Execute actions and render template
Trans-->>Act: Return execution result
Act->>UT: Stream response data directly to HttpServerExchange
deactivate Act
Service-->>UT: Processing complete (return true)
deactivate Service
UT-->>Client: Send HTTP Response
com.aspectran.web.service: Extends the root interfaceWebService, ensuring strict compliance with web specifications.com.aspectran.undertow.activity: InstantiatesTowActivityfor each HTTP request exchange, bridging it directly into the core engine’s standard lifecycle.io.undertow.server: Communicates directly with Undertow’s nativeHttpServerExchangeAPIs to govern headers, status codes, and buffer streams.
4. Package Summary and Architectural Significance
The com.aspectran.undertow.service package serves as a prime adapter layer demonstrating how the Aspectran framework breaks free from traditional servlet constraints to evolve into a modern, ultra-fast asynchronous microservice web server:
- Zero Servlet Overhead: Bypasses
HttpServletRequest/HttpServletResponsewrappers and servlet filter chains, operating directly on Undertow nativeHttpServerExchangechannels to achieve maximum I/O throughput. - Configuration Reusability and Portability: Allows deployment infrastructure to be switched immediately to an embedded Undertow server without altering
<web>configuration blocks or translet business code written for servlet environments. - Intelligent Routing and Redirection: Combines URI decoding, automated trailing slash correction, and smooth coordination with static resource managers to provide a comprehensive enterprise web feature set within a lightweight footprint.