Java Code Examples for org.springframework.context.i18n.LocaleContext#getLocale()

The following examples show how to use org.springframework.context.i18n.LocaleContext#getLocale() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RequestContext.java    From spring-analysis-note with MIT License 6 votes vote down vote up
public RequestContext(ServerWebExchange exchange, Map<String, Object> model, MessageSource messageSource,
		@Nullable RequestDataValueProcessor dataValueProcessor) {

	Assert.notNull(exchange, "ServerWebExchange is required");
	Assert.notNull(model, "Model is required");
	Assert.notNull(messageSource, "MessageSource is required");
	this.exchange = exchange;
	this.model = model;
	this.messageSource = messageSource;

	LocaleContext localeContext = exchange.getLocaleContext();
	Locale locale = localeContext.getLocale();
	this.locale = (locale != null ? locale : Locale.getDefault());
	TimeZone timeZone = (localeContext instanceof TimeZoneAwareLocaleContext ?
			((TimeZoneAwareLocaleContext) localeContext).getTimeZone() : null);
	this.timeZone = (timeZone != null ? timeZone : TimeZone.getDefault());

	this.defaultHtmlEscape = null;  // TODO
	this.dataValueProcessor = dataValueProcessor;
}
 
Example 2
Source File: CookieLocaleResolver.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public void setLocaleContext(HttpServletRequest request, @Nullable HttpServletResponse response,
		@Nullable LocaleContext localeContext) {

	Assert.notNull(response, "HttpServletResponse is required for CookieLocaleResolver");

	Locale locale = null;
	TimeZone timeZone = null;
	if (localeContext != null) {
		locale = localeContext.getLocale();
		if (localeContext instanceof TimeZoneAwareLocaleContext) {
			timeZone = ((TimeZoneAwareLocaleContext) localeContext).getTimeZone();
		}
		addCookie(response,
				(locale != null ? toLocaleValue(locale) : "-") + (timeZone != null ? '/' + timeZone.getID() : ""));
	}
	else {
		removeCookie(response);
	}
	request.setAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME,
			(locale != null ? locale : determineDefaultLocale(request)));
	request.setAttribute(TIME_ZONE_REQUEST_ATTRIBUTE_NAME,
			(timeZone != null ? timeZone : determineDefaultTimeZone(request)));
}
 
Example 3
Source File: CookieLocaleResolver.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public void setLocaleContext(HttpServletRequest request, HttpServletResponse response, LocaleContext localeContext) {
	Locale locale = null;
	TimeZone timeZone = null;
	if (localeContext != null) {
		locale = localeContext.getLocale();
		if (localeContext instanceof TimeZoneAwareLocaleContext) {
			timeZone = ((TimeZoneAwareLocaleContext) localeContext).getTimeZone();
		}
		addCookie(response, (locale != null ? locale : "-") + (timeZone != null ? ' ' + timeZone.getID() : ""));
	}
	else {
		removeCookie(response);
	}
	request.setAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME,
			(locale != null ? locale: determineDefaultLocale(request)));
	request.setAttribute(TIME_ZONE_REQUEST_ATTRIBUTE_NAME,
			(timeZone != null ? timeZone : determineDefaultTimeZone(request)));
}
 
Example 4
Source File: HttpComponentsHttpInvokerRequestExecutor.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Create a HttpPost for the given configuration.
 * <p>The default implementation creates a standard HttpPost with
 * "application/x-java-serialized-object" as "Content-Type" header.
 * @param config the HTTP invoker configuration that specifies the
 * target service
 * @return the HttpPost instance
 * @throws java.io.IOException if thrown by I/O methods
 */
