Package org.actioncontroller.servlet
Class ServletHttpExchange
- java.lang.Object
-
- org.actioncontroller.servlet.ServletHttpExchange
-
- All Implemented Interfaces:
ApiHttpExchange
public class ServletHttpExchange extends Object implements ApiHttpExchange
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.actioncontroller.ApiHttpExchange
ApiHttpExchange.OutputStreamConsumer, ApiHttpExchange.WriterConsumer
-
-
Constructor Summary
Constructors Constructor Description ServletHttpExchange(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddResponseHeader(String key, String value)voidauthenticate()Map<String,List<String>>getAllHeaders()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.static List<String>getCookie(String name, javax.servlet.http.HttpServletRequest req)List<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()javax.servlet.http.HttpServletRequestgetRequest()Break the encapsulation of ApiHttpExchange and access the underlying implementation directory.javax.servlet.http.HttpServletResponsegetResponse()Break the encapsulation of ApiHttpExchange and access the underlying implementation directory.StringgetServerURL()Returns the scheme, hostname and port part of the requesting URL, for examplehttp://www.example.comorhttps://localhost:8443Optional<?>getSessionAttribute(String name, boolean createIfMissing)PrincipalgetUserPrincipal()booleanisUserInRole(String role)booleanisUserLoggedIn()voidoutput(String contentType, ApiHttpExchange.OutputStreamConsumer consumer)Callback with a OutputStream to generate response bodyStringpathParam(String name)voidsendError(int statusCode)voidsendError(int statusCode, String message)voidsendRedirect(String location)voidsetCookie(String name, String value, boolean secure, boolean isHttpOnly, String path, 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)StringtoString()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-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.actioncontroller.ApiHttpExchange
accept, getContextURL, getRequestURL, getSessionAttribute, getSessionAttribute, getSessionAttribute, hasParameter, optionalParameter, requiredParameter, setCookie, setCookie
-
-
-
-
Field Detail
-
CHARSET
public static final Charset CHARSET
-
-
Method Detail
-
getRequest
public javax.servlet.http.HttpServletRequest getRequest()
Break the encapsulation of ApiHttpExchange and access the underlying implementation directory. Should be avoided - prefer to improve ApiHttpExchange
-
getResponse
public javax.servlet.http.HttpServletResponse getResponse()
Break the encapsulation of ApiHttpExchange and access the underlying implementation directory. Should be avoided - prefer to improve ApiHttpExchange
-
getHttpMethod
public String getHttpMethod()
- Specified by:
getHttpMethodin interfaceApiHttpExchange
-
getServerURL
public String getServerURL()
Description copied from interface:ApiHttpExchangeReturns the scheme, hostname and port part of the requesting URL, for examplehttp://www.example.comorhttps://localhost:8443- Specified by:
getServerURLin interfaceApiHttpExchange
-
getContextPath
public String getContextPath()
Description copied from interface:ApiHttpExchangeReturns the context path of the server. Blank if the server context is the root of the server- Specified by:
getContextPathin interfaceApiHttpExchange
-
getApiURL
public URL getApiURL()
Description copied from interface:ApiHttpExchangeReturns 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.- Specified by:
getApiURLin interfaceApiHttpExchange
-
getPathInfo
public String getPathInfo()
Description copied from interface:ApiHttpExchangeReturns 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"- Specified by:
getPathInfoin interfaceApiHttpExchange
-
write
public void write(String contentType, ApiHttpExchange.WriterConsumer consumer) throws IOException
Description copied from interface:ApiHttpExchangeCallback with a Writer to generate response body- Specified by:
writein interfaceApiHttpExchange- Throws:
IOException
-
writeBody
public void writeBody(String contentType, String body) throws IOException
Description copied from interface:ApiHttpExchangeOutput String directory to the response body- Specified by:
writeBodyin interfaceApiHttpExchange- Throws:
IOException
-
output
public void output(String contentType, ApiHttpExchange.OutputStreamConsumer consumer) throws IOException
Description copied from interface:ApiHttpExchangeCallback with a OutputStream to generate response body- Specified by:
outputin interfaceApiHttpExchange- Throws:
IOException
-
setResponseHeader
public void setResponseHeader(String key, String value)
- Specified by:
setResponseHeaderin interfaceApiHttpExchange
-
addResponseHeader
public void addResponseHeader(String key, String value)
- Specified by:
addResponseHeaderin interfaceApiHttpExchange
-
sendRedirect
public void sendRedirect(String location) throws IOException
- Specified by:
sendRedirectin interfaceApiHttpExchange- Throws:
IOException
-
pathParam
public String pathParam(String name) throws HttpActionException
- Specified by:
pathParamin interfaceApiHttpExchange- Throws:
HttpActionException- throws 500 if the name was not matched with a path parameter
-
setPathParameters
public void setPathParameters(Map<String,String> pathParameters)
- Specified by:
setPathParametersin interfaceApiHttpExchange
-
getParameters
public List<String> getParameters(String name)
Description copied from interface:ApiHttpExchangeReturns 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"].- Specified by:
getParametersin interfaceApiHttpExchange- Parameters:
name- The query parameter name- Returns:
- the non-empty list of parameters matching the name or null if none were provided
-
getQueryString
public String getQueryString()
Description copied from interface:ApiHttpExchangeReturns 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.- Specified by:
getQueryStringin interfaceApiHttpExchange
-
getReader
public Reader getReader() throws IOException
- Specified by:
getReaderin interfaceApiHttpExchange- Throws:
IOException
-
getInputStream
public InputStream getInputStream() throws IOException
- Specified by:
getInputStreamin interfaceApiHttpExchange- Throws:
IOException
-
getClientIp
public String getClientIp()
- Specified by:
getClientIpin interfaceApiHttpExchange
-
setCookie
public void setCookie(String name, String value, boolean secure, boolean isHttpOnly, String path, int maxAge, String domain, String comment)
Description copied from interface:ApiHttpExchangeSets a cookie with default values. If value == null, deletes the cookie- Specified by:
setCookiein interfaceApiHttpExchange
-
getCookies
public List<String> getCookies(String name)
Description copied from interface:ApiHttpExchangeReturns all values of this cookie- Specified by:
getCookiesin interfaceApiHttpExchange- Parameters:
name- The cookie name- Returns:
- a non-null list of cookies matching the name or empty if none were provided
-
getCookie
public static List<String> getCookie(String name, javax.servlet.http.HttpServletRequest req)
-
getHeaders
public List<String> getHeaders(String name)
Description copied from interface:ApiHttpExchangeReturns the values for all instances of this header in the HTTP request- Specified by:
getHeadersin interfaceApiHttpExchange- Parameters:
name- The header name- Returns:
- the non-empty list of headers matching the name or null if none were provided
-
getAllHeaders
public Map<String,List<String>> getAllHeaders()
- Specified by:
getAllHeadersin interfaceApiHttpExchange
-
sendError
public void sendError(int statusCode, String message) throws IOException- Specified by:
sendErrorin interfaceApiHttpExchange- Throws:
IOException
-
sendError
public void sendError(int statusCode) throws IOException- Specified by:
sendErrorin interfaceApiHttpExchange- Throws:
IOException
-
isUserInRole
public boolean isUserInRole(String role)
- Specified by:
isUserInRolein interfaceApiHttpExchange
-
isUserLoggedIn
public boolean isUserLoggedIn()
- Specified by:
isUserLoggedInin interfaceApiHttpExchange
-
setSessionAttribute
public void setSessionAttribute(String name, Object value, boolean invalidate)
- Specified by:
setSessionAttributein interfaceApiHttpExchange
-
getSessionAttribute
public Optional<?> getSessionAttribute(String name, boolean createIfMissing)
- Specified by:
getSessionAttributein interfaceApiHttpExchange
-
getClientCertificate
public X509Certificate[] getClientCertificate()
- Specified by:
getClientCertificatein interfaceApiHttpExchange
-
getUserPrincipal
public Principal getUserPrincipal()
- Specified by:
getUserPrincipalin interfaceApiHttpExchange
-
setStatus
public void setStatus(int statusCode)
Description copied from interface:ApiHttpExchangeSets the http status code, but don't trigger the error handler (servlets)- Specified by:
setStatusin interfaceApiHttpExchange
-
authenticate
public void authenticate() throws IOException- Specified by:
authenticatein interfaceApiHttpExchange- Throws:
IOException
-
-