Class Server
- All Implemented Interfaces:
Handler, Handler.Container, Handler.Singleton, Request.Handler, Attributes, Container, Destroyable, Dumpable, Dumpable.DumpableContainer, LifeCycle, Invocable
-
Nested Class Summary
Nested classes/interfaces inherited from class Handler.Abstract
Handler.Abstract.NonBlockingNested classes/interfaces inherited from class AbstractLifeCycle
AbstractLifeCycle.AbstractLifeCycleListener, AbstractLifeCycle.StopExceptionNested classes/interfaces inherited from interface Attributes
Attributes.Layer, Attributes.Lazy, Attributes.Mapped, Attributes.Synthetic, Attributes.WrapperNested classes/interfaces inherited from interface Container
Container.InheritedListener, Container.ListenerNested classes/interfaces inherited from interface Dumpable
Dumpable.DumpableContainerNested classes/interfaces inherited from interface Handler
Handler.Abstract, Handler.AbstractContainer, Handler.Collection, Handler.Container, Handler.Sequence, Handler.Singleton, Handler.WrapperNested classes/interfaces inherited from interface Invocable
Invocable.Callable, Invocable.InvocationType, Invocable.ReadyTask, Invocable.TaskNested classes/interfaces inherited from interface LifeCycle
LifeCycle.ListenerNested classes/interfaces inherited from interface Request.Handler
Request.Handler.AbortException -
Field Summary
Fields inherited from interface Attributes
NULLFields inherited from interface Invocable
__nonBlocking, NOOP -
Constructor Summary
ConstructorsConstructorDescriptionServer()Server(int port) Convenience constructor Creates server and aServerConnectorat the passed port.Server(InetSocketAddress addr) Convenience constructorServer(ThreadPool pool) Server(ThreadPool threadPool, Scheduler scheduler, ByteBufferPool bufferPool) -
Method Summary
Modifier and TypeMethodDescriptionvoidaddBeanToAllConnectors(Object bean) Add a bean to all connectors on the server.voidaddConnector(Connector connector) voidClear all attribute namesprotected voiddoStart()Starts the managed lifecycle beans in the order they were added.protected voiddoStop()Stops the managed lifecycle beans in the reverse order they were added.voiddump(Appendable out, String indent) Dump this object (and children) into an Appendable using the provided indent after any new lines.getAttribute(String name) Get an attributeGet the immutable set of attribute names.Get the default FaviconGet the Default CSSbooleanlonggetURI()static StringbooleanInvoked to handle the passed HTTP request and response.booleanisDryRun()booleanbooleanbooleanvoidjoin()static voidremoveAttribute(String name) Remove an attributevoidremoveConnector(Connector connector) Convenience method which callsgetConnectors()andsetConnectors(Connector[])to remove a connector.setAttribute(String name, Object attribute) Set an attributevoidsetConnectors(Connector[] connectors) Set the connectors for this server.voidsetDefaultHandler(Handler defaultHandler) voidsetDryRun(boolean dryRun) voidsetDumpAfterStart(boolean dumpAfterStart) Set true ifContainerLifeCycle.dumpStdErr()is called after starting.voidsetDumpBeforeStop(boolean dumpBeforeStop) Set true ifContainerLifeCycle.dumpStdErr()is called before stopping.voidsetErrorHandler(Request.Handler errorHandler) voidsetOpenEarly(boolean openEarly) Allows to disable early opening of network sockets.voidsetRequestLog(RequestLog requestLog) voidsetServerInfo(String serverInfo) voidsetStopAtShutdown(boolean stop) Set stop server at shutdown behaviour.voidsetStopTimeout(long stopTimeout) voidsetTempDirectory(File temp) Set the temporary directory returned byContext.getTempDirectory()for the rootContextreturnedgetContext().voidsetTempDirectory(String temp) Convenience method to callsetTempDirectory(File)from a String representation of the temporary directory.protected voidStarts the given lifecycle.toString()Methods inherited from class Handler.Wrapper
getHandler, setHandlerMethods inherited from class Handler.AbstractContainer
findContainerOf, getDescendant, getDescendants, isDynamic, setDynamic, setServerMethods inherited from class Handler.Abstract
destroyMethods inherited from class ContainerLifeCycle
addBean, addBean, addEventListener, addManaged, contains, dump, dump, dumpObjects, dumpStdErr, getBean, getBeans, getBeans, getContainedBeans, getContainedBeans, installBean, installBean, isAuto, isManaged, isUnmanaged, manage, removeBean, removeBeans, removeEventListener, setBeans, stop, unmanage, updateBean, updateBean, updateBeans, updateBeansMethods inherited from class AbstractLifeCycle
getEventListeners, getState, getState, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, setEventListeners, start, stopMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface Attributes
asAttributeMap, equals, hashCodeMethods inherited from interface Container
getCachedBeans, getEventListenersMethods inherited from interface Destroyable
destroyMethods inherited from interface Dumpable.DumpableContainer
isDumpableMethods inherited from interface Handler.Container
getContainer, getDescendant, getDescendants, getDescendantsMethods inherited from interface Handler.Singleton
getHandlers, getTail, insertHandler, setHandlerMethods inherited from interface LifeCycle
addEventListener, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, removeEventListener, start, stop
-
Constructor Details
-
Server
public Server() -
Server
Convenience constructor Creates server and aServerConnectorat the passed port.- Parameters:
port- The port of a network HTTP connector (or 0 for a randomly allocated port).- See Also:
-
Server
Convenience constructorCreates server and a
ServerConnectorat the passed address.- Parameters:
addr- the inet socket address to create the connector from
-
Server
-
Server
public Server(@Name("threadPool") ThreadPool threadPool, @Name("scheduler") Scheduler scheduler, @Name("bufferPool") ByteBufferPool bufferPool)
-
-
Method Details
-
getDefaultHandler
-
setDefaultHandler
- Parameters:
defaultHandler- The handler to use if no other handler is set or has handled the request. This handler should always accept the request, even if only to send a 404.
-
handle
Description copied from interface:Request.HandlerInvoked to handle the passed HTTP request and response.
The request is accepted by returning true, then handling must be concluded by completing the passed callback. The handling may be asynchronous, i.e. this method may return true and complete the given callback later, possibly from a different thread. If this method returns false, then the callback must not be invoked and any mutation on the response reversed.
Exceptions thrown by this method may be subsequently handled by an error
Request.Handler, if present, otherwise a default HTTP 500 error is generated and the callback completed while writing the error response.The simplest implementation is:
public boolean handle(Request request, Response response, Callback callback) { callback.succeeded(); return true; }A HelloWorld implementation is:
public boolean handle(Request request, Response response, Callback callback) { response.write(true, ByteBuffer.wrap("Hello World\n".getBytes(StandardCharsets.UTF_8)), callback); return true; }- Specified by:
handlein interfaceRequest.Handler- Overrides:
handlein classHandler.Wrapper- Parameters:
request- the HTTP request to handleresponse- the HTTP response to handlecallback- the callback to complete when the handling is complete- Returns:
- True if and only if the request will be handled, a response generated and the callback eventually called. This may occur within the scope of the call to this method, or asynchronously some time later. If false is returned, then this method must not generate a response, nor complete the callback.
- Throws:
Exception- if there is a failure during the handling. Catchers cannot assume that the callback will be called and thus should attempt to complete the request as if a false had been returned.- See Also:
-
getServerInfo
-
setTempDirectory
Convenience method to call
setTempDirectory(File)from a String representation of the temporary directory.- Parameters:
temp- A string representation of the temporary directory.- See Also:
-
setTempDirectory
Set the temporary directory returned by
Context.getTempDirectory()for the rootContextreturnedgetContext(). If not set explicitly here, then the rootContext.getTempDirectory()will return either the directory found atnew File(IO.asFile(System.getProperty("jetty.base")), "work")if it exists, else the JVMs temporary directory asIO.asFile(System.getProperty("java.io.tmpdir")).- Parameters:
temp- A directory that must exist and be writable or null to get the default.
-
getTempDirectory
@ManagedAttribute(value="The server temporary directory", readonly=true) public File getTempDirectory()- Returns:
- The server temporary directory if set, else null. To always obtain a non-null
temporary directory use
Context.getTempDirectory()ongetContext(). - See Also:
-
setServerInfo
-
getContext
Get theContextassociated with allRequests prior to being handled by aContextHandler. AServer'sContext:- has a
nullcontext path - returns the
ClassLoaderthat loaded theServerfromContext.getClassLoader(). - is an
Executorthat delegates to theServer ThreadPool - is a
Decoratorusing theDecoratedObjectFactoryfound as abeanof theServer - has the same
temporary directorof theserver
- has a
-
getMimeTypes
-
getInvocationType
- Specified by:
getInvocationTypein interfaceInvocable- Specified by:
getInvocationTypein interfaceRequest.Handler- Overrides:
getInvocationTypein classHandler.Wrapper- Returns:
- The InvocationType of this object
-
isOpenEarly
public boolean isOpenEarly() -
setOpenEarly
public void setOpenEarly(boolean openEarly) Allows to disable early opening of network sockets. Network sockets are opened early by default.- Parameters:
openEarly- IfopenEarlyistrue(default), network sockets are opened before starting other components. IfopenEarlyisfalse, network connectors open sockets when they're started.
-
isDryRun
public boolean isDryRun() -
setDryRun
public void setDryRun(boolean dryRun) -
getRequestLog
-
getErrorHandler
-
setRequestLog
-
setErrorHandler
-
getVersion
-
setStopTimeout
public void setStopTimeout(long stopTimeout) -
getStopTimeout
public long getStopTimeout() -
getStopAtShutdown
public boolean getStopAtShutdown() -
setStopAtShutdown
public void setStopAtShutdown(boolean stop) Set stop server at shutdown behaviour.- Parameters:
stop- If true, this server instance will be explicitly stopped when the JVM is shutdown. Otherwise the JVM is stopped with the server running.- See Also:
-
getConnectors
@ManagedAttribute(value="connectors for this server", readonly=true) public Connector[] getConnectors()- Returns:
- Returns the connectors.
-
addConnector
-
removeConnector
Convenience method which callsgetConnectors()andsetConnectors(Connector[])to remove a connector.- Parameters:
connector- The connector to remove.
-
setConnectors
Set the connectors for this server. Each connector has this server set as it's ThreadPool and its Handler.- Parameters:
connectors- The connectors to set.
-
addBeanToAllConnectors
Add a bean to all connectors on the server. If the bean is an instance ofConnection.Listenerit will also be registered as a listener on all connections accepted by the connectors.- Parameters:
bean- the bean to be added.
-
getThreadPool
- Returns:
- Returns the threadPool.
-
getScheduler
-
getByteBufferPool
-
isDumpAfterStart
@ManagedAttribute("Whether to dump the server to stderr after start") public boolean isDumpAfterStart()- Returns:
- true if
ContainerLifeCycle.dumpStdErr()is called after starting
-
setDumpAfterStart
public void setDumpAfterStart(boolean dumpAfterStart) Set true ifContainerLifeCycle.dumpStdErr()is called after starting.- Parameters:
dumpAfterStart- true ifContainerLifeCycle.dumpStdErr()is called after starting
-
isDumpBeforeStop
@ManagedAttribute("Whether to dump the server to stderr before stop") public boolean isDumpBeforeStop()- Returns:
- true if
ContainerLifeCycle.dumpStdErr()is called before stopping
-
setDumpBeforeStop
public void setDumpBeforeStop(boolean dumpBeforeStop) Set true ifContainerLifeCycle.dumpStdErr()is called before stopping.- Parameters:
dumpBeforeStop- true ifContainerLifeCycle.dumpStdErr()is called before stopping
-
getDateField
-
doStart
Description copied from class:ContainerLifeCycleStarts the managed lifecycle beans in the order they were added.- Overrides:
doStartin classHandler.Abstract- Throws:
Exception- If there was a problem starting. Will cause a transition to FAILED state
-
start
Description copied from class:ContainerLifeCycleStarts the given lifecycle.- Overrides:
startin classContainerLifeCycle- Parameters:
l- the lifecycle to start- Throws:
Exception- if unable to start lifecycle
-
doStop
Description copied from class:ContainerLifeCycleStops the managed lifecycle beans in the reverse order they were added.- Overrides:
doStopin classHandler.Abstract- Throws:
Exception- If there was a problem stopping. Will cause a transition to FAILED state
-
join
- Throws:
InterruptedException
-
setAttribute
Description copied from interface:AttributesSet an attribute- Specified by:
setAttributein interfaceAttributes- Parameters:
name- the attribute to setattribute- the value to set. A null value is equivalent to removing the attribute.- Returns:
- the previous value of the attribute if set, else
null
-
removeAttribute
Description copied from interface:AttributesRemove an attribute- Specified by:
removeAttributein interfaceAttributes- Parameters:
name- the attribute to remove- Returns:
- the value of the attribute if removed, else
null
-
getAttribute
Description copied from interface:AttributesGet an attribute- Specified by:
getAttributein interfaceAttributes- Parameters:
name- the attribute to get- Returns:
- the value of the attribute, or
nullif no such attribute exists
-
getAttributeNameSet
Description copied from interface:AttributesGet the immutable set of attribute names.- Specified by:
getAttributeNameSetin interfaceAttributes- Returns:
- Set of attribute names, or an empty set if there are no attributes.
-
clearAttributes
public void clearAttributes()Description copied from interface:AttributesClear all attribute names- Specified by:
clearAttributesin interfaceAttributes
-
getURI
- Returns:
- The URI of the first
NetworkConnectorand firstContextHandler, or null
-
getServer
- Specified by:
getServerin interfaceHandler- Overrides:
getServerin classHandler.Abstract- Returns:
- the
Serverassociated with thisHandler
-
getDefaultStyleSheet
-
getDefaultFavicon
-
toString
- Overrides:
toStringin classAbstractLifeCycle
-
dump
Description copied from interface:DumpableDump this object (and children) into an Appendable using the provided indent after any new lines. The indent should not be applied to the first object dumped.- Specified by:
dumpin interfaceDumpable- Overrides:
dumpin classContainerLifeCycle- Parameters:
out- The appendable to dump toindent- The indent to apply after any new lines.- Throws:
IOException- if unable to write to Appendable
-
main
-