Class JdkHttpRestServer
- All Implemented Interfaces:
Closable, Closable.CloseBuilder<LinkComponent.LinkComponentBuilder<HttpRestServer>>, ConnectionStatusAccessor, LinkComponent, LinkComponent.LinkComponentBuilder<HttpRestServer>, Openable, Openable.OpenBuilder<LinkComponent.LinkComponentBuilder<HttpRestServer>>, OpenedAccessor, MaxConnectionsAccessor, Disposable, PortAccessor, Observable<RestEndpoint>, Observers<RestEndpoint, RestServer>, HttpExceptionHandlerAccessor, HttpExceptionHandlerAccessor.HttpExceptionHandlerBuilder<HttpRestServer>, HttpExceptionHandlerAccessor.HttpExceptionHandlerMutator, HttpExceptionHandlerAccessor.HttpExceptionHandlerProperty, HttpExceptionHandlingAccessor, HttpExceptionHandlingAccessor.HttpExceptionHandlingBuilder<HttpRestServer>, HttpExceptionHandlingAccessor.HttpExceptionHandlingMutator, HttpExceptionHandlingAccessor.HttpExceptionHandlingProperty, HttpRestServer, RestServer, KeyStoreDescriptorAccessor, BaseLocatorAccessor, BasicAuthObservable<HttpRestServer>, HttpsConnectionRequestObservable<HttpRestServer>, HttpServerInterceptable, MediaTypeFactoryLookup, MediaTypeFactoryLookup.MutableMediaTypeFactoryLookup, PostHttpInterceptable<PostHttpServerInterceptor>, PostHttpServerInterceptable, PreHttpInterceptable<PreHttpServerInterceptor>, PreHttpServerInterceptable, RealmAccessor, SchemeAccessor
HttpRestServer interface using the
com.sun.net.httpserver package provided by the Java runtime.
The JdkHttpRestServer provides a lightweight embeddable REST server
implementation supporting HTTP as well as HTTPS communication alongside
request dispatching, media type negotiation, marshaling, interceptor chains,
exception handling and optional HTTP Basic-Authentication.
The server integrates with the REFCODES web and REST infrastructure and is
preconfigured with several common MediaTypeFactory instances,
including:
HTTPS support is enabled through a KeyStoreDescriptor together with
the configured transport protocol such as TransportLayerProtocol.TLS.
SSL/TLS configuration is delegated to the underlying JDK SSLContext.
Incoming requests are routed to the registered REST endpoints based on:
- HTTP method
- resource locator / path
- request and response media types
- request metadata and headers
The server supports:
- pre and post request interceptors
- custom HTTP exception handling
- custom HTTPS connection configuration
- HTTP Basic-Authentication
- streaming responses via
InputStream - custom executor services
Configuration is provided through HttpServerOptions, enabling a typed
ontology driven configuration model consistent with the REFCODES runtime and
diagnostics architecture.
This implementation is intended for lightweight embeddable HTTP/REST services, testing scenarios, infrastructure services, standalone runtimes and applications preferring minimal external dependencies.
TODO 2026-03-08 Support HTTPS/2 with the https://github.com/robaho/httpserver drop in replacement for the com.sun.net.httpserver.HttpsServer.- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder for constructingJdkHttpRestServerinstances using a fluent and strongly typed configuration API.Nested classes/interfaces inherited from interface BaseLocatorAccessor
BaseLocatorAccessor.BaseLocatorBuilder<B>, BaseLocatorAccessor.BaseLocatorMutator, BaseLocatorAccessor.BaseLocatorPropertyNested classes/interfaces inherited from interface Closable
Closable.CloseAutomaton, Closable.CloseBuilder<B>Nested classes/interfaces inherited from interface ConnectionStatusAccessor
ConnectionStatusAccessor.ConnectionStatusMutator, ConnectionStatusAccessor.ConnectionStatusPropertyNested classes/interfaces inherited from interface HttpExceptionHandlerAccessor
HttpExceptionHandlerAccessor.HttpExceptionHandlerBuilder<B>, HttpExceptionHandlerAccessor.HttpExceptionHandlerMutator, HttpExceptionHandlerAccessor.HttpExceptionHandlerPropertyNested classes/interfaces inherited from interface HttpExceptionHandlingAccessor
HttpExceptionHandlingAccessor.HttpExceptionHandlingBuilder<B>, HttpExceptionHandlingAccessor.HttpExceptionHandlingMutator, HttpExceptionHandlingAccessor.HttpExceptionHandlingPropertyNested classes/interfaces inherited from interface KeyStoreDescriptorAccessor
KeyStoreDescriptorAccessor.KeyStoreDescriptorBuilder<B>, KeyStoreDescriptorAccessor.KeyStoreDescriptorMutator, KeyStoreDescriptorAccessor.KeyStoreDescriptorPropertyNested classes/interfaces inherited from interface LinkComponent
LinkComponent.LinkAutomaton, LinkComponent.LinkComponentBuilder<B>Nested classes/interfaces inherited from interface MaxConnectionsAccessor
MaxConnectionsAccessor.MaxConnectionsBuilder<B>, MaxConnectionsAccessor.MaxConnectionsMutator, MaxConnectionsAccessor.MaxConnectionsPropertyNested classes/interfaces inherited from interface MediaTypeFactoryLookup
MediaTypeFactoryLookup.MutableMediaTypeFactoryLookupNested classes/interfaces inherited from interface Openable
Openable.OpenAutomaton, Openable.OpenBuilder<B>Nested classes/interfaces inherited from interface OpenedAccessor
OpenedAccessor.OpenedMutator, OpenedAccessor.OpenedPropertyNested classes/interfaces inherited from interface PortAccessor
PortAccessor.PortBuilder<B>, PortAccessor.PortMutator, PortAccessor.PortPropertyNested classes/interfaces inherited from interface RealmAccessor
RealmAccessor.RealmBuilder<B>, RealmAccessor.RealmMutator, RealmAccessor.RealmPropertyNested classes/interfaces inherited from interface SchemeAccessor
SchemeAccessor.SchemeBuilder<B>, SchemeAccessor.SchemeMutator, SchemeAccessor.SchemeProperty -
Field Summary
FieldsFields inherited from class AbstractRestServer
_baseLocator, _realmFields inherited from class AbstractObservable
_executorService -
Constructor Summary
ConstructorsConstructorDescriptionJdkHttpRestServer(HttpServerOptions aHttpServerOptions) Instantiates a new HTTP rest server. -
Method Summary
Modifier and TypeMethodDescriptionbooleanaddPostHttpInterceptor(PostHttpServerInterceptor aPostInterceptor) booleanaddPreHttpInterceptor(PreHttpServerInterceptor aPreInterceptor) static JdkHttpRestServer.Builderbuilder()Creates a newJdkHttpRestServer.Builderfor constructingJdkHttpRestServerinstances.voidclose()Retrieves theHttpExceptionHandlerfrom theHttpExceptionHandlerproperty.Retrieves theHttpExceptionHandlingfrom theHttpExceptionHandlingproperty.protected HttpServerGets the HTTP server.intintgetPort()booleanhasPostHttpInterceptor(PostHttpServerInterceptor aPostInterceptor) booleanhasPreHttpInterceptor(PreHttpServerInterceptor aPreInterceptor) onBasicAuthRequest(BasicAuthObserver aBasicAuthObserver) voidopen()protected voidopen(HttpServer aHttpServer) A hook to be used when using customHttpRestServer(HttpsServer) by custom open(...) methods of sub-classes of thisJdkHttpRestServer.protected voidpostIntercept(HttpServerRequest aRequest, HttpServerResponse aResponse) Invoked to post-process aHttpServerRequestalongside aHttpServerResponse.protected voidpreIntercept(HttpServerRequest aRequest, HttpServerResponse aResponse) Invoked to pre-process aHttpServerRequestalongside aHttpServerResponse.booleanremovePostHttpInterceptor(PostHttpServerInterceptor aPostInterceptor) booleanremovePreHttpInterceptor(PreHttpServerInterceptor aPreInterceptor) voidsetHttpExceptionHandler(HttpExceptionHandler aHttpErrorHandler) Sets theHttpExceptionHandlerfor theHttpExceptionHandlerproperty.voidsetHttpExceptionHandling(HttpExceptionHandling aHttpErrorHandling) Sets theHttpExceptionHandlingfor theHttpExceptionHandlingproperty.withCloseIn(int aCloseMillis) withHttpExceptionHandler(HttpExceptionHandler aHttpErrorHandler) Sets theHttpExceptionHandlerfor theHttpExceptionHandlerproperty.withHttpExceptionHandling(HttpExceptionHandling aHttpErrorHandling) Sets theHttpExceptionHandlingfor theHttpExceptionHandlingproperty.withObserversActive(boolean isActive) withOnHttpException(HttpExceptionHandler aHttpExceptionHandler) Sets theHttpExceptionHandlerfor theHttpExceptionHandlerproperty.withOpen()Methods inherited from class AbstractRestServer
addMediaTypeFactory, dispose, fireEvent, getBaseLocator, getFactoryMediaTypes, getRealm, initMedaTypeFactories, isObserversActive, observers, onHttpRequest, setObserversActive, subscribeObserver, toMediaTypeFactory, toMediaTypeFactory, toNegotiatedContenType, toResponseBody, unsubscribeObserverMethods inherited from class AbstractObservable
clear, doHandleEventListenerException, fireEvent, getThreadPriority, hasObserver, isEmpty, setThreadPriority, sizeMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface BaseLocatorAccessor
getBaseLocatorMethods inherited from interface Closable
closeIn, closeQuietly, closeUncheckedMethods inherited from interface ConnectionStatusAccessor
isOpenedMethods inherited from interface HttpExceptionHandlerAccessor.HttpExceptionHandlerMutator
onHttpExceptionMethods inherited from interface HttpExceptionHandlerAccessor.HttpExceptionHandlerProperty
letHttpExceptionHandlerMethods inherited from interface HttpExceptionHandlingAccessor.HttpExceptionHandlingProperty
letHttpExceptionHandlingMethods inherited from interface HttpServerInterceptable
addHttpInterceptor, hasHttpInterceptor, removeHttpInterceptorMethods inherited from interface MediaTypeFactoryLookup
getFactoryMediaTypes, hasMediaTypeFactory, toMediaTypeFactoryMethods inherited from interface MediaTypeFactoryLookup.MutableMediaTypeFactoryLookup
addMediaTypeFactoryMethods inherited from interface Observable
hasObserver, subscribeObserver, unsubscribeObserverMethods inherited from interface Observers
disableObservers, enableObservers, isObserversActive, observers, setObserversActiveMethods inherited from interface RealmAccessor
getRealm
-
Field Details
-
CONTEXT_PATH
-
-
Constructor Details
-
JdkHttpRestServer
Instantiates a new HTTP rest server.- Parameters:
aHttpServerOptions- The options to use for configuring the HTTP rest server.
-
-
Method Details
-
close
- Specified by:
closein interfaceClosable- Throws:
IOException
-
open
- Specified by:
openin interfaceHttpRestServer- Specified by:
openin interfaceOpenable- Throws:
IOException
-
getConnectionStatus
- Specified by:
getConnectionStatusin interfaceConnectionStatusAccessor
-
getHttpExceptionHandler
Retrieves theHttpExceptionHandlerfrom theHttpExceptionHandlerproperty.- Specified by:
getHttpExceptionHandlerin interfaceHttpExceptionHandlerAccessor- Returns:
- The
HttpExceptionHandlerstored by theHttpExceptionHandlerproperty.
-
setHttpExceptionHandler
Sets theHttpExceptionHandlerfor theHttpExceptionHandlerproperty.- Specified by:
setHttpExceptionHandlerin interfaceHttpExceptionHandlerAccessor.HttpExceptionHandlerMutator- Parameters:
aHttpErrorHandler- TheHttpExceptionHandlerto be stored by theHttpExceptionHandlerproperty.
-
getHttpExceptionHandling
Retrieves theHttpExceptionHandlingfrom theHttpExceptionHandlingproperty.- Specified by:
getHttpExceptionHandlingin interfaceHttpExceptionHandlingAccessor- Returns:
- The
HttpExceptionHandlingstored by theHttpExceptionHandlingproperty.
-
setHttpExceptionHandling
Sets theHttpExceptionHandlingfor theHttpExceptionHandlingproperty.- Specified by:
setHttpExceptionHandlingin interfaceHttpExceptionHandlingAccessor.HttpExceptionHandlingMutator- Parameters:
aHttpErrorHandling- TheHttpExceptionHandlingto be stored by theHttpExceptionHandlingproperty.
-
onConnectionRequest
- Specified by:
onConnectionRequestin interfaceHttpsConnectionRequestObservable<HttpRestServer>
-
onBasicAuthRequest
- Specified by:
onBasicAuthRequestin interfaceBasicAuthObservable<HttpRestServer>
-
getPort
public int getPort()- Specified by:
getPortin interfacePortAccessor
-
getScheme
- Specified by:
getSchemein interfaceSchemeAccessor
-
toProtocol
- Specified by:
toProtocolin interfaceSchemeAccessor
-
getKeyStoreDescriptor
- Specified by:
getKeyStoreDescriptorin interfaceKeyStoreDescriptorAccessor
-
getMaxConnections
public int getMaxConnections()- Specified by:
getMaxConnectionsin interfaceMaxConnectionsAccessor
-
hasPreHttpInterceptor
- Specified by:
hasPreHttpInterceptorin interfacePreHttpInterceptable<PreHttpServerInterceptor>
-
addPreHttpInterceptor
- Specified by:
addPreHttpInterceptorin interfacePreHttpInterceptable<PreHttpServerInterceptor>
-
removePreHttpInterceptor
- Specified by:
removePreHttpInterceptorin interfacePreHttpInterceptable<PreHttpServerInterceptor>
-
hasPostHttpInterceptor
- Specified by:
hasPostHttpInterceptorin interfacePostHttpInterceptable<PostHttpServerInterceptor>
-
addPostHttpInterceptor
- Specified by:
addPostHttpInterceptorin interfacePostHttpInterceptable<PostHttpServerInterceptor>
-
removePostHttpInterceptor
- Specified by:
removePostHttpInterceptorin interfacePostHttpInterceptable<PostHttpServerInterceptor>
-
withOpen
- Specified by:
withOpenin interfaceOpenable.OpenBuilder<LinkComponent.LinkComponentBuilder<HttpRestServer>>- Throws:
IOException
-
withCloseUnchecked
- Specified by:
withCloseUncheckedin interfaceClosable.CloseBuilder<LinkComponent.LinkComponentBuilder<HttpRestServer>>
-
withObserversActive
- Specified by:
withObserversActivein interfaceObservers<RestEndpoint, RestServer>- Specified by:
withObserversActivein interfaceRestServer
-
withEnableObservers
- Specified by:
withEnableObserversin interfaceObservers<RestEndpoint, RestServer>- Specified by:
withEnableObserversin interfaceRestServer
-
withDisableObservers
- Specified by:
withDisableObserversin interfaceObservers<RestEndpoint, RestServer>- Specified by:
withDisableObserversin interfaceRestServer
-
withOnHttpException
Sets theHttpExceptionHandlerfor theHttpExceptionHandlerproperty.- Specified by:
withOnHttpExceptionin interfaceHttpExceptionHandlerAccessor.HttpExceptionHandlerBuilder<HttpRestServer>- Parameters:
aHttpExceptionHandler- TheHttpExceptionHandlerto be stored by theHttpExceptionHandlerproperty.- Returns:
- The builder for applying multiple build operations.
-
withHttpExceptionHandler
Sets theHttpExceptionHandlerfor theHttpExceptionHandlerproperty.- Specified by:
withHttpExceptionHandlerin interfaceHttpExceptionHandlerAccessor.HttpExceptionHandlerBuilder<HttpRestServer>- Specified by:
withHttpExceptionHandlerin interfaceHttpRestServer- Parameters:
aHttpErrorHandler- TheHttpExceptionHandlerto be stored by theHttpExceptionHandlerproperty.- Returns:
- The builder for applying multiple build operations.
-
withHttpExceptionHandling
Sets theHttpExceptionHandlingfor theHttpExceptionHandlingproperty.- Specified by:
withHttpExceptionHandlingin interfaceHttpExceptionHandlingAccessor.HttpExceptionHandlingBuilder<HttpRestServer>- Specified by:
withHttpExceptionHandlingin interfaceHttpRestServer- Parameters:
aHttpErrorHandling- TheHttpExceptionHandlingto be stored by theHttpExceptionHandlingproperty.- Returns:
- The builder for applying multiple build operations.
-
withClose
- Specified by:
withClosein interfaceClosable.CloseBuilder<LinkComponent.LinkComponentBuilder<HttpRestServer>>- Specified by:
withClosein interfaceHttpRestServer- Throws:
IOException
-
withCloseQuietly
- Specified by:
withCloseQuietlyin interfaceClosable.CloseBuilder<LinkComponent.LinkComponentBuilder<HttpRestServer>>- Specified by:
withCloseQuietlyin interfaceHttpRestServer
-
withCloseIn
- Specified by:
withCloseInin interfaceClosable.CloseBuilder<LinkComponent.LinkComponentBuilder<HttpRestServer>>- Specified by:
withCloseInin interfaceHttpRestServer
-
builder
Creates a newJdkHttpRestServer.Builderfor constructingJdkHttpRestServerinstances.- Returns:
- A new builder instance.
-
preIntercept
Invoked to pre-process aHttpServerRequestalongside aHttpServerResponse.- Overrides:
preInterceptin classAbstractRestServer- Parameters:
aRequest- TheHttpServerRequestto pre-process.aResponse- TheHttpServerResponseto post-process.
-
postIntercept
Invoked to post-process aHttpServerRequestalongside aHttpServerResponse.- Overrides:
postInterceptin classAbstractRestServer- Parameters:
aRequest- TheHttpServerRequestto post-process.aResponse- TheHttpServerResponseto post-process.
-
getHttpServer
-
open
A hook to be used when using customHttpRestServer(HttpsServer) by custom open(...) methods of sub-classes of thisJdkHttpRestServer. E.gJdkHttpRestServeruses this hook to pre-configure aHttpsServerfor HTTPS. The passedHttpRestServer(HttpsServer) must already be bound to a port and enabled with the number of concurrent connections as ofHttpServer.bind(InetSocketAddress, int).- Parameters:
aHttpServer- TheHttpRestServerto be used. E.g. anHttpsServermight be used to enable HTTPS.- Throws:
IOException- in case opening with the providedHttpRestServerfails.
-