Using :session predefined parameters in REST query (WITH SOLUTION)

I need to dynamically filter the results of a REST query based on the actual user is calling it, but it seems REST queries do not support predefined parameters like :session$userId.

Is it currently possible, or do I have to override the underlying QueriesControllerManager bean to automatically add this param before running the actual query?


Workaround (tested only in 6.6.4):

create and register these two beans in rest-dispatcher-spring.xml (change your namespace accordingly)

<bean id="cuba_RestQueriesConfiguration" class="com.myapp.restapi.CustomRestQueriesConfiguration"/>
<bean id="cuba_QueriesControllerManager" class="com.myapp.restapi.CustomQueriesControllerManager"/>

create these two new beans in your target namespace (I omitted the package declaration):

import com.haulmont.cuba.core.entity.Entity;
import com.haulmont.restapi.service.QueriesControllerManager;

import javax.annotation.Nullable;
import javax.inject.Inject;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class CustomQueriesControllerManager extends QueriesControllerManager {
    protected UserSessionSource userSessionSource;

    protected LoadContext<Entity> createQueryLoadContext(String entityName, String queryName, @Nullable Integer limit, @Nullable Integer offset, Map<String, String> params) throws ClassNotFoundException, ParseException {
        // add some predefined params, if available
        if (userSessionSource.checkCurrentUserSession()) {
            UUID userId = userSessionSource.getUserSession().getUser().getId();
            params = new HashMap<>(params);
            params.put("session$userId", userId.toString());
        return super.createQueryLoadContext(entityName, queryName, limit, offset, params);
import com.haulmont.restapi.config.RestQueriesConfiguration;
import org.dom4j.Element;

public class CustomRestQueriesConfiguration extends RestQueriesConfiguration {
    protected void loadConfig(Element rootElem) {
        // add predefined parameters if needed in loaded queries
        for (QueryInfo query : queries) {
            if (query.getJpql().contains(":session$userId")) {
                query.getParams().add(new QueryParamInfo("session$userId", "java.util.UUID"));

that’s it, you are now able to use :session$userId parameter without passing it explicitly, and without defining it in params collection (remember that a client can always pass that parameter explicitly, but you’re overriding it with put, cause params are a Map).

Hope this helps others,

Hi Paolo,

Thanks for your feedback, here is the link to an existing issue (for the reference).

1 Like

Thanks @minaev for the link.

In the meantime I changed my post by including an overridden RestQueriesConfiguration, so there’s no need to explicitly add the parameter to the params collection in rest-queries.xml.