Hooks

Hooks intercept the request execution flow at some important stages and let you customize the default behavior of the YAWP! APIs.

For instance, you can intercept entities right before they are saved and change some of its attributes. Another example is that you can intercept queries and force them to have a specific filter, order, limit, etc.

To create hooks, you need to extend the Hook class and override its methods. If the generic type of you hook is Object, then this hook will intercept the flow of all endpoint models.

This is a example of the Hook feature for the User endpoint model:

public class UserHook extends Hook<User> {

    @Override
    public void beforeQuery(DatastoreQuery<User> q) {
        q.where("company", "=", Session.getLoggedUser().getCompany());
    }

    @Override
    public void beforeSave(User user) {
        if (user.getAge() < 18) {
            throw new HttpException(422, "You must be 18 or more to sign up.");
        }
    }
}

The complete list of events that you can intercept is:

Method Flow stage May be used to…
beforeShield Before security is applied set entity information before the security shield is applied. For instance, it can be used to set the current logged user as the owner of the model.
beforeQuery Before every query is executed add mandatory filter, order or limit to queries based on the current logged user.
beforeSave Right before the entity is saved pre-calculate or cache some values in the entity, as well as make validations before saving.
afterSave Right after the entity is saved trigger asynchronous actions or log events.
beforeDestroy Right before the entity is destroyed do things same as beforeSave.
afterDestroy Right after the entity is destroyed do things same as afterSave.

Exceptions

As you could see in the example above, to interrupt the flow and create the request response with an error information, just throw a HttpException.