- java.lang.Object
-
- com.kttdevelopment.simplehttpserver.SimpleHttpExchange
-
public abstract class SimpleHttpExchange extends Object
This class is a simplified implementation ofHttpExchange.
This class provides methods to process requests from the client and send a response back. The handler will keep executing until a response is sent.
Get/post type requests forapplication/x-www-form-urlencoded(default) will be a simple key/value map.
Get/post type requests formultipart/form-datawill use aMultipartFormData.
If a file is submitted the value will be in bytes unless it is plain text.- Since:
- 02.00.00
- Version:
- 4.0.0
- Author:
- Ktt Development
- See Also:
HttpExchange
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedSimpleHttpExchange()Create an emptySimpleHttpExchange.
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description abstract voidclose()Closes the exchange between the client and server.abstract ObjectgetAttribute(String name)Returns attribute for filters use.abstract Map<String,String>getCookies()Returns the client's existing cookies as a map of keys and values.abstract Map<String,String>getGetMap()Returns the GET request as a map with its keys and values.abstract HttpContextgetHttpContext()Returns the context associated with the exchange.abstract HttpExchangegetHttpExchange()Returns the native http exchange.abstract HttpPrincipalgetHttpPrincipal()Returns the http principle for the exchange.abstract HttpServergetHttpServer()Returns the server where the request came from.abstract InetSocketAddressgetLocalAddress()Returns the client's local address on the server.abstract MultipartFormDatagetMultipartFormData()Returns a multipart/form-data as an object or null if there is none.abstract OutputStreamgetOutputStream()Returns the exchange response stream.abstract MapgetPostMap()Returns the POST request as keys mapped to values.abstract StringgetProtocol()Returns the protocol for the URI address.abstract InetSocketAddressgetPublicAddress()Returns the client's public address.abstract StringgetRawGet()Returns the GET request as a string.abstract StringgetRawPost()Returns the POST request as a string.abstract HeadersgetRequestHeaders()Returns the request headers.abstract RequestMethodgetRequestMethod()Returns the request method.abstract intgetResponseCode()Returns the response headers to be sent.abstract HeadersgetResponseHeaders()Returns the response headers to be sent.abstract URIgetURI()Returns the exchange URI.abstract booleanhasGet()Returns if there is a GET request.abstract booleanhasPost()Returns if there is a POST request.abstract voidsend(byte[] response)Sends a response to the client.abstract voidsend(byte[] response, boolean gzip)Sends a response to the client.abstract voidsend(byte[] response, int responseCode)Sends a response with response code to the client.abstract voidsend(byte[] response, int responseCode, boolean gzip)Sends a response with response code to the client.abstract voidsend(int responseCode)Sends a response code to the client.abstract voidsend(File file)Sends a file to the client.abstract voidsend(File file, boolean gzip)Sends a file to the client.abstract voidsend(File file, int responseCode)Sends a file with response code to the client.abstract voidsend(File file, int responseCode, boolean gzip)Sends a file with response code to the client.abstract voidsend(String response)Sends a response to the client.abstract voidsend(String response, boolean gzip)Sends a response to the client.abstract voidsend(String response, int responseCode)Sends a response with response code to the client.abstract voidsend(String response, int responseCode, boolean gzip)Sends a response with response code to the client.abstract voidsendResponseHeaders(int code, long length)Sends response headers to the client.abstract voidsetAttribute(String name, Object value)Sets attribute for filter use.abstract voidsetCookie(SimpleHttpCookie cookie)Sets a cookie in the response header.abstract voidsetCookie(String key, String value)Sets a cookie in the response header.
-
-
-
Constructor Detail
-
SimpleHttpExchange
protected SimpleHttpExchange()
Create an emptySimpleHttpExchange. Applications don't use this method.- Since:
- 02.00.00
- See Also:
SimpleHttpExchangeImpl.create(HttpExchange)
-
-
Method Detail
-
getHttpServer
public abstract HttpServer getHttpServer()
Returns the server where the request came from.- Returns:
- server associated with the exchange
- Since:
- 02.00.00
- See Also:
HttpServer
-
getHttpExchange
public abstract HttpExchange getHttpExchange()
Returns the native http exchange.- Returns:
- native http exchange
- Since:
- 02.00.00
- See Also:
HttpExchange
-
getURI
public abstract URI getURI()
Returns the exchange URI.- Returns:
- exchange URI
- Since:
- 02.00.00
- See Also:
URI
-
getPublicAddress
public abstract InetSocketAddress getPublicAddress()
Returns the client's public address.- Returns:
- client's public address
- Since:
- 02.00.00
- See Also:
InetSocketAddress,getLocalAddress()
-
getLocalAddress
public abstract InetSocketAddress getLocalAddress()
Returns the client's local address on the server.- Returns:
- client's local address
- Since:
- 02.00.00
- See Also:
InetSocketAddress,getPublicAddress()
-
getHttpContext
public abstract HttpContext getHttpContext()
Returns the context associated with the exchange.- Returns:
- associated context
- Since:
- 02.00.00
- See Also:
HttpContext
-
getHttpPrincipal
public abstract HttpPrincipal getHttpPrincipal()
Returns the http principle for the exchange.- Returns:
- http principal
- Since:
- 02.00.00
- See Also:
HttpPrincipal
-
getProtocol
public abstract String getProtocol()
Returns the protocol for the URI address.- Returns:
- url protocol
- Since:
- 02.00.00
-
getRequestHeaders
public abstract Headers getRequestHeaders()
Returns the request headers.- Returns:
- request headers
- Since:
- 02.00.00
- See Also:
Headers
-
getRequestMethod
public abstract RequestMethod getRequestMethod()
Returns the request method.- Returns:
- request method
- Since:
- 02.00.00
- See Also:
RequestMethod
-
getRawGet
public abstract String getRawGet()
Returns the GET request as a string.- Returns:
- GET request as a string
- Since:
- 02.00.00
- See Also:
getGetMap(),hasGet(),URI.getQuery()
-
getGetMap
public abstract Map<String,String> getGetMap()
Returns the GET request as a map with its keys and values.- Returns:
- GET request as a map
- Since:
- 02.00.00
- See Also:
getRawGet(),hasGet()
-
hasGet
public abstract boolean hasGet()
Returns if there is a GET request.- Returns:
- if GET request exists
- Since:
- 02.00.00
- See Also:
getRawGet(),getGetMap()
-
getRawPost
public abstract String getRawPost()
Returns the POST request as a string.- Returns:
- POST request as a string
- Since:
- 02.00.00
- See Also:
HttpExchange.getRequestBody(),getPostMap(),getMultipartFormData(),hasPost()
-
getPostMap
public abstract Map getPostMap()
Returns the POST request as keys mapped to values.- Returns:
- POST request as a map
- Since:
- 02.00.00
- See Also:
getRawPost(),getMultipartFormData(),hasPost()
-
getMultipartFormData
public abstract MultipartFormData getMultipartFormData()
Returns a multipart/form-data as an object or null if there is none.- Returns:
- POST request as a multipart/form-data
- Since:
- 4.0.0
- See Also:
MultipartFormData,getRawPost(),getPostMap(),hasPost()
-
hasPost
public abstract boolean hasPost()
Returns if there is a POST request.- Returns:
- if POST request exists
- Since:
- 02.00.00
- See Also:
getRawPost(),getPostMap(),getMultipartFormData()
-
getResponseHeaders
public abstract Headers getResponseHeaders()
Returns the response headers to be sent.- Returns:
- response headers
- Since:
- 02.00.00
- See Also:
Headers,sendResponseHeaders(int, long)
-
getResponseCode
public abstract int getResponseCode()
Returns the response headers to be sent.- Returns:
- response code
- Since:
- 02.00.00
- See Also:
HttpURLConnection,sendResponseHeaders(int, long)
-
getCookies
public abstract Map<String,String> getCookies()
Returns the client's existing cookies as a map of keys and values.- Returns:
- client's cookies
- Since:
- 02.00.00
- See Also:
setCookie(String, String),setCookie(SimpleHttpCookie)
-
setCookie
public abstract void setCookie(String key, String value)
Sets a cookie in the response header.- Parameters:
key- name of cookie to setvalue- value of cookie- Throws:
IllegalArgumentException- if the cookie name is reserved by the server- Since:
- 02.03.00
- See Also:
SimpleHttpCookie,setCookie(SimpleHttpCookie),getCookies(),getResponseHeaders()
-
setCookie
public abstract void setCookie(SimpleHttpCookie cookie)
Sets a cookie in the response header.- Parameters:
cookie- cookie to set- Throws:
IllegalArgumentException- if the cookie name is reserved by the server- Since:
- 02.00.00
- See Also:
SimpleHttpCookie,setCookie(String, String),getCookies(),getResponseHeaders()
-
getOutputStream
public abstract OutputStream getOutputStream()
Returns the exchange response stream.- Returns:
- exchange output stream
- Since:
- 03.01.00
-
sendResponseHeaders
public abstract void sendResponseHeaders(int code, long length) throws IOExceptionSends response headers to the client.- Parameters:
code- response codelength- the size of the response in bytes- Throws:
IOException- internal server error- Since:
- 02.00.00
- See Also:
getResponseHeaders(),HttpURLConnection
-
send
public abstract void send(int responseCode) throws IOExceptionSends a response code to the client.- Parameters:
responseCode- response code- Throws:
IOException- internal server error- Since:
- 02.00.00
- See Also:
sendResponseHeaders(int, long),send(byte[]),send(byte[], boolean),send(byte[], int),send(byte[], int, boolean),send(String),send(String, boolean),send(String, int),send(String, int, boolean),send(File),send(File, boolean),send(File, int),send(File, int, boolean)
-
send
public abstract void send(byte[] response) throws IOExceptionSends a response to the client.- Parameters:
response- response in bytes- Throws:
IOException- internal server error- Since:
- 02.00.00
- See Also:
sendResponseHeaders(int, long),send(int),send(byte[], boolean),send(byte[], int),send(byte[], int, boolean),send(String),send(String, boolean),send(String, int),send(String, int, boolean),send(File),send(File, boolean),send(File, int),send(File, int, boolean)
-
send
public abstract void send(byte[] response, boolean gzip) throws IOExceptionSends a response to the client.- Parameters:
response- response in bytesgzip- if response should be gziped before sending- Throws:
IOException- internal server error- Since:
- 02.00.00
- See Also:
sendResponseHeaders(int, long),send(int),send(byte[]),send(byte[], int),send(byte[], int, boolean),send(String),send(String, boolean),send(String, int),send(String, int, boolean),send(File),send(File, boolean),send(File, int),send(File, int, boolean)
-
send
public abstract void send(byte[] response, int responseCode) throws IOExceptionSends a response with response code to the client.- Parameters:
response- response in bytesresponseCode- response code- Throws:
IOException- internal server error- Since:
- 02.00.00
- See Also:
sendResponseHeaders(int, long),send(int),send(byte[]),send(byte[], boolean),send(byte[], int, boolean),send(String),send(String, boolean),send(String, int),send(String, int, boolean),send(File),send(File, boolean),send(File, int),send(File, int, boolean)
-
send
public abstract void send(byte[] response, int responseCode, boolean gzip) throws IOExceptionSends a response with response code to the client.- Parameters:
response- response in bytesresponseCode- response codegzip- if response should be gziped before sending it to the client- Throws:
IOException- internal server error- Since:
- 02.00.00
- See Also:
sendResponseHeaders(int, long),send(int),send(byte[]),send(byte[], boolean),send(byte[], int),send(String),send(String, boolean),send(String, int),send(String, int, boolean),send(File),send(File, boolean),send(File, int),send(File, int, boolean)
-
send
public abstract void send(String response) throws IOException
Sends a response to the client.- Parameters:
response- response- Throws:
IOException- internal server error- Since:
- 02.00.00
- See Also:
sendResponseHeaders(int, long),send(int),send(byte[]),send(byte[], boolean),send(byte[], int),send(byte[], int, boolean),send(String, boolean),send(String, int),send(String, int, boolean),send(File),send(File, boolean),send(File, int),send(File, int, boolean)
-
send
public abstract void send(String response, boolean gzip) throws IOException
Sends a response to the client.- Parameters:
response- responsegzip- if response should be gziped before sending it to the client- Throws:
IOException- internal server error- Since:
- 02.00.00
- See Also:
sendResponseHeaders(int, long),send(int),send(byte[]),send(byte[], boolean),send(byte[], int),send(byte[], int, boolean),send(String),send(String, int),send(String, int, boolean),send(File),send(File, boolean),send(File, int),send(File, int, boolean)
-
send
public abstract void send(String response, int responseCode) throws IOException
Sends a response with response code to the client.- Parameters:
response- responseresponseCode- response code- Throws:
IOException- internal server error- Since:
- 02.00.00
- See Also:
sendResponseHeaders(int, long),send(int),send(byte[]),send(byte[], boolean),send(byte[], int),send(byte[], int, boolean),send(String),send(String, boolean),send(String, int, boolean),send(File),send(File, boolean),send(File, int),send(File, int, boolean)
-
send
public abstract void send(String response, int responseCode, boolean gzip) throws IOException
Sends a response with response code to the client.- Parameters:
response- responseresponseCode- response codegzip- if the response should be gziped before sending it to the client- Throws:
IOException- internal server error- Since:
- 02.00.00
- See Also:
sendResponseHeaders(int, long),send(int),send(byte[]),send(byte[], boolean),send(byte[], int),send(byte[], int, boolean),send(String),send(String, boolean),send(String, int),send(File),send(File, boolean),send(File, int),send(File, int, boolean)
-
send
public abstract void send(File file) throws IOException
Sends a file to the client.- Parameters:
file- file to send- Throws:
IOException- internal server error or file read error- Since:
- 4.0.0
- See Also:
sendResponseHeaders(int, long),send(int),send(byte[]),send(byte[], boolean),send(byte[], int),send(byte[], int, boolean),send(String),send(String, boolean),send(String, int),send(String, int, boolean),send(File, boolean),send(File, int),send(File, int, boolean)
-
send
public abstract void send(File file, boolean gzip) throws IOException
Sends a file to the client.- Parameters:
file- file to sendgzip- if the response should be gziped before sending it to the client- Throws:
IOException- internal server error or file read error- Since:
- 4.0.0
- See Also:
sendResponseHeaders(int, long),send(int),send(byte[]),send(byte[], boolean),send(byte[], int),send(byte[], int, boolean),send(String),send(String, boolean),send(String, int),send(String, int, boolean),send(File),send(File, int),send(File, int, boolean)
-
send
public abstract void send(File file, int responseCode) throws IOException
Sends a file with response code to the client.- Parameters:
file- file to sendresponseCode- response code- Throws:
IOException- internal server error or file read error- Since:
- 4.0.0
- See Also:
sendResponseHeaders(int, long),send(int),send(byte[]),send(byte[], boolean),send(byte[], int),send(byte[], int, boolean),send(String),send(String, boolean),send(String, int),send(String, int, boolean),send(File),send(File, boolean),send(File, int, boolean)
-
send
public abstract void send(File file, int responseCode, boolean gzip) throws IOException
Sends a file with response code to the client.- Parameters:
file- file to sendresponseCode- response codegzip- if the response should be gziped before sending it to the client- Throws:
IOException- internal server error or file read error- Since:
- 4.0.0
- See Also:
sendResponseHeaders(int, long),send(int),send(byte[]),send(byte[], boolean),send(byte[], int),send(byte[], int, boolean),send(String),send(String, boolean),send(String, int),send(String, int, boolean),send(File),send(File, boolean),send(File, int)
-
close
public abstract void close()
Closes the exchange between the client and server.- Since:
- 02.00.00
-
getAttribute
public abstract Object getAttribute(String name)
Returns attribute for filters use.- Parameters:
name- name- Returns:
- value
- Since:
- 02.00.00
- See Also:
HttpExchange.getAttribute(String),setAttribute(String, Object)
-
setAttribute
public abstract void setAttribute(String name, Object value)
Sets attribute for filter use.- Parameters:
name- namevalue- value- Since:
- 02.00.00
- See Also:
HttpExchange.setAttribute(String, Object),getAttribute(String)
-
-