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

serendip.struts.plugins.thymeleaf.spring.SpringWebContext Maven / Gradle / Ivy

There is a newer version: 1.2.0-RELEASE
Show newest version
/**
 * 
 */
package serendip.struts.plugins.thymeleaf.spring;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.context.ApplicationContext;
import org.thymeleaf.context.AbstractContext;
import org.thymeleaf.context.IWebContext;

/**
 * @author A-pZ
 *
 */
public class SpringWebContext extends AbstractContext implements IWebContext {

    public static final String BEANS_VARIABLE_NAME = "beans";
    private static final ConcurrentHashMap> variableMapPrototypes =
            new ConcurrentHashMap>();
    
    private final ApplicationContext applicationContext;
    private final HttpServletRequest httpServletRequest;
    private final HttpServletResponse httpServletResponse;
    private final ServletContext servletContext;


    /**
     * 

* Creates a new instance of a SpringWebContext. *

* * @param request the request object * @param response the response object * @param servletContext the servlet context * @param locale the locale * @param variables the variables to be included into the context * @param appctx the Spring application context */ public SpringWebContext(final HttpServletRequest request, final HttpServletResponse response, final ServletContext servletContext , final Locale locale, final Map variables, final ApplicationContext appctx) { //super(request, response, servletContext, locale, addSpringSpecificVariables(variables, appctx)); super(locale,addSpringSpecificVariables(variables, appctx)); this.httpServletRequest = request; this.httpServletResponse = response; this.servletContext = servletContext; this.applicationContext = appctx; } @SuppressWarnings("unchecked") private static Map addSpringSpecificVariables(final Map variables, final ApplicationContext appctx) { HashMap variableMapPrototype = variableMapPrototypes.get(appctx); if (variableMapPrototype == null) { variableMapPrototype = new HashMap(20, 1.0f); // We will use a singleton-per-appctx Beans instance, and that's alright final Beans beans = new Beans(appctx); variableMapPrototype.put(BEANS_VARIABLE_NAME, beans); variableMapPrototypes.put(appctx, variableMapPrototype); } final Map newVariables; synchronized (variableMapPrototype) { newVariables = (Map) variableMapPrototype.clone(); } if (variables != null) { newVariables.putAll(variables); } return newVariables; } public ApplicationContext getApplicationContext() { return this.applicationContext; } @Override public HttpServletRequest getRequest() { return this.httpServletRequest; } @Override public HttpServletResponse getResponse() { return this.httpServletResponse; } @Override public HttpSession getSession() { return this.httpServletRequest.getSession(false); } @Override public ServletContext getServletContext() { return this.servletContext; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy