Skip to content

1.0.1 Intercepting executions in the controller

Anders Mikkelsen edited this page Dec 14, 2017 · 1 revision

The controller can be overriden at any point in its execution to perform particular business logic. When overriding it's methods, remember to call the next method in the chain to continue execution. This enables async operations at any point in execution.

Intercept methods:

Show

default void preShow(RoutingContext routingContext) {
    performShow(routingContext);
}

Index

default void preIndex(RoutingContext routingContext, String customQuery) {
    prepareQuery(routingContext, customQuery);
}

default void preProcessQuery(RoutingContext routingContext, Map<String, List<String>> queryMap) {
    processQuery(routingContext, queryMap);
}

default void postProcessQuery(RoutingContext routingContext, AggregateFunction aggregateFunction,
                              Queue<OrderByParameter> orderByQueue, Map<String, List<FilterParameter>> params,
                              @Nonnull String[] projections, String indexName, Integer limit) {
    postPrepareQuery(routingContext, aggregateFunction, orderByQueue, params, projections, indexName, limit);
}

default void postPrepareQuery(RoutingContext routingContext, AggregateFunction aggregateFunction,
                              Queue<OrderByParameter> orderByQueue, Map<String, List<FilterParameter>> params,
                              String[] projections, String indexName, Integer limit) {
    createIdObjectForIndex(routingContext, aggregateFunction, orderByQueue, params, projections, indexName, limit);
}

Create

default void preCreate(RoutingContext routingContext) {
    if (denyQuery(routingContext)) return;

    parseBodyForCreate(routingContext);
}

default void preVerifyNotExists(E newRecord, RoutingContext routingContext) {
    verifyNotExists(newRecord, routingContext);
}

default void postVerifyNotExists(E newRecord, RoutingContext routingContext) {
    preSetIdentifiers(newRecord, routingContext);
}

default void preSetIdentifiers(E newRecord, RoutingContext routingContext) {
    setIdentifiers(newRecord, routingContext);
}

default void preSanitizeForCreate(E record, RoutingContext routingContext) {
    performSanitizeForCreate(record, routingContext);
}

default void performSanitizeForCreate(E record, RoutingContext routingContext) {
    record.sanitize();

    postSanitizeForCreate(record, routingContext);
}

default void postSanitizeForCreate(E record, RoutingContext routingContext) {
    preValidateForCreate(record, routingContext);
}

default void preValidateForCreate(E record, RoutingContext routingContext) {
    performValidateForCreate(record, routingContext);
}

default void postValidateForCreate(E record, RoutingContext routingContext) {
    performCreate(record, routingContext);
}

default void postCreate(@Nonnull E createdRecord, RoutingContext routingContext) {
    long initialNanoTime = routingContext.get(REQUEST_PROCESS_TIME_TAG);

    routingContext.response().putHeader(HttpHeaders.CONTENT_TYPE, "application/json; charset=utf-8");
    routingContext.put(BODY_CONTENT_TAG, createdRecord.toJsonString());
    setStatusCodeAndContinue(201, routingContext, initialNanoTime);
}

Update

default void preUpdate(RoutingContext routingContext) {
    if (denyQuery(routingContext)) return;

    parseBodyForUpdate(routingContext);
}

default void preVerifyExistsForUpdate(E newRecord, RoutingContext routingContext) {
    verifyExistsForUpdate(newRecord, routingContext);
}

default void postVerifyExistsForUpdate(E oldRecord, E newRecord, RoutingContext routingContext) {
    preSanitizeForUpdate(oldRecord, newRecord, routingContext);
}

default void preSanitizeForUpdate(E record, E newRecord, RoutingContext routingContext) {
    performSanitizeForUpdate(record, newRecord, routingContext);
}

default void performSanitizeForUpdate(E record, E newRecord, RoutingContext routingContext) {
    Function<E, E> setNewValues = rec -> {
        rec.setModifiables(newRecord);
        rec.sanitize();

        return rec;
    };

    postSanitizeForUpdate(setNewValues.apply(record), setNewValues, routingContext);
}

default void postSanitizeForUpdate(E record, Function<E, E> setNewValues, RoutingContext routingContext) {
    preValidateForUpdate(record, setNewValues, routingContext);
}

default void preValidateForUpdate(E record, Function<E, E> setNewValues, RoutingContext routingContext) {
    performValidateForUpdate(record, setNewValues, routingContext);
}

default void postValidateForUpdate(E record, Function<E, E> setNewValues, RoutingContext routingContext) {
    performUpdate(record, setNewValues, routingContext);
}

void performUpdate(E updatedRecord, Function<E, E> setNewValues, RoutingContext routingContext);

default void postUpdate(@Nonnull E updatedRecord, RoutingContext routingContext) {
    long initialNanoTime = routingContext.get(REQUEST_PROCESS_TIME_TAG);

    routingContext.response().putHeader(HttpHeaders.CONTENT_TYPE, "application/json; charset=utf-8");
    routingContext.put(BODY_CONTENT_TAG, updatedRecord.toJsonString());
    setStatusCodeAndContinue(200, routingContext, initialNanoTime);
}

Delete

default void preDestroy(RoutingContext routingContext) {
    if (denyQuery(routingContext)) return;

    verifyExistsForDestroy(routingContext);
}

void verifyExistsForDestroy(RoutingContext routingContext);

default void postVerifyExistsForDestroy(E recordForDestroy, RoutingContext routingContext) {
    performDestroy(recordForDestroy, routingContext);
}

default void postDestroy(@Nonnull E destroyedRecord, RoutingContext routingContext) {
    long initialNanoTime = routingContext.get(REQUEST_PROCESS_TIME_TAG);

    setStatusCodeAndContinue(204, routingContext, initialNanoTime);
}