- All Implemented Interfaces:
HttpHandler
The method named
handle(com.sun.net.httpserver.HttpExchange)
(defined by HttpHandler
) handles
HTTP requests. If an error occurs, and error page is obtained from
the FileHandler's WebMap. If the requested URL's path matches that
of the prefix for this FileHandler, a welcome page is requested
from this FileHandler's WebMap On success, the resource provided in
the HTML response is obtained from the WebMap.
The status codes that may be returned in an HTTP response are:
- 100—Continue.
- 200—OK.
- 302—Found.
- 404—Not Found
- 405—Method Not Allowed
- 406—Not Accepted
- 417—Expectation Failed
- 500—Internal Server Error
-
Constructor Summary
ConstructorsConstructorDescriptionFileHandler
(String protocol, Object root, Class<? extends WebMap> clasz, boolean nowebxml) Constructor specifying a WebMap's class and using defaults for flags.FileHandler
(String protocol, Object root, Class<? extends WebMap> clasz, boolean nowebxml, boolean displayDir, boolean hideWebInf) Constructor specifying a WebMap's class.FileHandler
(String protocol, Object root, String className, boolean nowebxml) Constructor specifying a WebMap's class name and using defaults for flags.FileHandler
(String protocol, Object root, String className, boolean nowebxml, boolean displayDir, boolean hideWebInf) Constructor specifying a WebMap's class name.FileHandler
(String protocol, WebMap webmap, boolean nowebxml, boolean displayDir, boolean hideWebInf) Constructor using a previously created WebMap. -
Method Summary
Modifier and TypeMethodDescriptionGet the login-alias string.Get the logout-alias string.Get the logout URI.int
Get the number of outstanding requests.void
boolean
Determine if a login is required.static void
sendResponseHeaders
(HttpExchange exchange, int code, long length) Send the response headers, adding a session cookie if sessions are enabled.protected void
setEWS
(EmbeddedWebServer ews) Set the embedded web server that uses this file handler.void
setLoginAlias
(String alias) Set the login-alias string.void
setLoginAlias
(String alias, String target) Set the login-alias string with a location to visit when the login is successful, with the location represented as a relative path.void
setLoginAlias
(String alias, String target, boolean required) Set the login-alias string with a location to visit when the login is successful, with the location represented as a relative path, and with a flag indicating if a login is required.void
setLoginAlias
(String alias, URI target) Set the login-alias string with a location to visit when the login is successful, with the location represented as a URL.void
setLoginAlias
(String alias, URI target, boolean required) Set the login-alias string with a location to visit when the login is successful, with the location represented as a URL, and with a flag indicating if a login is required.void
setLogoutAlias
(String alias, URI target) Set the logout-alias string with a location to visit when the logout is successful, with the location represented as a URL.void
setTracer
(Appendable tracer) Set an Appendable for tracing.void
setTracer
(Appendable tracer, boolean stacktrace) Set an Appendable for tracing with a stack trace.
-
Constructor Details
-
FileHandler
public FileHandler(String protocol, Object root, Class<? extends WebMap> clasz, boolean nowebxml) throws Exception Constructor specifying a WebMap's class and using defaults for flags.- Parameters:
protocol
- the protocol used by the HTTP server - HTTP or HTTPSroot
- argument used to initialize an instance ofclasz
.clasz
- the subclass of WebMap to use for locating resourcesnowebxml
- true if a web.xml file should be ignored; false otherwise- Throws:
Exception
- an error occurred
-
FileHandler
public FileHandler(String protocol, Object root, Class<? extends WebMap> clasz, boolean nowebxml, boolean displayDir, boolean hideWebInf) throws IOException, SAXException Constructor specifying a WebMap's class.- Parameters:
protocol
- the protocol used by the HTTP server - HTTP or HTTPSroot
- argument used to initialize an instance ofclasz
.clasz
- the subclass of WebMap to use for locating resourcesnowebxml
- true if a web.xml file should be ignored; false otherwisedisplayDir
- true to request that directory resource be displayed; false if a directory resource should not be displayedhideWebInf
- true if the WEB-INF directory should be hidden; false if it should be visible- Throws:
IOException
- an IO error occurredSAXException
- a SAX exception occurred
-
FileHandler
public FileHandler(String protocol, Object root, String className, boolean nowebxml) throws IOException, SAXException Constructor specifying a WebMap's class name and using defaults for flags.- Parameters:
protocol
- the protocol used by the HTTP server - HTTP or HTTPSroot
- argument used to initialize an instance ofclasz
.className
- the name of a subclass of WebMap, an instance of which will be used to map paths to resources.nowebxml
- true if a web.xml file should be ignored; false otherwise- Throws:
IOException
- an IO error occurredSAXException
- a SAX exception occurred
-
FileHandler
public FileHandler(String protocol, Object root, String className, boolean nowebxml, boolean displayDir, boolean hideWebInf) throws IOException, SAXException Constructor specifying a WebMap's class name.- Parameters:
protocol
- the protocol used by the HTTP server - HTTP or HTTPSroot
- argument used to initialize an instance ofclasz
.className
- the name of a subclass of WebMap, an instance of which will be used to map paths to resources.nowebxml
- true if a web.xml file should be ignored; false otherwisedisplayDir
- true to request that directory resource be displayed; false if a directory resource should not be displayedhideWebInf
- true if the WEB-INF directory should be hidden; false if it should be visible- Throws:
IOException
- an IO error occurredSAXException
- a SAX exception occurred
-
FileHandler
public FileHandler(String protocol, WebMap webmap, boolean nowebxml, boolean displayDir, boolean hideWebInf) throws Exception Constructor using a previously created WebMap. The WebMap provided will not be cloned, and may be modified, so it should not be used with another FileHandler constructor.- Parameters:
protocol
- the protocol used by the HTTP server - HTTP or HTTPSwebmap
- a subclass of WebMap, which will be used to map paths to resources.nowebxml
- true if a web.xml file should be ignored; false otherwisedisplayDir
- true to request that directory resource be displayed; false if a directory resource should not be displayedhideWebInf
- true if the WEB-INF directory should be hidden; false if it should be visible- Throws:
Exception
- an error occurred
-
-
Method Details
-
isLoginRequired
public boolean isLoginRequired()Determine if a login is required. When a login is required,EjwsSecureBasicAuth
will reject an authorization request unless the user has first been authorized when visiting the page denoted by the login alias.- Returns:
- true if a login is required; false otherwise
-
setLoginAlias
Set the login-alias string. This string is the path component of a URL and may not contain a "/". An HTTP request whose path is the context path, followed by a "/" if the context path does not end in "/", followed by the login-alias, that URL will be redirected to a location specified by the context path. In addition, that URL may be treated specially by authenticators.- Parameters:
alias
- the alias string; null to remove it- Throws:
IllegalArgumentException
- if the first argument contains a "/"- See Also:
-
EjwsSecureBasicAuth.setLoginFunction(java.util.function.BiConsumer<org.bzdev.ejws.EjwsPrincipal, com.sun.net.httpserver.HttpExchange>)
EjwsSecureBasicAuth.setLogoutFunction(java.util.function.BiConsumer<org.bzdev.ejws.EjwsPrincipal, com.sun.net.httpserver.HttpExchange>)
EjwsBasicAuthenticator.setLoginFunction(java.util.function.BiConsumer<org.bzdev.ejws.EjwsPrincipal, com.sun.net.httpserver.HttpExchange>)
EjwsBasicAuthenticator.setLogoutFunction(java.util.function.BiConsumer<org.bzdev.ejws.EjwsPrincipal, com.sun.net.httpserver.HttpExchange>)
-
setLoginAlias
Set the login-alias string with a location to visit when the login is successful, with the location represented as a relative path. The login alias is the path component of a URL and may not contain a "/". An HTTP request whose path is the context path, followed by a "/" if the context path does not end in "/", followed by the login-alias, that URL will be redirected to a location specified by the context path. In addition, the login URL may be treated specially by authenticators.- Parameters:
alias
- the alias string; null to remove ittarget
- the relative path from this handler's context to the page that should be visited after a successful login.- Throws:
IllegalArgumentException
- if the first argument contains a "/" or the second argument starts with a "/"- See Also:
-
EjwsSecureBasicAuth.setLoginFunction(java.util.function.BiConsumer<org.bzdev.ejws.EjwsPrincipal, com.sun.net.httpserver.HttpExchange>)
EjwsSecureBasicAuth.setLogoutFunction(java.util.function.BiConsumer<org.bzdev.ejws.EjwsPrincipal, com.sun.net.httpserver.HttpExchange>)
EjwsBasicAuthenticator.setLoginFunction(java.util.function.BiConsumer<org.bzdev.ejws.EjwsPrincipal, com.sun.net.httpserver.HttpExchange>)
EjwsBasicAuthenticator.setLogoutFunction(java.util.function.BiConsumer<org.bzdev.ejws.EjwsPrincipal, com.sun.net.httpserver.HttpExchange>)
-
setLoginAlias
public void setLoginAlias(String alias, String target, boolean required) throws IllegalArgumentException Set the login-alias string with a location to visit when the login is successful, with the location represented as a relative path, and with a flag indicating if a login is required. The login alias is the path component of a URL and may not contain a "/". An HTTP request whose path is the context path, followed by a "/" if the context path does not end in "/", followed by the login-alias, that URL will be redirected to a location specified by the context path. In addition, the login URL may be treated specially by authenticators.When a login is required, an authenticaor that supports this behavior will reject all authorization requests from a user until the user is authorized by visiting the login-alias page. A logout will restore this behavior.
- Parameters:
alias
- the alias string; null to remove ittarget
- the relative path from this handler's context to the page that should be visited after a successful login.required
- true if a login is required; false (the default) otherwise- Throws:
IllegalArgumentException
- if the first argument contains a "/" or the second argument starts with a "/"- See Also:
-
EjwsSecureBasicAuth.setLoginFunction(java.util.function.BiConsumer<org.bzdev.ejws.EjwsPrincipal, com.sun.net.httpserver.HttpExchange>)
EjwsSecureBasicAuth.setLogoutFunction(java.util.function.BiConsumer<org.bzdev.ejws.EjwsPrincipal, com.sun.net.httpserver.HttpExchange>)
EjwsBasicAuthenticator.setLoginFunction(java.util.function.BiConsumer<org.bzdev.ejws.EjwsPrincipal, com.sun.net.httpserver.HttpExchange>)
EjwsBasicAuthenticator.setLogoutFunction(java.util.function.BiConsumer<org.bzdev.ejws.EjwsPrincipal, com.sun.net.httpserver.HttpExchange>)
-
setLoginAlias
public void setLoginAlias(String alias, URI target) throws IllegalArgumentException, MalformedURLException Set the login-alias string with a location to visit when the login is successful, with the location represented as a URL. The login alias is the path component of a URL and may not contain a "/". An HTTP request whose path is the context path, followed by a "/" if the context path does not end in "/", followed by the login-alias, that URL will be redirected to a location specified by the target. In addition, the login URL may be treated specially by authenticators.- Parameters:
alias
- the alias string; null to remove ittarget
- the relative path from this handler's context to the page that should be visited after a successful login.- Throws:
IllegalArgumentException
- if the first argument contains a "/" or the URI provided by the second argument is not absoluteMalformedURLException
- if the second argument could not be converted to a URL- See Also:
-
EjwsSecureBasicAuth.setLoginFunction(java.util.function.BiConsumer<org.bzdev.ejws.EjwsPrincipal, com.sun.net.httpserver.HttpExchange>)
EjwsSecureBasicAuth.setLogoutFunction(java.util.function.BiConsumer<org.bzdev.ejws.EjwsPrincipal, com.sun.net.httpserver.HttpExchange>)
EjwsBasicAuthenticator.setLoginFunction(java.util.function.BiConsumer<org.bzdev.ejws.EjwsPrincipal, com.sun.net.httpserver.HttpExchange>)
EjwsBasicAuthenticator.setLogoutFunction(java.util.function.BiConsumer<org.bzdev.ejws.EjwsPrincipal, com.sun.net.httpserver.HttpExchange>)
-
setLoginAlias
public void setLoginAlias(String alias, URI target, boolean required) throws IllegalArgumentException, MalformedURLException Set the login-alias string with a location to visit when the login is successful, with the location represented as a URL, and with a flag indicating if a login is required. The login alias is the path component of a URL and may not contain a "/". An HTTP request whose path is the context path, followed by a "/" if the context path does not end in "/", followed by the login-alias, that URL will be redirected to a location specified by the target. In addition, the login URL may be treated specially by authenticators.When a login is required, an authenticaor that supports this behavior will reject all authorization requests from a user until the user is authorized by visiting the login-alias page. A logout will restore this behavior.
- Parameters:
alias
- the alias string; null to remove ittarget
- the relative path from this handler's context to the page that should be visited after a successful login.required
- true if a login is required; false (the default) otherwise- Throws:
IllegalArgumentException
- if the first argument contains a "/" or the URI provided by the second argument is not absoluteMalformedURLException
- if the second argument could not be converted to a URL- See Also:
-
EjwsSecureBasicAuth.setLoginFunction(java.util.function.BiConsumer<org.bzdev.ejws.EjwsPrincipal, com.sun.net.httpserver.HttpExchange>)
EjwsSecureBasicAuth.setLogoutFunction(java.util.function.BiConsumer<org.bzdev.ejws.EjwsPrincipal, com.sun.net.httpserver.HttpExchange>)
EjwsBasicAuthenticator.setLoginFunction(java.util.function.BiConsumer<org.bzdev.ejws.EjwsPrincipal, com.sun.net.httpserver.HttpExchange>)
EjwsBasicAuthenticator.setLogoutFunction(java.util.function.BiConsumer<org.bzdev.ejws.EjwsPrincipal, com.sun.net.httpserver.HttpExchange>)
-
getLoginAlias
Get the login-alias string.- Returns:
- the string; null if none has been set
-
setEWS
Set the embedded web server that uses this file handler.- Parameters:
ews
- the embedded web server for this file handler; null if there is none
-
setLogoutAlias
public void setLogoutAlias(String alias, URI target) throws IllegalArgumentException, UnknownHostException Set the logout-alias string with a location to visit when the logout is successful, with the location represented as a URL. The logout alias is the path component of a URL and may not contain a "/". An HTTP request whose path is the context path, followed by a "/" if the context path does not end in "/", followed by the logout-alias, that URL will be redirected to a location specified by the target. In addition, the logout URL may be treated specially by authenticators.The URI provided by the second argument must not be one that will be authenticated by this handler's context's authenticator. The test for this is more precise when the web server is created by an instance of
EmbeddedWebServer
becauseEmbeddedWebServer
allows one to find all the context paths. Otherwise a simple test using this handler's context path is used: For the current server, if the URI's path starts with the context path, the logout URI is rejected.- Parameters:
alias
- the alias string; null to remove ittarget
- the URI for the page that should be visited after a successful logout.- Throws:
IllegalArgumentException
- if the first argument contains a "/" or the URI provided by the second argument is not absoluteUnknownHostException
- if the host component of a URI passed as this method's second argument is not recognized by a name server- See Also:
-
getLogoutAlias
Get the logout-alias string.- Returns:
- the string; null if none has been set
-
getLogoutURI
Get the logout URI. The URI is the one to which a user will be redirected.- Returns:
- the logout URI
-
getPendingCount
public int getPendingCount()Get the number of outstanding requests. Provided primarily for debugging or monitoring.- Returns:
- the number of outstanding requests
-
setTracer
Set an Appendable for tracing. This method should be used only for debugging. IfsetTracer(Appendable,boolean)
was previously called, the handling of stack traces will not be change.- Parameters:
tracer
- the Appendable for tracing requests and responses; null to disable tracing
-
setTracer
Set an Appendable for tracing with a stack trace. This method should be used only for debugging. The stack trace will be printed ifhandle(com.sun.net.httpserver.HttpExchange)
throws an exception and the first argument is not null.- Parameters:
tracer
- the Appendable for tracing requests and responses; null to disable tracingstacktrace
- true for a stack trace; false otherwise
-
sendResponseHeaders
public static void sendResponseHeaders(HttpExchange exchange, int code, long length) throws IOException Send the response headers, adding a session cookie if sessions are enabled. The third argument uses the same convention as that used inHttpExchange.sendResponseHeaders(int,long)
.- Parameters:
exchange
- the Http exchangecode
- the status codelength
- the length of the response; 0 if the length is not known and -1 if there is no response- Throws:
IOException
- an IO error occurred
-
handle
- Specified by:
handle
in interfaceHttpHandler
- Throws:
IOException
-