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

org.thymeleaf.context.WebContext Maven / Gradle / Ivy

Go to download

Modern server-side Java template engine for both web and standalone environments

There is a newer version: 3.1.3.RELEASE
Show newest version
/*
 * =============================================================================
 * 
 *   Copyright (c) 2011, The THYMELEAF team (http://www.thymeleaf.org)
 * 
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 * 
 * =============================================================================
 */
package org.thymeleaf.context;

import java.util.Calendar;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

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

import org.thymeleaf.util.Validate;

/**
 * 

* Standard implementation for the {@link IWebContext} interface. *

*

* This {@link IContext} implementation uses a {@link WebContextExecutionInfo} object as its * {@link IContextExecutionInfo} implementation. *

* * @author Daniel Fernández * * @since 1.0 * */ public class WebContext extends AbstractContext implements IWebContext { /** *

* Name of the context variable that contains the request parameters. *

*/ public static final String PARAM_VARIABLE_NAME = "param"; /** *

* Name of the context variable that contains the session attributes. *

*/ public static final String SESSION_VARIABLE_NAME = "session"; /** *

* Name of the context variable that contains the application (servlet context) * attributes. *

*/ public static final String APPLICATION_VARIABLE_NAME = "application"; private final ServletContext servletContext; private final String contextPath; private final String sessionID; private final boolean sessionIdFromCookie; private final VariablesMap requestParameters; private final VariablesMap requestAttributes; private final VariablesMap sessionAttributes; private final VariablesMap applicationAttributes; /** *

* Create an instance without specifying a locale. Using this constructor, * the default locale (Locale.getDefault()) will be used. *

* * @param request the {@link HttpServletRequest} that this context will be related to. */ public WebContext(final HttpServletRequest request) { this(request, Locale.getDefault()); } /** *

* Create an instance specifying a locale. *

* * @param request the {@link HttpServletRequest} that this context will be related to. * @param locale the locale to be used. */ public WebContext(final HttpServletRequest request, final Locale locale) { this(request, locale, new HashMap()); } /** *

* Create an instance specifying a locale and an initial set of context * variables. *

* * @param request the {@link HttpServletRequest} that this context will be related to. * @param locale the locale to be used. * @param variables the initial set of context variables. */ public WebContext(final HttpServletRequest request, final Locale locale, final Map variables) { super(locale, variables); Validate.notNull(locale, "Locale cannot be null"); Validate.notNull(request, "Request cannot be null"); final HttpSession session = request.getSession(true); this.servletContext = session.getServletContext(); this.contextPath = request.getContextPath(); this.sessionID = session.getId(); this.sessionIdFromCookie = request.isRequestedSessionIdFromCookie(); final Map totalVariables = new HashMap(); final VariablesMap requestAttributesMap = new VariablesMap(); final Enumeration attributeNames = request.getAttributeNames(); while (attributeNames.hasMoreElements()) { final String attributeName = (String) attributeNames.nextElement(); requestAttributesMap.put(attributeName, request.getAttribute(attributeName)); } totalVariables.putAll(requestAttributesMap); final VariablesMap requestParametersMap = new VariablesMap(); final Enumeration requestParameterNames = request.getParameterNames(); while (requestParameterNames.hasMoreElements()) { final String requestParameterName = (String) requestParameterNames.nextElement(); final String[] requestParameterValues = request.getParameterValues(requestParameterName); requestParametersMap.put(requestParameterName, requestParameterValues); } totalVariables.put(PARAM_VARIABLE_NAME, requestParametersMap); final VariablesMap sessionAttributesMap = new VariablesMap(); final Enumeration sessionAttributeNames = session.getAttributeNames(); while (sessionAttributeNames.hasMoreElements()) { final String sessionAttributeName = (String) sessionAttributeNames.nextElement(); sessionAttributesMap.put(sessionAttributeName, session.getAttribute(sessionAttributeName)); } totalVariables.put(SESSION_VARIABLE_NAME, sessionAttributesMap); final VariablesMap applicationAttributesMap = new VariablesMap(); final Enumeration applicationAttributeNames = this.servletContext.getAttributeNames(); while (applicationAttributeNames.hasMoreElements()) { final String applicationAttributeName = (String) applicationAttributeNames.nextElement(); applicationAttributesMap.put(applicationAttributeName, this.servletContext.getAttribute(applicationAttributeName)); } totalVariables.put(APPLICATION_VARIABLE_NAME, applicationAttributesMap); setVariables(totalVariables); this.requestParameters = requestParametersMap; this.requestAttributes = requestAttributesMap; this.sessionAttributes = sessionAttributesMap; this.applicationAttributes = applicationAttributesMap; } public ServletContext getServletContext() { return this.servletContext; } public String getContextPath() { return this.contextPath; } public String getSessionID() { return this.sessionID; } public boolean isSessionIdFromCookie() { return this.sessionIdFromCookie; } public VariablesMap getRequestParameters() { return this.requestParameters; } public VariablesMap getRequestAttributes() { return this.requestAttributes; } public VariablesMap getSessionAttributes() { return this.sessionAttributes; } public VariablesMap getApplicationAttributes() { return this.applicationAttributes; } @Override protected IContextExecutionInfo buildContextExecutionInfo(final String templateName) { final Calendar now = Calendar.getInstance(); return new WebContextExecutionInfo(templateName, now); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy