org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver Java Examples

The following examples show how to use org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver. 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: VelocityRenderTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
	wac = new StaticWebApplicationContext();
	wac.setServletContext(new MockServletContext());

	final Template expectedTemplate = new Template();
	VelocityConfig vc = new VelocityConfig() {
		@Override
		public VelocityEngine getVelocityEngine() {
			return new TestVelocityEngine("test.vm", expectedTemplate);
		}
	};
	wac.getDefaultListableBeanFactory().registerSingleton("velocityConfigurer", vc);
	wac.refresh();

	request = new MockHttpServletRequest();
	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
	request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver());
	response = new MockHttpServletResponse();
}
 
Example #2
Source File: FreeMarkerMacroTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
	wac = new StaticWebApplicationContext();
	wac.setServletContext(new MockServletContext());

	// final Template expectedTemplate = new Template();
	fc = new FreeMarkerConfigurer();
	fc.setTemplateLoaderPaths("classpath:/", "file://" + System.getProperty("java.io.tmpdir"));
	fc.afterPropertiesSet();

	wac.getDefaultListableBeanFactory().registerSingleton("freeMarkerConfigurer", fc);
	wac.refresh();

	request = new MockHttpServletRequest();
	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
	request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver());
	response = new MockHttpServletResponse();
}
 
Example #3
Source File: FreeMarkerMacroTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Before
public void setUp() throws Exception {
	ServletContext sc = new MockServletContext();
	wac = new StaticWebApplicationContext();
	wac.setServletContext(sc);

	// final Template expectedTemplate = new Template();
	fc = new FreeMarkerConfigurer();
	fc.setTemplateLoaderPaths("classpath:/", "file://" + System.getProperty("java.io.tmpdir"));
	fc.setServletContext(sc);
	fc.afterPropertiesSet();

	wac.getDefaultListableBeanFactory().registerSingleton("freeMarkerConfigurer", fc);
	wac.refresh();

	request = new MockHttpServletRequest();
	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
	request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver());
	response = new MockHttpServletResponse();
}
 
Example #4
Source File: AbstractTagTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
protected MockPageContext createPageContext() {
	MockServletContext sc = new MockServletContext();
	SimpleWebApplicationContext wac = new SimpleWebApplicationContext();
	wac.setServletContext(sc);
	wac.setNamespace("test");
	wac.refresh();

	MockHttpServletRequest request = new MockHttpServletRequest(sc);
	MockHttpServletResponse response = new MockHttpServletResponse();
	if (inDispatcherServlet()) {
		request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
		LocaleResolver lr = new AcceptHeaderLocaleResolver();
		request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, lr);
		ThemeResolver tr = new FixedThemeResolver();
		request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, tr);
		request.setAttribute(DispatcherServlet.THEME_SOURCE_ATTRIBUTE, wac);
	}
	else {
		sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	}

	return new MockPageContext(sc, request, response);
}
 
Example #5
Source File: VelocityMacroTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
	wac = new StaticWebApplicationContext();
	wac.setServletContext(new MockServletContext());

	final Template expectedTemplate = new Template();
	VelocityConfig vc = new VelocityConfig() {
		@Override
		public VelocityEngine getVelocityEngine() {
			return new TestVelocityEngine(TEMPLATE_FILE, expectedTemplate);
		}
	};
	wac.getDefaultListableBeanFactory().registerSingleton("velocityConfigurer", vc);
	wac.refresh();

	request = new MockHttpServletRequest();
	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
	request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver());
	response = new MockHttpServletResponse();
}
 
Example #6
Source File: AbstractTagTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
protected MockPageContext createPageContext() {
	MockServletContext sc = new MockServletContext();
	SimpleWebApplicationContext wac = new SimpleWebApplicationContext();
	wac.setServletContext(sc);
	wac.setNamespace("test");
	wac.refresh();

	MockHttpServletRequest request = new MockHttpServletRequest(sc);
	MockHttpServletResponse response = new MockHttpServletResponse();
	if (inDispatcherServlet()) {
		request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
		LocaleResolver lr = new AcceptHeaderLocaleResolver();
		request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, lr);
		ThemeResolver tr = new FixedThemeResolver();
		request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, tr);
		request.setAttribute(DispatcherServlet.THEME_SOURCE_ATTRIBUTE, wac);
	}
	else {
		sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	}

	return new MockPageContext(sc, request, response);
}
 
Example #7
Source File: AbstractTagTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
protected MockPageContext createPageContext() {
	MockServletContext sc = new MockServletContext();
	SimpleWebApplicationContext wac = new SimpleWebApplicationContext();
	wac.setServletContext(sc);
	wac.setNamespace("test");
	wac.refresh();

	MockHttpServletRequest request = new MockHttpServletRequest(sc);
	MockHttpServletResponse response = new MockHttpServletResponse();
	if (inDispatcherServlet()) {
		request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
		LocaleResolver lr = new AcceptHeaderLocaleResolver();
		request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, lr);
		ThemeResolver tr = new FixedThemeResolver();
		request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, tr);
		request.setAttribute(DispatcherServlet.THEME_SOURCE_ATTRIBUTE, wac);
	}
	else {
		sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	}

	return new MockPageContext(sc, request, response);
}
 
Example #8
Source File: FreeMarkerMacroTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Before
public void setUp() throws Exception {
	ServletContext sc = new MockServletContext();
	wac = new StaticWebApplicationContext();
	wac.setServletContext(sc);

	// final Template expectedTemplate = new Template();
	fc = new FreeMarkerConfigurer();
	fc.setTemplateLoaderPaths("classpath:/", "file://" + System.getProperty("java.io.tmpdir"));
	fc.setServletContext(sc);
	fc.afterPropertiesSet();

	wac.getDefaultListableBeanFactory().registerSingleton("freeMarkerConfigurer", fc);
	wac.refresh();

	request = new MockHttpServletRequest();
	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
	request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver());
	response = new MockHttpServletResponse();
}
 
Example #9
Source File: FreeMarkerViewTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void keepExistingContentType() throws Exception {
	FreeMarkerView fv = new FreeMarkerView();

	WebApplicationContext wac = mock(WebApplicationContext.class);
	MockServletContext sc = new MockServletContext();

	Map<String, FreeMarkerConfig> configs = new HashMap<>();
	FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
	configurer.setConfiguration(new TestConfiguration());
	configurer.setServletContext(sc);
	configs.put("configurer", configurer);
	given(wac.getBeansOfType(FreeMarkerConfig.class, true, false)).willReturn(configs);
	given(wac.getServletContext()).willReturn(sc);

	fv.setUrl("templateName");
	fv.setApplicationContext(wac);

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.addPreferredLocale(Locale.US);
	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
	HttpServletResponse response = new MockHttpServletResponse();
	response.setContentType("myContentType");

	Map<String, Object> model = new HashMap<>();
	model.put("myattr", "myvalue");
	fv.render(model, request, response);

	assertEquals("myContentType", response.getContentType());
}
 
Example #10
Source File: FreeMarkerViewTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void keepExistingContentType() throws Exception {
	FreeMarkerView fv = new FreeMarkerView();

	WebApplicationContext wac = mock(WebApplicationContext.class);
	MockServletContext sc = new MockServletContext();

	Map<String, FreeMarkerConfig> configs = new HashMap<String, FreeMarkerConfig>();
	FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
	configurer.setConfiguration(new TestConfiguration());
	configs.put("configurer", configurer);
	given(wac.getBeansOfType(FreeMarkerConfig.class, true, false)).willReturn(configs);
	given(wac.getServletContext()).willReturn(sc);

	fv.setUrl("templateName");
	fv.setApplicationContext(wac);

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.addPreferredLocale(Locale.US);
	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
	HttpServletResponse response = new MockHttpServletResponse();
	response.setContentType("myContentType");

	Map<String, Object> model = new HashMap<String, Object>();
	model.put("myattr", "myvalue");
	fv.render(model, request, response);

	assertEquals("myContentType", response.getContentType());
}
 
Example #11
Source File: FreeMarkerViewTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void validTemplateName() throws Exception {
	FreeMarkerView fv = new FreeMarkerView();

	WebApplicationContext wac = mock(WebApplicationContext.class);
	MockServletContext sc = new MockServletContext();

	Map<String, FreeMarkerConfig> configs = new HashMap<String, FreeMarkerConfig>();
	FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
	configurer.setConfiguration(new TestConfiguration());
	configs.put("configurer", configurer);
	given(wac.getBeansOfType(FreeMarkerConfig.class, true, false)).willReturn(configs);
	given(wac.getServletContext()).willReturn(sc);

	fv.setUrl("templateName");
	fv.setApplicationContext(wac);

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.addPreferredLocale(Locale.US);
	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
	HttpServletResponse response = new MockHttpServletResponse();

	Map<String, Object> model = new HashMap<String, Object>();
	model.put("myattr", "myvalue");
	fv.render(model, request, response);

	assertEquals(AbstractView.DEFAULT_CONTENT_TYPE, response.getContentType());
}
 
Example #12
Source File: ViewResolverTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testInternalResourceViewResolverWithSpecificContextBeans() throws Exception {
	MockServletContext sc = new MockServletContext();
	final StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.registerSingleton("myBean", TestBean.class);
	wac.registerSingleton("myBean2", TestBean.class);
	wac.setServletContext(sc);
	wac.refresh();
	InternalResourceViewResolver vr = new InternalResourceViewResolver();
	Properties props = new Properties();
	props.setProperty("key1", "value1");
	vr.setAttributes(props);
	Map map = new HashMap();
	map.put("key2", new Integer(2));
	vr.setAttributesMap(map);
	vr.setExposedContextBeanNames(new String[] {"myBean2"});
	vr.setApplicationContext(wac);

	MockHttpServletRequest request = new MockHttpServletRequest(sc) {
		@Override
		public RequestDispatcher getRequestDispatcher(String path) {
			return new MockRequestDispatcher(path) {
				@Override
				public void forward(ServletRequest forwardRequest, ServletResponse forwardResponse) {
					assertTrue("Correct rc attribute", forwardRequest.getAttribute("rc") == null);
					assertEquals("value1", forwardRequest.getAttribute("key1"));
					assertEquals(new Integer(2), forwardRequest.getAttribute("key2"));
					assertNull(forwardRequest.getAttribute("myBean"));
					assertSame(wac.getBean("myBean2"), forwardRequest.getAttribute("myBean2"));
				}
			};
		}
	};
	HttpServletResponse response = new MockHttpServletResponse();
	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
	View view = vr.resolveViewName("example1", Locale.getDefault());
	view.render(new HashMap(), request, response);
}
 
Example #13
Source File: ViewResolverTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testInternalResourceViewResolverWithContextBeans() throws Exception {
	MockServletContext sc = new MockServletContext();
	final StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.registerSingleton("myBean", TestBean.class);
	wac.registerSingleton("myBean2", TestBean.class);
	wac.setServletContext(sc);
	wac.refresh();
	InternalResourceViewResolver vr = new InternalResourceViewResolver();
	Properties props = new Properties();
	props.setProperty("key1", "value1");
	vr.setAttributes(props);
	Map map = new HashMap();
	map.put("key2", new Integer(2));
	vr.setAttributesMap(map);
	vr.setExposeContextBeansAsAttributes(true);
	vr.setApplicationContext(wac);

	MockHttpServletRequest request = new MockHttpServletRequest(sc) {
		@Override
		public RequestDispatcher getRequestDispatcher(String path) {
			return new MockRequestDispatcher(path) {
				@Override
				public void forward(ServletRequest forwardRequest, ServletResponse forwardResponse) {
					assertTrue("Correct rc attribute", forwardRequest.getAttribute("rc") == null);
					assertEquals("value1", forwardRequest.getAttribute("key1"));
					assertEquals(new Integer(2), forwardRequest.getAttribute("key2"));
					assertSame(wac.getBean("myBean"), forwardRequest.getAttribute("myBean"));
					assertSame(wac.getBean("myBean2"), forwardRequest.getAttribute("myBean2"));
				}
			};
		}
	};
	HttpServletResponse response = new MockHttpServletResponse();
	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
	View view = vr.resolveViewName("example1", Locale.getDefault());
	view.render(new HashMap(), request, response);
}
 
Example #14
Source File: ViewResolverTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private void doTestUrlBasedViewResolverWithoutPrefixes(UrlBasedViewResolver vr) throws Exception {
	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(new MockServletContext());
	wac.refresh();
	vr.setApplicationContext(wac);
	vr.setContentType("myContentType");
	vr.setRequestContextAttribute("rc");

	View view = vr.resolveViewName("example1", Locale.getDefault());
	assertEquals("Correct view class", JstlView.class, view.getClass());
	assertEquals("Correct URL", "example1", ((InternalResourceView) view).getUrl());
	assertEquals("Correct textContentType", "myContentType", ((InternalResourceView) view).getContentType());

	view = vr.resolveViewName("example2", Locale.getDefault());
	assertEquals("Correct view class", JstlView.class, view.getClass());
	assertEquals("Correct URL", "example2", ((InternalResourceView) view).getUrl());
	assertEquals("Correct textContentType", "myContentType", ((InternalResourceView) view).getContentType());

	HttpServletRequest request = new MockHttpServletRequest(wac.getServletContext());
	HttpServletResponse response = new MockHttpServletResponse();
	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
	request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver());
	Map model = new HashMap();
	TestBean tb = new TestBean();
	model.put("tb", tb);
	view.render(model, request, response);
	assertTrue("Correct tb attribute", tb.equals(request.getAttribute("tb")));
	assertTrue("Correct rc attribute", request.getAttribute("rc") instanceof RequestContext);

	view = vr.resolveViewName("redirect:myUrl", Locale.getDefault());
	assertEquals("Correct view class", RedirectView.class, view.getClass());
	assertEquals("Correct URL", "myUrl", ((RedirectView) view).getUrl());
	assertSame("View not initialized as bean", wac, ((RedirectView) view).getApplicationContext());

	view = vr.resolveViewName("forward:myUrl", Locale.getDefault());
	assertEquals("Correct view class", InternalResourceView.class, view.getClass());
	assertEquals("Correct URL", "myUrl", ((InternalResourceView) view).getUrl());
}
 