protected HttpPost createHttpPost(HttpInvokerClientConfiguration config) throws IOException {
	HttpPost httpPost = new HttpPost(config.getServiceUrl());

	RequestConfig requestConfig = createRequestConfig(config);
	if (requestConfig != null) {
		httpPost.setConfig(requestConfig);
	}

	LocaleContext localeContext = LocaleContextHolder.getLocaleContext();
	if (localeContext != null) {
		Locale locale = localeContext.getLocale();
		if (locale != null) {
			httpPost.addHeader(HTTP_HEADER_ACCEPT_LANGUAGE, locale.toLanguageTag());
		}
	}

	if (isAcceptGzipEncoding()) {
		httpPost.addHeader(HTTP_HEADER_ACCEPT_ENCODING, ENCODING_GZIP);
	}

	return httpPost;
}
 
Example 5
Source File: SimpleHttpInvokerRequestExecutor.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Prepare the given HTTP connection.
 * <p>The default implementation specifies POST as method,
 * "application/x-java-serialized-object" as "Content-Type" header,
 * and the given content length as "Content-Length" header.
 * @param connection the HTTP connection to prepare
 * @param contentLength the length of the content to send
 * @throws IOException if thrown by HttpURLConnection methods
 * @see java.net.HttpURLConnection#setRequestMethod
 * @see java.net.HttpURLConnection#setRequestProperty
 */
protected void prepareConnection(HttpURLConnection connection, int contentLength) throws IOException {
	if (this.connectTimeout >= 0) {
		connection.setConnectTimeout(this.connectTimeout);
	}
	if (this.readTimeout >= 0) {
		connection.setReadTimeout(this.readTimeout);
	}

	connection.setDoOutput(true);
	connection.setRequestMethod(HTTP_METHOD_POST);
	connection.setRequestProperty(HTTP_HEADER_CONTENT_TYPE, getContentType());
	connection.setRequestProperty(HTTP_HEADER_CONTENT_LENGTH, Integer.toString(contentLength));

	LocaleContext localeContext = LocaleContextHolder.getLocaleContext();
	if (localeContext != null) {
		Locale locale = localeContext.getLocale();
		if (locale != null) {
			connection.setRequestProperty(HTTP_HEADER_ACCEPT_LANGUAGE, locale.toLanguageTag());
		}
	}

	if (isAcceptGzipEncoding()) {
		connection.setRequestProperty(HTTP_HEADER_ACCEPT_ENCODING, ENCODING_GZIP);
	}
}
 
Example 6
Source File: RequestContext.java    From java-technology-stack with MIT License 6 votes vote down vote up
public RequestContext(ServerWebExchange exchange, Map<String, Object> model, MessageSource messageSource,
		@Nullable RequestDataValueProcessor dataValueProcessor) {

	Assert.notNull(exchange, "ServerWebExchange is required");
	Assert.notNull(model, "Model is required");
	Assert.notNull(messageSource, "MessageSource is required");
	this.exchange = exchange;
	this.model = model;
	this.messageSource = messageSource;

	LocaleContext localeContext = exchange.getLocaleContext();
	Locale locale = localeContext.getLocale();
	this.locale = (locale != null ? locale : Locale.getDefault());
	TimeZone timeZone = (localeContext instanceof TimeZoneAwareLocaleContext ?
			((TimeZoneAwareLocaleContext) localeContext).getTimeZone() : null);
	this.timeZone = (timeZone != null ? timeZone : TimeZone.getDefault());

	this.defaultHtmlEscape = null;  // TODO
	this.dataValueProcessor = dataValueProcessor;
}
 
Example 7
Source File: SimpleHttpInvokerRequestExecutor.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Prepare the given HTTP connection.
 * <p>The default implementation specifies POST as method,
 * "application/x-java-serialized-object" as "Content-Type" header,
 * and the given content length as "Content-Length" header.
 * @param connection the HTTP connection to prepare
 * @param contentLength the length of the content to send
 * @throws IOException if thrown by HttpURLConnection methods
 * @see java.net.HttpURLConnection#setRequestMethod
 * @see java.net.HttpURLConnection#setRequestProperty
 */
protected void prepareConnection(HttpURLConnection connection, int contentLength) throws IOException {
	if (this.connectTimeout >= 0) {
		connection.setConnectTimeout(this.connectTimeout);
	}
	if (this.readTimeout >= 0) {
		connection.setReadTimeout(this.readTimeout);
	}

	connection.setDoOutput(true);
	connection.setRequestMethod(HTTP_METHOD_POST);
	connection.setRequestProperty(HTTP_HEADER_CONTENT_TYPE, getContentType());
	connection.setRequestProperty(HTTP_HEADER_CONTENT_LENGTH, Integer.toString(contentLength));

	LocaleContext localeContext = LocaleContextHolder.getLocaleContext();
	if (localeContext != null) {
		Locale locale = localeContext.getLocale();
		if (locale != null) {
			connection.setRequestProperty(HTTP_HEADER_ACCEPT_LANGUAGE, StringUtils.toLanguageTag(locale));
		}
	}

	if (isAcceptGzipEncoding()) {
		connection.setRequestProperty(HTTP_HEADER_ACCEPT_ENCODING, ENCODING_GZIP);
	}
}
 
Example 8
Source File: CookieLocaleResolver.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
public void setLocaleContext(HttpServletRequest request, @Nullable HttpServletResponse response,
		@Nullable LocaleContext localeContext) {

	Assert.notNull(response, "HttpServletResponse is required for CookieLocaleResolver");

	Locale locale = null;
	TimeZone timeZone = null;
	if (localeContext != null) {
		locale = localeContext.getLocale();
		if (localeContext instanceof TimeZoneAwareLocaleContext) {
			timeZone = ((TimeZoneAwareLocaleContext) localeContext).getTimeZone();
		}
		addCookie(response,
				(locale != null ? toLocaleValue(locale) : "-") + (timeZone != null ? '/' + timeZone.getID() : ""));
	}
	else {
		removeCookie(response);
	}
	request.setAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME,
			(locale != null ? locale : determineDefaultLocale(request)));
	request.setAttribute(TIME_ZONE_REQUEST_ATTRIBUTE_NAME,
			(timeZone != null ? timeZone : determineDefaultTimeZone(request)));
}
 
Example 9
Source File: HttpComponentsHttpInvokerRequestExecutor.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Create a HttpPost for the given configuration.
 * <p>The default implementation creates a standard HttpPost with
 * "application/x-java-serialized-object" as "Content-Type" header.
 * @param config the HTTP invoker configuration that specifies the
 * target service
 * @return the HttpPost instance
 * @throws java.io.IOException if thrown by I/O methods
 */
protected HttpPost createHttpPost(HttpInvokerClientConfiguration config) throws IOException {
	HttpPost httpPost = new HttpPost(config.getServiceUrl());

	RequestConfig requestConfig = createRequestConfig(config);
	if (requestConfig != null) {
		httpPost.setConfig(requestConfig);
	}

	LocaleContext localeContext = LocaleContextHolder.getLocaleContext();
	if (localeContext != null) {
		Locale locale = localeContext.getLocale();
		if (locale != null) {
			httpPost.addHeader(HTTP_HEADER_ACCEPT_LANGUAGE, StringUtils.toLanguageTag(locale));
		}
	}

	if (isAcceptGzipEncoding()) {
		httpPost.addHeader(HTTP_HEADER_ACCEPT_ENCODING, ENCODING_GZIP);
	}

	return httpPost;
}
 
Example 10
Source File: HttpComponentsHttpInvokerRequestExecutor.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Create a HttpPost for the given configuration.
 * <p>The default implementation creates a standard HttpPost with
 * "application/x-java-serialized-object" as "Content-Type" header.
 * @param config the HTTP invoker configuration that specifies the
 * target service
 * @return the HttpPost instance
 * @throws java.io.IOException if thrown by I/O methods
 */
protected HttpPost createHttpPost(HttpInvokerClientConfiguration config) throws IOException {
	HttpPost httpPost = new HttpPost(config.getServiceUrl());

	RequestConfig requestConfig = createRequestConfig(config);
	if (requestConfig != null) {
		httpPost.setConfig(requestConfig);
	}

	LocaleContext localeContext = LocaleContextHolder.getLocaleContext();
	if (localeContext != null) {
		Locale locale = localeContext.getLocale();
		if (locale != null) {
			httpPost.addHeader(HTTP_HEADER_ACCEPT_LANGUAGE, locale.toLanguageTag());
		}
	}

	if (isAcceptGzipEncoding()) {
		httpPost.addHeader(HTTP_HEADER_ACCEPT_ENCODING, ENCODING_GZIP);
	}

	return httpPost;
}
 
Example 11
Source File: SimpleHttpInvokerRequestExecutor.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Prepare the given HTTP connection.
 * <p>The default implementation specifies POST as method,
 * "application/x-java-serialized-object" as "Content-Type" header,
 * and the given content length as "Content-Length" header.
 * @param connection the HTTP connection to prepare
 * @param contentLength the length of the content to send
 * @throws IOException if thrown by HttpURLConnection methods
 * @see java.net.HttpURLConnection#setRequestMethod
 * @see java.net.HttpURLConnection#setRequestProperty
 */
protected void prepareConnection(HttpURLConnection connection, int contentLength) throws IOException {
	if (this.connectTimeout >= 0) {
		connection.setConnectTimeout(this.connectTimeout);
	}
	if (this.readTimeout >= 0) {
		connection.setReadTimeout(this.readTimeout);
	}

	connection.setDoOutput(true);
	connection.setRequestMethod(HTTP_METHOD_POST);
	connection.setRequestProperty(HTTP_HEADER_CONTENT_TYPE, getContentType());
	connection.setRequestProperty(HTTP_HEADER_CONTENT_LENGTH, Integer.toString(contentLength));

	LocaleContext localeContext = LocaleContextHolder.getLocaleContext();
	if (localeContext != null) {
		Locale locale = localeContext.getLocale();
		if (locale != null) {
			connection.setRequestProperty(HTTP_HEADER_ACCEPT_LANGUAGE, locale.toLanguageTag());
		}
	}

	if (isAcceptGzipEncoding()) {
		connection.setRequestProperty(HTTP_HEADER_ACCEPT_ENCODING, ENCODING_GZIP);
	}
}
 
Example 12
Source File: HttpComponentsHttpInvokerRequestExecutor.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Create a HttpPost for the given configuration.
 * <p>The default implementation creates a standard HttpPost with
 * "application/x-java-serialized-object" as "Content-Type" header.
 * @param config the HTTP invoker configuration that specifies the
 * target service
 * @return the HttpPost instance
 * @throws java.io.IOException if thrown by I/O methods
 */
protected HttpPost createHttpPost(HttpInvokerClientConfiguration config) throws IOException {
	HttpPost httpPost = new HttpPost(config.getServiceUrl());
	RequestConfig requestConfig = createRequestConfig(config);
	if (requestConfig != null) {
		httpPost.setConfig(requestConfig);
	}
	LocaleContext localeContext = LocaleContextHolder.getLocaleContext();
	if (localeContext != null) {
		Locale locale = localeContext.getLocale();
		if (locale != null) {
			httpPost.addHeader(HTTP_HEADER_ACCEPT_LANGUAGE, StringUtils.toLanguageTag(locale));
		}
	}
	if (isAcceptGzipEncoding()) {
		httpPost.addHeader(HTTP_HEADER_ACCEPT_ENCODING, ENCODING_GZIP);
	}
	return httpPost;
}
 
Example 13
Source File: SessionLocaleResolver.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void setLocaleContext(HttpServletRequest request, HttpServletResponse response, LocaleContext localeContext) {
	Locale locale = null;
	TimeZone timeZone = null;
	if (localeContext != null) {
		locale = localeContext.getLocale();
		if (localeContext instanceof TimeZoneAwareLocaleContext) {
			timeZone = ((TimeZoneAwareLocaleContext) localeContext).getTimeZone();
		}
	}
	WebUtils.setSessionAttribute(request, LOCALE_SESSION_ATTRIBUTE_NAME, locale);
	WebUtils.setSessionAttribute(request, TIME_ZONE_SESSION_ATTRIBUTE_NAME, timeZone);
}
 
Example 14
Source File: SessionLocaleResolver.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void setLocaleContext(HttpServletRequest request, HttpServletResponse response, LocaleContext localeContext) {
	Locale locale = null;
	TimeZone timeZone = null;
	if (localeContext != null) {
		locale = localeContext.getLocale();
		if (localeContext instanceof TimeZoneAwareLocaleContext) {
			timeZone = ((TimeZoneAwareLocaleContext) localeContext).getTimeZone();
		}
	}
	WebUtils.setSessionAttribute(request, this.localeAttributeName, locale);
	WebUtils.setSessionAttribute(request, this.timeZoneAttributeName, timeZone);
}
 
Example 15
Source File: SessionLocaleResolver.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void setLocaleContext(HttpServletRequest request, @Nullable HttpServletResponse response,
		@Nullable LocaleContext localeContext) {

	Locale locale = null;
	TimeZone timeZone = null;
	if (localeContext != null) {
		locale = localeContext.getLocale();
		if (localeContext instanceof TimeZoneAwareLocaleContext) {
			timeZone = ((TimeZoneAwareLocaleContext) localeContext).getTimeZone();
		}
	}
	WebUtils.setSessionAttribute(request, this.localeAttributeName, locale);
	WebUtils.setSessionAttribute(request, this.timeZoneAttributeName, timeZone);
}
 
Example 16
Source File: SessionLocaleResolver.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void setLocaleContext(HttpServletRequest request, @Nullable HttpServletResponse response,
		@Nullable LocaleContext localeContext) {

	Locale locale = null;
	TimeZone timeZone = null;
	if (localeContext != null) {
		locale = localeContext.getLocale();
		if (localeContext instanceof TimeZoneAwareLocaleContext) {
			timeZone = ((TimeZoneAwareLocaleContext) localeContext).getTimeZone();
		}
	}
	WebUtils.setSessionAttribute(request, this.localeAttributeName, locale);
	WebUtils.setSessionAttribute(request, this.timeZoneAttributeName, timeZone);
}
 
Example 17
Source File: RequestContext.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Initialize this context with the given request, using the given model attributes for Errors retrieval.
 * <p>Delegates to {@code getFallbackLocale} and {@code getFallbackTheme} for determining the fallback
 * locale and theme, respectively, if no LocaleResolver and/or ThemeResolver can be found in the request.
 * @param request current HTTP request
 * @param servletContext the servlet context of the web application (can be {@code null}; necessary for
 * fallback to root WebApplicationContext)
 * @param model the model attributes for the current view (can be {@code null}, using the request attributes
 * for Errors retrieval)
 * @see #getFallbackLocale
 * @see #getFallbackTheme
 * @see org.springframework.web.servlet.DispatcherServlet#LOCALE_RESOLVER_ATTRIBUTE
 * @see org.springframework.web.servlet.DispatcherServlet#THEME_RESOLVER_ATTRIBUTE
 */
