Femto Web Server V1.4-F

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 (ZoneTime) and the log output (Stdout).

Each main thread has its own built-in multithreaded HTTP request engine, its own properties and is able to serve concurrent requests at 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(85);
 httpdThread.setTimeZone("ECT");
 httpdThread.addFilter("dfischer.femtowebserver.filter.HttpSessionCookieHandler");

 // 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) at server side direct from static HTML pages.

As additional feature HTTP Request and Response Filter, HTTP Sessions and Connection Adapters are supported.

See Also:
HttpdWebletInterface, HttpdSssiInterface, HttpdFilter, HttpdSession, HttpdConnectionAdapterInterface

Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
Httpd()
          Creates a new instance of the Femto Web Server with its own properties and its own session hashtable.
 
Method Summary
 void addFilter(java.lang.String className)
          Adds a HttpdFilter to the current Femto Web Server instance with no filter arguments.
 void addFilter(java.lang.String className, java.lang.String[] filterArguments)
          Adds a HttpdFilter to the current Femto Web Server instance with filter arguments.
 void addMimeMapping(java.lang.String fileExtension, java.lang.String mimeType)
          Allows to add additional MIME types to the current MIME mapping.
 void addUrlFileMapping(java.lang.String requestFilePath, java.lang.String mappedPath)
          Maps the path of a requested file to an other (internal) path and continues further processing with the internal path.
 java.lang.String[] getFilterArguments(java.lang.String className)
          Returns the arguments of a HttpdFilter.
 java.lang.String[] getFilterClassNames()
          Returns a list of all HttpdFilters.
 void removeFilter(java.lang.String className)
          Removes a HttpdFilter from the current Femto Web Server instance.
 void removeUrlFileMapping(java.lang.String requestFilePath)
          Removes the mapping of a request file path.
 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 setConnectionAdapterClass(java.lang.String className)
          Allows to set an own connection adapter which wire the Femto Web Server to a specific network protocol.
 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 setProperty(java.lang.String key, java.lang.Object value)
          Allows to add any object to the HttpdProperties.
 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.
 void setSessionHashtable(HttpdSessionHashtable sessionHashtable)
          Allows to set an own session hashtable.
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 a Femto Web Server main thread.
 void updateMimeMapping(java.lang.String fileExtension, java.lang.String mimeType)
          Allows to modify the MIME type of an already existing file extension inside the current MIME mapping.
 
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.lang.Exception
Creates a new instance of the Femto Web Server with its own properties and its own session hashtable.
See Also:
HttpdProperties, HttpdSessionHashtable
Method Detail

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

setConnectionAdapterClass

public void setConnectionAdapterClass(java.lang.String className)
                               throws java.lang.ClassNotFoundException
Allows to set an own connection adapter which wire the Femto Web Server to a specific network protocol.

The HttpdSocketConnectionAdapter is used as default adapter if this method never was called.

Parameters:
className - the full name of the connection adapter class using '.' and not '/' as the separator and without a trailing ".class" suffix
See Also:
HttpdConnectionAdapterInterface, HttpdProperties.getConnectionAdapterClass()

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()

addUrlFileMapping

public void addUrlFileMapping(java.lang.String requestFilePath,
                              java.lang.String mappedPath)
Maps the path of a requested file to an other (internal) path and continues further processing with the internal path. Wildcards in form of a '*' char at the end of request file path are supported.
 Example: /dcs/abc/a* -> /package/doit
 
Parameters:
requestFilePath - the path of the request file which will be mapped to the new path
mappedPath - the new path
See Also:
removeUrlFileMapping(java.lang.String), HttpdProperties.getUrlFileMapping()

removeUrlFileMapping

public void removeUrlFileMapping(java.lang.String requestFilePath)
Removes the mapping of a request file path.
Parameters:
requestFilePath - the path of the request file which will be un-mapped.
See Also:
addUrlFileMapping(java.lang.String, java.lang.String), HttpdProperties.getUrlFileMapping()

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:
updateMimeMapping(java.lang.String, java.lang.String), setRestrictedMimeMapping(), setExtendedMimeMapping()

updateMimeMapping

public void updateMimeMapping(java.lang.String fileExtension,
                              java.lang.String mimeType)
Allows to modify the MIME type of an already existing file extension inside the current MIME mapping. If the file extension is not found the method addMimeMapping(java.lang.String, java.lang.String) will be called.
Parameters:
fileExtension - An already existing file extension without leading dot (Example: "html")
mimeType - The new MIME type of the file extension. Example: "text/html;charset=iso-8859-1" will replace the default "text/html".
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()

addFilter

public void addFilter(java.lang.String className)
Adds a HttpdFilter to the current Femto Web Server instance with no filter arguments. This method can called more than one time to add several chained filters. The filters are (later) executeted by the order how they are added.
Parameters:
className - full classname of the new filter. Will be loaded from the CLASSPATH of the Femto Web Server.
See Also:
addFilter(String, String[]), removeFilter(java.lang.String), getFilterClassNames(), getFilterArguments(java.lang.String), HttpdFilter

addFilter

public void addFilter(java.lang.String className,
                      java.lang.String[] filterArguments)
Adds a HttpdFilter to the current Femto Web Server instance with filter arguments. This method can called more than one time to add several chained filters. The filters are (later) executeted by the order how they are added.
Parameters:
className - full classname of the new filter. Will be loaded from the CLASSPATH of the Femto Web Server.
filterArguments - filter arguments, used to control the behaviour of the filter.
See Also:
addFilter(String), removeFilter(java.lang.String), getFilterClassNames(), getFilterArguments(java.lang.String), HttpdFilter, HttpdFilter.getFilterArguments()

removeFilter

public void removeFilter(java.lang.String className)
Removes a HttpdFilter from the current Femto Web Server instance.
Parameters:
className - full classname of the filter.
See Also:
addFilter(String), addFilter(String, String[]), getFilterClassNames(), getFilterArguments(java.lang.String), HttpdFilter

getFilterClassNames

public java.lang.String[] getFilterClassNames()
Returns a list of all HttpdFilters.
Returns:
string[] array of full classnames of all (active) filters. If no filter has been added, an empty string[] array will be returned.
See Also:
addFilter(String), addFilter(String, String[]), removeFilter(java.lang.String), getFilterArguments(java.lang.String), HttpdFilter

getFilterArguments

public java.lang.String[] getFilterArguments(java.lang.String className)
Returns the arguments of a HttpdFilter.
Parameters:
className - full classname of the filter.
Returns:
arguments of the HttpdFilter. If no arguments have been set, an empty string[] array will be returned.
See Also:
addFilter(String), addFilter(String, String[]), removeFilter(java.lang.String), getFilterClassNames(), HttpdFilter, HttpdFilter.getFilterArguments()

setSessionHashtable

public void setSessionHashtable(HttpdSessionHashtable sessionHashtable)
Allows to set an own session hashtable. The new session hashtable must extend the default hashtable HttpdSessionHashtable.
Parameters:
sessionHashtable - the own session hashtable.
See Also:
HttpdProperties.getSessionHashtable(), HttpdSession, HttpSessionCookieHandler

setProperty

public void setProperty(java.lang.String key,
                        java.lang.Object value)
Allows to add any object to the HttpdProperties.
Parameters:
key - Unique key of the object. If more than one object with the same key is added to the properties, the old object is overwritten.
value - reference to the object.
See Also:
HttpdProperties.getProperty(java.lang.String)

run

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

shutdown

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

Femto Web Server V1.4-F

Copyright 2002, 2003, 2006 by Ingenieurbüro David Fischer GmbH, Switzerland. All rights reserved.