public class Server extends Logging.Loggable implements java.lang.Runnable
User
s and their Session
s.ServerContext
s and their
corresponding ServerContext.ServerContextHandler
s such as the
SampleRequestHandler
.
Authentication:
/auth?user=<USERNAME>&pass=<PASSWORD_AS_MD5>
Returns: The session token or "ERROR: INCORRECT AUTHENTICATION
DATA" (401), "ERROR: INCOMPLETE AUTHENTICATION DATA" (400)
Logout: /deauth?key=<SESSION_TOKEN>
Returns: true (if successful) or "ERROR: INTERNAL SERVER ERROR"
(500)
ServerContext
s added through registerContext(ServerContext)
:
/<CONTEXT_PATH>?key=<SESSION_TOKEN> + PARAMS (key only if context
requires authentication)
Returns standardized: "ERROR: AUTHENTICATION REQUIRED" (401),
"ERROR: SESSION NOT FOUND OR EXPIRED" (401), "ERROR: USER NOT
ALLOWED TO ACCESS THIS PATH" (403), "ERROR: INTERNAL SERVER
ERROR" (500)
Modifier and Type | Class and Description |
---|---|
static class |
Server.HttpStatusCodeRepresentation
Contains common HTTP status codes used by the system.
|
Constructor and Description |
---|
Server(int port)
Constructor for the server.
|
Modifier and Type | Method and Description |
---|---|
void |
addUser(User user)
Adds a
User to the system. |
java.lang.String |
generateSessionId(int len)
Generates a unique session ID String.
|
long |
getTokenExpirationTime()
Returns the expiration time of a token.
|
int |
getTokenLength()
Returns the token length.
|
User |
getUser(java.lang.String username)
Returns a
User object. |
void |
registerContext(ServerContext context)
Registers a new
ServerContext in the server. |
void |
removeUser(java.lang.String username)
Removes a certain
User from the system. |
void |
run() |
void |
setTokenExpirationTime(long tokenExpirationTime)
Sets the expiration time of a token.
|
void |
setTokenLength(int tokenLength)
Sets the token length.
|
void |
start()
Starts the web server listening on the specified port.
|
void |
stop()
Stops the web server.
|
getLogger, setLogger
public Server(int port)
port
- The port the server will run at.public long getTokenExpirationTime()
public void setTokenExpirationTime(long tokenExpirationTime)
tokenExpirationTime
- The token expiration time to set.public int getTokenLength()
public void setTokenLength(int tokenLength)
tokenLength
- The token length to set.public void registerContext(ServerContext context) throws java.lang.IllegalArgumentException
ServerContext
in the server.context
- The ServerContext
to register.java.lang.IllegalArgumentException
- When there is already a registered ServerContext
with
the same route as context
.public void start() throws java.lang.Exception
java.lang.Exception
- When the specified port is already in use.public void stop()
public void run()
run
in interface java.lang.Runnable
public void addUser(User user)
User
to the system.user
- The User
to add.public void removeUser(java.lang.String username)
User
from the system.username
- The User
's name to remove.public java.lang.String generateSessionId(int len)
len
- The length of the session ID.