protected void initContext(HttpServletRequest request, HttpServletResponse response, ServletContext servletContext,
		Map<String, Object> model) {

	this.request = request;
	this.response = response;
	this.model = model;

	// Fetch WebApplicationContext, either from DispatcherServlet or the root context.
	// ServletContext needs to be specified to be able to fall back to the root context!
	this.webApplicationContext = (WebApplicationContext) request.getAttribute(WEB_APPLICATION_CONTEXT_ATTRIBUTE);
	if (this.webApplicationContext == null) {
		this.webApplicationContext = RequestContextUtils.findWebApplicationContext(request, servletContext);
		if (this.webApplicationContext == null) {
			throw new IllegalStateException("No WebApplicationContext found: not in a DispatcherServlet " +
					"request and no ContextLoaderListener registered?");
		}
	}

	// Determine locale to use for this RequestContext.
	LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
	if (localeResolver instanceof LocaleContextResolver) {
		LocaleContext localeContext = ((LocaleContextResolver) localeResolver).resolveLocaleContext(request);
		this.locale = localeContext.getLocale();
		if (localeContext instanceof TimeZoneAwareLocaleContext) {
			this.timeZone = ((TimeZoneAwareLocaleContext) localeContext).getTimeZone();
		}
	}
	else if (localeResolver != null) {
		// Try LocaleResolver (we're within a DispatcherServlet request).
		this.locale = localeResolver.resolveLocale(request);
	}

	// Try JSTL fallbacks if necessary.
	if (this.locale == null) {
		this.locale = getFallbackLocale();
	}
	if (this.timeZone == null) {
		this.timeZone = getFallbackTimeZone();
	}

	// Determine default HTML escape setting from the "defaultHtmlEscape"
	// context-param in web.xml, if any.
	this.defaultHtmlEscape = WebUtils.getDefaultHtmlEscape(this.webApplicationContext.getServletContext());

	// Determine response-encoded HTML escape setting from the "responseEncodedHtmlEscape"
	// context-param in web.xml, if any.
	this.responseEncodedHtmlEscape = WebUtils.getResponseEncodedHtmlEscape(this.webApplicationContext.getServletContext());

	this.urlPathHelper = new UrlPathHelper();

	if (this.webApplicationContext.containsBean(RequestContextUtils.REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME)) {
		this.requestDataValueProcessor = this.webApplicationContext.getBean(
				RequestContextUtils.REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME, RequestDataValueProcessor.class);
	}
}
 
Example 18
Source File: RequestContext.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * Create a new RequestContext for the given request, using the given model attributes for Errors retrieval.
 * <p>This works with all View implementations. It will typically be used by View implementations.
 * <p>If a ServletContext is specified, the RequestContext will also work with a root
 * WebApplicationContext (outside a DispatcherServlet).
 * @param request current HTTP request
 * @param response current HTTP response
 * @param servletContext the servlet context of the web application (can be {@code null}; necessary for
 * fallback to root WebApplicationContext)
 * @param model the model attributes for the current view (can be {@code null}, using the request attributes
 * for Errors retrieval)
 * @see org.springframework.web.context.WebApplicationContext
 * @see org.springframework.web.servlet.DispatcherServlet
 */
