Class Engine
- java.lang.Object
-
- io.inversion.Engine
-
public class Engine extends java.lang.ObjectMatches inbound Request Url paths to an Api Endpoint and executes associated Actions.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceEngine.EngineListenerReceivesEngineandApilifecycle, per request and per error callback notifications.
-
Field Summary
Fields Modifier and Type Field Description protected java.util.List<Api>apisTheApis being service by this Engineprotected io.inversion.config.Configconfigprotected java.lang.StringconfigPathOptional override for the configPath sys/env prop used by Config to locate configuration property filesprotected java.lang.StringconfigProfileOptional override for the sys/env prop used by Config to determine which profile specific configuration property files to loadprotected io.inversion.context.Contextcontextprotected java.lang.StringcorsAllowHeadersBase value for the CORS "Access-Control-Allow-Headers" response header.protected java.util.List<Action>filtersprotected ResponselastResponseThe lastResponseserved by this Engine, primarily used for writing test cases.protected java.util.List<Engine.EngineListener>listenersListeners that will receive Engine and Api lifecycle, request, and error callbacks.protected org.slf4j.Loggerlog
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidautowire(io.inversion.context.Context context)static io.inversion.json.JSNodebuildErrorJson(java.lang.Throwable ex)Responsedelete(java.lang.String url)Convenience overloading of#service(Request, Response)to run a REST DELETE Request on this Engine.Responsedelete(java.lang.String url, io.inversion.json.JSList hrefs)Convenience overloading of#service(Request, Response)to run a REST DELETE Request on this Engine.protected static voidexclude(io.inversion.json.JSMap node, java.util.Set<java.lang.String> includes, java.util.Set<java.lang.String> excludes, java.lang.String path)protected static voidexclude(Request req, Response res)protected static booleanexclude(java.lang.String path, java.util.Set<java.lang.String> includes, java.util.Set<java.lang.String> excludes)protected static booleanfind(java.util.Collection<java.lang.String> paths, java.lang.String path, boolean matchStart)Responseget(java.lang.String url)Convenience overloading of#service(Request, Response)to run a REST GET Request on this Engine.Responseget(java.lang.String url, java.util.List queryTerms)Convenience overloading of#service(Request, Response)to run a REST GET Request on this Engine.Responseget(java.lang.String url, java.util.Map<java.lang.String,java.lang.String> params)Convenience overloading of#service(Request, Response)to run a REST GET Request on this Engine.ApigetApi(java.lang.String apiName)java.util.List<Api>getApis()io.inversion.config.ConfiggetConfig()java.lang.StringgetConfigPath()java.lang.StringgetConfigProfile()io.inversion.context.ContextgetContext()java.util.List<Action>getFilters()ResponsegetLastResponse()java.net.URLgetResource(java.lang.String name)booleanisStarted()booleanmatchApi(Request req)Responsepatch(java.lang.String url, io.inversion.json.JSNode body)Convenience overloading of#service(Request, Response)to run a REST PATCH Request on this Engine.Responsepost(java.lang.String url, io.inversion.json.JSNode body)Convenience overloading of#service(Request, Response)to run a REST POST Request on this Engine.Responseput(java.lang.String url, io.inversion.json.JSNode body)Convenience overloading of#service(Request, Response)to run a REST PUT Request on this Engine.voidremoveApi(Api api)Removes the api, notifies EngineListeners and calls api.shutdown().Chainservice(Request req, Response res)The main entry point for processing a Request and generating Response content.Responseservice(java.lang.String method, java.lang.String url)Convenience overloading of#service(Request, Response)Responseservice(java.lang.String method, java.lang.String url, java.lang.String body)Convenience overloading of#service(Request, Response)Responseservice(java.lang.String method, java.lang.String url, java.lang.String body, java.util.Map<java.lang.String,java.lang.String> params)Convenience overloading of#service(Request, Response)voidshutdown()Removes all Apis and notifies listeners.onShutdownprotected voidshutdownApi(Api api)Enginestartup()Runs theWirerand callsstartupApifor each Api.protected voidstartup0()Convenient pre-startup hook for subclasses guaranteed to only be called once.protected voidstartupApi(Api api)EnginewithAllowHeaders(java.lang.String allowHeaders)EnginewithApi(Api api)EnginewithConfig(io.inversion.config.Config config)EnginewithConfigPath(java.lang.String configPath)EnginewithConfigProfile(java.lang.String configProfile)EnginewithContext(io.inversion.context.Context context)EnginewithEngineListener(Engine.EngineListener listener)Registerslistenerto receive Engine, Api, request and error callbacks.EnginewithFilters(Action... filters)
-
-
-
Field Detail
-
log
protected final transient org.slf4j.Logger log
-
configPath
protected transient java.lang.String configPath
Optional override for the configPath sys/env prop used by Config to locate configuration property files
-
configProfile
protected transient java.lang.String configProfile
Optional override for the sys/env prop used by Config to determine which profile specific configuration property files to load
-
context
protected transient io.inversion.context.Context context
-
config
protected transient io.inversion.config.Config config
-
listeners
protected final transient java.util.List<Engine.EngineListener> listeners
Listeners that will receive Engine and Api lifecycle, request, and error callbacks.
-
lastResponse
protected transient volatile Response lastResponse
The lastResponseserved by this Engine, primarily used for writing test cases.
-
apis
protected java.util.List<Api> apis
TheApis being service by this Engine
-
filters
protected final java.util.List<Action> filters
-
corsAllowHeaders
protected java.lang.String corsAllowHeaders
Base value for the CORS "Access-Control-Allow-Headers" response header.Values from the request "Access-Control-Request-Header" header are concatenated to this resulting in the final value of "Access-Control-Allow-Headers" sent in the response.
Unless you are really doing something specific with browser security you probably won't need to customize this list.
-
-
Constructor Detail
-
Engine
public Engine()
-
Engine
public Engine(java.lang.String configPath, java.lang.String configProfile)
-
Engine
public Engine(Api... apis)
-
-
Method Detail
-
startup0
protected void startup0()
Convenient pre-startup hook for subclasses guaranteed to only be called once.Called after
startinghas been set to true but before theWireris run or anyApis have been started.
-
startup
public Engine startup()
Runs theWirerand callsstartupApifor each Api.An Engine can only be started once. Any calls to
startupafter the initial call will not have any affect.- Returns:
- this Engine
-
shutdown
public void shutdown()
Removes all Apis and notifies listeners.onShutdown
-
get
public Response get(java.lang.String url)
Convenience overloading of#service(Request, Response)to run a REST GET Request on this Engine.IMPORTANT: This method does not make an external HTTP request, it runs the request on this Engine. If you want to make an external HTTP request see io.inversion.ApiClient.
GET requests for a specific resource should return 200 of 404. GET requests with query string search conditions should return 200 even if the search did not yield any results.
- Parameters:
url- the url that will be serviced by this Engine- Returns:
- the Response generated by handling the Request
- See Also:
service(Request, Response)
-
get
public Response get(java.lang.String url, java.util.Map<java.lang.String,java.lang.String> params)
Convenience overloading of#service(Request, Response)to run a REST GET Request on this Engine.IMPORTANT: This method does not make an external HTTP request, it runs the request on this Engine. If you want to make an external HTTP request see io.inversion.ApiClient.
GET requests for a specific resource should return 200 of 404. GET requests with query string search conditions should return 200 even if the search did not yield any results.
- Parameters:
url- the url that will be serviced by this Engineparams- additional key/value pairs to add to the url query string- Returns:
- the Response generated by handling the Request
- See Also:
service(Request, Response)
-
get
public Response get(java.lang.String url, java.util.List queryTerms)
Convenience overloading of#service(Request, Response)to run a REST GET Request on this Engine.IMPORTANT: This method does not make an external HTTP request, it runs the request on this Engine. If you want to make an external HTTP request see io.inversion.ApiClient.
GET requests for a specific resource should return 200 of 404. GET requests with query string search conditions should return 200 even if the search did not yield any results.
- Parameters:
url- the url that will be serviced by this EnginequeryTerms- additional keys (no values) to add to the url query string- Returns:
- the Response generated by handling the Request
- See Also:
service(Request, Response)
-
post
public Response post(java.lang.String url, io.inversion.json.JSNode body)
Convenience overloading of#service(Request, Response)to run a REST POST Request on this Engine.IMPORTANT: This method does not make an external HTTP request, it runs the request on this Engine. If you want to make an external HTTP request see io.inversion.ApiClient.
Successful POSTs that create a new resource should return a 201.
- Parameters:
url- the url that will be serviced by this Enginebody- the JSON body to POST which will be stringified first- Returns:
- the Response generated by handling the Request
- See Also:
service(Request, Response)
-
put
public Response put(java.lang.String url, io.inversion.json.JSNode body)
Convenience overloading of#service(Request, Response)to run a REST PUT Request on this Engine.IMPORTANT: This method does not make an external HTTP request, it runs the request on this Engine. If you want to make an external HTTP request see io.inversion.ApiClient.
Successful PUTs that update an existing resource should return a 204. If the PUT references a resource that does not exist, a 404 will be returned.
- Parameters:
url- the url that will be serviced by this Enginebody- the JSON body to POST which will be stringified first- Returns:
- the Response generated by handling the Request
- See Also:
service(Request, Response)
-
patch
public Response patch(java.lang.String url, io.inversion.json.JSNode body)
Convenience overloading of#service(Request, Response)to run a REST PATCH Request on this Engine.IMPORTANT: This method does not make an external HTTP request, it runs the request on this Engine. If you want to make an external HTTP request see io.inversion.ApiClient.
Successful PATCHes that update an existing resource should return a 204. If the PATCH references a resource that does not exist, a 404 will be returned.
- Parameters:
url- the url for a specific resource that should be PATCHed that will be serviced by this Enginebody- the JSON body to POST which will be stringified first- Returns:
- the Response generated by handling the Request
- See Also:
service(Request, Response)
-
delete
public Response delete(java.lang.String url)
Convenience overloading of#service(Request, Response)to run a REST DELETE Request on this Engine.IMPORTANT: This method does not make an external HTTP request, it runs the request on this Engine. If you want to make an external HTTP request see io.inversion.ApiClient.
- Parameters:
url- the url of the resource to be DELETED- Returns:
- the Response generated by handling the Request with status 204 if the delete was successful or 404 if the resource was not found
- See Also:
service(Request, Response)
-
delete
public Response delete(java.lang.String url, io.inversion.json.JSList hrefs)
Convenience overloading of#service(Request, Response)to run a REST DELETE Request on this Engine.IMPORTANT: This method does not make an external HTTP request, it runs the request on this Engine. If you want to make an external HTTP request see io.inversion.ApiClient.
- Parameters:
url- the url of the resource to be DELETEDhrefs- the hrefs of the resource to delete- Returns:
- the Response generated by handling the Request with status 204 if the delete was successful or 404 if the resource was not found
- See Also:
service(Request, Response)
-
service
public Response service(java.lang.String method, java.lang.String url)
Convenience overloading of#service(Request, Response)IMPORTANT: This method does not make an external HTTP request, it runs the request on this Engine. If you want to make an external HTTP request see io.inversion.ApiClient.
- Parameters:
method- the http method of the requested operationurl- the url that will be serviced by this Engine- Returns:
- the Response generated by handling the Request
- See Also:
service(Request, Response)
-
service
public Response service(java.lang.String method, java.lang.String url, java.lang.String body)
Convenience overloading of#service(Request, Response)IMPORTANT: This method does not make an external HTTP request, it runs the request on this Engine. If you want to make an external HTTP request see io.inversion.ApiClient.
- Parameters:
method- the http method of the requested operationurl- the url that will be serviced by this Engine.body- a stringified JSON body presumably to PUT/POST/PATCH- Returns:
- the Response generated by handling the Request
- See Also:
service(Request, Response)
-
service
public Response service(java.lang.String method, java.lang.String url, java.lang.String body, java.util.Map<java.lang.String,java.lang.String> params)
Convenience overloading of#service(Request, Response)IMPORTANT: This method does not make an external HTTP request, it runs the request on this Engine. If you want to make an external HTTP request see io.inversion.ApiClient.
- Parameters:
method- the http method of the requested operationurl- the url that will be serviced by this Engine.body- a stringified JSON body presumably to PUT/POST/PATCHparams- additional key/value pairs to add to the url query string- Returns:
- the Response generated by handling the Request
- See Also:
service(Request, Response)
-
service
public Chain service(Request req, Response res)
The main entry point for processing a Request and generating Response content.This method is designed to be called by integrating runtimes such as
EngineServletor byActions that need to make recursive calls to the Engine when performing composite operations.The host and port component of the Request url are ignored assuming that this Engine instance is supposed to be servicing the request. The url does not have to start with "http[s]://". If it does not, urls that start with "/" or not are handled the same.
All of the following would be processed the same way:
- https://library.com/v1/library/books?ISBN=1234567890
- https://library.com:8080/v1/library/books?ISBN=1234567890
- https://localhost/v1/library/books?ISBN=1234567890
- /v1/library/books?ISBN=1234567890
- v1/library/books?ISBN=1234567890
- Parameters:
req- the api Requestres- the api Response- Returns:
- the Chain representing all of the actions executed in populating the Response
-
matchApi
public boolean matchApi(Request req)
-
buildErrorJson
public static io.inversion.json.JSNode buildErrorJson(java.lang.Throwable ex)
-
isStarted
public boolean isStarted()
-
withEngineListener
public Engine withEngineListener(Engine.EngineListener listener)
Registerslistenerto receive Engine, Api, request and error callbacks.- Parameters:
listener- the listener to add- Returns:
- this
-
getApis
public java.util.List<Api> getApis()
-
getApi
public Api getApi(java.lang.String apiName)
-
startupApi
protected void startupApi(Api api)
-
removeApi
public void removeApi(Api api)
Removes the api, notifies EngineListeners and calls api.shutdown().- Parameters:
api- the api to be removed
-
shutdownApi
protected void shutdownApi(Api api)
-
withAllowHeaders
public Engine withAllowHeaders(java.lang.String allowHeaders)
-
getLastResponse
public Response getLastResponse()
- Returns:
- the last response serviced by this Engine.
-
getResource
public java.net.URL getResource(java.lang.String name)
-
getConfigPath
public java.lang.String getConfigPath()
-
withConfigPath
public Engine withConfigPath(java.lang.String configPath)
-
getConfigProfile
public java.lang.String getConfigProfile()
-
withConfigProfile
public Engine withConfigProfile(java.lang.String configProfile)
-
getConfig
public io.inversion.config.Config getConfig()
-
withConfig
public Engine withConfig(io.inversion.config.Config config)
-
getContext
public io.inversion.context.Context getContext()
-
withContext
public Engine withContext(io.inversion.context.Context context)
-
getFilters
public java.util.List<Action> getFilters()
-
exclude
protected static void exclude(io.inversion.json.JSMap node, java.util.Set<java.lang.String> includes, java.util.Set<java.lang.String> excludes, java.lang.String path)
-
exclude
protected static boolean exclude(java.lang.String path, java.util.Set<java.lang.String> includes, java.util.Set<java.lang.String> excludes)
-
find
protected static boolean find(java.util.Collection<java.lang.String> paths, java.lang.String path, boolean matchStart)
-
autowire
protected void autowire(io.inversion.context.Context context)
-
-