Initializer

Initializer is available starting from RESTHeart 3.3

You can have a preview from the current 3.3.8-SNAPSHOT version, download it from sonatype

An initializer is a class with the method init() that is invoked at RESTHeart startup.

It can be used to perform initialization code. For instance, it can programmatically add Transformers and Checkers and init the db.

The Initializer class can be specified in the configuration file

### Initializer

# A custom initializer implmenting the org.restheart.init.Initializer interface
# Can be used to inizialize data or add global transformers, checkers or security predicates
# See org.restheart.init.TestInitializer for a simple example

initializer-class: org.restheart.init.TestInitializer

For an example look at org.restheart.init.TestInitializer

The class must implement the Initializer interface

package org.restheart.init;

public interface Initializer {
    public void init();
}

Best practices

Get the MongoClient

// get the MongoClient
MongoClient client = MongoDBClientSingleton.getInstance().getClient();

Add Global Transformers

Global Transformers are applied to all requests.

See Request Transformers for more information on GlobalTransformer

GlobalTranformer globalTranformer;

// transform the request
RequestTransformerHandler.getGlobalTransformers().add(globalTranformer);

// transform the response
ResponseTransformerHandler.getGlobalTransformers().add(globalTranformer);

Add Global Checkers

Global Checkers are applied to all requests.

See Request Checkers for more information on GlobalChecker.

// check the request
GlobalChecker globalChecker;

CheckerHandler.getGlobalCheckers().add(globalChecker);

Add Global Security Predicates

Global Security Predicates are applied to all requests.

// allow users with role "ADMIN" to GET /
RequestContextPredicate securityPredicate = new RequestContextPredicate() {
            @Override
            public boolean resolve(HttpServerExchange hse, RequestContext context) {
                return context.isRoot()
                        && context.isGet()
                        && context.getAuthenticatedAccount() != null
                        && context.getAuthenticatedAccount().getRoles().contains("ADMIN");
            }
        }

// add the global predicate
AccessManagerHandler.getGlobalSecurityPredicates().add(securityPredicate);