All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.wimdeblauwe.htmx.spring.boot.thymeleaf.HtmxExpressionObjectFactory Maven / Gradle / Ivy

There is a newer version: 3.6.1
Show newest version
package io.github.wimdeblauwe.htmx.spring.boot.thymeleaf;

import io.github.wimdeblauwe.htmx.spring.boot.mvc.HtmxHandlerMethodArgumentResolver;
import jakarta.servlet.http.HttpServletRequest;
import org.thymeleaf.context.IExpressionContext;
import org.thymeleaf.context.IWebContext;
import org.thymeleaf.expression.IExpressionObjectFactory;
import org.thymeleaf.web.IWebExchange;
import org.thymeleaf.web.servlet.IServletWebRequest;

import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;

public class HtmxExpressionObjectFactory implements IExpressionObjectFactory {

    /*
     * Any new objects added here should also be added to the "ALL_EXPRESSION_OBJECT_NAMES" See below.
     */
    public static final String HTMX_REQUEST_EXPRESSION_OBJECT_NAME = "htmxRequest";
    public static final Set ALL_EXPRESSION_OBJECT_NAMES;

    static {
        final Set allExpressionObjectNames = new LinkedHashSet<>();
        allExpressionObjectNames.add(HTMX_REQUEST_EXPRESSION_OBJECT_NAME);

        ALL_EXPRESSION_OBJECT_NAMES = Collections.unmodifiableSet(allExpressionObjectNames);
    }

    public Set getAllExpressionObjectNames() {
        return ALL_EXPRESSION_OBJECT_NAMES;
    }

    public Object buildObject(final IExpressionContext context, final String expressionObjectName) {
        if (HTMX_REQUEST_EXPRESSION_OBJECT_NAME.equals(expressionObjectName) && context instanceof IWebContext webContext) {
            IWebExchange exchange = webContext.getExchange();
            IServletWebRequest request = (IServletWebRequest) exchange.getRequest();
            return HtmxHandlerMethodArgumentResolver.createHtmxRequest((HttpServletRequest) request.getNativeRequestObject());
        }

        return null;
    }

    @Override
    public boolean isCacheable(String expressionObjectName) {
        return HTMX_REQUEST_EXPRESSION_OBJECT_NAME.equals(expressionObjectName);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy