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 TypeMethodDescriptionvoidaddCookies(Object exchange, Map<String, Cookie> cookies) Adds cookies.voidaddHttpAuthentication(String realmName, String username, String password) Adds a user to an HTTP protected realm.voidaddStaticResourceToServe(StaticResource<?> staticResource) Adds a static resource to serve directly by the server.voidcreateHttpAuthenticationRealm(String pathPrefix, String realmName) Creates HTTP authentication protection (realm) for the specified path prefix.voidEnds the exchange.voidflushBytes(Object exchange, byte[] bytes, boolean end) Flushes some bytes to the response.booleanforceRequestSizeValidation(Object exchange) Is the request size valid?getContentTypeBestMatch(Object exchange) Gets the bestContent-Typeto 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.booleanisResponseClosed(Object exchange) Is the response closed?booleanisResponseHeadersSent(Object exchange) Are the response headers sent?booleanIs this server running (started)?voidpush(Object exchange, Set<ResourceToPush> resourcesToPush) IfHTTP/2is used, you can push extra resources at the same time you response to a request.voidRemoves all static resources served directly by the server.voidremoveHttpAuthentication(String username) Removes a user from all HTTP protected realms.voidremoveHttpAuthentication(String username, String realmName) Removes a user to an HTTP protected realm.voidremoveResponseHeader(Object exchange, String name) Removes a response header.voidremoveStaticResourcesServed(StaticResourceType staticResourceType, String urlPath) Removes a static resource served directly by the server.voidsetResponseHeader(Object exchange, String name, List<String> values) Sets a response header.voidSets the response headers.voidsetResponseStatusCode(Object exchange, int statusCode) Sets the response status code.voidstart()Starts the server.voidstop()Stops the servervoidstop(boolean sendClosingMessageToPeers) Stops the servervoidwebsocketCloseEndpoint(String endpointId) Closes a Websocket endpoint.voidwebsocketCloseEndpoint(String endpointId, int closingCode, String closingReason) Closes the entire Websocket endpoint.voidwebsocketConnection(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-Typeto 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'snameattribute. -
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
nullif not found.
-
getIp
Gets the IP of the current request. -
push
IfHTTP/2is 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
Linkheaders 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).
-