Top Description Inners Fields Constructors Methods
org.springframework.http.server.reactive

public Class ServletHttpHandlerAdapter

extends Object
implements Servlet
Class Inheritance
All Implemented Interfaces
jakarta.servlet.Servlet
Known Direct Subclasses
org.springframework.http.server.reactive.TomcatHttpHandlerAdapter, org.springframework.http.server.reactive.JettyHttpHandlerAdapter
Imports
java.io.IOException, java.net.URISyntaxException, java.util.Collection, java.util.concurrent.atomic.AtomicBoolean, jakarta.servlet.AsyncContext, .AsyncEvent, .AsyncListener, .DispatcherType, .Servlet, .ServletConfig, .ServletException, .ServletRegistration, .ServletRequest, .ServletResponse, jakarta.servlet.http.HttpServlet, .HttpServletRequest, .HttpServletResponse, org.apache.commons.logging.Log, org.reactivestreams.Subscriber, .Subscription, org.springframework.core.io.buffer.DataBufferFactory, .DefaultDataBufferFactory, org.springframework.http.HttpLogging, .HttpMethod, org.springframework.lang.Nullable, org.springframework.util.Assert

Adapt HttpHandler to an HttpServlet using Servlet Async support and Servlet non-blocking I/O.
Authors
Arjen Poutsma, Rossen Stoyanchev
Since
5.0
See Also
org.springframework.web.server.adapter.AbstractReactiveWebInitializer

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
private static class
ServletHttpHandlerAdapter.HttpHandlerAsyncListener

AsyncListener to complete the AsyncContext in case of error or timeout notifications from the container

Additional AsyncListeners are registered in ServletServerHttpRequest to signal onError/onComplete to the request body Subscriber, and in ServletServerHttpResponse to cancel the write Publisher and signal onError/onComplete downstream to the writing result Subscriber.

Field Summary

Modifier and TypeField and Description
private int
private DataBufferFactory
private static final int
private final HttpHandler
private static final Log
private String
private static final String

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected ServletServerHttpRequest
protected ServletServerHttpResponse
public void
destroy()

Implements jakarta.servlet.Servlet.destroy.

Called by the servlet container to indicate to a servlet that the servlet is being taken out of service.
public int
getBufferSize()

Return the configured input buffer size.

public DataBufferFactory
public ServletConfig
getServletConfig()

Implements jakarta.servlet.Servlet.getServletConfig.

Returns a ServletConfig object, which contains initialization and startup parameters for this servlet.
public String
getServletInfo()

Implements jakarta.servlet.Servlet.getServletInfo.

Returns information about the servlet, such as author, version, and copyright.
public String

Returns:

the path, or an empty string if the Servlet is deployed without a prefix (i.e. "/" or "/*"), or null if this method is invoked before the init(ServletConfig) Servlet container callback.
getServletPath
()

Return the Servlet path under which the Servlet is deployed by checking the Servlet registration from init(ServletConfig).

private String
public void
init(ServletConfig
a ServletConfig object containing the servlet's configuration and initialization parameters
config
)

Implements jakarta.servlet.Servlet.init.

Called by the servlet container to indicate to a servlet that the servlet is being placed into service.
private static void
runIfAsyncNotComplete(AsyncContext asyncContext, AtomicBoolean isCompleted, Runnable task)

public void
service(ServletRequest
the ServletRequest object that contains the client's request
request
,
ServletResponse
the ServletResponse object that contains the servlet's response
response
)

Implements jakarta.servlet.Servlet.service.

Called by the servlet container to allow the servlet to respond to a request.
public void
setBufferSize(int bufferSize)

Set the size of the input buffer used for reading in bytes.

public void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

bufferSizeback to summary
private int bufferSize
dataBufferFactoryback to summary
private DataBufferFactory dataBufferFactory
DEFAULT_BUFFER_SIZEback to summary
private static final int DEFAULT_BUFFER_SIZE
httpHandlerback to summary
private final HttpHandler httpHandler
loggerback to summary
private static final Log logger
servletPathback to summary
private String servletPath
Annotations
@Nullable
WRITE_ERROR_ATTRIBUTE_NAMEback to summary
private static final String WRITE_ERROR_ATTRIBUTE_NAME

Constructor Detail

ServletHttpHandlerAdapterback to summary
public ServletHttpHandlerAdapter(HttpHandler httpHandler)

Method Detail

createRequestback to summary
protected ServletServerHttpRequest createRequest(HttpServletRequest request, AsyncContext context) throws IOException, URISyntaxException
createResponseback to summary
protected ServletServerHttpResponse createResponse(HttpServletResponse response, AsyncContext context, ServletServerHttpRequest request) throws IOException
destroyback to summary
public void destroy()

Implements jakarta.servlet.Servlet.destroy.

Doc from jakarta.servlet.Servlet.destroy.

Called by the servlet container to indicate to a servlet that the servlet is being taken out of service. This method is only called once all threads within the servlet's service method have exited or after a timeout period has passed. After the servlet container calls this method, it will not call the service method again on this servlet.

This method gives the servlet an opportunity to clean up any resources that are being held (for example, memory, file handles, threads) and make sure that any persistent state is synchronized with the servlet's current state in memory.

Annotations
@Override
getBufferSizeback to summary
public int getBufferSize()

Return the configured input buffer size.

getDataBufferFactoryback to summary
public DataBufferFactory getDataBufferFactory()
getServletConfigback to summary
public ServletConfig getServletConfig()

Implements jakarta.servlet.Servlet.getServletConfig.

Doc from jakarta.servlet.Servlet.getServletConfig.

Returns a ServletConfig object, which contains initialization and startup parameters for this servlet. The ServletConfig object returned is the one passed to the init method.

Implementations of this interface are responsible for storing the ServletConfig object so that this method can return it. The GenericServlet class, which implements this interface, already does this.

Returns:ServletConfig

the ServletConfig object that initializes this servlet

Annotations
@Override
@Nullable
getServletInfoback to summary
public String getServletInfo()

Implements jakarta.servlet.Servlet.getServletInfo.

Doc from jakarta.servlet.Servlet.getServletInfo.

Returns information about the servlet, such as author, version, and copyright.

The string that this method returns should be plain text and not markup of any kind (such as HTML, XML, etc.).

Returns:String

a String containing servlet information

Annotations
@Override
getServletPathback to summary
public String getServletPath()

Return the Servlet path under which the Servlet is deployed by checking the Servlet registration from init(ServletConfig).

Returns:String

the path, or an empty string if the Servlet is deployed without a prefix (i.e. "/" or "/*"), or null if this method is invoked before the init(ServletConfig) Servlet container callback.

Annotations
@Nullable
getServletPathback to summary
private String getServletPath(ServletConfig config)
initback to summary
public void init(ServletConfig config)

Implements jakarta.servlet.Servlet.init.

Doc from jakarta.servlet.Servlet.init.

Called by the servlet container to indicate to a servlet that the servlet is being placed into service.

The servlet container calls the init method exactly once after instantiating the servlet. The init method must complete successfully before the servlet can receive any requests.

The servlet container cannot place the servlet into service if the init method

  1. Throws a ServletException
  2. Does not return within a time period defined by the Web server
Parameters
config:ServletConfig

a ServletConfig object containing the servlet's configuration and initialization parameters

Annotations
@Override
runIfAsyncNotCompleteback to summary
private static void runIfAsyncNotComplete(AsyncContext asyncContext, AtomicBoolean isCompleted, Runnable task)
serviceback to summary
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException

Implements jakarta.servlet.Servlet.service.

Doc from jakarta.servlet.Servlet.service.

Called by the servlet container to allow the servlet to respond to a request.

This method is only called after the servlet's init() method has completed successfully.

The status code of the response always should be set for a servlet that throws or sends an error.

Servlets typically run inside multithreaded servlet containers that can handle multiple requests concurrently. Developers must be aware to synchronize access to any shared resources such as files, network connections, and as well as the servlet's class and instance variables. More information on multithreaded programming in Java is available in the Java tutorial on multi-threaded programming.

Parameters
request:ServletRequest

the ServletRequest object that contains the client's request

response:ServletResponse

the ServletResponse object that contains the servlet's response

Annotations
@Override
Exceptions
ServletException:
if an exception occurs that interferes with the servlet's normal operation
IOException:
if an input or output exception occurs
setBufferSizeback to summary
public void setBufferSize(int bufferSize)

Set the size of the input buffer used for reading in bytes.

By default this is set to 8192.

setDataBufferFactoryback to summary
public void setDataBufferFactory(DataBufferFactory dataBufferFactory)
org.springframework.http.server.reactive back to summary

private Class ServletHttpHandlerAdapter.HandlerResultSubscriber

extends Object
implements Subscriber<Void>, Runnable
Class Inheritance
  • java.lang.Object
  • org.springframework.http.server.reactive.ServletHttpHandlerAdapter.HandlerResultSubscriber
All Implemented Interfaces
java.lang.Runnable, org.reactivestreams.Subscriber

Field Summary

Modifier and TypeField and Description
private final AsyncContext
private final AtomicBoolean
private final String
private volatile Subscription

Constructor Summary

AccessConstructor and Description
public
HandlerResultSubscriber(AsyncContext asyncContext, AtomicBoolean completionFlag, String logPrefix)

Method Summary

Modifier and TypeMethod and Description
public void
onComplete()

Implements org.reactivestreams.Subscriber.onComplete.

Successful terminal state.
public void
onError(Throwable
the throwable signaled
ex
)

Implements org.reactivestreams.Subscriber.onError.

Failed terminal state.
public void
onNext(Void
the element signaled
aVoid
)

Implements org.reactivestreams.Subscriber.onNext.

Data notification sent by the Publisher in response to requests to Subscription#request(long).
public void
public void
run()

Implements java.lang.Runnable.run.

Runs this operation.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

asyncContextback to summary
private final AsyncContext asyncContext
completionFlagback to summary
private final AtomicBoolean completionFlag
logPrefixback to summary
private final String logPrefix
subscriptionback to summary
private volatile Subscription subscription
Annotations
@Nullable

Constructor Detail

HandlerResultSubscriberback to summary
public HandlerResultSubscriber(AsyncContext asyncContext, AtomicBoolean completionFlag, String logPrefix)

Method Detail

onCompleteback to summary
public void onComplete()

Implements org.reactivestreams.Subscriber.onComplete.

Doc from org.reactivestreams.Subscriber.onComplete.

Successful terminal state.

No further events will be sent even if Subscription#request(long) is invoked again.

Annotations
@Override
onErrorback to summary
public void onError(Throwable ex)

Implements org.reactivestreams.Subscriber.onError.

Doc from org.reactivestreams.Subscriber.onError.

Failed terminal state.

No further events will be sent even if Subscription#request(long) is invoked again.

Parameters
ex:Throwable

the throwable signaled

Annotations
@Override
onNextback to summary
public void onNext(Void aVoid)

Implements org.reactivestreams.Subscriber.onNext.

Doc from org.reactivestreams.Subscriber.onNext.

Data notification sent by the Publisher in response to requests to Subscription#request(long).

Parameters
aVoid:Void

the element signaled

Annotations
@Override
onSubscribeback to summary
public void onSubscribe(Subscription subscription)

Implements org.reactivestreams.Subscriber.onSubscribe.

Doc from org.reactivestreams.Subscriber.onSubscribe.

Invoked after calling Publisher#subscribe(Subscriber).

No data will start flowing until Subscription#request(long) is invoked.

It is the responsibility of this Subscriber instance to call Subscription#request(long) whenever more data is wanted.

The Publisher will send notifications only in response to Subscription#request(long).

Parameters
subscription:Subscription

the Subscription that allows requesting data via Subscription#request(long)

Annotations
@Override
runback to summary
public void run()

Implements java.lang.Runnable.run.

Doc from java.lang.Runnable.run.

Runs this operation.

Annotations
@Override
org.springframework.http.server.reactive back to summary

private Class ServletHttpHandlerAdapter.HttpHandlerAsyncListener

extends Object
implements AsyncListener
Class Inheritance
  • java.lang.Object
  • org.springframework.http.server.reactive.ServletHttpHandlerAdapter.HttpHandlerAsyncListener
All Implemented Interfaces
jakarta.servlet.AsyncListener, java.util.EventListener

AsyncListener to complete the AsyncContext in case of error or timeout notifications from the container

Additional AsyncListeners are registered in ServletServerHttpRequest to signal onError/onComplete to the request body Subscriber, and in ServletServerHttpResponse to cancel the write Publisher and signal onError/onComplete downstream to the writing result Subscriber.

Field Summary

Modifier and TypeField and Description
private final AtomicBoolean
private final Runnable
private final String
private final AsyncListener
private final AsyncListener

Constructor Summary

AccessConstructor and Description
public
HttpHandlerAsyncListener(AsyncListener requestAsyncListener, AsyncListener responseAsyncListener, Runnable handlerDisposeTask, AtomicBoolean completionFlag, String logPrefix)

Method Summary

Modifier and TypeMethod and Description
private static void
private static void
private static void
private void
public void
onComplete(AsyncEvent
Provides access to the objects associated with the event
event
)

Implements jakarta.servlet.AsyncListener.onComplete.

This event is fired after the call to AsyncContext#complete() has been processed by the container.
public void
onError(AsyncEvent
Provides access to the objects associated with the event
event
)

Implements jakarta.servlet.AsyncListener.onError.

This event is fired if an error occurs during an asynchronous operation but before the container takes any action as a result of the error.
public void
onStartAsync(AsyncEvent
Provides access to the objects associated with the event
event
)

Implements jakarta.servlet.AsyncListener.onStartAsync.

This event is fired if new call is made to ServletRequest#startAsync() after the completion of the AsyncContext to which this listener was added.
public void
onTimeout(AsyncEvent
Provides access to the objects associated with the event
event
)

Implements jakarta.servlet.AsyncListener.onTimeout.

This event is fired if an asynchronous operation times out but before the container takes any action as a result of the timeout.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

completionFlagback to summary
private final AtomicBoolean completionFlag
handlerDisposeTaskback to summary
private final Runnable handlerDisposeTask
logPrefixback to summary
private final String logPrefix
requestAsyncListenerback to summary
private final AsyncListener requestAsyncListener
responseAsyncListenerback to summary
private final AsyncListener responseAsyncListener

Constructor Detail

HttpHandlerAsyncListenerback to summary
public HttpHandlerAsyncListener(AsyncListener requestAsyncListener, AsyncListener responseAsyncListener, Runnable handlerDisposeTask, AtomicBoolean completionFlag, String logPrefix)

Method Detail

delegateCompleteback to summary
private static void delegateComplete(AsyncListener listener, AsyncEvent event)
delegateErrorback to summary
private static void delegateError(AsyncListener listener, AsyncEvent event)
delegateTimeoutback to summary
private static void delegateTimeout(AsyncListener listener, AsyncEvent event)
handleTimeoutOrErrorback to summary
private void handleTimeoutOrError(AsyncEvent event)
onCompleteback to summary
public void onComplete(AsyncEvent event)

Implements jakarta.servlet.AsyncListener.onComplete.

Doc from jakarta.servlet.AsyncListener.onComplete.

This event is fired after the call to AsyncContext#complete() has been processed by the container.

Parameters
event:AsyncEvent

Provides access to the objects associated with the event

Annotations
@Override
onErrorback to summary
public void onError(AsyncEvent event)

Implements jakarta.servlet.AsyncListener.onError.

Doc from jakarta.servlet.AsyncListener.onError.

This event is fired if an error occurs during an asynchronous operation but before the container takes any action as a result of the error.

Parameters
event:AsyncEvent

Provides access to the objects associated with the event

Annotations
@Override
onStartAsyncback to summary
public void onStartAsync(AsyncEvent event)

Implements jakarta.servlet.AsyncListener.onStartAsync.

Doc from jakarta.servlet.AsyncListener.onStartAsync.

This event is fired if new call is made to ServletRequest#startAsync() after the completion of the AsyncContext to which this listener was added.

Parameters
event:AsyncEvent

Provides access to the objects associated with the event

Annotations
@Override
onTimeoutback to summary
public void onTimeout(AsyncEvent event)

Implements jakarta.servlet.AsyncListener.onTimeout.

Doc from jakarta.servlet.AsyncListener.onTimeout.

This event is fired if an asynchronous operation times out but before the container takes any action as a result of the timeout.

Parameters
event:AsyncEvent

Provides access to the objects associated with the event

Annotations
@Override