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

org.zodiac.template.velocity.view.reactive.ReactiveViewToolContext Maven / Gradle / Ivy

package org.zodiac.template.velocity.view.reactive;

import java.util.List;

import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.context.Context;
import org.apache.velocity.tools.ToolContext;
import org.apache.velocity.tools.Toolbox;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.lang.NonNull;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebSession;
import org.zodiac.commons.util.web.ReactiveRequests;
import org.zodiac.sdk.toolkit.util.AssertUtil;
import org.zodiac.template.velocity.constants.VelocityTemplateConstants;

import reactor.core.publisher.Mono;

public class ReactiveViewToolContext extends ToolContext {

    private final ServerWebExchange exchange;
    private final VelocityEngine velocity;
    private String toolboxKey = VelocityTemplateConstants.DEFAULT_TOOLBOX_KEY;

    public ReactiveViewToolContext(VelocityEngine velocity, @NonNull ServerWebExchange exchange) {
        super(velocity);

        this.velocity = velocity;
        this.exchange = AssertUtil.assertNotNull(exchange, "Exchange is reuired");
        // automagically set common tool properties
        putToolProperties();
    }

    public Context getVelocityContext() {
        return this;
    }

    /**
     * @return A reference to the VelocityEngine.
     */
    public VelocityEngine getVelocityEngine() {
        return velocity;
    }

    public ServerWebExchange getExchange() {
        return exchange;
    }

    public Mono getSession() {
        return getExchange().getSession();
    }

    public ServerHttpRequest getRequest() {
        return getExchange().getRequest();
    }

    public ServerHttpResponse getResponse() {
        return getExchange().getResponse();
    }

    @Override
    public Object get(String key) {
        boolean overwrite = getUserCanOverwriteTools();
        Object o = overwrite ? getUserVar(key) : getToolVar(key);
        if (o == null) {
            o = overwrite ? getToolVar(key) : getUserVar(key);
        }
        return o;
    }

    public Object getAttribute(String key) {
        Object o = null;
        if (getRequest() != null) {
            o = getExchange().getAttribute(key);
        }
        if (o == null) {
            WebSession session = getSession().block();
            if (session != null) {
                try {
                    o = session.getAttribute(key);
                } catch (IllegalStateException ise) {
                    // Handle invalidated session state
                    o = null;
                }
            }
        }
        return o;
    }

    @Override
    public boolean containsKey(String key) {
        return super.containsKey(key) || getAttribute(key) != null
            || key.equals(VelocityTemplateConstants.REQUEST) && getRequest() != null
            || key.equals(VelocityTemplateConstants.RESPONSE) && getResponse() != null
            || key.equals(VelocityTemplateConstants.SESSION) && getSession().block() != null;
    }

    @Override
    protected List getToolboxes() {
        // this means once we find any toolbox,
        // then we stop looking. adding boxes to
        // the request/session/application attributes
        // later will not work. once one is in, any
        // later additions must be via addToolbox(Toolbox)
        // or addToolboxesUnderKey(String toolboxKey)
        if (super.getToolboxes().isEmpty()) {
            addToolboxesUnderKey(this.toolboxKey);
        }
        return super.getToolboxes();
    }

    protected void setToolboxKey(String key) {
        this.toolboxKey = key;
    }

    protected void putToolProperties() {
        putToolProperty(VelocityTemplateConstants.REQUEST, getRequest());
        if (getRequest() != null) {
            putToolProperty(VelocityTemplateConstants.LOCALES, getRequest().getHeaders().getAcceptLanguageAsLocales());
            putToolProperty(VelocityTemplateConstants.LOCALE,
                getRequest().getHeaders().getAcceptLanguageAsLocales().get(0));
        }
        putToolProperty(VelocityTemplateConstants.RESPONSE, getResponse());
        putToolProperty(VelocityTemplateConstants.SESSION, getSession().block());
        if (getRequest() != null) {
            putToolProperty(VelocityTemplateConstants.REQUEST_PATH, ReactiveRequests.getRequestPath(getRequest()));
        }
    }

    protected void addToolboxesUnderKey(String toolboxKey) {
        if (getRequest() != null) {
            Toolbox reqTools = (Toolbox)getExchange().getAttribute(toolboxKey);
            if (reqTools != null) {
                addToolbox(reqTools);
            }
        }
        if (getSession() != null) {
            Toolbox sessTools = (Toolbox)getSession().block().getAttribute(toolboxKey);
            if (sessTools != null) {
                addToolbox(sessTools);
            }
        }
    }

    protected Object getUserVar(String key) {
        Object o = internalGet(key);
        if (o != null) {
            return o;
        }
        return getAttribute(key);
    }

    protected Object getToolVar(String key) {
        Object o = findTool(key);
        if (o != null) {
            return o;
        }
        return getServletApi(key);
    }

    protected Object getServletApi(String key) {
        if (key.equals(VelocityTemplateConstants.REQUEST)) {
            return getRequest();
        } else if (key.equals(VelocityTemplateConstants.RESPONSE)) {
            return getResponse();
        } else if (key.equals(VelocityTemplateConstants.SESSION)) {
            return getSession().block();
        } else if (key.equals(VelocityTemplateConstants.EXCHANGE)) {
            return getExchange();
        }
        return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy