Interface Server
- All Known Implementing Classes:
SpincastUndertowServer
The "exchange" object is a request scoped object provided by the HTTP server to identify the request.
A Server implementation has to receive FrontController
as
a dependency and call handle(...)
on it when
a new request is made.
Note that the server will automatically remove any cache buster codes
from the request's path.
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addCookies
(Object exchange, Map<String, Cookie> cookies) Adds cookies.void
addHttpAuthentication
(String realmName, String username, String password) Adds a user to an HTTP protected realm.void
addStaticResourceToServe
(StaticResource<?> staticResource) Adds a static resource to serve directly by the server.void
createHttpAuthenticationRealm
(String pathPrefix, String realmName) Creates HTTP authentication protection (realm) for the specified path prefix.void
Ends the exchange.void
flushBytes
(Object exchange, byte[] bytes, boolean end) Flushes some bytes to the response.boolean
forceRequestSizeValidation
(Object exchange) Is the request size valid?getContentTypeBestMatch
(Object exchange) Gets the bestContent-Type
to use for the current request.getCookies
(Object exchange) Gets the request cookies values.getFormData
(Object exchange) The form data, if any.getFullUrlOriginal
(Object exchange) The full encoded URL of the original, non proxied, request, including the queryString.getFullUrlOriginal
(Object exchange, boolean keepCacheBusters) The full encoded URL of the original, non proxied, request, including the queryString.getFullUrlProxied
(Object exchange) The full encoded URL of the potentially proxied request, including the queryString.getFullUrlProxied
(Object exchange, boolean keepCacheBusters) The full encoded URL of the potentially proxied request, including the queryString.Returns the existing HTTP authentication realms, the key being the realm's name and the value being the prefix path associated to this realm.getHttpMethod
(Object exchange) Gets the HTTP method associated with the request.Gets the IP of the current request.getQueryStringParams
(Object exchange) Gets the queryString parameters.getRawInputStream
(Object exchange) The raw InputStream of the current request.getRequestHeaders
(Object exchange) The headers from the request.getRequestScheme
(Object exchange) Gets the request scheme, "http" for example.getResponseHeaders
(Object exchange) Gets the response headers.getStaticResourceServed
(String urlPath) Gets a static resource served directly by the server, using its path.Set<StaticResource<?>>
Gets all static resource served directly by the server.getUploadedFiles
(Object exchange) The uploaded files, if any.getWebsocketEndpointManager
(String endpointId) Returns the manager for a Websockets endpoint.Returns the managers of the existing Websockets endpoints.boolean
isResponseClosed
(Object exchange) Is the response closed?boolean
isResponseHeadersSent
(Object exchange) Are the response headers sent?boolean
Is this server running (started)?void
push
(Object exchange, Set<ResourceToPush> resourcesToPush) IfHTTP/2
is used, you can push extra resources at the same time you response to a request.void
Removes all static resources served directly by the server.void
removeHttpAuthentication
(String username) Removes a user from all HTTP protected realms.void
removeHttpAuthentication
(String username, String realmName) Removes a user to an HTTP protected realm.void
removeResponseHeader
(Object exchange, String name) Removes a response header.void
removeStaticResourcesServed
(StaticResourceType staticResourceType, String urlPath) Removes a static resource served directly by the server.void
setResponseHeader
(Object exchange, String name, List<String> values) Sets a response header.void
Sets the response headers.void
setResponseStatusCode
(Object exchange, int statusCode) Sets the response status code.void
start()
Starts the server.void
stop()
Stops the servervoid
stop
(boolean sendClosingMessageToPeers) Stops the servervoid
websocketCloseEndpoint
(String endpointId) Closes a Websocket endpoint.void
websocketCloseEndpoint
(String endpointId, int closingCode, String closingReason) Closes the entire Websocket endpoint.void
websocketConnection
(Object exchange, String endpointId, String peerId) Transforms the request to a peer Websocket connection on the endpoint 'endpointId'.websocketCreateEndpoint
(String endpointId, WebsocketEndpointHandler endpointHandler) Creates a new Websocket endpoint.
-
Method Details
-
start
void start()Starts the server. -
stop
void stop()Stops the serverWill try to send a "closing" message to any WebSocket peer before closing their connections.
-
stop
void stop(boolean sendClosingMessageToPeers) Stops the server- Parameters:
sendClosingMessageToPeers
- iftrue
, Spincast will try to send a "closing" message to any WebSocket peer before closing their connections.
-
isRunning
boolean isRunning()Is this server running (started)? -
addStaticResourceToServe
Adds a static resource to serve directly by the server. -
removeStaticResourcesServed
Removes a static resource served directly by the server. -
removeAllStaticResourcesServed
void removeAllStaticResourcesServed()Removes all static resources served directly by the server. -
getStaticResourceServed
Gets a static resource served directly by the server, using its path. -
getStaticResourcesServed
Set<StaticResource<?>> getStaticResourcesServed()Gets all static resource served directly by the server. -
getHttpMethod
Gets the HTTP method associated with the request. -
getFullUrlOriginal
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.
-
getFullUrlOriginal
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.
- Parameters:
keepCacheBusters
- iftrue
, the returned URL will contain the cache buster codes, if there were any. The default behavior is to automatically remove them.
-
getFullUrlProxied
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.
-
getFullUrlProxied
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.
- Parameters:
keepCacheBusters
- iftrue
, the returned URL will contain the cache buster codes, if there were any. The default behavior is to automatically remove them.
-
getContentTypeBestMatch
Gets the bestContent-Type
to use for the current request. -
setResponseHeaders
Sets the response headers. Override any existing ones. -
setResponseHeader
Sets a response header. Override any existing one with the same name. -
getResponseHeaders
Gets the response headers. -
removeResponseHeader
Removes a response header. -
addCookies
Adds cookies. -
getCookies
Gets the request cookies values. -
getQueryStringParams
Gets the queryString parameters. -
setResponseStatusCode
Sets the response status code. -
flushBytes
Flushes some bytes to the response.- Parameters:
end
- iftrue
, the exchange will be closed and nothing more can be send.
-
end
Ends the exchange. Nothing more can be send. -
isResponseClosed
Is the response closed? -
isResponseHeadersSent
Are the response headers sent? -
getRequestScheme
Gets the request scheme, "http" for example. -
getRawInputStream
The raw InputStream of the current request. -
getFormData
The form data, if any. -
getUploadedFiles
The uploaded files, if any. The key of the map if the HTML'sname
attribute. -
forceRequestSizeValidation
Is the request size valid? -
getRequestHeaders
The headers from the request. The keys will be case insensitive. -
createHttpAuthenticationRealm
Creates HTTP authentication protection (realm) for the specified path prefix.- Parameters:
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.
-
getHttpAuthenticationRealms
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.
-
addHttpAuthentication
Adds a user to an HTTP protected realm. -
removeHttpAuthentication
Removes a user to an HTTP protected realm. -
removeHttpAuthentication
Removes a user from all HTTP protected realms. -
websocketCreateEndpoint
WebsocketEndpointManager websocketCreateEndpoint(String endpointId, WebsocketEndpointHandler endpointHandler) Creates a new Websocket endpoint.- Returns:
- the manager for this endpoint.
-
websocketCloseEndpoint
Closes a Websocket endpoint. No more connections will be accepter -
websocketCloseEndpoint
Closes the entire Websocket endpoint. All peer connections of this endpoint will be closed.- Parameters:
closingCode
- The closing code.closingReason
- The closing reason.
-
websocketConnection
Transforms the request to a peer Websocket connection on the endpoint 'endpointId'. -
getWebsocketEndpointManagers
List<WebsocketEndpointManager> getWebsocketEndpointManagers()Returns the managers of the existing Websockets endpoints. -
getWebsocketEndpointManager
Returns the manager for a Websockets endpoint.- Returns:
- the manager or
null
if not found.
-
getIp
Gets the IP of the current request. -
push
IfHTTP/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).
-