|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--com.dalsemi.tininet.http.HTTPServer
This class implements an HTTP server. This class currently only supports the
GET method. The serviceRequests
method blocks on a ServerSocket
accept call. A new thread is spawned to service each new connection.
The method serviceRequest(Object lock)
can be used to
synchronize access, using the lock object, to a requested web page. This might be useful if the
server dynamically updates a page.
boolean webServerEnabled = true;
Object lock = new Object();
HTTPServer httpServer = new HTTPServer();
boolean loggingFailed = false;
//...
try
{
// create an instance of HTTPServer on port httpPort
httpServer = new HTTPServer(httpPort);
// override the default index page
httpServer.setIndexPage(webIndex);
// override the default HTTP root
httpServer.setHTTPRoot(webRoot);
// override the default log file name
httpServer.setLogFilename(webLog);
}
catch(HTTPServerException h)
{
System.out.println(h.toString());
}
try
{
// enable logging
// NOTE: the log file is always appended and
// will eventually consume all free memory
// if it is not managed.
httpServer.setLogging(true);
}
catch(HTTPServerException h)
{
// problem with log file
loggingFailed = true;
if(debugOn)
{
System.out.println(h.toString());
}
}
while(webServerEnabled)
{
httpServer.serviceRequests(lock);
//...
}
Field Summary | |
static int |
DEFAULT_HTTP_PORT
Default port number (port 80) |
static int |
DELETE
DELETE request |
static int |
GET
GET request |
static int |
HEAD
HEAD request |
static int |
HTTP_BAD_REQUEST
Standard HTTP_BAD_REQUEST response |
static int |
HTTP_CREATED
Standard HTTP_CREATED response |
static int |
HTTP_FORBIDDEN
Standard HTTP_FORBIDDEN response |
static int |
HTTP_INTERNAL_ERROR
Standard HTTP_INTERNAL_ERROR response |
static int |
HTTP_NOT_FOUND
Standard HTTP_NOT_FOUND response |
static int |
HTTP_OK
Standard HTTP_OK response |
static int |
HTTP_SERVER_ERROR
Standard HTTP_SERVER_ERROR response |
static int |
HTTP_UNAUTHORIZED
Standard HTTP_UNAUTHORIZED response |
static int |
HTTP_UNSUPPORTED_TYPE
Standard HTTP_UNSUPPORTED_TYPE response |
static int |
OPTIONS
OPTIONS request |
static int |
POST
POST request |
static int |
PUT
PUT request |
static int |
TRACE
TRACE request |
static int |
TYPE_FULL_REQUEST
Standard type TYPE_FULL_REQUEST |
static int |
TYPE_FULL_RESPONSE
Standard type TYPE_FULL_RESPONSE |
static int |
TYPE_SIMPLE_REQUEST
Standard type TYPE_SIMPLE_REQUEST |
static int |
UNSUPPORTED
Request unsupported |
Constructor Summary | |
HTTPServer()
Default constructor. |
|
HTTPServer(int httpPort)
Creates an HTTPServer using port httpPort . |
|
HTTPServer(int httpPort,
boolean logEnabled)
Creates an HTTPServer using port httpPort . |
Method Summary | |
String |
getHTTPRoot()
Returns the HTTP root of the server. |
String |
getIndexPage()
Returns the server's default index page. |
String |
getLogFilename()
Returns the name of the log file. |
boolean |
getLogging()
Returns the logging status. |
int |
getPortNumber()
Returns the server's current port number. |
int |
serviceRequests()
Checks for incoming client HTTP request and services supported requests that are detected. |
int |
serviceRequests(Object lock)
Checks for incoming client HTTP request and services supported requests that are detected. |
static void |
setBitmapMimeType(String newMimeType)
Allows user to change the mime type for bitmaps. |
void |
setHTTPRoot(String httpRoot)
Sets the http root. |
void |
setIndexPage(String indexPage)
Sets the server's index page. |
void |
setLogFilename(String logFileName)
Sets the log file name. |
void |
setLogging(boolean logEnabled)
Sets the logging status. |
void |
setPortNumber(int httpPort)
Sets the server's port number. |
Methods inherited from class java.lang.Object |
clone,
equals,
finalize,
getClass,
hashCode,
notify,
notifyAll,
toString,
wait,
wait,
wait |
Field Detail |
public static final int HTTP_OK
public static final int HTTP_CREATED
public static final int HTTP_BAD_REQUEST
public static final int HTTP_UNAUTHORIZED
public static final int HTTP_FORBIDDEN
public static final int HTTP_NOT_FOUND
public static final int HTTP_UNSUPPORTED_TYPE
public static final int HTTP_SERVER_ERROR
public static final int HTTP_INTERNAL_ERROR
public static final int TYPE_SIMPLE_REQUEST
public static final int TYPE_FULL_REQUEST
public static final int TYPE_FULL_RESPONSE
public static final int DEFAULT_HTTP_PORT
public static final int UNSUPPORTED
public static final int GET
public static final int POST
public static final int HEAD
public static final int OPTIONS
public static final int PUT
public static final int DELETE
public static final int TRACE
Constructor Detail |
public HTTPServer() throws HTTPServerException
public HTTPServer(int httpPort) throws HTTPServerException
HTTPServer
using port httpPort
. Logging is disabled.httpPort
- port number for serverpublic HTTPServer(int httpPort, boolean logEnabled) throws HTTPServerException
HTTPServer
using port httpPort
. Logging is enabled
if logEnabled
is true
.httpPort
- port number for serverlogEnabled
- set logging optionMethod Detail |
public static void setBitmapMimeType(String newMimeType)
public boolean getLogging()
true
if logging is enabledpublic void setLogging(boolean logEnabled) throws HTTPServerException
logEnabled
- true
if the server is to write to a default log filepublic String getLogFilename()
public void setLogFilename(String logFileName)
logFileName
- name of the log filepublic String getHTTPRoot()
public void setHTTPRoot(String httpRoot)
httpRoot
to the leaves of
the directory tree can be accessed by HTTP requests. Files between the
root of the file system and httpRoot
cannot be accessed with
HTTP requests.httpRoot
- path indicating the root of the serverpublic String getIndexPage()
public void setIndexPage(String indexPage)
indexPage
- page to be used by the server as the default index pagepublic int getPortNumber()
public void setPortNumber(int httpPort) throws HTTPServerException
httpPort
- number of the port to be openedpublic int serviceRequests() throws HTTPServerException
public int serviceRequests(Object lock) throws HTTPServerException
lock
- lock for exclusive access to web page
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |