Top Description Methods
io.vertx.core.http

public Interface HttpServer

extends Measured
Known Direct Implementers
io.vertx.core.http.impl.HttpServerImpl
Annotations
@VertxGen
Imports
io.vertx.codegen.annotations.CacheReturn, .GenIgnore, .Fluent, .VertxGen, io.vertx.core.AsyncResult, .Future, .Handler, io.vertx.core.metrics.Measured, io.vertx.core.net.SSLOptions, .SocketAddress, .TrafficShapingOptions, io.vertx.core.net.impl.SocketAddressImpl, io.vertx.core.streams.ReadStream

An HTTP and WebSockets server.

You receive HTTP requests by providing a requestHandler. As requests arrive on the server the handler will be called with the requests.

You receive WebSockets by providing a webSocketHandler. As WebSocket connections arrive on the server, the WebSocket is passed to the handler.

Author
Tim Fox

Method Summary

Modifier and TypeMethod and Description
public int

Returns:

the actual port the server is listening on.
actualPort
()

The actual port the server is listening on.

public Future<Void>

Returns:

a future completed with the result
close
()

Close the server.

public void
close(Handler<AsyncResult<Void>>
the handler
completionHandler
)

Like close but supplying a handler that will be called when the server is actually closed (or has failed).

public HttpServer

Returns:

a reference to this, so the API can be used fluently
connectionHandler
(Handler<HttpConnection> handler)

Set a connection handler for the server.

public HttpServer

Returns:

a reference to this, so the API can be used fluently
exceptionHandler
(Handler<Throwable>
the handler to set
handler
)

Set an exception handler called for socket errors happening before the HTTP connection is established, e.g during the TLS handshake.

public HttpServer

Returns:

a reference to this, so the API can be used fluently
invalidRequestHandler
(Handler<HttpServerRequest> handler)

Set a handler for handling invalid requests.

public Future<HttpServer>

Returns:

a future completed with the listen operation result
listen
()

Tell the server to start listening.

public default Future<HttpServer>

Returns:

a future completed with the listen operation result
listen
(int
the port to listen on
port
,
String
the host to listen on
host
)

Tell the server to start listening.

public default HttpServer
listen(int
the port to listen on
port
,
String
the host to listen on
host
,
Handler<AsyncResult<HttpServer>>
the listen handler
listenHandler
)

Like listen(int, String) but supplying a handler that will be called when the server is actually listening (or has failed).

public default HttpServer
listen(SocketAddress
the address to listen on
address
,
Handler<AsyncResult<HttpServer>>
the listen handler
listenHandler
)

Tell the server to start listening on the given address supplying a handler that will be called when the server is actually listening (or has failed).

public Future<HttpServer>
listen(SocketAddress address)

Like listen(SocketAddress, Handler) but returns a Future of the asynchronous result

public default Future<HttpServer>

Returns:

a future completed with the listen operation result
listen
(int
the port to listen on
port
)

Like listen(int, String) but the server will listen on host "0.0.0.0" and port specified here ignoring any value in the io.vertx.core.http.HttpServerOptions that was used when creating the server.

public default HttpServer
listen(int
the port to listen on
port
,
Handler<AsyncResult<HttpServer>>
the listen handler
listenHandler
)

Like listen(int) but supplying a handler that will be called when the server is actually listening (or has failed).

public default HttpServer
listen(Handler<AsyncResult<HttpServer>>
the listen handler
listenHandler
)

Like listen but supplying a handler that will be called when the server is actually listening (or has failed).

public HttpServer

Returns:

a reference to this, so the API can be used fluently
requestHandler
(Handler<HttpServerRequest> handler)

Set the request handler for the server to requestHandler.

public Handler<HttpServerRequest>

Returns:

the request handler
requestHandler
()

public ReadStream<HttpServerRequest>

Returns:

the request stream
requestStream
()
Deprecated instead use requestHandler(Handler)

Return the request stream for the server.

public default Future<Boolean>

Returns:

a future signaling the update success
updateSSLOptions
(SSLOptions
the new SSL options
options
)

Update the server with new SSL options, the update happens if the options object is valid and different from the existing options object.

public default void
updateSSLOptions(SSLOptions
the new SSL options
options
,
Handler<AsyncResult<Boolean>>
the update handler
handler
)

Like updateSSLOptions(SSLOptions) but supplying a handler that will be called when the update happened (or has failed).

public Future<Boolean>

Returns:

a future signaling the update success
updateSSLOptions
(SSLOptions
the new SSL options
options
,
boolean
force the update when options are equals
force
)

Update the server with new SSL options, the update happens if the options object is valid and different from the existing options object.

public default void
updateSSLOptions(SSLOptions
the new SSL options
options
,
boolean
force the update when options are equals
force
,
Handler<AsyncResult<Boolean>>
the update handler
handler
)

Like updateSSLOptions(SSLOptions) but supplying a handler that will be called when the update happened (or has failed).

