Package com.gmt2001.httpwsserver
Class HttpServerPageHandler
java.lang.Object
io.netty.channel.ChannelHandlerAdapter
io.netty.channel.ChannelInboundHandlerAdapter
io.netty.channel.SimpleChannelInboundHandler<FullHttpRequest>
com.gmt2001.httpwsserver.HttpServerPageHandler
- All Implemented Interfaces:
ChannelHandler
,ChannelInboundHandler
Processes HTTP requests and passes successful ones to the appropriate registered final handler
- Author:
- gmt2001
-
Nested Class Summary
Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
Method Summary
Modifier and TypeMethodDescriptionprotected void
channelRead0
(ChannelHandlerContext ctx, FullHttpRequest req) Handles incoming HTTP requests and passes valid ones to the appropriateHttpRequestHandler
If a handler is not available for the requested path, then404 NOT FOUND
is sent back to the clientstatic boolean
checkFilePermissions
(ChannelHandlerContext ctx, FullHttpRequest req, Path p, boolean directoryAllowed) Checks if the file or directory pointed to byp
exists, is not hidden, is not a symlink, and is readable Sends back a404 NOT FOUND
or403 FORBIDDEN
on failurestatic void
deregisterHttpHandler
(String path) Deregisters a HTTP URI pathstatic String
detectContentType
(String fileNameOrType) Detects the content MIME type based on the filename or manually provided type extension NOTE: This method ignores everything before the last.
in the filenamevoid
exceptionCaught
(ChannelHandlerContext ctx, Throwable cause) Handles exceptions that are thrown up the stackstatic void
listDirectory
(ChannelHandlerContext ctx, FullHttpRequest req, Path p) Transmits aFullHttpResponse
back to the client that lists the contents of the directory pointed to byp
parseCookies
(HttpHeaders headers) Parses out cookies and converts them to a MapparsePost
(FullHttpRequest req) Parses out post data and converts it to a Mapstatic FullHttpResponse
Creates and prepares aFullHttpResponse
for transmission of a status code only If the value ofstatus
is in theCLIENT ERROR 4xx
,SERVER ERROR 5xx
, or an unknown class, then the standard name of the status code is appended to the beginning of the HTML responsestatic FullHttpResponse
prepareHttpResponse
(HttpResponseStatus status, byte[] content, String fileNameOrType) Creates and prepares aFullHttpResponse
for transmission This method automatically sets theContent-Type
andContent-Length
headers If the value ofstatus
is in theCLIENT ERROR 4xx
,SERVER ERROR 5xx
, or an unknown class, then the standard name of the status code is appended to the beginning of the HTML response, along with 2 line breaks and the MIME type is set totext/html
, unlessfileNameOrType
ends withjson
orxml
static FullHttpResponse
prepareHttpResponse
(HttpResponseStatus status, String content) Creates and prepares aFullHttpResponse
for transmission as text/plain This method automatically sets theContent-Type
andContent-Length
headers If the value ofstatus
is in theCLIENT ERROR 4xx
,SERVER ERROR 5xx
, or an unknown class, then the standard name of the status code is appended to the beginning of the HTML response, along with 2 line breaks and the MIME type is set totext/html
, unlessfileNameOrType
ends withjson
orxml
static FullHttpResponse
prepareHttpResponse
(HttpResponseStatus status, String content, String fileNameOrType) Creates and prepares aFullHttpResponse
for transmission This method automatically sets theContent-Type
andContent-Length
headers If the value ofstatus
is in theCLIENT ERROR 4xx
,SERVER ERROR 5xx
, or an unknown class, then the standard name of the status code is appended to the beginning of the HTML response, along with 2 line breaks and the MIME type is set totext/html
, unlessfileNameOrType
ends withjson
orxml
static void
registerHttpHandler
(String path, HttpRequestHandler handler) Registers a HTTP URI path to aHttpRequestHandler
static void
sendHttpResponse
(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) Transmits aFullHttpResponse
back to the clientstatic void
sendHttpResponse
(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res, boolean forceclose) Transmits aFullHttpResponse
back to the clientMethods inherited from class io.netty.channel.SimpleChannelInboundHandler
acceptInboundMessage, channelRead
Methods inherited from class io.netty.channel.ChannelInboundHandlerAdapter
channelActive, channelInactive, channelReadComplete, channelRegistered, channelUnregistered, channelWritabilityChanged, userEventTriggered
Methods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, handlerAdded, handlerRemoved, isSharable
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface io.netty.channel.ChannelHandler
handlerAdded, handlerRemoved
-
Method Details
-
channelRead0
Handles incoming HTTP requests and passes valid ones to the appropriateHttpRequestHandler
If a handler is not available for the requested path, then404 NOT FOUND
is sent back to the client- Specified by:
channelRead0
in classSimpleChannelInboundHandler<FullHttpRequest>
- Parameters:
ctx
- TheChannelHandlerContext
of the sessionreq
- TheFullHttpRequest
containing the request- Throws:
Exception
- Passes any thrown exceptions up the stack
-
exceptionCaught
Handles exceptions that are thrown up the stack- Specified by:
exceptionCaught
in interfaceChannelHandler
- Specified by:
exceptionCaught
in interfaceChannelInboundHandler
- Overrides:
exceptionCaught
in classChannelInboundHandlerAdapter
- Parameters:
ctx
- TheChannelHandlerContext
of the sessioncause
- The exception
-
detectContentType
Detects the content MIME type based on the filename or manually provided type extension NOTE: This method ignores everything before the last.
in the filename- Parameters:
fileNameOrType
- The filename or type extension to check- Returns:
- The valid MIME type, or
text/plain
if not recognized
-
prepareHttpResponse
Creates and prepares aFullHttpResponse
for transmission of a status code only If the value ofstatus
is in theCLIENT ERROR 4xx
,SERVER ERROR 5xx
, or an unknown class, then the standard name of the status code is appended to the beginning of the HTML response- Parameters:
status
- TheHttpResponseStatus
to return- Returns:
- A
FullHttpRequest
that is ready to transmit
-
prepareHttpResponse
Creates and prepares aFullHttpResponse
for transmission as text/plain This method automatically sets theContent-Type
andContent-Length
headers If the value ofstatus
is in theCLIENT ERROR 4xx
,SERVER ERROR 5xx
, or an unknown class, then the standard name of the status code is appended to the beginning of the HTML response, along with 2 line breaks and the MIME type is set totext/html
, unlessfileNameOrType
ends withjson
orxml
- Parameters:
status
- TheHttpResponseStatus
to returncontent
- The content to send- Returns:
- A
FullHttpRequest
that is ready to transmit
-
prepareHttpResponse
public static FullHttpResponse prepareHttpResponse(HttpResponseStatus status, String content, String fileNameOrType) Creates and prepares aFullHttpResponse
for transmission This method automatically sets theContent-Type
andContent-Length
headers If the value ofstatus
is in theCLIENT ERROR 4xx
,SERVER ERROR 5xx
, or an unknown class, then the standard name of the status code is appended to the beginning of the HTML response, along with 2 line breaks and the MIME type is set totext/html
, unlessfileNameOrType
ends withjson
orxml
- Parameters:
status
- TheHttpResponseStatus
to returncontent
- The content to sendfileNameOrType
- The filename or type extension for MIME type detection- Returns:
- A
FullHttpRequest
that is ready to transmit
-
prepareHttpResponse
public static FullHttpResponse prepareHttpResponse(HttpResponseStatus status, byte[] content, String fileNameOrType) Creates and prepares aFullHttpResponse
for transmission This method automatically sets theContent-Type
andContent-Length
headers If the value ofstatus
is in theCLIENT ERROR 4xx
,SERVER ERROR 5xx
, or an unknown class, then the standard name of the status code is appended to the beginning of the HTML response, along with 2 line breaks and the MIME type is set totext/html
, unlessfileNameOrType
ends withjson
orxml
- Parameters:
status
- TheHttpResponseStatus
to returncontent
- The content to sendfileNameOrType
- The filename or type extension for MIME type detection- Returns:
- A
FullHttpRequest
that is ready to transmit
-
sendHttpResponse
public static void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) Transmits aFullHttpResponse
back to the client- Parameters:
ctx
- TheChannelHandlerContext
of the sessionreq
- TheFullHttpRequest
containing the requestres
- TheFullHttpResponse
to transmit
-
sendHttpResponse
public static void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res, boolean forceclose) Transmits aFullHttpResponse
back to the client- Parameters:
ctx
- TheChannelHandlerContext
of the sessionreq
- TheFullHttpRequest
containing the requestres
- TheFullHttpResponse
to transmitforceclose
- If true, connection is closed regardless of status code; otherwise, only errors or unknown status codes will explicitly close the connection
-
listDirectory
Transmits aFullHttpResponse
back to the client that lists the contents of the directory pointed to byp
- Parameters:
ctx
- TheChannelHandlerContext
of the sessionreq
- TheFullHttpRequest
containing the requestp
- ThePath
to the directory to list
-
checkFilePermissions
public static boolean checkFilePermissions(ChannelHandlerContext ctx, FullHttpRequest req, Path p, boolean directoryAllowed) Checks if the file or directory pointed to byp
exists, is not hidden, is not a symlink, and is readable Sends back a404 NOT FOUND
or403 FORBIDDEN
on failure- Parameters:
ctx
- TheChannelHandlerContext
of the sessionreq
- TheFullHttpRequest
containing the requestp
- ThePath
to the file or directory to checkdirectoryAllowed
- Indicates if directories are allowed. If set tofalse
, will cause a403 FORBIDDEN
ifp
is a directory- Returns:
- and passed the same tests.
false
otherwise
-
registerHttpHandler
Registers a HTTP URI path to aHttpRequestHandler
- Parameters:
path
- The URI path to bind the handler tohandler
- TheHttpRequestHandler
that will handle the requests- Throws:
IllegalArgumentException
- Ifpath
is either already registered, or illegal- See Also:
-
validateUriPath
-
deregisterHttpHandler
Deregisters a HTTP URI path- Parameters:
path
- The path to deregister
-
parseCookies
Parses out cookies and converts them to a Map- Parameters:
headers
- TheFullHttpRequest
containing the request- Returns:
- A Map of cookies
-
parsePost
Parses out post data and converts it to a Map- Parameters:
req
- TheFullHttpRequest
containing the request- Returns:
- A Map of post data
-