Example #15
Source File: SimpleWebApplicationContext.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("deprecation")
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {
	if (!(RequestContextUtils.getWebApplicationContext(request) instanceof SimpleWebApplicationContext)) {
		throw new ServletException("Incorrect WebApplicationContext");
	}
	if (!(RequestContextUtils.getLocaleResolver(request) instanceof AcceptHeaderLocaleResolver)) {
		throw new ServletException("Incorrect LocaleResolver");
	}
	if (!Locale.CANADA.equals(RequestContextUtils.getLocale(request))) {
		throw new ServletException("Incorrect Locale");
	}
	return null;
}
 
Example #16
Source File: SimpleWebApplicationContext.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {
	if (!(RequestContextUtils.findWebApplicationContext(request) instanceof SimpleWebApplicationContext)) {
		throw new ServletException("Incorrect WebApplicationContext");
	}
	if (!(RequestContextUtils.getLocaleResolver(request) instanceof AcceptHeaderLocaleResolver)) {
		throw new ServletException("Incorrect LocaleResolver");
	}
	if (!Locale.CANADA.equals(RequestContextUtils.getLocale(request))) {
		throw new ServletException("Incorrect Locale");
	}
	return null;
}
 
Example #17
Source File: FreeMarkerViewTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void validTemplateName() throws Exception {
	FreeMarkerView fv = new FreeMarkerView();

	WebApplicationContext wac = mock(WebApplicationContext.class);
	MockServletContext sc = new MockServletContext();

	Map<String, FreeMarkerConfig> configs = new HashMap<>();
	FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
	configurer.setConfiguration(new TestConfiguration());
	configurer.setServletContext(sc);
	configs.put("configurer", configurer);
	given(wac.getBeansOfType(FreeMarkerConfig.class, true, false)).willReturn(configs);
	given(wac.getServletContext()).willReturn(sc);

	fv.setUrl("templateName");
	fv.setApplicationContext(wac);

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.addPreferredLocale(Locale.US);
	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
	HttpServletResponse response = new MockHttpServletResponse();

	Map<String, Object> model = new HashMap<>();
	model.put("myattr", "myvalue");
	fv.render(model, request, response);

	assertEquals(AbstractView.DEFAULT_CONTENT_TYPE, response.getContentType());
}
 
Example #18
Source File: ViewResolverTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testInternalResourceViewResolverWithSpecificContextBeans() throws Exception {
	MockServletContext sc = new MockServletContext();
	final StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.registerSingleton("myBean", TestBean.class);
	wac.registerSingleton("myBean2", TestBean.class);
	wac.setServletContext(sc);
	wac.refresh();
	InternalResourceViewResolver vr = new InternalResourceViewResolver();
	Properties props = new Properties();
	props.setProperty("key1", "value1");
	vr.setAttributes(props);
	Map<String, Object> map = new HashMap<>();
	map.put("key2", new Integer(2));
	vr.setAttributesMap(map);
	vr.setExposedContextBeanNames(new String[] {"myBean2"});
	vr.setApplicationContext(wac);

	MockHttpServletRequest request = new MockHttpServletRequest(sc) {
		@Override
		public RequestDispatcher getRequestDispatcher(String path) {
			return new MockRequestDispatcher(path) {
				@Override
				public void forward(ServletRequest forwardRequest, ServletResponse forwardResponse) {
					assertTrue("Correct rc attribute", forwardRequest.getAttribute("rc") == null);
					assertEquals("value1", forwardRequest.getAttribute("key1"));
					assertEquals(new Integer(2), forwardRequest.getAttribute("key2"));
					assertNull(forwardRequest.getAttribute("myBean"));
					assertSame(wac.getBean("myBean2"), forwardRequest.getAttribute("myBean2"));
				}
			};
		}
	};
	HttpServletResponse response = new MockHttpServletResponse();
	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
	View view = vr.resolveViewName("example1", Locale.getDefault());
	view.render(new HashMap<String, Object>(), request, response);
}
 
Example #19
Source File: ViewResolverTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testInternalResourceViewResolverWithContextBeans() throws Exception {
	MockServletContext sc = new MockServletContext();
	final StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.registerSingleton("myBean", TestBean.class);
	wac.registerSingleton("myBean2", TestBean.class);
	wac.setServletContext(sc);
	wac.refresh();
	InternalResourceViewResolver vr = new InternalResourceViewResolver();
	Properties props = new Properties();
	props.setProperty("key1", "value1");
	vr.setAttributes(props);
	Map<String, Object> map = new HashMap<>();
	map.put("key2", new Integer(2));
	vr.setAttributesMap(map);
	vr.setExposeContextBeansAsAttributes(true);
	vr.setApplicationContext(wac);

	MockHttpServletRequest request = new MockHttpServletRequest(sc) {
		@Override
		public RequestDispatcher getRequestDispatcher(String path) {
			return new MockRequestDispatcher(path) {
				@Override
				public void forward(ServletRequest forwardRequest, ServletResponse forwardResponse) {
					assertTrue("Correct rc attribute", forwardRequest.getAttribute("rc") == null);
					assertEquals("value1", forwardRequest.getAttribute("key1"));
					assertEquals(new Integer(2), forwardRequest.getAttribute("key2"));
					assertSame(wac.getBean("myBean"), forwardRequest.getAttribute("myBean"));
					assertSame(wac.getBean("myBean2"), forwardRequest.getAttribute("myBean2"));
				}
			};
		}
	};
	HttpServletResponse response = new MockHttpServletResponse();
	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
	View view = vr.resolveViewName("example1", Locale.getDefault());
	view.render(new HashMap<String, Object>(), request, response);
}
 
Example #20
Source File: MessageBundleController.java    From attic-rave with Apache License 2.0 5 votes vote down vote up
public MessageBundleController() {
    clientMessagesCache = new HashMap<Locale, String>();
    acceptHeaderLocaleResolver = new AcceptHeaderLocaleResolver();
    clientMessagesResponseHeaders = new HttpHeaders();

    // set the common response headers that will be used by the getClientMessages response
    clientMessagesResponseHeaders.setCacheControl("max-age=" + CLIENT_MESSAGE_BUNDLE_CACHE_CONTROL_MAX_AGE);
    clientMessagesResponseHeaders.setContentType(MediaType.parseMediaType(JAVASCRIPT_CONTENT_TYPE));
    Locale.setDefault(Locale.ENGLISH);
}
 
Example #21
Source File: ViewResolverTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void doTestUrlBasedViewResolverWithoutPrefixes(UrlBasedViewResolver vr) throws Exception {
	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(new MockServletContext());
	wac.refresh();
	vr.setApplicationContext(wac);
	vr.setContentType("myContentType");
	vr.setRequestContextAttribute("rc");

	View view = vr.resolveViewName("example1", Locale.getDefault());
	assertEquals("Correct view class", JstlView.class, view.getClass());
	assertEquals("Correct URL", "example1", ((InternalResourceView) view).getUrl());
	assertEquals("Correct textContentType", "myContentType", ((InternalResourceView) view).getContentType());

	view = vr.resolveViewName("example2", Locale.getDefault());
	assertEquals("Correct view class", JstlView.class, view.getClass());
	assertEquals("Correct URL", "example2", ((InternalResourceView) view).getUrl());
	assertEquals("Correct textContentType", "myContentType", ((InternalResourceView) view).getContentType());

	HttpServletRequest request = new MockHttpServletRequest(wac.getServletContext());
	HttpServletResponse response = new MockHttpServletResponse();
	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
	request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver());
	Map<String, Object> model = new HashMap<>();
	TestBean tb = new TestBean();
	model.put("tb", tb);
	view.render(model, request, response);
	assertTrue("Correct tb attribute", tb.equals(request.getAttribute("tb")));
	assertTrue("Correct rc attribute", request.getAttribute("rc") instanceof RequestContext);

	view = vr.resolveViewName("redirect:myUrl", Locale.getDefault());
	assertEquals("Correct view class", RedirectView.class, view.getClass());
	assertEquals("Correct URL", "myUrl", ((RedirectView) view).getUrl());
	assertSame("View not initialized as bean", wac, ((RedirectView) view).getApplicationContext());

	view = vr.resolveViewName("forward:myUrl", Locale.getDefault());
	assertEquals("Correct view class", InternalResourceView.class, view.getClass());
	assertEquals("Correct URL", "myUrl", ((InternalResourceView) view).getUrl());
}
 
Example #22
Source File: SimpleWebApplicationContext.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
		throws ServletException, IOException {

	if (!(RequestContextUtils.findWebApplicationContext(request) instanceof SimpleWebApplicationContext)) {
		throw new ServletException("Incorrect WebApplicationContext");
	}
	if (!(RequestContextUtils.getLocaleResolver(request) instanceof AcceptHeaderLocaleResolver)) {
		throw new ServletException("Incorrect LocaleResolver");
	}
	if (!Locale.CANADA.equals(RequestContextUtils.getLocale(request))) {
		throw new ServletException("Incorrect Locale");
	}
	return null;
}
 
Example #23
Source File: FreeMarkerViewTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void keepExistingContentType() throws Exception {
	FreeMarkerView fv = new FreeMarkerView();

	WebApplicationContext wac = mock(WebApplicationContext.class);
	MockServletContext sc = new MockServletContext();

	Map<String, FreeMarkerConfig> configs = new HashMap<>();
	FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
	configurer.setConfiguration(new TestConfiguration());
	configurer.setServletContext(sc);
	configs.put("configurer", configurer);
	given(wac.getBeansOfType(FreeMarkerConfig.class, true, false)).willReturn(configs);
	given(wac.getServletContext()).willReturn(sc);

	fv.setUrl("templateName");
	fv.setApplicationContext(wac);

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.addPreferredLocale(Locale.US);
	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
	HttpServletResponse response = new MockHttpServletResponse();
	response.setContentType("myContentType");

	Map<String, Object> model = new HashMap<>();
	model.put("myattr", "myvalue");
	fv.render(model, request, response);

	assertEquals("myContentType", response.getContentType());
}
 
Example #24
Source File: FreeMarkerViewTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void validTemplateName() throws Exception {
	FreeMarkerView fv = new FreeMarkerView();

	WebApplicationContext wac = mock(WebApplicationContext.class);
	MockServletContext sc = new MockServletContext();

	Map<String, FreeMarkerConfig> configs = new HashMap<>();
	FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
	configurer.setConfiguration(new TestConfiguration());
	configurer.setServletContext(sc);
	configs.put("configurer", configurer);
	given(wac.getBeansOfType(FreeMarkerConfig.class, true, false)).willReturn(configs);
	given(wac.getServletContext()).willReturn(sc);

	fv.setUrl("templateName");
	fv.setApplicationContext(wac);

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.addPreferredLocale(Locale.US);
	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
	HttpServletResponse response = new MockHttpServletResponse();

	Map<String, Object> model = new HashMap<>();
	model.put("myattr", "myvalue");
	fv.render(model, request, response);

	assertEquals(AbstractView.DEFAULT_CONTENT_TYPE, response.getContentType());
}
 
Example #25
Source File: ViewResolverTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testInternalResourceViewResolverWithSpecificContextBeans() throws Exception {
	MockServletContext sc = new MockServletContext();
	final StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.registerSingleton("myBean", TestBean.class);
	wac.registerSingleton("myBean2", TestBean.class);
	wac.setServletContext(sc);
	wac.refresh();
	InternalResourceViewResolver vr = new InternalResourceViewResolver();
	Properties props = new Properties();
	props.setProperty("key1", "value1");
	vr.setAttributes(props);
	Map<String, Object> map = new HashMap<>();
	map.put("key2", new Integer(2));
	vr.setAttributesMap(map);
	vr.setExposedContextBeanNames(new String[] {"myBean2"});
	vr.setApplicationContext(wac);

	MockHttpServletRequest request = new MockHttpServletRequest(sc) {
		@Override
		public RequestDispatcher getRequestDispatcher(String path) {
			return new MockRequestDispatcher(path) {
				@Override
				public void forward(ServletRequest forwardRequest, ServletResponse forwardResponse) {
					assertTrue("Correct rc attribute", forwardRequest.getAttribute("rc") == null);
					assertEquals("value1", forwardRequest.getAttribute("key1"));
					assertEquals(new Integer(2), forwardRequest.getAttribute("key2"));
					assertNull(forwardRequest.getAttribute("myBean"));
					assertSame(wac.getBean("myBean2"), forwardRequest.getAttribute("myBean2"));
				}
			};
		}
	};
	HttpServletResponse response = new MockHttpServletResponse();
	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
	View view = vr.resolveViewName("example1", Locale.getDefault());
	view.render(new HashMap<String, Object>(), request, response);
}
 
Example #26
Source File: ViewResolverTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testInternalResourceViewResolverWithContextBeans() throws Exception {
	MockServletContext sc = new MockServletContext();
	final StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.registerSingleton("myBean", TestBean.class);
	wac.registerSingleton("myBean2", TestBean.class);
	wac.setServletContext(sc);
	wac.refresh();
	InternalResourceViewResolver vr = new InternalResourceViewResolver();
	Properties props = new Properties();
	props.setProperty("key1", "value1");
	vr.setAttributes(props);
	Map<String, Object> map = new HashMap<>();
	map.put("key2", new Integer(2));
	vr.setAttributesMap(map);
	vr.setExposeContextBeansAsAttributes(true);
	vr.setApplicationContext(wac);

	MockHttpServletRequest request = new MockHttpServletRequest(sc) {
		@Override
		public RequestDispatcher getRequestDispatcher(String path) {
			return new MockRequestDispatcher(path) {
				@Override
				public void forward(ServletRequest forwardRequest, ServletResponse forwardResponse) {
					assertTrue("Correct rc attribute", forwardRequest.getAttribute("rc") == null);
					assertEquals("value1", forwardRequest.getAttribute("key1"));
					assertEquals(new Integer(2), forwardRequest.getAttribute("key2"));
					assertSame(wac.getBean("myBean"), forwardRequest.getAttribute("myBean"));
					assertSame(wac.getBean("myBean2"), forwardRequest.getAttribute("myBean2"));
				}
			};
		}
	};
	HttpServletResponse response = new MockHttpServletResponse();
	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
	View view = vr.resolveViewName("example1", Locale.getDefault());
	view.render(new HashMap<String, Object>(), request, response);
}
 
Example #27
Source File: ViewResolverTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void doTestUrlBasedViewResolverWithoutPrefixes(UrlBasedViewResolver vr) throws Exception {
	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(new MockServletContext());
	wac.refresh();
	vr.setApplicationContext(wac);
	vr.setContentType("myContentType");
	vr.setRequestContextAttribute("rc");

	View view = vr.resolveViewName("example1", Locale.getDefault());
	assertEquals("Correct view class", JstlView.class, view.getClass());
	assertEquals("Correct URL", "example1", ((InternalResourceView) view).getUrl());
	assertEquals("Correct textContentType", "myContentType", ((InternalResourceView) view).getContentType());

	view = vr.resolveViewName("example2", Locale.getDefault());
	assertEquals("Correct view class", JstlView.class, view.getClass());
	assertEquals("Correct URL", "example2", ((InternalResourceView) view).getUrl());
	assertEquals("Correct textContentType", "myContentType", ((InternalResourceView) view).getContentType());

	HttpServletRequest request = new MockHttpServletRequest(wac.getServletContext());
	HttpServletResponse response = new MockHttpServletResponse();
	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
	request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver());
	Map<String, Object> model = new HashMap<>();
	TestBean tb = new TestBean();
	model.put("tb", tb);
	view.render(model, request, response);
	assertTrue("Correct tb attribute", tb.equals(request.getAttribute("tb")));
	assertTrue("Correct rc attribute", request.getAttribute("rc") instanceof RequestContext);

	view = vr.resolveViewName("redirect:myUrl", Locale.getDefault());
	assertEquals("Correct view class", RedirectView.class, view.getClass());
	assertEquals("Correct URL", "myUrl", ((RedirectView) view).getUrl());
	assertSame("View not initialized as bean", wac, ((RedirectView) view).getApplicationContext());

	view = vr.resolveViewName("forward:myUrl", Locale.getDefault());
	assertEquals("Correct view class", InternalResourceView.class, view.getClass());
	assertEquals("Correct URL", "myUrl", ((InternalResourceView) view).getUrl());
}
 
Example #28
Source File: SpringViewResolverProviderTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void testConstructorWithViewResolverNull() {
    new SpringViewResolverProvider(null, new AcceptHeaderLocaleResolver());
}
 
