Class SimpleHttpExchange

java.lang.Object
com.sun.net.httpserver.HttpExchange
dev.katsute.simplehttpserver.SimpleHttpExchange
All Implemented Interfaces:
AutoCloseable

public abstract class SimpleHttpExchange extends com.sun.net.httpserver.HttpExchange
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 Records and FileRecords. 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 the getCookie(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 with HttpExchange.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:
  • HttpExchange
  • Method Summary

    Modifier and Type
    Method
    Description
    abstract String
    getCookie(String cookie)
    Returns the value of a given cookie for a client.
    abstract Map<String,String>
    Returns a map of all the cookies for a client.
    abstract Map<String,String>
    Returns GET query as a map of keys and values.
    abstract com.sun.net.httpserver.HttpExchange
    Returns the underlying http exchange.
    abstract com.sun.net.httpserver.HttpServer
    Returns the http server associated with an exchange.
    For content type multipart/form-data, returns POST body as a MultipartFormData object.
    abstract Map<String,?>
    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
    Returns if there is a GET query.
    abstract boolean
    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
    send(File file)
    Sends a file to the client.
    abstract void
    send(File file, boolean gzip)
    Sends a byte array to the client that can be gziped.
    abstract void
    send(File file, int responseCode)
    Sends a byte array to the client with a response code.
    abstract void
    send(File file, int responseCode, boolean gzip)
    Sends a file to the client with a response code that can be gziped.
    abstract void
    send(String string)
    Sends a string to the client.
    abstract void
    send(String string, boolean gzip)
    Sends a string to the client that can be gziped.
    abstract void
    send(String string, int responseCode)
    Sends a string to the client with a response code
    abstract void
    send(String string, int responseCode, boolean gzip)
    Sends a string to the client with a response code that can be gziped.
    abstract void
    setCookie(String key, String value)
    Sets a cookie for the client.
    abstract void
    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

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait