Package org.actioncontroller
Interface ApiHttpExchange
-
- All Known Implementing Classes:
JakartaServletHttpExchange,JdkHttpExchange,ServletHttpExchange,SocketHttpExchange
public interface ApiHttpExchangeAbstracts the interaction with the http request and response. This interface acts as an abstraction that enables Action Controller to work both with both the servlet API andHttpServer.ApiHttpExchangealso provides convenience methods used by severalHttpParameterMapperandHttpReturnMappingimplementations.
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static interfaceApiHttpExchange.OutputStreamConsumerstatic interfaceApiHttpExchange.WriterConsumer
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default booleanaccept(String contentType)Returns true if the "Accept" request header matches the argument content typevoidaddResponseHeader(String key, String value)voidauthenticate()Map<String,List<String>>getAllHeaders()default URLgetApiURL()Returns the path to where the controllers paths are evaluated relative to, that is, the Servlet's context URL.X509Certificate[]getClientCertificate()StringgetClientIp()StringgetContextPath()Returns the context path of the server.default URLgetContextURL()Returns the scheme, hostname, port and context path of the serverList<String>getCookies(String name)Returns all values of this cookieList<String>getHeaders(String name)Returns the values for all instances of this header in the HTTP requestStringgetHttpMethod()InputStreamgetInputStream()List<String>getParameters(String name)Returns the specified query string of the requested url for a get request or x-www-form-urlencoded body parameter for a POST or PUT request.StringgetPathInfo()Returns the part of the URL after getApiURL.StringgetQueryString()Returns the query string of the requested url.ReadergetReader()default StringgetRequestURL()StringgetServerURL()Returns the scheme, hostname and port part of the requesting URL, for examplehttp://www.example.comorhttps://localhost:8443default <T> Optional<T>getSessionAttribute(Class<T> name)default <T> Optional<T>getSessionAttribute(Class<T> name, boolean createIfMissing)default OptionalgetSessionAttribute(String name)OptionalgetSessionAttribute(String name, boolean createIfMissing)PrincipalgetUserPrincipal()default booleanhasParameter(String name)booleanisUserInRole(String role)booleanisUserLoggedIn()default Optional<String>optionalParameter(String name)voidoutput(String contentType, ApiHttpExchange.OutputStreamConsumer consumer)Callback with a OutputStream to generate response bodyStringpathParam(String name)default StringrequiredParameter(String name)voidsendError(int statusCode)voidsendError(int statusCode, String message)voidsendRedirect(String path)default voidsetCookie(String name, String value)Sets a cookie with default values.default voidsetCookie(String name, String value, boolean secure, boolean isHttpOnly)Sets a cookie with default values.voidsetCookie(String name, String value, boolean secure, boolean isHttpOnly, String contextPath, int maxAge, String domain, String comment)Sets a cookie with default values.voidsetPathParameters(Map<String,String> pathParameters)voidsetResponseHeader(String key, String value)voidsetSessionAttribute(String name, Object value, boolean invalidate)voidsetStatus(int statusCode)Sets the http status code, but don't trigger the error handler (servlets)voidwrite(String contentType, ApiHttpExchange.WriterConsumer consumer)Callback with a Writer to generate response bodyvoidwriteBody(String contentType, String body)Output String directory to the response body
-
-
-
Method Detail
-
getHttpMethod
String getHttpMethod()
-
getContextPath
String getContextPath()
Returns the context path of the server. Blank if the server context is the root of the server
-
getServerURL
String getServerURL()
Returns the scheme, hostname and port part of the requesting URL, for examplehttp://www.example.comorhttps://localhost:8443
-
getContextURL
default URL getContextURL()
Returns the scheme, hostname, port and context path of the server
-
getApiURL
default URL getApiURL()
Returns the path to where the controllers paths are evaluated relative to, that is, the Servlet's context URL. For example, if anApiServletis bound as "/api/*" in a webapp mounted at "/app", getApiURL might returnhttps://example.com:7443/app/api.
-
getPathInfo
String getPathInfo()
Returns the part of the URL after getApiURL. For example if a controller is mounted athttps://example.com:7443/app/apiand the client requests GEThttps://example.com:7443/app/api/hello/worldgetPathInfo returns"/hello/world"
-
getRequestURL
default String getRequestURL()
-
getQueryString
String getQueryString()
Returns the query string of the requested url. For example if the client requests GEThttps://example.com:7443/app/api/hello/world?greeting=Hello+TheregetQueryString()returnsgreeting=Hello+There.
-
getParameters
List<String> getParameters(String name)
Returns the specified query string of the requested url for a get request or x-www-form-urlencoded body parameter for a POST or PUT request. For example if the client requests GEThttps://example.com:7443/app/api/hello/world?greeting=Hello+There&greeting=HigetParameter("greeting")returns["Hello There", "Hi"].- Parameters:
name- The query parameter name- Returns:
- the non-empty list of parameters matching the name or null if none were provided
-
hasParameter
default boolean hasParameter(String name)
-
write
void write(String contentType, ApiHttpExchange.WriterConsumer consumer) throws IOException
Callback with a Writer to generate response body- Throws:
IOException
-
writeBody
void writeBody(String contentType, String body) throws IOException
Output String directory to the response body- Throws:
IOException
-
output
void output(String contentType, ApiHttpExchange.OutputStreamConsumer consumer) throws IOException
Callback with a OutputStream to generate response body- Throws:
IOException
-
getHeaders
List<String> getHeaders(String name)
Returns the values for all instances of this header in the HTTP request- Parameters:
name- The header name- Returns:
- the non-empty list of headers matching the name or null if none were provided
-
accept
default boolean accept(String contentType)
Returns true if the "Accept" request header matches the argument content type
-
getClientIp
String getClientIp()
-
sendRedirect
void sendRedirect(String path) throws IOException
- Throws:
IOException
-
pathParam
String pathParam(String name) throws HttpActionException
- Throws:
HttpActionException- throws 500 if the name was not matched with a path parameter
-
getReader
Reader getReader() throws IOException
- Throws:
IOException
-
getInputStream
InputStream getInputStream() throws IOException
- Throws:
IOException
-
setCookie
default void setCookie(String name, String value)
Sets a cookie with default values. If value == null, deletes the cookie
-
setCookie
default void setCookie(String name, String value, boolean secure, boolean isHttpOnly)
Sets a cookie with default values. If value == null, deletes the cookie
-
setCookie
void setCookie(String name, String value, boolean secure, boolean isHttpOnly, String contextPath, int maxAge, String domain, String comment)
Sets a cookie with default values. If value == null, deletes the cookie
-
getCookies
List<String> getCookies(String name)
Returns all values of this cookie- Parameters:
name- The cookie name- Returns:
- a non-null list of cookies matching the name or empty if none were provided
-
sendError
void sendError(int statusCode, String message) throws IOException- Throws:
IOException
-
sendError
void sendError(int statusCode) throws IOException- Throws:
IOException
-
setStatus
void setStatus(int statusCode) throws IOExceptionSets the http status code, but don't trigger the error handler (servlets)- Throws:
IOException
-
isUserLoggedIn
boolean isUserLoggedIn()
-
isUserInRole
boolean isUserInRole(String role)
-
getSessionAttribute
default <T> Optional<T> getSessionAttribute(Class<T> name, boolean createIfMissing)
-
getClientCertificate
X509Certificate[] getClientCertificate()
-
getUserPrincipal
Principal getUserPrincipal()
-
authenticate
void authenticate() throws IOException- Throws:
IOException
-
-