Example #29
Source File: VelocityViewTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Test
public void testExposeHelpers() throws Exception {
	final String templateName = "test.vm";

	WebApplicationContext wac = mock(WebApplicationContext.class);
	given(wac.getServletContext()).willReturn(new MockServletContext());

	final Template expectedTemplate = new Template();
	VelocityConfig vc = new VelocityConfig() {
		@Override
		public VelocityEngine getVelocityEngine() {
			return new TestVelocityEngine(templateName, expectedTemplate);
		}
	};
	Map<String, VelocityConfig> configurers = new HashMap<String, VelocityConfig>();
	configurers.put("velocityConfigurer", vc);
	given(wac.getBeansOfType(VelocityConfig.class, true, false)).willReturn(configurers);


	// let it ask for locale
	HttpServletRequest req = mock(HttpServletRequest.class);
	given(req.getAttribute(View.PATH_VARIABLES)).willReturn(null);
	given(req.getAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE)).willReturn(new AcceptHeaderLocaleResolver());
	given(req.getLocale()).willReturn(Locale.CANADA);

	final HttpServletResponse expectedResponse = new MockHttpServletResponse();

	VelocityView vv = new VelocityView() {
		@Override
		protected void mergeTemplate(Template template, Context context, HttpServletResponse response) throws Exception {
			assertTrue(template == expectedTemplate);
			assertTrue(response == expectedResponse);

			assertEquals("myValue", context.get("myHelper"));
			assertTrue(context.get("math") instanceof MathTool);

			assertTrue(context.get("dateTool") instanceof DateTool);
			DateTool dateTool = (DateTool) context.get("dateTool");
			assertTrue(dateTool.getLocale().equals(Locale.CANADA));

			assertTrue(context.get("numberTool") instanceof NumberTool);
			NumberTool numberTool = (NumberTool) context.get("numberTool");
			assertTrue(numberTool.getLocale().equals(Locale.CANADA));
		}

		@Override
		protected void exposeHelpers(Map<String, Object> model, HttpServletRequest request) throws Exception {
			model.put("myHelper", "myValue");
		}
	};

	vv.setUrl(templateName);
	vv.setApplicationContext(wac);
	Map<String, Class<?>> toolAttributes = new HashMap<String, Class<?>>();
	toolAttributes.put("math", MathTool.class);
	vv.setToolAttributes(toolAttributes);
	vv.setDateToolAttribute("dateTool");
	vv.setNumberToolAttribute("numberTool");
	vv.setExposeSpringMacroHelpers(false);

	vv.render(new HashMap<String, Object>(), req, expectedResponse);

	assertEquals(AbstractView.DEFAULT_CONTENT_TYPE, expectedResponse.getContentType());
}
 
Example #30
Source File: ViewResolverTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Test
public void testInternalResourceViewResolverWithAttributes() throws Exception {
	MockServletContext sc = new MockServletContext();
	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(sc);
	wac.refresh();
	InternalResourceViewResolver vr = new InternalResourceViewResolver();
	Properties props = new Properties();
	props.setProperty("key1", "value1");
	vr.setAttributes(props);
	Map map = new HashMap();
	map.put("key2", new Integer(2));
	vr.setAttributesMap(map);
	vr.setApplicationContext(wac);

	View view = vr.resolveViewName("example1", Locale.getDefault());
	assertEquals("Correct view class", JstlView.class, view.getClass());
	assertEquals("Correct URL", "example1", ((InternalResourceView) view).getUrl());
	Map attributes = ((InternalResourceView) view).getStaticAttributes();
	assertEquals("value1", attributes.get("key1"));
	assertEquals(new Integer(2), attributes.get("key2"));

	view = vr.resolveViewName("example2", Locale.getDefault());
	assertEquals("Correct view class", JstlView.class, view.getClass());
	assertEquals("Correct URL", "example2", ((InternalResourceView) view).getUrl());
	attributes = ((InternalResourceView) view).getStaticAttributes();
	assertEquals("value1", attributes.get("key1"));
	assertEquals(new Integer(2), attributes.get("key2"));

	MockHttpServletRequest request = new MockHttpServletRequest(sc);
	HttpServletResponse response = new MockHttpServletResponse();
	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
	Map model = new HashMap();
	TestBean tb = new TestBean();
	model.put("tb", tb);
	view.render(model, request, response);

	assertTrue("Correct tb attribute", tb.equals(request.getAttribute("tb")));
	assertTrue("Correct rc attribute", request.getAttribute("rc") == null);
	assertEquals("value1", request.getAttribute("key1"));
	assertEquals(new Integer(2), request.getAttribute("key2"));
}