java.lang.Object
com.sun.net.httpserver.HttpExchange
dev.katsute.simplehttpserver.SimpleHttpExchange
- All Implemented Interfaces:
Request
,AutoCloseable
A
If a user sends a
If a user sends a
For requests that have content type
Cookies can be set by using the
An exchange must be sent in order to change on the client.
A server must have a
The
Data can be sent as a
HttpExchange
with additional extensions to simplify usage.
Requests
GET
Request
If a user sends a GET
request to the server, a map of keys and values of that request can be retrieved by using the getGetMap()
method.
POST
Request
If a user sends a POST
request to the server, a map of keys and values; or a MultipartFormData
can be retrieved by using getPostMap()
and getMultipartFormData()
.
multipart/form-data
For requests that have content type multipart/form-data
, data must be retrieved using getMultipartFormData()
, which returns a MultipartFormData
using Record
s and FileRecord
s. Files sent through here are sent as a Byte
array and not a File
.
Cookies
A clients browser cookies for the site can be retrieved by using thegetCookie(String)
or getCookies()
method.
Cookies can be set by using the
setCookie(HttpCookie)
or setCookie(String, String)
.
An exchange must be sent in order to change on the client.
Session
Normally the only "identifier" that we can retrieve from the user is their address and port provided from an exchange. This however, doesn't work across multiple tabs or when the user refreshes the page; instead we use a session cookie to track a user.A server must have a
HttpSessionHandler
set using HttpServerExtensions.setSessionHandler(HttpSessionHandler)
for this to work.
The
HttpSessionHandler
assigns session IDs to clients and allows the server to retrieve them using HttpServerExtensions.getSession(HttpExchange)
.
Response
To send response headers you must first retrieve then withHttpExchange.getResponseHeaders()
, modify them, then send them using HttpExchange.sendResponseHeaders(int, long)
or any other of the send methods.
Data can be sent as a
Byte
array, String
, or as a File
. Responses can optionally gziped to compress the data sent.
Note: An exchange must be sent or closed, otherwise the connection may resend the request until it gets response or times out.- Since:
- 5.0.0
- Version:
- 5.0.0
- Author:
- Katsute
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionabstract String
Returns the value of a given cookie for a client.Returns a map of all the cookies for a client.Returns GET query as a map of keys and values.abstract HttpExchange
Returns the underlying http exchange.abstract HttpServer
Returns the http server associated with an exchange.abstract MultipartFormData
For content typemultipart/form-data
, returns POST body as aMultipartFormData
object.Returns the POST body as a map of keys and values.abstract String
Returns the raw string representation of a GET query.abstract String
Returns the raw string representation of a POST body.abstract boolean
hasGet()
Returns if there is a GET query.abstract boolean
hasPost()
Returns if there is a POST body.abstract void
send
(byte[] bytes) Sends a byte array to the client.abstract void
send
(byte[] bytes, boolean gzip) Sends a byte array to the client that can be gziped.abstract void
send
(byte[] bytes, int responseCode) Sends a byte array to the client with a response code.abstract void
send
(byte[] bytes, int responseCode, boolean gzip) Sends a byte array to the client with a response code that can be gziped.abstract void
send
(int responseCode) Sends a response code to the client.abstract void
Sends a file to the client.abstract void
Sends a byte array to the client that can be gziped.abstract void
Sends a byte array to the client with a response code.abstract void
Sends a file to the client with a response code that can be gziped.abstract void
Sends a string to the client.abstract void
Sends a string to the client that can be gziped.abstract void
Sends a string to the client with a response codeabstract void
Sends a string to the client with a response code that can be gziped.abstract void
Sets a cookie for the client.abstract void
setCookie
(HttpCookie cookie) Sets a cookie for the client.Methods inherited from class com.sun.net.httpserver.HttpExchange
close, getAttribute, getHttpContext, getLocalAddress, getPrincipal, getProtocol, getRemoteAddress, getRequestBody, getRequestHeaders, getRequestMethod, getRequestURI, getResponseBody, getResponseCode, getResponseHeaders, sendResponseHeaders, setAttribute, setStreams
-
Method Details
-
getHttpServer
Returns the http server associated with an exchange.- Returns:
- http server
- Since:
- 5.0.0
- See Also:
-
getHttpExchange
Returns the underlying http exchange.- Returns:
- http exchange
- Since:
- 5.0.0
- See Also:
-
getRawGet
Returns the raw string representation of a GET query. The GET query is the string of characters located after the?
in the URL.- Returns:
- raw GET
- Since:
- 5.0.0
- See Also:
-
getGetMap
Returns GET query as a map of keys and values. The GET query is the string of characters located after the?
in the URL.- Returns:
- GET map
- Since:
- 5.0.0
- See Also:
-
hasGet
public abstract boolean hasGet()Returns if there is a GET query. The GET query is the string of characters located after the?
in the URL.- Returns:
- if GET query exists
- Since:
- 5.0.0
- See Also:
-
getRawPost
Returns the raw string representation of a POST body.- Returns:
- raw POST body
- Since:
- 5.0.0
- See Also:
-
getPostMap
Returns the POST body as a map of keys and values. For content typemultipart/form-data
usegetMultipartFormData()
.- Returns:
- POST map
- Since:
- 5.0.0
- See Also:
-
getMultipartFormData
For content typemultipart/form-data
, returns POST body as aMultipartFormData
object.- Returns:
- multipart/form-data object
- Since:
- 5.0.0
- See Also:
-
hasPost
public abstract boolean hasPost()Returns if there is a POST body.- Returns:
- if POST body exists
- Since:
- 5.0.0
- See Also:
-
getCookie
Returns the value of a given cookie for a client.- Parameters:
cookie
- name of cookie- Returns:
- value of cookie
- Since:
- 5.0.0
- See Also:
-
getCookies
Returns a map of all the cookies for a client.- Returns:
- map of cookies
- Since:
- 5.0.0
- See Also:
-
setCookie
Sets a cookie for the client. Response must be sent for cookies to update on the client.- Parameters:
key
- cookie keyvalue
- value of cookie- Since:
- 5.0.0
- See Also:
-
setCookie
Sets a cookie for the client. Response must be sent for cookies to update on the client.- Parameters:
cookie
- http cookie- See Also:
-
send
Sends a response code to the client.- Parameters:
responseCode
- response code- Throws:
IOException
- IO exception- Since:
- 5.0.0
- See Also:
-
send
Sends a byte array to the client.- Parameters:
bytes
- byte array- Throws:
IOException
- IO exception- Since:
- 5.0.0
- See Also:
-
send
Sends a byte array to the client with a response code.- Parameters:
bytes
- byte arrayresponseCode
- response code- Throws:
IOException
- IO exception- Since:
- 5.0.0
- See Also:
-
send
Sends a byte array to the client that can be gziped.- Parameters:
bytes
- byte arraygzip
- if the response should be compressed- Throws:
IOException
- IO exception- Since:
- 5.0.0
- See Also:
-
send
Sends a byte array to the client with a response code that can be gziped.- Parameters:
bytes
- byte arrayresponseCode
- response codegzip
- if the response should be compressed- Throws:
IOException
- IO exception- Since:
- 5.0.0
- See Also:
-
send
Sends a string to the client.- Parameters:
string
- string to send- Throws:
IOException
- IO exception- Since:
- 5.0.0
- See Also:
-
send
Sends a string to the client with a response code- Parameters:
string
- string to sendresponseCode
- response code- Throws:
IOException
- IO exception- Since:
- 5.0.0
- See Also:
-
send
Sends a string to the client that can be gziped.- Parameters:
string
- string to sendgzip
- if the response should be compressed- Throws:
IOException
- IO exception- Since:
- 5.0.0
- See Also:
-
send
Sends a string to the client with a response code that can be gziped.- Parameters:
string
- string to sendresponseCode
- response codegzip
- if the response should be compressed- Throws:
IOException
- IO exception- Since:
- 5.0.0
- See Also:
-
send
Sends a file to the client.- Parameters:
file
- file- Throws:
IOException
- IO exception- Since:
- 5.0.0
- See Also:
-
send
Sends a byte array to the client with a response code.- Parameters:
file
- file to sendresponseCode
- response code- Throws:
IOException
- IO exception- Since:
- 5.0.0
- See Also:
-
send
Sends a byte array to the client that can be gziped.- Parameters:
file
- file to sendgzip
- if the response should be compressed- Throws:
IOException
- IO exception- Since:
- 5.0.0
- See Also:
-
send
Sends a file to the client with a response code that can be gziped.- Parameters:
file
- file to sendresponseCode
- response codegzip
- if the response should be compressed- Throws:
IOException
- IO exception- Since:
- 5.0.0
- See Also:
-