Module aeonics.http
Package aeonics.http

Class Endpoint.Rest.Type

  • All Implemented Interfaces:
    Exportable, Snapshotable
    Enclosing class:
    Endpoint.Rest

    public static class Endpoint.Rest.Type
    extends Endpoint.Type
    This is the base REST endpoint type.

    Unless overridden, all instances of this class are marked as internal in the constructor.

    • Constructor Detail

      • Type

        public Type()
    • Method Detail

      • url

        public <T extends Endpoint.Type> T url​(java.lang.String value)
        Description copied from class: Endpoint.Type
        Sets the URL of this endpoint
        Overrides:
        url in class Endpoint.Type
        Type Parameters:
        T - this endpoint type
        Parameters:
        value - the URL of this endpoint
        Returns:
        this
      • matches

        public boolean matches​(java.lang.String url)
        Returns true if the provided URL matches this endpoint
        Overrides:
        matches in class Endpoint.Type
        Parameters:
        url - the URL to compare
        Returns:
        true if the provided URL matches this endpoint
      • error

        public <T extends Endpoint.Rest.Type> T error​(Functions.TriFunction<Data,​User.Type,​java.lang.Exception,​java.lang.Object> handler)
        Sets an error handler for this endpoint. This is the opportunity to catch the error and return another response instead.

        The handler has the following signature: TriFunction<Data, User.Type, Exception, Data>

        1. The first argument represents the http request parameters. It may be null if the error is triggered while parsing the parameters.
        2. The second argument is the authenticated user, or User.ANONYMOUS
        3. The third argument is the error thrown
        4. The handler should return a new response, or null to propagate the error in the response

        If the handler throws an exception, it has precedence over the endpoint error and will be sent as a response instead. The output from this handler will be processed by the after handler.

        Type Parameters:
        T - this
        Parameters:
        handler - the error handler
        Returns:
        this
      • error

        public <T extends Endpoint.Rest.Type> T error​(Functions.TriConsumer<Data,​User.Type,​java.lang.Exception> handler)
        Sets an error handler for this endpoint. The consumer cannot return a value to override the response, but may throw another exception if needed.
        Type Parameters:
        T - this
        Parameters:
        handler - the error handler
        Returns:
        this
        See Also:
        error(TriFunction)
      • before

        public <T extends Endpoint.Rest.Type> T before​(Functions.BiFunction<Data,​User.Type,​java.lang.Object> handler)
        Sets a handler to intercept the request before it is processed by this endpoint.

        It is the place to perform precondition or security checks and alter the input parameters if needed. The handler has the following signature: BiFunction<Data, User.Type, Data>

        1. The first argument represents the http request parameters
        2. The second argument is the authenticated user
        3. The handler may return a totally new request data object, or null to keep the original

        It is allowed for the handler to modify the original request data in-place and return null afterwards. If the handler throws an exception, it will proceed through the error handler.

        Type Parameters:
        T - this
        Parameters:
        handler - the before processing handler
        Returns:
        this
      • before

        public <T extends Endpoint.Rest.Type> T before​(Functions.BiConsumer<Data,​User.Type> handler)
        Sets a handler to intercept the request before it is processed by this endpoint. The consumer cannot return a value to override the request, but may still modify it in-place or throw an exception if needed.
        Type Parameters:
        T - this
        Parameters:
        handler - the before processing handler
        Returns:
        this
        See Also:
        before(BiFunction)
      • after

        public <T extends Endpoint.Rest.Type> T after​(Functions.TriFunction<Data,​User.Type,​Data,​java.lang.Object> handler)
        Sets a handler to intercept the response after it has been generated by this endpoint.

        It is the place to perform output filtering before sending the response. The handler has the following signature: TriFunction<Data, User.Type, Data, Data>

        1. The first argument represents the http request parameters
        2. The second argument is the authenticated user
        3. The third argument is the response generated by this endpoint
        4. The handler may return a totally new response data object, or null to keep the original

        It is allowed for the handler to modify the original response data in-place and return null afterwards. If the handler throws an exception, it will be directly sent as a response instead without proceeding through the error handler.

        Type Parameters:
        T - this
        Parameters:
        handler - the after processing handler
        Returns:
        this
      • after

        public <T extends Endpoint.Rest.Type> T after​(Functions.TriConsumer<Data,​User.Type,​Data> handler)
        Sets a handler to intercept the response after it has been generated by this endpoint. The consumer cannot return a value to override the response, but may still modify it in-place or throw an exception if needed.
        Type Parameters:
        T - this
        Parameters:
        handler - the after processing handler
        Returns:
        this
        See Also:
        after(TriFunction)
      • process

        public final Data process​(Message request)
                           throws java.lang.Exception
        Processes the request and generates a response.

        This method will collect the parameters and execute the before, after and error handlers as expected.

        The expected input message request content shall contain:

        • method: the http method
        • path: the full path url
        • get: decoded key-value pair query string parameters
        • post: decoded key-value pair body parameters
        • files: the key-value pair uploaded files with the parameter name as key and the following value
          • name: the uploaded file name
          • mime: the uploaded file mime type
          • content: the uploaded file content
        • body: the raw request body
        • headers: decoded key-value pair headers

        The generated response can be any of the following:

        • null: a 204 response is sent
        • an exception: it is the same as if you throw it
        • a scalar data type: it is sent as-is with an 'application/octet-stream' mime type and a code 200
        • a list or map data type: it is converted to its JSON representation and sent with an 'application/json' mime type and a code 200
        • a special response data map containing:
          • isHttpResponse [required]: true [required]
          • code: the http status code to send. Default 200.
          • status: the http status text. If not specified, it will be derived from the response code.
          • mime: the mime type to send. Default 'application/octet-stream'.
          • body: the content to send (if a data map or list, then its JSON representation is used). Default to "".
          • headers: any http response headers to send as a data map.
        Specified by:
        process in class Endpoint.Type
        Parameters:
        request - the original message data
        Returns:
        the generated response
        Throws:
        java.lang.Exception - if anything happens, the exception will be wrapped in an HttpException
      • process

        public Data process​(Data params,
                            User.Type user,
                            Message request)
                     throws java.lang.Exception
        Processes the request and generates a response. You may override this method if you only need the validated input parameters but also the original message data.
        Parameters:
        params - the validated input parameters
        user - the associated user
        request - the original message data
        Returns:
        the endpoint response
        Throws:
        java.lang.Exception - if anything happens, the exception will be wrapped in an HttpException
        See Also:
        process(Message)
      • process

        public Data process​(Data params,
                            User.Type user)
                     throws java.lang.Exception
        Processes the request and generates a response. You may override this method if you only need the validated input parameters.
        Parameters:
        params - the validated input parameters
        user - the associated user
        Returns:
        the endpoint response
        Throws:
        java.lang.Exception - if anything happens, the exception will be wrapped in an HttpException
        See Also:
        process(Message)
      • process

        public <T extends Endpoint.Rest.Type> T process​(Functions.Function<Data,​java.lang.Object> processor)
        Sets the process function that will replace the process(Data) method.
        Type Parameters:
        T - this
        Parameters:
        processor - the process function with 1 input parameter
        Returns:
        this
      • process

        public Data process​(Data params)
                     throws java.lang.Exception
        Processes the request and generates a response. You may override this method if you only need the validated input parameters.
        Parameters:
        params - the validated input parameters
        Returns:
        the endpoint response
        Throws:
        java.lang.Exception - if anything happens, the exception will be wrapped in an HttpException
        See Also:
        process(Message)
      • process

        public <T extends Endpoint.Rest.Type> T process​(Functions.Supplier<java.lang.Object> processor)
        Sets the process function that will replace the process() method.
        Type Parameters:
        T - this
        Parameters:
        processor - the process function without input parameters
        Returns:
        this
      • process

        public Data process()
                     throws java.lang.Exception
        Processes the request and generates a response. You may override this method if you do not need any input parameters.
        Returns:
        the endpoint response
        Throws:
        java.lang.Exception - if anything happens, the exception will be wrapped in an HttpException
        See Also:
        process(Message)
      • collectAndValidateParameters

        public Data collectAndValidateParameters​(Message request)
                                          throws java.lang.Exception
        Collects input parameters based on the original message data. All parameters are validated according to this instance's Entity.parameters().

        The parameters are collected regardless of their provenance in the original request. The order of precedence is first GET parameters, then POST parameters, then FILES parameters.

        If the parameter is not found in the request, the configured default value is used.

        Parameters:
        request - the original message data
        Returns:
        a key-value pair of all parameters and their value
        Throws:
        java.lang.Exception - if any parameter validation fails or if the input message is malformed