dfischer.femtowebserver.httpd
Class Httpd

java.lang.Object
  |
  +--java.lang.Thread
        |
        +--dfischer.femtowebserver.httpd.Httpd
All Implemented Interfaces:
java.lang.Runnable

public class Httpd
extends java.lang.Thread

This is the Femto Web Server main class which is implemented as an independent thread.
You may start as much different threads as you want - each with a different configuration. The only thing that is common/shared between all instances is the time zone and the log output.

Each main thread has his built-in multithreaded HTTP request engine and is able to serve concurrent requests on the same time. To stop a main thread you should call the method shutdown() of the instance.

Programming Example:

 ...
 // create new instance of the femto web server
 Httpd httpdThread = new Httpd();

 // configure the femto web server
 String serverName = "127.0.0.1";
 try { serverName = InetAddress.getLocalHost().getHostName(); } catch (Exception ex) {}
 httpdThread.setServerName(serverName);
 httpdThread.setServerPort(83);
 httpdThread.setWelcomePage("/dfischer/femtowebserver/test/Welcome.html");
 httpdThread.setRestrictedMimeMapping();
 httpdThread.setTimeZone("ECT");
		
 // start the femto web server
 httpdThread.start();

 ...

 // and later if you want to stop the femto web server
 httpdThread.shutdown();
 httpdThread.join();
 ...
 
The Femto Web Server has no base directory for his served content - it searches all inside the java CLASSPATH. So a good approach could be, to pack all your content to a separate jar archive and add this archive to the CLASSPATH.

The Femto Web Server is able to execute Java classes direct from the web browser - called Weblets - similar to CGI scripts or Java Servlets. It is also able to execute "Simple Server Side Includes" (SSSI) from HTML pages.

See Also:
HttpdWebletInterface, HttpdSssiInterface

Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
Httpd()
           
 
Method Summary
 void addMimeMapping(java.lang.String fileExtension, java.lang.String mimeType)
          Allows to add additional MIME types to the current MIME mapping.
 void run()
          Starts the Femto Web Server instance (as thread).
 void setBindAddress(java.lang.String bindAddress)
          Binds the server socket to a specific TCP/IP address.
 void setDefaultContent(java.lang.String defaultContent)
          Sets the default request file, if the HTTP request points only to a directory path.
 void setDefaultMimeType(java.lang.String mimeType)
          Sets the default MIME type if the Femto Web Server is unable determine the MIME type from the HTTP request file.
 void setExtendedMimeMapping()
          Sets extended MIME mapping (less secure).
static void setLogStream(java.io.PrintStream logStream)
          Sets the log output for all instances of the Femto Web Server.
 void setMimeMapping(java.lang.String[][] mimeMappimg)
          Allows to set an own, specific MIME mapping.
 void setRequestTimeout(int timeoutSeconds)
          Sets the (processing) timeout of an URL request.
 void setRestrictedMimeMapping()
          Sets restricted MIME mapping (more secure).
 void setServerName(java.lang.String serverName)
          Sets the (TCP/IP) HTTP server name.
 void setServerPort(int serverPort)
          Sets the HTTP server port.
static void setTimeZone(java.lang.String timeZone)
          Sets the time zone for all instances of the Femto Web Server.
 void setWelcomePage(java.lang.String welcomePage)
          Loads for HTTP requests to the top level default URL "/" to a welcome page (or a welcome Weblet).
 void shutdown()
          Does a graceful shutdown of the main thread.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getContextClassLoader, getName, getPriority, getThreadGroup, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setName, setPriority, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Httpd

public Httpd()
      throws java.io.IOException
Method Detail

shutdown

public void shutdown()
Does a graceful shutdown of the main thread.
See Also:
run()

setTimeZone

public static void setTimeZone(java.lang.String timeZone)
Sets the time zone for all instances of the Femto Web Server. The default configuration is "ECT".
Parameters:
timeZone - 3-Letter time zone ID.
See Also:
ZoneTime.TIME_ZONE

setServerName

public void setServerName(java.lang.String serverName)
Sets the (TCP/IP) HTTP server name. You may use any name - or if you have no idea, don't configure it. The default configuration is "127.0.0.1". If you make HTTP redirections inside a Weblet, it's a good ide to set the real server name here.
Parameters:
serverName - HTTP server name. Used on log output and accessable also on weblets
See Also:
HttpdProperties.getServerName()

setServerPort

public void setServerPort(int serverPort)
Sets the HTTP server port. The default configuration is 80. If you have already an other HTTP server like Apache on port 80, you must choose an other server port. Consider that you must have root privileges on UNIX systems, if you use a port number less than 1000.
Parameters:
serverPort - HTTP server port number
See Also:
HttpdProperties.getServerPort()

setBindAddress

