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 org.eclipse.jetty.server.Handler.Abstract
Handler.Abstract.NonBlocking
Nested classes/interfaces inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle
AbstractLifeCycle.AbstractLifeCycleListener, AbstractLifeCycle.StopException
Nested classes/interfaces inherited from interface org.eclipse.jetty.util.Attributes
Attributes.Layer, Attributes.Lazy, Attributes.Mapped, Attributes.Synthetic, Attributes.Wrapper
Nested classes/interfaces inherited from interface org.eclipse.jetty.util.component.Container
Container.InheritedListener, Container.Listener
Nested classes/interfaces inherited from interface org.eclipse.jetty.util.component.Dumpable
Dumpable.DumpableContainer
Nested classes/interfaces inherited from interface org.eclipse.jetty.server.Handler
Handler.Abstract, Handler.AbstractContainer, Handler.Collection, Handler.Container, Handler.Sequence, Handler.Singleton, Handler.Wrapper
Nested classes/interfaces inherited from interface org.eclipse.jetty.util.thread.Invocable
Invocable.Callable, Invocable.InvocationType, Invocable.ReadyTask, Invocable.Task
Nested classes/interfaces inherited from interface org.eclipse.jetty.util.component.LifeCycle
LifeCycle.Listener
Nested classes/interfaces inherited from interface org.eclipse.jetty.server.Request.Handler
Request.Handler.AbortException
-
Field Summary
Fields inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle
FAILED, STARTED, STARTING, STOPPED, STOPPING
Fields inherited from interface org.eclipse.jetty.util.Attributes
NULL
Fields inherited from interface org.eclipse.jetty.util.thread.Invocable
__nonBlocking, NOOP
-
Constructor Summary
ConstructorDescriptionServer()
Server
(int port) Convenience constructor Creates server and aServerConnector
at the passed port.Server
(InetSocketAddress addr) Convenience constructorServer
(ThreadPool pool) Server
(ThreadPool threadPool, Scheduler scheduler, ByteBufferPool bufferPool) -
Method Summary
Modifier and TypeMethodDescriptionvoid
addBeanToAllConnectors
(Object bean) Add a bean to all connectors on the server.void
addConnector
(Connector connector) void
Clear all attribute namesprotected void
doStart()
Starts the managed lifecycle beans in the order they were added.protected void
doStop()
Stops the managed lifecycle beans in the reverse order they were added.void
dump
(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 CSSboolean
long
getURI()
static String
boolean
Invoked to handle the passed HTTP request and response.boolean
isDryRun()
boolean
boolean
boolean
void
join()
static void
removeAttribute
(String name) Remove an attributevoid
removeConnector
(Connector connector) Convenience method which callsgetConnectors()
andsetConnectors(Connector[])
to remove a connector.setAttribute
(String name, Object attribute) Set an attributevoid
setConnectors
(Connector[] connectors) Set the connectors for this server.void
setDefaultHandler
(Handler defaultHandler) void
setDryRun
(boolean dryRun) void
setDumpAfterStart
(boolean dumpAfterStart) Set true ifContainerLifeCycle.dumpStdErr()
is called after starting.void
setDumpBeforeStop
(boolean dumpBeforeStop) Set true ifContainerLifeCycle.dumpStdErr()
is called before stopping.void
setErrorHandler
(Request.Handler errorHandler) void
setOpenEarly
(boolean openEarly) Allows to disable early opening of network sockets.void
setRequestLog
(RequestLog requestLog) void
setServerInfo
(String serverInfo) void
setStopAtShutdown
(boolean stop) Set stop server at shutdown behaviour.void
setStopTimeout
(long stopTimeout) void
setTempDirectory
(File temp) Set the temporary directory returned byContext.getTempDirectory()
for the rootContext
returnedgetContext()
.void
setTempDirectory
(String temp) Convenience method to callsetTempDirectory(File)
from a String representation of the temporary directory.protected void
Starts the given lifecycle.toString()
Methods inherited from class org.eclipse.jetty.server.Handler.Wrapper
getHandler, setHandler
Methods inherited from class org.eclipse.jetty.server.Handler.AbstractContainer
findContainerOf, getDescendant, getDescendants, isDynamic, setDynamic, setServer
Methods inherited from class org.eclipse.jetty.server.Handler.Abstract
destroy
Methods inherited from class org.eclipse.jetty.util.component.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, updateBeans
Methods inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle
getEventListeners, getState, getState, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, setEventListeners, start, stop
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.eclipse.jetty.util.Attributes
asAttributeMap, equals, hashCode
Methods inherited from interface org.eclipse.jetty.util.component.Container
getCachedBeans, getEventListeners
Methods inherited from interface org.eclipse.jetty.util.component.Destroyable
destroy
Methods inherited from interface org.eclipse.jetty.util.component.Dumpable.DumpableContainer
isDumpable
Methods inherited from interface org.eclipse.jetty.server.Handler.Container
getContainer, getDescendant, getDescendants, getDescendants
Methods inherited from interface org.eclipse.jetty.server.Handler.Singleton
getHandlers, getTail, insertHandler, setHandler
Methods inherited from interface org.eclipse.jetty.util.component.LifeCycle
addEventListener, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, removeEventListener, start, stop
-
Constructor Details
-
Server
public Server() -
Server
Convenience constructor Creates server and aServerConnector
at 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
ServerConnector
at 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.Handler
Invoked 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:
handle
in interfaceRequest.Handler
- Overrides:
handle
in 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 rootContext
returnedgetContext()
. 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 theContext
associated with allRequest
s prior to being handled by aContextHandler
. AServer
'sContext
:- has a
null
context path
- returns the
ClassLoader
that loaded theServer
fromContext.getClassLoader()
. - is an
Executor
that delegates to theServer ThreadPool
- is a
Decorator
using theDecoratedObjectFactory
found as abean
of theServer
- has the same
temporary director
of theserver
- has a
-
getMimeTypes
-
getInvocationType
- Specified by:
getInvocationType
in interfaceInvocable
- Specified by:
getInvocationType
in interfaceRequest.Handler
- Overrides:
getInvocationType
in 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
- IfopenEarly
istrue
(default), network sockets are opened before starting other components. IfopenEarly
isfalse
, 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.Listener
it 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
- Returns:
- A
HttpField
instance efficiently recording the current time to a second resolution, that cannot be cleared from aResponseHttpFields
instance. - See Also:
-
doStart
Description copied from class:ContainerLifeCycle
Starts the managed lifecycle beans in the order they were added.- Overrides:
doStart
in classHandler.Abstract
- Throws:
AbstractLifeCycle.StopException
- If thrown, the lifecycle will immediately be stopped.Exception
- If there was a problem starting. Will cause a transition to FAILED state
-
start
Description copied from class:ContainerLifeCycle
Starts the given lifecycle.- Overrides:
start
in classContainerLifeCycle
- Parameters:
l
- the lifecycle to start- Throws:
Exception
- if unable to start lifecycle
-
doStop
Description copied from class:ContainerLifeCycle
Stops the managed lifecycle beans in the reverse order they were added.- Overrides:
doStop
in 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:Attributes
Set an attribute- Specified by:
setAttribute
in 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:Attributes
Remove an attribute- Specified by:
removeAttribute
in interfaceAttributes
- Parameters:
name
- the attribute to remove- Returns:
- the value of the attribute if removed, else
null
-
getAttribute
Description copied from interface:Attributes
Get an attribute- Specified by:
getAttribute
in interfaceAttributes
- Parameters:
name
- the attribute to get- Returns:
- the value of the attribute, or
null
if no such attribute exists
-
getAttributeNameSet
Description copied from interface:Attributes
Get the immutable set of attribute names.- Specified by:
getAttributeNameSet
in interfaceAttributes
- Returns:
- Set of attribute names, or an empty set if there are no attributes.
-
clearAttributes
public void clearAttributes()Description copied from interface:Attributes
Clear all attribute names- Specified by:
clearAttributes
in interfaceAttributes
-
getURI
- Returns:
- The URI of the first
NetworkConnector
and firstContextHandler
, or null
-
getServer
- Specified by:
getServer
in interfaceHandler
- Overrides:
getServer
in classHandler.Abstract
- Returns:
- the
Server
associated with thisHandler
-
getDefaultStyleSheet
Get the Default CSS- Returns:
- the default CSS
-
getDefaultFavicon
Get the default Favicon- Returns:
- the default Favicon
-
toString
- Overrides:
toString
in classAbstractLifeCycle
-
dump
Description copied from interface:Dumpable
Dump 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:
dump
in interfaceDumpable
- Overrides:
dump
in 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
-