
org.thymeleaf.context.WebContext Maven / Gradle / Ivy
Show all versions of thymeleaf Show documentation
/*
* =============================================================================
*
* 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
* @author Josh Long
* @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 HttpServletRequest httpServletRequest;
private final HttpSession httpSession;
private final ServletContext servletContext;
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.
* @deprecated Will be removed in 1.2.0. Use the version of this constructor receiving
* also the {@link ServletContext} instead.
*
*/
@Deprecated
public WebContext(final HttpServletRequest request) {
this(request, request.getSession(true).getServletContext(), 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.
* @deprecated Will be removed in 1.2.0. Use the version of this constructor receiving
* also the {@link ServletContext} instead.
*/
@Deprecated
public WebContext(final HttpServletRequest request, final Locale locale) {
this(request, request.getSession(true).getServletContext(), 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.
* @deprecated Will be removed in 1.2.0. Use the version of this constructor receiving
* also the {@link ServletContext} instead.
*/
@Deprecated
public WebContext(final HttpServletRequest request,
final Locale locale, final Map variables) {
this(request, request.getSession(true).getServletContext(), locale, variables);
}
/**
*
* Create an instance without specifying a locale. Using this constructor,
* the default locale (Locale.getDefault()) will be used.
*
*
* @since 1.1.3
* @param request the {@link HttpServletRequest} that this context will be related to.
* @param servletContext the servlet context object
*/
public WebContext(final HttpServletRequest request, final ServletContext servletContext) {
this(request, servletContext, Locale.getDefault());
}
/**
*
* Create an instance specifying a locale.
*
*
* @since 1.1.3
* @param request the {@link HttpServletRequest} that this context will be related to.
* @param servletContext the servlet context object
* @param locale the locale to be used.
*/
public WebContext(final HttpServletRequest request, final ServletContext servletContext, final Locale locale) {
this(request, servletContext, locale, new HashMap());
}
/**
*
* Create an instance specifying a locale and an initial set of context
* variables.
*
*
* @since 1.1.3
* @param request the {@link HttpServletRequest} that this context will be related to.
* @param servletContext the servlet context object
* @param locale the locale to be used.
* @param variables the initial set of context variables.
*/
public WebContext(final HttpServletRequest request,
final ServletContext servletContext,
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(false);
this.httpServletRequest = request;
this.httpSession = session;
this.servletContext = servletContext;
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();
if (null != session) {
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 HttpServletRequest getHttpServletRequest() {
return this.httpServletRequest;
}
public HttpSession getHttpSession() {
return this.httpSession;
}
public ServletContext getServletContext() {
return this.servletContext;
}
public String getContextPath() {
return this.httpServletRequest.getContextPath();
}
public String getSessionID() {
return this.httpSession.getId();
}
public boolean isSessionIdFromCookie() {
// We force the obtention of the session id beforehand
this.httpSession.getId();
return this.httpServletRequest.isRequestedSessionIdFromCookie();
}
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);
}
}