Java Code Examples for org.springframework.context.i18n.LocaleContextHolder#resetLocaleContext()

The following examples show how to use org.springframework.context.i18n.LocaleContextHolder#resetLocaleContext() . 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: RequestContextListener.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public void requestDestroyed(ServletRequestEvent requestEvent) {
	ServletRequestAttributes attributes = null;
	Object reqAttr = requestEvent.getServletRequest().getAttribute(REQUEST_ATTRIBUTES_ATTRIBUTE);
	if (reqAttr instanceof ServletRequestAttributes) {
		attributes = (ServletRequestAttributes) reqAttr;
	}
	RequestAttributes threadAttributes = RequestContextHolder.getRequestAttributes();
	if (threadAttributes != null) {
		// We're assumably within the original request thread...
		LocaleContextHolder.resetLocaleContext();
		RequestContextHolder.resetRequestAttributes();
		if (attributes == null && threadAttributes instanceof ServletRequestAttributes) {
			attributes = (ServletRequestAttributes) threadAttributes;
		}
	}
	if (attributes != null) {
		attributes.requestCompleted();
	}
}
 
Example 2
Source File: ResponseStatusExceptionResolverTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void statusCodeAndReasonMessage() {
	Locale locale = Locale.CHINESE;
	LocaleContextHolder.setLocale(locale);
	try {
		StaticMessageSource messageSource = new StaticMessageSource();
		messageSource.addMessage("gone.reason", locale, "Gone reason message");
		exceptionResolver.setMessageSource(messageSource);

		StatusCodeAndReasonMessageException ex = new StatusCodeAndReasonMessageException();
		exceptionResolver.resolveException(request, response, null, ex);
		assertEquals("Invalid status reason", "Gone reason message", response.getErrorMessage());
	}
	finally {
		LocaleContextHolder.resetLocaleContext();
	}
}
 
Example 3
Source File: DataBinderTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testBindingErrorWithFormatterAgainstList() {
	BeanWithIntegerList tb = new BeanWithIntegerList();
	DataBinder binder = new DataBinder(tb);
	FormattingConversionService conversionService = new FormattingConversionService();
	DefaultConversionService.addDefaultConverters(conversionService);
	conversionService.addFormatterForFieldType(Float.class, new NumberStyleFormatter());
	binder.setConversionService(conversionService);
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("integerList[0]", "1x2");

	LocaleContextHolder.setLocale(Locale.GERMAN);
	try {
		binder.bind(pvs);
		assertTrue(tb.getIntegerList().isEmpty());
		assertEquals("1x2", binder.getBindingResult().getFieldValue("integerList[0]"));
		assertTrue(binder.getBindingResult().hasFieldErrors("integerList[0]"));
	}
	finally {
		LocaleContextHolder.resetLocaleContext();
	}
}
 
Example 4
Source File: HttpRequestHandlerServlet.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {

	LocaleContextHolder.setLocale(request.getLocale());
	try {
		this.target.handleRequest(request, response);
	}
	catch (HttpRequestMethodNotSupportedException ex) {
		String[] supportedMethods = ex.getSupportedMethods();
		if (supportedMethods != null) {
			response.setHeader("Allow", StringUtils.arrayToDelimitedString(supportedMethods, ", "));
		}
		response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, ex.getMessage());
	}
	finally {
		LocaleContextHolder.resetLocaleContext();
	}
}
 
Example 5
Source File: RequestContextListener.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void requestDestroyed(ServletRequestEvent requestEvent) {
	ServletRequestAttributes attributes = null;
	Object reqAttr = requestEvent.getServletRequest().getAttribute(REQUEST_ATTRIBUTES_ATTRIBUTE);
	if (reqAttr instanceof ServletRequestAttributes) {
		attributes = (ServletRequestAttributes) reqAttr;
	}
	RequestAttributes threadAttributes = RequestContextHolder.getRequestAttributes();
	if (threadAttributes != null) {
		// We're assumably within the original request thread...
		LocaleContextHolder.resetLocaleContext();
		RequestContextHolder.resetRequestAttributes();
		if (attributes == null && threadAttributes instanceof ServletRequestAttributes) {
			attributes = (ServletRequestAttributes) threadAttributes;
		}
	}
	if (attributes != null) {
		attributes.requestCompleted();
	}
}
 
Example 6
Source File: DataBinderTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testBindingErrorWithCustomFormatter() {
	TestBean tb = new TestBean();
	DataBinder binder = new DataBinder(tb);
	binder.addCustomFormatter(new NumberStyleFormatter());
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("myFloat", "1x2");

	LocaleContextHolder.setLocale(Locale.GERMAN);
	try {
		binder.bind(pvs);
		assertEquals(new Float(0.0), tb.getMyFloat());
		assertEquals("1x2", binder.getBindingResult().getFieldValue("myFloat"));
		assertTrue(binder.getBindingResult().hasFieldErrors("myFloat"));
		assertEquals("typeMismatch", binder.getBindingResult().getFieldError("myFloat").getCode());
	}
	finally {
		LocaleContextHolder.resetLocaleContext();
	}
}
 
Example 7
Source File: FormattingConversionServiceFactoryBeanTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testDefaultFormattersOn() throws Exception {
	FormattingConversionServiceFactoryBean factory = new FormattingConversionServiceFactoryBean();
	factory.afterPropertiesSet();
	FormattingConversionService fcs = factory.getObject();
	TypeDescriptor descriptor = new TypeDescriptor(TestBean.class.getDeclaredField("pattern"));

	LocaleContextHolder.setLocale(Locale.GERMAN);
	try {
		Object value = fcs.convert("15,00", TypeDescriptor.valueOf(String.class), descriptor);
		assertEquals(15.0, value);
		value = fcs.convert(15.0, descriptor, TypeDescriptor.valueOf(String.class));
		assertEquals("15", value);
	}
	finally {
		LocaleContextHolder.resetLocaleContext();
	}
}
 
Example 8
Source File: HttpRequestHandlerServlet.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {

	Assert.state(this.target != null, "No HttpRequestHandler available");

	LocaleContextHolder.setLocale(request.getLocale());
	try {
		this.target.handleRequest(request, response);
	}
	catch (HttpRequestMethodNotSupportedException ex) {
		String[] supportedMethods = ex.getSupportedMethods();
		if (supportedMethods != null) {
			response.setHeader("Allow", StringUtils.arrayToDelimitedString(supportedMethods, ", "));
		}
		response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, ex.getMessage());
	}
	finally {
		LocaleContextHolder.resetLocaleContext();
	}
}
 
Example 9
Source File: DataBinderTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testBindingWithFormatterAgainstList() {
	BeanWithIntegerList tb = new BeanWithIntegerList();
	DataBinder binder = new DataBinder(tb);
	FormattingConversionService conversionService = new FormattingConversionService();
	DefaultConversionService.addDefaultConverters(conversionService);
	conversionService.addFormatterForFieldType(Float.class, new NumberStyleFormatter());
	binder.setConversionService(conversionService);
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("integerList[0]", "1");

	LocaleContextHolder.setLocale(Locale.GERMAN);
	try {
		binder.bind(pvs);
		assertEquals(new Integer(1), tb.getIntegerList().get(0));
		assertEquals("1", binder.getBindingResult().getFieldValue("integerList[0]"));
	}
	finally {
		LocaleContextHolder.resetLocaleContext();
	}
}
 
Example 10
Source File: DataBinderTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testBindingWithFormatterAgainstList() {
	BeanWithIntegerList tb = new BeanWithIntegerList();
	DataBinder binder = new DataBinder(tb);
	FormattingConversionService conversionService = new FormattingConversionService();
	DefaultConversionService.addDefaultConverters(conversionService);
	conversionService.addFormatterForFieldType(Float.class, new NumberStyleFormatter());
	binder.setConversionService(conversionService);
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("integerList[0]", "1");

	LocaleContextHolder.setLocale(Locale.GERMAN);
	try {
		binder.bind(pvs);
		assertEquals(new Integer(1), tb.getIntegerList().get(0));
		assertEquals("1", binder.getBindingResult().getFieldValue("integerList[0]"));
	}
	finally {
		LocaleContextHolder.resetLocaleContext();
	}
}
 
Example 11
Source File: HttpRequestHandlerServlet.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {

	Assert.state(this.target != null, "No HttpRequestHandler available");

	LocaleContextHolder.setLocale(request.getLocale());
	try {
		this.target.handleRequest(request, response);
	}
	catch (HttpRequestMethodNotSupportedException ex) {
		String[] supportedMethods = ex.getSupportedMethods();
		if (supportedMethods != null) {
			response.setHeader("Allow", StringUtils.arrayToDelimitedString(supportedMethods, ", "));
		}
		response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, ex.getMessage());
	}
	finally {
		LocaleContextHolder.resetLocaleContext();
	}
}
 
Example 12
Source File: DataBinderTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testBindingErrorWithFormatter() {
	TestBean tb = new TestBean();
	DataBinder binder = new DataBinder(tb);
	FormattingConversionService conversionService = new FormattingConversionService();
	DefaultConversionService.addDefaultConverters(conversionService);
	conversionService.addFormatterForFieldType(Float.class, new NumberStyleFormatter());
	binder.setConversionService(conversionService);
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("myFloat", "1x2");

	LocaleContextHolder.setLocale(Locale.GERMAN);
	try {
		binder.bind(pvs);
		assertEquals(new Float(0.0), tb.getMyFloat());
		assertEquals("1x2", binder.getBindingResult().getFieldValue("myFloat"));
		assertTrue(binder.getBindingResult().hasFieldErrors("myFloat"));
	}
	finally {
		LocaleContextHolder.resetLocaleContext();
	}
}
 
Example 13
Source File: SpringletsJsonpAdviceAutoConfigurationTest.java    From springlets with Apache License 2.0 5 votes vote down vote up
@After
public void close() {
  LocaleContextHolder.resetLocaleContext();
  if (this.context != null) {
    this.context.close();
  }
}
 
Example 14
Source File: SpringletsWebJacksonAutoConfigurationTest.java    From springlets with Apache License 2.0 5 votes vote down vote up
@After
public void close() {
  LocaleContextHolder.resetLocaleContext();
  if (this.context != null) {
    this.context.close();
  }
}
 
Example 15
Source File: DataBinderTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testBindingWithFormatterAgainstFields() {
	TestBean tb = new TestBean();
	DataBinder binder = new DataBinder(tb);
	FormattingConversionService conversionService = new FormattingConversionService();
	DefaultConversionService.addDefaultConverters(conversionService);
	conversionService.addFormatterForFieldType(Float.class, new NumberStyleFormatter());
	binder.setConversionService(conversionService);
	binder.initDirectFieldAccess();
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("myFloat", "1,2");

	LocaleContextHolder.setLocale(Locale.GERMAN);
	try {
		binder.bind(pvs);
		assertEquals(new Float(1.2), tb.getMyFloat());
		assertEquals("1,2", binder.getBindingResult().getFieldValue("myFloat"));

		PropertyEditor editor = binder.getBindingResult().findEditor("myFloat", Float.class);
		assertNotNull(editor);
		editor.setValue(new Float(1.4));
		assertEquals("1,4", editor.getAsText());

		editor = binder.getBindingResult().findEditor("myFloat", null);
		assertNotNull(editor);
		editor.setAsText("1,6");
		assertEquals(new Float(1.6), editor.getValue());
	}
	finally {
		LocaleContextHolder.resetLocaleContext();
	}
}
 
Example 16
Source File: SpringletsImageFileConverterAutoConfigurationTest.java    From springlets with Apache License 2.0 5 votes vote down vote up
@After
public void close() {
  LocaleContextHolder.resetLocaleContext();
  if (this.context != null) {
    this.context.close();
  }
}
 
Example 17
Source File: DataBinderTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testBindingWithCustomFormatter() {
	TestBean tb = new TestBean();
	DataBinder binder = new DataBinder(tb);
	binder.addCustomFormatter(new NumberStyleFormatter(), Float.class);
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("myFloat", "1,2");

	LocaleContextHolder.setLocale(Locale.GERMAN);
	try {
		binder.bind(pvs);
		assertEquals(new Float(1.2), tb.getMyFloat());
		assertEquals("1,2", binder.getBindingResult().getFieldValue("myFloat"));

		PropertyEditor editor = binder.getBindingResult().findEditor("myFloat", Float.class);
		assertNotNull(editor);
		editor.setValue(new Float(1.4));
		assertEquals("1,4", editor.getAsText());

		editor = binder.getBindingResult().findEditor("myFloat", null);
		assertNotNull(editor);
		editor.setAsText("1,6");
		assertTrue(((Number) editor.getValue()).floatValue() == 1.6f);
	}
	finally {
		LocaleContextHolder.resetLocaleContext();
	}
}
 
Example 18
Source File: SecurityAdviceTraitTest.java    From problem-spring-web with MIT License 4 votes vote down vote up
@AfterEach
void tearDown() {
    LocaleContextHolder.resetLocaleContext();
}
 
Example 19
Source File: RequestContextFilter.java    From java-technology-stack with MIT License 4 votes vote down vote up
private void resetContextHolders() {
	LocaleContextHolder.resetLocaleContext();
	RequestContextHolder.resetRequestAttributes();
}
 
Example 20
Source File: BindAdviceTraitTest.java    From problem-spring-web with MIT License 4 votes vote down vote up
@AfterEach
void tearDown() {
    LocaleContextHolder.resetLocaleContext();
}