public void setBindAddress(java.lang.String bindAddress)
                    throws java.net.UnknownHostException
Binds the server socket to a specific TCP/IP address. This makes only sense, if your host has more than one TCP/IP address. The default configuration is that the Femto Web Server is listening to all TCP/IP addresses of the host.
Parameters:
bindAddress - specific TCP/IP address to listen.
See Also:
HttpdProperties.getBindAddress()

setLogStream

public static void setLogStream(java.io.PrintStream logStream)
Sets the log output for all instances of the Femto Web Server. The default configuration is System.out . You may pass null as argument to disable logging.
Parameters:
logStream - log output stream or null, to disable logging
See Also:
Stdout

setDefaultContent

public void setDefaultContent(java.lang.String defaultContent)
Sets the default request file, if the HTTP request points only to a directory path. In such a case the default request file will be appended to the directory path before the request will be processed. The default configuration is "index.html".
Parameters:
defaultContent - default request file, if the HTTP request points only to a directory path
See Also:
HttpdProperties.getDefaultContent()

setWelcomePage

public void setWelcomePage(java.lang.String welcomePage)
Loads for HTTP requests to the top level default URL "/" to a welcome page (or a welcome Weblet). You should use this method, if on the top of your CLASSPATH is no "index.html". Consider that this method does not make a redirection - it loads only the content of the welcome page.
Parameters:
welcomePage - path to welcome page. Example: mypackage/htdocspackage/welcome.class
See Also:
HttpdProperties.getWelcomePage()

setRestrictedMimeMapping

public void setRestrictedMimeMapping()
Sets restricted MIME mapping (more secure). Any file extensions which don't fit to this MIME mapping will give as result a "400 Bad Request" HTTP response, if no default MIME type has been set. This is also the default configuration.
See Also:
HttpdProperties.RESTRICTED_MIME_MAPPING, setExtendedMimeMapping(), setMimeMapping(java.lang.String[][]), setDefaultMimeType(java.lang.String)

setExtendedMimeMapping

public void setExtendedMimeMapping()
Sets extended MIME mapping (less secure). Any file extensions which don't fit to this MIME mapping will give as result a "400 Bad Request" HTTP response, if no default MIME type has been set. The default configuration is restricted MIME mapping.
See Also:
HttpdProperties.EXTENDED_MIME_MAPPING, setRestrictedMimeMapping(), setMimeMapping(java.lang.String[][]), setDefaultMimeType(java.lang.String)

addMimeMapping

public void addMimeMapping(java.lang.String fileExtension,
                           java.lang.String mimeType)
Allows to add additional MIME types to the current MIME mapping.
Parameters:
fileExtension - File extension without leading dot (Example: "zip")
mimeType - MIME type of file extension (Example: "application/zip")
See Also:
setRestrictedMimeMapping(), setExtendedMimeMapping()

setMimeMapping

public void setMimeMapping(java.lang.String[][] mimeMappimg)
Allows to set an own, specific MIME mapping. Any file extensions which don't fit to this MIME mapping will give as result a "400 Bad Request" HTTP response, if no default MIME type has been set. The default configuration is restricted MIME mapping.
Parameters:
mimeMappimg - MIME mapping array to set.
Example: { {"html", "text/html"}, {"gif", "image/gif"} }
Hint: if {"class", "text/html"} is not part of the MIME mapping, the Femto Web Server wil not serve Weblets.
See Also:
setRestrictedMimeMapping(), setExtendedMimeMapping(), setDefaultMimeType(java.lang.String)

setDefaultMimeType

public void setDefaultMimeType(java.lang.String mimeType)
Sets the default MIME type if the Femto Web Server is unable determine the MIME type from the HTTP request file. The default configuration is null - this means there is no default MIME type and a "400 Bad Request" HTTP response will be the result in such a case.

SECURITY WARNING: YOU SHOULD NEVER SET A DEFAULT MIME TYPE BECAUSE THIS WILL OPEN A BIG SECURITY HOLE. THE FEMTO WEB SERVER WILL AFTERWARDS SERVE ALL INSIDE THE CLASSPATH EXCEPT NON WEBLET CLASSES !!!

Parameters:
mimeType - the default mime type. Example: "TEXT/HTML"
See Also:
HttpdProperties.getDefaultMimeType()

setRequestTimeout

public void setRequestTimeout(int timeoutSeconds)
Sets the (processing) timeout of an URL request. If the timeout expires, the request will be aborted and the network connection to the browser will be closed. The default value is 30 seconds.
Parameters:
timeoutSeconds - request timeout in seconds
See Also:
HttpdProperties.getRequestTimeout()

run

public void run()
Starts the Femto Web Server instance (as thread).
Overrides:
run in class java.lang.Thread
See Also:
shutdown()