Package io.inversion

Class Engine


  • public class Engine
    extends java.lang.Object
    Matches inbound Request Url paths to an Api Endpoint and executes associated Actions.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static interface  Engine.EngineListener
      Receives Engine and Api lifecycle, per request and per error callback notifications.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected java.util.List<Api> apis
      The Apis being service by this Engine
      protected io.inversion.config.Config config  
      protected java.lang.String configPath
      Optional override for the configPath sys/env prop used by Config to locate configuration property files
      protected java.lang.String configProfile
      Optional override for the sys/env prop used by Config to determine which profile specific configuration property files to load
      protected io.inversion.context.Context context  
      protected java.lang.String corsAllowHeaders
      Base value for the CORS "Access-Control-Allow-Headers" response header.
      protected java.util.List<Action> filters  
      protected Response lastResponse
      The last Response served by this Engine, primarily used for writing test cases.
      protected java.util.List<Engine.EngineListener> listeners
      Listeners that will receive Engine and Api lifecycle, request, and error callbacks.
      protected org.slf4j.Logger log  
    • Constructor Summary

      Constructors 
      Constructor Description
      Engine()  
      Engine​(Api... apis)  
      Engine​(java.lang.String configPath, java.lang.String configProfile)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected void autowire​(io.inversion.context.Context context)  
      static io.inversion.json.JSNode buildErrorJson​(java.lang.Throwable ex)  
      Response delete​(java.lang.String url)
      Convenience overloading of #service(Request, Response) to run a REST DELETE Request on this Engine.
      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.
      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)  
      protected static void exclude​(Request req, Response res)  
      protected static boolean exclude​(java.lang.String path, java.util.Set<java.lang.String> includes, java.util.Set<java.lang.String> excludes)  
      protected static boolean find​(java.util.Collection<java.lang.String> paths, java.lang.String path, boolean matchStart)  
      Response get​(java.lang.String url)
      Convenience overloading of #service(Request, Response) to run a REST GET Request on this Engine.
      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.
      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.
      Api getApi​(java.lang.String apiName)  
      java.util.List<Api> getApis()  
      io.inversion.config.Config getConfig()  
      java.lang.String getConfigPath()  
      java.lang.String getConfigProfile()  
      io.inversion.context.Context getContext()  
      java.util.List<Action> getFilters()  
      Response getLastResponse()  
      java.net.URL getResource​(java.lang.String name)  
      boolean isStarted()  
      boolean matchApi​(Request req)  
      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.
      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.
      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.
      void removeApi​(Api api)
      Removes the api, notifies EngineListeners and calls api.shutdown().
      Chain service​(Request req, Response res)
      The main entry point for processing a Request and generating Response content.
      Response service​(java.lang.String method, java.lang.String url)
      Convenience overloading of #service(Request, Response)
      Response service​(java.lang.String method, java.lang.String url, java.lang.String body)
      Convenience overloading of #service(Request, Response)
      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)
      void shutdown()
      Removes all Apis and notifies listeners.onShutdown
      protected void shutdownApi​(Api api)  
      Engine startup()
      Runs the Wirer and calls startupApi for each Api.
      protected void startup0()
      Convenient pre-startup hook for subclasses guaranteed to only be called once.
      protected void startupApi​(Api api)  
      Engine withAllowHeaders​(java.lang.String allowHeaders)  
      Engine withApi​(Api api)  
      Engine withConfig​(io.inversion.config.Config config)  
      Engine withConfigPath​(java.lang.String configPath)  
      Engine withConfigProfile​(java.lang.String configProfile)  
      Engine withContext​(io.inversion.context.Context context)  
      Engine withEngineListener​(Engine.EngineListener listener)
      Registers listener to receive Engine, Api, request and error callbacks.
      Engine withFilters​(Action... filters)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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 last Response served by this Engine, primarily used for writing test cases.
      • apis

        protected java.util.List<Api> apis
        The Apis 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 starting has been set to true but before the Wirer is run or any Apis have been started.

      • startup

        public Engine startup()
        Runs the Wirer and calls startupApi for each Api.

        An Engine can only be started once. Any calls to startup after 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 Engine
        params - 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 Engine
        queryTerms - 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 Engine
        body - 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 Engine
        body - 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 Engine
        body - 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 DELETED
        hrefs - 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 operation
        url - 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 operation
        url - 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 operation
        url - the url that will be serviced by this Engine.
        body - a stringified JSON body presumably to PUT/POST/PATCH
        params - 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 EngineServlet or by Actions 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 Request
        res - 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)
        Registers listener to 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)
      • withApi

        public Engine withApi​(Api api)
      • 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)
      • withFilters

        public Engine withFilters​(Action... filters)
      • 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)