public class HttpServer
extends java.lang.Object
start()
.Modifier and Type | Class and Description |
---|---|
(package private) class |
HttpServer.RecordingHandler |
(package private) class |
HttpServer.ResHandler |
Modifier and Type | Field and Description |
---|---|
private java.util.Map<java.lang.String,java.lang.String> |
filterTokens |
private int |
httpPort |
private int |
httpsPort |
private java.lang.String |
keyStoreLocation |
private java.lang.String |
keyStorePassword |
private long |
latency |
private boolean |
needClientAuth |
private java.lang.String |
proxyPassword |
private java.lang.String |
proxyUsername |
private java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.String>> |
recordedHeaders |
private java.util.Collection<java.lang.String> |
recordedPatterns |
private java.util.List<java.lang.String> |
recordedRequests |
private boolean |
redirectToHttps |
private java.util.Map<java.lang.String,java.io.File> |
resourceDirs |
private java.util.Map<java.lang.String,java.lang.String[]> |
resourceFilters |
private java.util.Map<java.lang.String,java.lang.String[]> |
securedRealms |
private org.eclipse.jetty.server.Server |
server |
private java.lang.String |
storePassword |
private java.lang.String |
trustStoreLocation |
private java.lang.String |
trustStorePassword |
private java.util.Map<java.lang.String,java.lang.String> |
userPasswords |
private java.util.Map<java.lang.String,java.lang.String[]> |
userRoles |
Constructor and Description |
---|
HttpServer() |
Modifier and Type | Method and Description |
---|---|
protected void |
addDefaultFilterTokens() |
HttpServer |
addResources(java.lang.String contextRoot,
java.lang.String baseDirectory,
java.lang.String... filteredExtensions)
Adds resources to the server.
|
HttpServer |
addSecuredRealm(java.lang.String pathSpec,
java.lang.String... roles)
Sets up a security realm.
|
HttpServer |
addUser(java.lang.String username,
java.lang.String password,
java.lang.String... roles)
Registers a user.
|
HttpServer |
enableRecording(java.lang.String... patterns)
Enables request recording for the specified URI patterns.
|
int |
getHttpPort()
Gets the port number of the server's HTTP connector.
|
int |
getHttpsPort()
Gets the port number of the server's HTTPS connector.
|
java.lang.String |
getHttpsUrl()
Gets the base URL to the server's HTTPS connector, e.g.
|
java.lang.String |
getHttpUrl()
Gets the base URL to the server's HTTP connector, e.g.
|
java.util.Map<java.lang.String,java.lang.String> |
getRecordedHeaders(java.lang.String uri)
Gets the headers sent in the most recent request to the specified path.
|
java.util.List<java.lang.String> |
getRecordedRequests()
Gets the sequence of requests that have been issued against context roots for which
enableRecording(String...) was called. |
protected org.eclipse.jetty.server.Connector |
newHttpConnector() |
protected org.eclipse.jetty.server.Connector |
newHttpsConnector() |
protected org.eclipse.jetty.server.Handler |
newProxyHandler() |
protected org.eclipse.jetty.server.Handler |
newResourceHandler() |
protected org.eclipse.jetty.security.SecurityHandler |
newSecurityHandler() |
protected org.eclipse.jetty.server.Handler |
newSleepHandler(long millis) |
protected org.eclipse.jetty.server.Handler |
newSslRedirectHandler() |
private static java.lang.String |
normalizeContextRoot(java.lang.String contextRoot) |
void |
resetRecording() |
HttpServer |
setFilterToken(java.lang.String token,
java.lang.String value)
Sets a token to replace during resource filtering.
|
HttpServer |
setHttpPort(int httpPort)
Sets the port to use for HTTP connections.
|
HttpServer |
setHttpsPort(int httpsPort)
Sets the port to use for HTTPS connections.
|
HttpServer |
setKeyStore(java.lang.String path,
java.lang.String password)
Sets the keystore to use for the server certificate on the SSL connector.
|
HttpServer |
setLatency(long millis)
Sets the latency of the server.
|
HttpServer |
setNeedClientAuth(boolean needClientAuth)
Enables/disables client-side certificate authentication.
|
HttpServer |
setProxyAuth(java.lang.String username,
java.lang.String password)
Sets the credentials to use for proxy authentication.
|
HttpServer |
setRedirectToHttps(boolean redirectToHttps)
Enforces redirection from HTTP to HTTPS.
|
HttpServer |
setStorePassword(java.lang.String password)
Sets the password to use for the SSL connector store.
|
HttpServer |
setTrustStore(java.lang.String path,
java.lang.String password)
Sets the truststore to use for validating client credentials via the SSL connector.
|
HttpServer |
start()
Starts the server.
|
void |
stop()
Stops the server.
|
private static java.lang.String |
trimContextRoot(java.lang.String uri,
java.lang.String contextRoot) |
protected void |
waitForConnectors() |
private org.eclipse.jetty.server.Server server
private int httpPort
private int httpsPort
private java.lang.String keyStoreLocation
private java.lang.String keyStorePassword
private java.lang.String trustStoreLocation
private java.lang.String trustStorePassword
private boolean needClientAuth
private java.lang.String proxyUsername
private java.lang.String proxyPassword
private boolean redirectToHttps
private long latency
private java.util.Map<java.lang.String,java.lang.String> userPasswords
private java.util.Map<java.lang.String,java.lang.String[]> userRoles
private java.util.Map<java.lang.String,java.lang.String[]> securedRealms
private java.util.Map<java.lang.String,java.io.File> resourceDirs
private java.util.Map<java.lang.String,java.lang.String[]> resourceFilters
private java.util.Map<java.lang.String,java.lang.String> filterTokens
private java.util.Collection<java.lang.String> recordedPatterns
private java.util.List<java.lang.String> recordedRequests
private java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.String>> recordedHeaders
private java.lang.String storePassword
protected org.eclipse.jetty.server.Connector newHttpConnector()
protected org.eclipse.jetty.server.Connector newHttpsConnector()
public HttpServer setHttpPort(int httpPort)
httpPort
- The port to use, may be 0
to pick a random port (default), if negative the HTTP connector
will be disabled.null
.public int getHttpPort()
public java.lang.String getHttpUrl()
"http://localhost:8080"
.null
.public HttpServer setHttpsPort(int httpsPort)
httpPort
- The port to use, may be 0
to pick a random port, if negative the HTTPS connector will be
disabled (default).null
.public int getHttpsPort()
public java.lang.String getHttpsUrl()
"https://localhost:8080"
.null
.public HttpServer setKeyStore(java.lang.String path, java.lang.String password)
path
- The path to the keystore to use for the server certificate, may be null
.password
- The password for the keystore, may be null
.null
.public HttpServer setTrustStore(java.lang.String path, java.lang.String password)
path
- The path to the truststore to use for the trusted client certificates, may be null
.password
- The password for the truststore, may be null
.null
.public HttpServer setStorePassword(java.lang.String password)
password
- The password for the store, may be null
.null
.public HttpServer setNeedClientAuth(boolean needClientAuth)
needClientAuth
- Whether the server should reject clients whose certificate can't be verified via the
truststore.null
.public HttpServer setProxyAuth(java.lang.String username, java.lang.String password)
null
, no proxy
authentication is required.username
- The username, may be null
.password
- The password, may be null
.null
.protected org.eclipse.jetty.server.Handler newProxyHandler()
public HttpServer setRedirectToHttps(boolean redirectToHttps)
redirectToHttps
- true
to redirect any HTTP requests to HTTPS, false
to handle HTTP normally.null
.protected org.eclipse.jetty.server.Handler newSslRedirectHandler()
public HttpServer addUser(java.lang.String username, java.lang.String password, java.lang.String... roles)
username
- The username, must not be null
.password
- The password, must not be null
.roles
- The roles of the user, may be empty or null
.null
.public HttpServer addSecuredRealm(java.lang.String pathSpec, java.lang.String... roles)
pathSpec
- The path to secure, e.g. "/files/*"
, must not be null
.roles
- The roles that have access to the realm, may be empty or null
.null
.protected org.eclipse.jetty.security.SecurityHandler newSecurityHandler()
public HttpServer addResources(java.lang.String contextRoot, java.lang.String baseDirectory, java.lang.String... filteredExtensions)
setFilterToken(String, String)
. The directory mounted into the server via this method will also be used to
store files sent via PUT. Upon requests, the server will try to match the context roots in reverse alphabetical
order, thereby giving longer path prefix matches precedence.contextRoot
- The context root to make the resources accessible at, must not be null
.baseDirectory
- The local base directory whose files should be served, must not be null
.filteredExtensions
- A list of extensions for files to filter, e.g. "xml, "properties"
, may be
null
.null
.public HttpServer enableRecording(java.lang.String... patterns)
getRecordedRequests()
.patterns
- The regular expressions denoting URIs to monitor, e.g. "/context/.*"
, must not be
null
.null
.public java.util.List<java.lang.String> getRecordedRequests()
enableRecording(String...)
was called. A request is encoded in the form <METHOD> <URI>
, e.g.
GET /context/some.jar
.null
.public java.util.Map<java.lang.String,java.lang.String> getRecordedHeaders(java.lang.String uri)
uri
- the pathpublic HttpServer setFilterToken(java.lang.String token, java.lang.String value)
@basedir@
, @baseurl@
, @baseuri@
, @port.http@
and
@port.https@
.token
- The token to replace, e.g. @basedir@
, must not be null
.value
- The replacement text of the token, may be null
.null
.protected org.eclipse.jetty.server.Handler newResourceHandler()
public HttpServer setLatency(long millis)
millis
- The latency in milliseconds, maybe negative for infinite delay.null
.protected org.eclipse.jetty.server.Handler newSleepHandler(long millis)
public HttpServer start() throws java.lang.Exception
null
.java.lang.Exception
- If the server could not be started.protected void waitForConnectors() throws java.lang.Exception
java.lang.Exception
protected void addDefaultFilterTokens()
public void stop()
private static java.lang.String normalizeContextRoot(java.lang.String contextRoot)
private static java.lang.String trimContextRoot(java.lang.String uri, java.lang.String contextRoot)
public void resetRecording()