public RequestContext(HttpServletRequest request, @Nullable HttpServletResponse response,
		@Nullable ServletContext servletContext, @Nullable Map<String, Object> model) {

	this.request = request;
	this.response = response;
	this.model = model;

	// Fetch WebApplicationContext, either from DispatcherServlet or the root context.
	// ServletContext needs to be specified to be able to fall back to the root context!
	WebApplicationContext wac = (WebApplicationContext) request.getAttribute(WEB_APPLICATION_CONTEXT_ATTRIBUTE);
	if (wac == null) {
		wac = RequestContextUtils.findWebApplicationContext(request, servletContext);
		if (wac == null) {
			throw new IllegalStateException("No WebApplicationContext found: not in a DispatcherServlet " +
					"request and no ContextLoaderListener registered?");
		}
	}
	this.webApplicationContext = wac;

	Locale locale = null;
	TimeZone timeZone = null;

	// Determine locale to use for this RequestContext.
	LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
	if (localeResolver instanceof LocaleContextResolver) {
		LocaleContext localeContext = ((LocaleContextResolver) localeResolver).resolveLocaleContext(request);
		locale = localeContext.getLocale();
		if (localeContext instanceof TimeZoneAwareLocaleContext) {
			timeZone = ((TimeZoneAwareLocaleContext) localeContext).getTimeZone();
		}
	}
	else if (localeResolver != null) {
		// Try LocaleResolver (we're within a DispatcherServlet request).
		locale = localeResolver.resolveLocale(request);
	}

	this.locale = locale;
	this.timeZone = timeZone;

	// Determine default HTML escape setting from the "defaultHtmlEscape"
	// context-param in web.xml, if any.
	this.defaultHtmlEscape = WebUtils.getDefaultHtmlEscape(this.webApplicationContext.getServletContext());

	// Determine response-encoded HTML escape setting from the "responseEncodedHtmlEscape"
	// context-param in web.xml, if any.
	this.responseEncodedHtmlEscape =
			WebUtils.getResponseEncodedHtmlEscape(this.webApplicationContext.getServletContext());

	this.urlPathHelper = new UrlPathHelper();

	if (this.webApplicationContext.containsBean(RequestContextUtils.REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME)) {
		this.requestDataValueProcessor = this.webApplicationContext.getBean(
				RequestContextUtils.REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME, RequestDataValueProcessor.class);
	}
}
 
Example 19
Source File: RequestContext.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Initialize this context with the given request, using the given model attributes for Errors retrieval.
 * <p>Delegates to {@code getFallbackLocale} and {@code getFallbackTheme} for determining the fallback
 * locale and theme, respectively, if no LocaleResolver and/or ThemeResolver can be found in the request.
 * @param request current HTTP request
 * @param servletContext the servlet context of the web application (can be {@code null}; necessary for
 * fallback to root WebApplicationContext)
 * @param model the model attributes for the current view (can be {@code null}, using the request attributes
 * for Errors retrieval)
 * @see #getFallbackLocale
 * @see #getFallbackTheme
 * @see org.springframework.web.servlet.DispatcherServlet#LOCALE_RESOLVER_ATTRIBUTE
 * @see org.springframework.web.servlet.DispatcherServlet#THEME_RESOLVER_ATTRIBUTE
 */
protected void initContext(HttpServletRequest request, HttpServletResponse response, ServletContext servletContext,
		Map<String, Object> model) {

	this.request = request;
	this.response = response;
	this.model = model;

	// Fetch WebApplicationContext, either from DispatcherServlet or the root context.
	// ServletContext needs to be specified to be able to fall back to the root context!
	this.webApplicationContext = (WebApplicationContext) request.getAttribute(WEB_APPLICATION_CONTEXT_ATTRIBUTE);
	if (this.webApplicationContext == null) {
		this.webApplicationContext = RequestContextUtils.findWebApplicationContext(request, servletContext);
		if (this.webApplicationContext == null) {
			throw new IllegalStateException("No WebApplicationContext found: not in a DispatcherServlet " +
					"request and no ContextLoaderListener registered?");
		}
	}

	// Determine locale to use for this RequestContext.
	LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
	if (localeResolver instanceof LocaleContextResolver) {
		LocaleContext localeContext = ((LocaleContextResolver) localeResolver).resolveLocaleContext(request);
		this.locale = localeContext.getLocale();
		if (localeContext instanceof TimeZoneAwareLocaleContext) {
			this.timeZone = ((TimeZoneAwareLocaleContext) localeContext).getTimeZone();
		}
	}
	else if (localeResolver != null) {
		// Try LocaleResolver (we're within a DispatcherServlet request).
		this.locale = localeResolver.resolveLocale(request);
	}

	// Try JSTL fallbacks if necessary.
	if (this.locale == null) {
		this.locale = getFallbackLocale();
	}
	if (this.timeZone == null) {
		this.timeZone = getFallbackTimeZone();
	}

	// Determine default HTML escape setting from the "defaultHtmlEscape"
	// context-param in web.xml, if any.
	this.defaultHtmlEscape = WebUtils.getDefaultHtmlEscape(this.webApplicationContext.getServletContext());

	// Determine response-encoded HTML escape setting from the "responseEncodedHtmlEscape"
	// context-param in web.xml, if any.
	this.responseEncodedHtmlEscape = WebUtils.getResponseEncodedHtmlEscape(this.webApplicationContext.getServletContext());

	this.urlPathHelper = new UrlPathHelper();

	if (this.webApplicationContext.containsBean(RequestContextUtils.REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME)) {
		this.requestDataValueProcessor = this.webApplicationContext.getBean(
				RequestContextUtils.REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME, RequestDataValueProcessor.class);
	}
}
 
Example 20
Source File: RequestContext.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * Create a new RequestContext for the given request, using the given model attributes for Errors retrieval.
 * <p>This works with all View implementations. It will typically be used by View implementations.
 * <p>If a ServletContext is specified, the RequestContext will also work with a root
 * WebApplicationContext (outside a DispatcherServlet).
 * @param request current HTTP request
 * @param response current HTTP response
 * @param servletContext the servlet context of the web application (can be {@code null}; necessary for
 * fallback to root WebApplicationContext)
 * @param model the model attributes for the current view (can be {@code null}, using the request attributes
 * for Errors retrieval)
 * @see org.springframework.web.context.WebApplicationContext
 * @see org.springframework.web.servlet.DispatcherServlet
 */
public RequestContext(HttpServletRequest request, @Nullable HttpServletResponse response,
		@Nullable ServletContext servletContext, @Nullable Map<String, Object> model) {

	this.request = request;
	this.response = response;
	this.model = model;

	// Fetch WebApplicationContext, either from DispatcherServlet or the root context.
	// ServletContext needs to be specified to be able to fall back to the root context!
	WebApplicationContext wac = (WebApplicationContext) request.getAttribute(WEB_APPLICATION_CONTEXT_ATTRIBUTE);
	if (wac == null) {
		wac = RequestContextUtils.findWebApplicationContext(request, servletContext);
		if (wac == null) {
			throw new IllegalStateException("No WebApplicationContext found: not in a DispatcherServlet " +
					"request and no ContextLoaderListener registered?");
		}
	}
	this.webApplicationContext = wac;

	Locale locale = null;
	TimeZone timeZone = null;

	// Determine locale to use for this RequestContext.
	LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
	if (localeResolver instanceof LocaleContextResolver) {
		LocaleContext localeContext = ((LocaleContextResolver) localeResolver).resolveLocaleContext(request);
		locale = localeContext.getLocale();
		if (localeContext instanceof TimeZoneAwareLocaleContext) {
			timeZone = ((TimeZoneAwareLocaleContext) localeContext).getTimeZone();
		}
	}
	else if (localeResolver != null) {
		// Try LocaleResolver (we're within a DispatcherServlet request).
		locale = localeResolver.resolveLocale(request);
	}

	this.locale = locale;
	this.timeZone = timeZone;

	// Determine default HTML escape setting from the "defaultHtmlEscape"
	// context-param in web.xml, if any.
	this.defaultHtmlEscape = WebUtils.getDefaultHtmlEscape(this.webApplicationContext.getServletContext());

	// Determine response-encoded HTML escape setting from the "responseEncodedHtmlEscape"
	// context-param in web.xml, if any.
	this.responseEncodedHtmlEscape =
			WebUtils.getResponseEncodedHtmlEscape(this.webApplicationContext.getServletContext());

	this.urlPathHelper = new UrlPathHelper();

	if (this.webApplicationContext.containsBean(RequestContextUtils.REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME)) {
		this.requestDataValueProcessor = this.webApplicationContext.getBean(
				RequestContextUtils.REQUEST_DATA_VALUE_PROCESSOR_BEAN_NAME, RequestDataValueProcessor.class);
	}
}