- java.lang.Object
-
- aeonics.entity.Entity
-
- aeonics.http.Endpoint.Type
-
- aeonics.http.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 Summary
Constructors Constructor Description Type()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <T extends Endpoint.Rest.Type>
Tafter(Functions.TriConsumer<Data,User.Type,Data> handler)
Sets a handler to intercept the response after it has been generated by this endpoint.<T extends Endpoint.Rest.Type>
Tafter(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.<T extends Endpoint.Rest.Type>
Tbefore(Functions.BiConsumer<Data,User.Type> handler)
Sets a handler to intercept the request before it is processed by this endpoint.<T extends Endpoint.Rest.Type>
Tbefore(Functions.BiFunction<Data,User.Type,java.lang.Object> handler)
Sets a handler to intercept the request before it is processed by this endpoint.Data
collectAndValidateParameters(Message request)
Collects input parameters based on the original message data.<T extends Endpoint.Rest.Type>
Terror(Functions.TriConsumer<Data,User.Type,java.lang.Exception> handler)
Sets an error handler for this endpoint.<T extends Endpoint.Rest.Type>
Terror(Functions.TriFunction<Data,User.Type,java.lang.Exception,java.lang.Object> handler)
Sets an error handler for this endpoint.boolean
matches(java.lang.String url)
Returns true if the provided URL matches this endpointData
process()
Processes the request and generates a response.Data
process(Data params)
Processes the request and generates a response.Data
process(Data params, User.Type user)
Processes the request and generates a response.Data
process(Data params, User.Type user, Message request)
Processes the request and generates a response.Data
process(Message request)
Processes the request and generates a response.<T extends Endpoint.Rest.Type>
Tprocess(Functions.BiFunction<Data,User.Type,java.lang.Object> processor)
Sets the process function that will replace theprocess(Data, User.Type)
method.<T extends Endpoint.Rest.Type>
Tprocess(Functions.Function<Data,java.lang.Object> processor)
Sets the process function that will replace theprocess(Data)
method.<T extends Endpoint.Rest.Type>
Tprocess(Functions.Supplier<java.lang.Object> processor)
Sets the process function that will replace theprocess()
method.<T extends Endpoint.Rest.Type>
Tprocess(Functions.TriFunction<Data,User.Type,Message,java.lang.Object> processor)
Sets the process function that will replace theprocess(Data, User.Type, Message)
method.<T extends Endpoint.Type>
Turl(java.lang.String value)
Sets the URL of this endpoint-
Methods inherited from class aeonics.http.Endpoint.Type
category, export, method, method, name, name, url
-
Methods inherited from class aeonics.entity.Entity
addRelation, addRelation, addRelation, cast, clearRelation, config, defineRelation, equals, firstRelation, hashCode, hasRelation, id, internal, onCreate, onRemove, onUpdate, parameter, relations, relationships, removeRelation, snapshot, type, valueOf, valueOf
-
-
-
-
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 classEndpoint.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 classEndpoint.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>
- The first argument represents the http request parameters. It may be null if the error is triggered while parsing the parameters.
- The second argument is the authenticated user, or
User.ANONYMOUS
- The third argument is the error thrown
- 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>
- The first argument represents the http request parameters
- The second argument is the authenticated user
- 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>
- The first argument represents the http request parameters
- The second argument is the authenticated user
- The third argument is the response generated by this endpoint
- 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
anderror
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 classEndpoint.Type
- Parameters:
request
- the original message data- Returns:
- the generated response
- Throws:
java.lang.Exception
- if anything happens, the exception will be wrapped in anHttpException
-
process
public <T extends Endpoint.Rest.Type> T process(Functions.TriFunction<Data,User.Type,Message,java.lang.Object> processor)
Sets the process function that will replace theprocess(Data, User.Type, Message)
method.- Type Parameters:
T
- this- Parameters:
processor
- the process function with 3 input parameters- Returns:
- this
-
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 parametersuser
- the associated userrequest
- the original message data- Returns:
- the endpoint response
- Throws:
java.lang.Exception
- if anything happens, the exception will be wrapped in anHttpException
- See Also:
process(Message)
-
process
public <T extends Endpoint.Rest.Type> T process(Functions.BiFunction<Data,User.Type,java.lang.Object> processor)
Sets the process function that will replace theprocess(Data, User.Type)
method.- Type Parameters:
T
- this- Parameters:
processor
- the process function with 2 input parameters- Returns:
- this
-
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 parametersuser
- the associated user- Returns:
- the endpoint response
- Throws:
java.lang.Exception
- if anything happens, the exception will be wrapped in anHttpException
- 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 theprocess(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 anHttpException
- 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 theprocess()
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 anHttpException
- 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'sEntity.parameters()
.The parameters are collected regardless of their provenance in the original request. The order of precedence is first
GET
parameters, thenPOST
parameters, thenFILES
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
-
-