APIdia Launch Partner
Top Description Fields Constructors Methods
sun.net.httpserver.simpleserver

public final Class FileServerHandler

extends Object
implements HttpHandler
Class Inheritance
All Implemented Interfaces
com.sun.net.httpserver.HttpHandler
Imports
java.io.IOException, .InputStream, .OutputStream, .UncheckedIOException, java.lang.System.Logger, java.net.URI, java.nio.file.Files, .Path, java.time.ZoneId, java.time.format.DateTimeFormatter, java.util.List, .Map, java.util.function.UnaryOperator, com.sun.net.httpserver.Headers, .HttpExchange, .HttpHandler, .HttpHandlers

A basic HTTP file server handler for static content.

Must be given an absolute pathname to the directory to be served. Supports only HEAD and GET requests. Directory listings and files can be served, content types are supported on a best-guess basis.

Field Summary

Modifier and TypeField and Description
private static final String
private static final String
private static final String
private static final String
private static final String
private final System.Logger
private static final HttpHandler
private final UnaryOperator<String>
private static final HttpHandler
private static final String
private static final Map<Integer, String>
private final Path
private static final UnaryOperator<String>
private static final List<String>
private static final List<String>

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private static Path
private static void
checkRequestWithinContext(String requestPath, String contextPath)

private static String
public static HttpHandler
create(Path root, UnaryOperator<String> mimeTable)

private static String
dirListing(HttpExchange exchange, Path path)

private static void
private static String
private String
public void
handle(HttpExchange exchange)

Implements com.sun.net.httpserver.HttpHandler.handle.

Handle the given request and generate an appropriate response.

private void
handleGET(HttpExchange exchange, Path path)

private void
handleHEAD(HttpExchange exchange, Path path)

private void
private void
private void
handleSupportedMethod(HttpExchange exchange, Path path, boolean writeBody)

private static String
private static Path
private static boolean
private void
listFiles(HttpExchange exchange, Path path, boolean writeBody)

private Path
mapToPath(HttpExchange exchange, Path root)

private String
private static boolean
private static String
private static String
private void
serveDefaultFavIcon(HttpExchange exchange, boolean writeBody)

private void
serveFile(HttpExchange exchange, Path path, boolean writeBody)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

closeHTMLback to summary
private static final String closeHTML
DEFAULT_CONTENT_TYPEback to summary
private static final String DEFAULT_CONTENT_TYPE
FAVICON_LAST_MODIFIEDback to summary
private static final String FAVICON_LAST_MODIFIED
FAVICON_RESOURCE_PATHback to summary
private static final String FAVICON_RESOURCE_PATH
hrefListItemTemplateback to summary
private static final String hrefListItemTemplate
loggerback to summary
private final System.Logger logger
METHOD_NOT_ALLOWED_HANDLERback to summary
private static final HttpHandler METHOD_NOT_ALLOWED_HANDLER
mimeTableback to summary
private final UnaryOperator<String> mimeTable
NOT_IMPLEMENTED_HANDLERback to summary
private static final HttpHandler NOT_IMPLEMENTED_HANDLER
openHTMLback to summary
private static final String openHTML
RESERVED_CHARSback to summary
private static final Map<Integer, String> RESERVED_CHARS
rootback to summary
private final Path root
sanitizeback to summary
private static final UnaryOperator<String> sanitize
SUPPORTED_METHODSback to summary
private static final List<String> SUPPORTED_METHODS
UNSUPPORTED_METHODSback to summary
private static final List<String> UNSUPPORTED_METHODS

Constructor Detail

FileServerHandlerback to summary
private FileServerHandler(Path root, UnaryOperator<String> mimeTable)

Method Detail

checkPathWithinRootback to summary
private static Path checkPathWithinRoot(Path path, Path root)
checkRequestWithinContextback to summary
private static void checkRequestWithinContext(String requestPath, String contextPath)
contextPathback to summary
private static String contextPath(HttpExchange exchange)
createback to summary
public static HttpHandler create(Path root, UnaryOperator<String> mimeTable)
dirListingback to summary
private static String dirListing(HttpExchange exchange, Path path) throws IOException
discardRequestBodyback to summary
private static void discardRequestBody(HttpExchange exchange) throws IOException
getLastModifiedback to summary
private static String getLastModified(Path path) throws IOException
getRedirectURIback to summary
private String getRedirectURI(URI uri)
handleback to summary
public void handle(HttpExchange exchange) throws IOException

Implements com.sun.net.httpserver.HttpHandler.handle.

Doc from com.sun.net.httpserver.HttpHandler.handle.

Handle the given request and generate an appropriate response. See HttpExchange for a description of the steps involved in handling an exchange.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs
handleGETback to summary
private void handleGET(HttpExchange exchange, Path path) throws IOException
handleHEADback to summary
private void handleHEAD(HttpExchange exchange, Path path) throws IOException
handleMovedPermanentlyback to summary
private void handleMovedPermanently(HttpExchange exchange) throws IOException
handleNotFoundback to summary
private void handleNotFound(HttpExchange exchange) throws IOException
handleSupportedMethodback to summary
private void handleSupportedMethod(HttpExchange exchange, Path path, boolean writeBody) throws IOException
hrefListItemForback to summary
private static String hrefListItemFor(URI uri)
indexFileback to summary
private static Path indexFile(Path path)
isFavIconRequestback to summary
private static boolean isFavIconRequest(HttpExchange exchange)
listFilesback to summary
private void listFiles(HttpExchange exchange, Path path, boolean writeBody) throws IOException
mapToPathback to summary
private Path mapToPath(HttpExchange exchange, Path root)
mediaTypeback to summary
private String mediaType(String file)
pathEndsWithSlashback to summary
private static boolean pathEndsWithSlash(HttpExchange exchange)
relativeRequestPathback to summary
private static String relativeRequestPath(HttpExchange exchange)
requestPathback to summary
private static String requestPath(HttpExchange exchange)
serveDefaultFavIconback to summary
private void serveDefaultFavIcon(HttpExchange exchange, boolean writeBody) throws IOException
serveFileback to summary
private void serveFile(HttpExchange exchange, Path path, boolean writeBody) throws IOException