public void
updateTrafficShapingOptions(TrafficShapingOptions
the new traffic shaping options
options
)

Update traffic shaping options options, the update happens if valid values are passed for traffic shaping options.

public HttpServer

Returns:

a reference to this, so the API can be used fluently
webSocketHandler
(Handler<ServerWebSocket> handler)

Set the WebSocket handler for the server to wsHandler.

public Handler<ServerWebSocket>

Returns:

the WebSocket handler
webSocketHandler
()

public HttpServer
webSocketHandshakeHandler(Handler<ServerWebSocketHandshake> handler)

Set a handler for WebSocket handshake.

public ReadStream<ServerWebSocket>

Returns:

the WebSocket stream
webSocketStream
()
Deprecated instead use webSocketHandler(Handler)

Return the WebSocket stream for the server.

Inherited from io.vertx.core.metrics.Measured:
isMetricsEnabled

Method Detail

actualPortback to summary
public int actualPort()

The actual port the server is listening on. This is useful if you bound the server specifying 0 as port number signifying an ephemeral port

Returns:int

the actual port the server is listening on.

closeback to summary
public Future<Void> close()

Close the server. Any open HTTP connections will be closed.

The close happens asynchronously and the server may not be closed until some time after the call has returned.

Returns:Future<Void>

a future completed with the result

closeback to summary
public void close(Handler<AsyncResult<Void>> completionHandler)

Like close but supplying a handler that will be called when the server is actually closed (or has failed).

Parameters
completionHandler:Handler<AsyncResult<Void>>

the handler

connectionHandlerback to summary
public HttpServer connectionHandler(Handler<HttpConnection> handler)

Set a connection handler for the server.
The handler will always be called on the event-loop thread.

Returns:HttpServer

a reference to this, so the API can be used fluently

Annotations
@Fluent
exceptionHandlerback to summary
public HttpServer exceptionHandler(Handler<Throwable> handler)

Set an exception handler called for socket errors happening before the HTTP connection is established, e.g during the TLS handshake.

Parameters
handler:Handler<Throwable>

the handler to set

Returns:HttpServer

a reference to this, so the API can be used fluently

Annotations
@Fluent
invalidRequestHandlerback to summary
public HttpServer invalidRequestHandler(Handler<HttpServerRequest> handler)

Set a handler for handling invalid requests. When an invalid request is received by the server this handler will be called with the request. The handler can send any HTTP response, when the response ends, the server shall close the connection. HttpServerRequest#decoderResult() can be used to obtain the Netty decoder result and the failure cause reported by the decoder.

Currently this handler is only used for HTTP/1.x requests.

When no specific handler is set, the HttpServerRequest#DEFAULT_INVALID_REQUEST_HANDLER is used.

Returns:HttpServer

a reference to this, so the API can be used fluently

listenback to summary
public Future<HttpServer> listen()

Tell the server to start listening. The server will listen on the port and host specified in the io.vertx.core.http.HttpServerOptions that was used when creating the server.

The listen happens asynchronously and the server may not be listening until some time after the call has returned.

Returns:Future<HttpServer>

a future completed with the listen operation result

listenback to summary
public default Future<HttpServer> listen(int port, String host)

Tell the server to start listening. The server will listen on the port and host specified here, ignoring any value set in the io.vertx.core.http.HttpServerOptions that was used when creating the server.

The listen happens asynchronously and the server may not be listening until some time after the call has returned.

Parameters
port:int

the port to listen on

host:String

the host to listen on

Returns:Future<HttpServer>

a future completed with the listen operation result

listenback to summary
public default HttpServer listen(int port, String host, Handler<AsyncResult<HttpServer>> listenHandler)

Like listen(int, String) but supplying a handler that will be called when the server is actually listening (or has failed).

Parameters
port:int

the port to listen on

host:String

the host to listen on

listenHandler:Handler<AsyncResult<HttpServer>>

the listen handler

Annotations
@Fluent
listenback to summary
public default HttpServer listen(SocketAddress address, Handler<AsyncResult<HttpServer>> listenHandler)

Tell the server to start listening on the given address supplying a handler that will be called when the server is actually listening (or has failed).

Parameters
address:SocketAddress

the address to listen on

listenHandler:Handler<AsyncResult<HttpServer>>

the listen handler

Annotations
@Fluent
listenback to summary
public Future<HttpServer> listen(SocketAddress address)

Like listen(SocketAddress, Handler) but returns a Future of the asynchronous result

listenback to summary
public default Future<HttpServer> listen(int port)

Like listen(int, String) but the server will listen on host "0.0.0.0" and port specified here ignoring any value in the io.vertx.core.http.HttpServerOptions that was used when creating the server.

Parameters
port:int

the port to listen on

Returns:Future<HttpServer>

a future completed with the listen operation result

listenback to summary
public default HttpServer listen(int port, Handler<AsyncResult<HttpServer>> listenHandler)

Like listen(int) but supplying a handler that will be called when the server is actually listening (or has failed).

Parameters
port:int

the port to listen on

listenHandler:Handler<AsyncResult<HttpServer>>

the listen handler

Annotations
@Fluent
listenback to summary
public default HttpServer listen(Handler<AsyncResult<HttpServer>> listenHandler)

Like listen but supplying a handler that will be called when the server is actually listening (or has failed).

Parameters
listenHandler:Handler<AsyncResult<HttpServer>>

the listen handler

Annotations
@Fluent
requestHandlerback to summary
public HttpServer requestHandler(Handler<HttpServerRequest> handler)

Set the request handler for the server to requestHandler. As HTTP requests are received by the server, instances of HttpServerRequest will be created and passed to this handler.

Returns:HttpServer

a reference to this, so the API can be used fluently

Annotations
@Fluent
requestHandlerback to summary
public Handler<HttpServerRequest> requestHandler()
Returns:Handler<HttpServerRequest>

the request handler

Annotations
@GenIgnore
requestStreamback to summary
public ReadStream<HttpServerRequest> requestStream()

Deprecated

instead use requestHandler(Handler)

Return the request stream for the server. As HTTP requests are received by the server, instances of HttpServerRequest will be created and passed to the stream io.vertx.core.streams.ReadStream#handler(io.vertx.core.Handler).

Returns:ReadStream<HttpServerRequest>

the request stream

Annotations
@Deprecated
@CacheReturn
updateSSLOptionsback to summary
public default Future<Boolean> updateSSLOptions(SSLOptions options)

Update the server with new SSL options, the update happens if the options object is valid and different from the existing options object.

The boolean succeeded future result indicates whether the update occurred.

Parameters
options:SSLOptions

the new SSL options

Returns:Future<Boolean>

a future signaling the update success

updateSSLOptionsback to summary
public default void updateSSLOptions(SSLOptions options, Handler<AsyncResult<Boolean>> handler)

Like updateSSLOptions(SSLOptions) but supplying a handler that will be called when the update happened (or has failed).

Parameters
options:SSLOptions

the new SSL options

handler:Handler<AsyncResult<Boolean>>

the update handler

updateSSLOptionsback to summary
public Future<Boolean> updateSSLOptions(SSLOptions options, boolean force)

Update the server with new SSL options, the update happens if the options object is valid and different from the existing options object.

The options object is compared using its equals method against the existing options to prevent an update when the objects are equals since loading options can be costly, this can happen for share TCP servers. When object are equals, setting force to true forces the update.

The boolean succeeded future result indicates whether the update occurred.

Parameters
options:SSLOptions

the new SSL options

force:boolean

force the update when options are equals

Returns:Future<Boolean>

a future signaling the update success

updateSSLOptionsback to summary
public default void updateSSLOptions(SSLOptions options, boolean force, Handler<AsyncResult<Boolean>> handler)

Like updateSSLOptions(SSLOptions) but supplying a handler that will be called when the update happened (or has failed).

Parameters
options:SSLOptions

the new SSL options

force:boolean

force the update when options are equals

handler:Handler<AsyncResult<Boolean>>

the update handler

updateTrafficShapingOptionsback to summary
public void updateTrafficShapingOptions(TrafficShapingOptions options)

Update traffic shaping options options, the update happens if valid values are passed for traffic shaping options. This update happens synchronously and at best effort for rate update to take effect immediately.

Parameters
options:TrafficShapingOptions

the new traffic shaping options

webSocketHandlerback to summary
public HttpServer webSocketHandler(Handler<ServerWebSocket> handler)

Set the WebSocket handler for the server to wsHandler. If a WebSocket connect handshake is successful a new ServerWebSocket instance will be created and passed to the handler.

Returns:HttpServer

a reference to this, so the API can be used fluently

Annotations
@Fluent
webSocketHandlerback to summary
public Handler<ServerWebSocket> webSocketHandler()
Returns:Handler<ServerWebSocket>

the WebSocket handler

Annotations
@GenIgnore
webSocketHandshakeHandlerback to summary
public HttpServer webSocketHandshakeHandler(Handler<ServerWebSocketHandshake> handler)

Set a handler for WebSocket handshake.

When an inbound HTTP request presents a WebSocket upgrade, this handler is called first. The handler can chose to ServerWebSocketHandshake#accept() or ServerWebSocketHandshake#reject() the request.

Setting no handler, implicitly accepts any HTTP request connection presenting an upgrade header and upgrades it to a WebSocket.

Annotations
@Fluent
webSocketStreamback to summary
public ReadStream<ServerWebSocket> webSocketStream()

Deprecated

instead use webSocketHandler(Handler)

Return the WebSocket stream for the server. If a WebSocket connect handshake is successful a new ServerWebSocket instance will be created and passed to the stream io.vertx.core.streams.ReadStream#handler(io.vertx.core.Handler).

Returns:ReadStream<ServerWebSocket>

the WebSocket stream

Annotations
@Deprecated
@CacheReturn