org.springframework.web.accept.FixedContentNegotiationStrategy Java Examples

The following examples show how to use org.springframework.web.accept.FixedContentNegotiationStrategy. 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: ContentNegotiatingViewResolverTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void nestedViewResolverIsNotSpringBean() throws Exception {
	StaticWebApplicationContext webAppContext = new StaticWebApplicationContext();
	webAppContext.setServletContext(new MockServletContext());
	webAppContext.refresh();

	InternalResourceViewResolver nestedResolver = new InternalResourceViewResolver();
	nestedResolver.setApplicationContext(webAppContext);
	nestedResolver.setViewClass(InternalResourceView.class);
	viewResolver.setViewResolvers(new ArrayList<>(Arrays.asList(nestedResolver)));

	FixedContentNegotiationStrategy fixedStrategy = new FixedContentNegotiationStrategy(MediaType.TEXT_HTML);
	viewResolver.setContentNegotiationManager(new ContentNegotiationManager(fixedStrategy));

	viewResolver.afterPropertiesSet();

	String viewName = "view";
	Locale locale = Locale.ENGLISH;

	View result = viewResolver.resolveViewName(viewName, locale);
	assertNotNull("Invalid view", result);
}
 
Example #2
Source File: ProducesRequestConditionTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test // gh-22853
public void matchAndCompare() {
	ContentNegotiationManager manager = new ContentNegotiationManager(
			new HeaderContentNegotiationStrategy(),
			new FixedContentNegotiationStrategy(MediaType.TEXT_HTML));

	ProducesRequestCondition none = new ProducesRequestCondition(new String[0], null, manager);
	ProducesRequestCondition html = new ProducesRequestCondition(new String[] {"text/html"}, null, manager);

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.addHeader("Accept", "*/*");

	ProducesRequestCondition noneMatch = none.getMatchingCondition(request);
	ProducesRequestCondition htmlMatch = html.getMatchingCondition(request);

	assertEquals(1, noneMatch.compareTo(htmlMatch, request));
}
 
Example #3
Source File: ContentNegotiatingViewResolverTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void nestedViewResolverIsNotSpringBean() throws Exception {
	StaticWebApplicationContext webAppContext = new StaticWebApplicationContext();
	webAppContext.setServletContext(new MockServletContext());
	webAppContext.refresh();

	InternalResourceViewResolver nestedResolver = new InternalResourceViewResolver();
	nestedResolver.setApplicationContext(webAppContext);
	nestedResolver.setViewClass(InternalResourceView.class);
	viewResolver.setViewResolvers(new ArrayList<>(Arrays.asList(nestedResolver)));

	FixedContentNegotiationStrategy fixedStrategy = new FixedContentNegotiationStrategy(MediaType.TEXT_HTML);
	viewResolver.setContentNegotiationManager(new ContentNegotiationManager(fixedStrategy));

	viewResolver.afterPropertiesSet();

	String viewName = "view";
	Locale locale = Locale.ENGLISH;

	View result = viewResolver.resolveViewName(viewName, locale);
	assertNotNull("Invalid view", result);
}
 
Example #4
Source File: ContentNegotiatingViewResolverTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void nestedViewResolverIsNotSpringBean() throws Exception {
	StaticWebApplicationContext webAppContext = new StaticWebApplicationContext();
	webAppContext.setServletContext(new MockServletContext());
	webAppContext.refresh();

	InternalResourceViewResolver nestedResolver = new InternalResourceViewResolver();
	nestedResolver.setApplicationContext(webAppContext);
	nestedResolver.setViewClass(InternalResourceView.class);
	viewResolver.setViewResolvers(new ArrayList<ViewResolver>(Arrays.asList(nestedResolver)));

	FixedContentNegotiationStrategy fixedStrategy = new FixedContentNegotiationStrategy(MediaType.TEXT_HTML);
	viewResolver.setContentNegotiationManager(new ContentNegotiationManager(fixedStrategy));

	viewResolver.afterPropertiesSet();

	String viewName = "view";
	Locale locale = Locale.ENGLISH;

	View result = viewResolver.resolveViewName(viewName, locale);
	assertNotNull("Invalid view", result);
}
 
Example #5
Source File: ContentNegotiatingViewResolverTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void resolveViewNameWithDefaultContentType() throws Exception {
	request.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");

	MediaType mediaType = new MediaType("application", "xml");
	FixedContentNegotiationStrategy fixedStrategy = new FixedContentNegotiationStrategy(mediaType);
	viewResolver.setContentNegotiationManager(new ContentNegotiationManager(fixedStrategy));

	ViewResolver viewResolverMock1 = mock(ViewResolver.class, "viewResolver1");
	ViewResolver viewResolverMock2 = mock(ViewResolver.class, "viewResolver2");
	viewResolver.setViewResolvers(Arrays.asList(viewResolverMock1, viewResolverMock2));
	viewResolver.afterPropertiesSet();

	View viewMock1 = mock(View.class, "application_xml");
	View viewMock2 = mock(View.class, "text_html");

	String viewName = "view";
	Locale locale = Locale.ENGLISH;

	given(viewResolverMock1.resolveViewName(viewName, locale)).willReturn(viewMock1);
	given(viewResolverMock2.resolveViewName(viewName, locale)).willReturn(viewMock2);
	given(viewMock1.getContentType()).willReturn("application/xml");
	given(viewMock2.getContentType()).willReturn("text/html;charset=ISO-8859-1");

	View result = viewResolver.resolveViewName(viewName, locale);
	assertSame("Invalid view", viewMock1, result);
}
 
Example #6
Source File: ContentNegotiationConfigurerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void setDefaultContentTypeStrategy() throws Exception {
	this.configurer.defaultContentTypeStrategy(new FixedContentNegotiationStrategy(MediaType.APPLICATION_JSON));
	ContentNegotiationManager manager = this.configurer.buildContentNegotiationManager();

	assertEquals(MediaType.APPLICATION_JSON, manager.resolveMediaTypes(this.webRequest).get(0));
}
 
Example #7
Source File: ContentNegotiatingViewResolverTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void resolveViewNameWithDefaultContentType() throws Exception {
	request.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");

	MediaType mediaType = new MediaType("application", "xml");
	FixedContentNegotiationStrategy fixedStrategy = new FixedContentNegotiationStrategy(mediaType);
	viewResolver.setContentNegotiationManager(new ContentNegotiationManager(fixedStrategy));

	ViewResolver viewResolverMock1 = mock(ViewResolver.class, "viewResolver1");
	ViewResolver viewResolverMock2 = mock(ViewResolver.class, "viewResolver2");
	viewResolver.setViewResolvers(Arrays.asList(viewResolverMock1, viewResolverMock2));
	viewResolver.afterPropertiesSet();

	View viewMock1 = mock(View.class, "application_xml");
	View viewMock2 = mock(View.class, "text_html");

	String viewName = "view";
	Locale locale = Locale.ENGLISH;

	given(viewResolverMock1.resolveViewName(viewName, locale)).willReturn(viewMock1);
	given(viewResolverMock2.resolveViewName(viewName, locale)).willReturn(viewMock2);
	given(viewMock1.getContentType()).willReturn("application/xml");
	given(viewMock2.getContentType()).willReturn("text/html;charset=ISO-8859-1");

	View result = viewResolver.resolveViewName(viewName, locale);
	assertSame("Invalid view", viewMock1, result);
}
 
Example #8
Source File: ContentNegotiationConfigurerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void setDefaultContentTypeStrategy() throws Exception {
	this.configurer.defaultContentTypeStrategy(new FixedContentNegotiationStrategy(MediaType.APPLICATION_JSON));
	ContentNegotiationManager manager = this.configurer.buildContentNegotiationManager();

	assertEquals(MediaType.APPLICATION_JSON, manager.resolveMediaTypes(this.webRequest).get(0));
}
 
Example #9
Source File: ContentNegotiatingViewResolverTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void resolveViewNameWithDefaultContentType() throws Exception {
	request.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");

	MediaType mediaType = new MediaType("application", "xml");
	FixedContentNegotiationStrategy fixedStrategy = new FixedContentNegotiationStrategy(mediaType);
	viewResolver.setContentNegotiationManager(new ContentNegotiationManager(fixedStrategy));

	ViewResolver viewResolverMock1 = mock(ViewResolver.class, "viewResolver1");
	ViewResolver viewResolverMock2 = mock(ViewResolver.class, "viewResolver2");
	viewResolver.setViewResolvers(Arrays.asList(viewResolverMock1, viewResolverMock2));
	viewResolver.afterPropertiesSet();

	View viewMock1 = mock(View.class, "application_xml");
	View viewMock2 = mock(View.class, "text_html");

	String viewName = "view";
	Locale locale = Locale.ENGLISH;

	given(viewResolverMock1.resolveViewName(viewName, locale)).willReturn(viewMock1);
	given(viewResolverMock2.resolveViewName(viewName, locale)).willReturn(viewMock2);
	given(viewMock1.getContentType()).willReturn("application/xml");
	given(viewMock2.getContentType()).willReturn("text/html;charset=ISO-8859-1");

	View result = viewResolver.resolveViewName(viewName, locale);
	assertSame("Invalid view", viewMock1, result);
}
 
Example #10
Source File: ContentNegotiationConfigurerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void setDefaultContentTypeStrategy() throws Exception {
	this.configurer.defaultContentTypeStrategy(new FixedContentNegotiationStrategy(MediaType.APPLICATION_JSON));
	ContentNegotiationManager manager = this.configurer.getContentNegotiationManager();

	assertEquals(Arrays.asList(MediaType.APPLICATION_JSON), manager.resolveMediaTypes(this.webRequest));
}
 
Example #11
Source File: ViewResolutionTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testContentNegotiation() throws Exception {

	Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
	marshaller.setClassesToBeBound(Person.class);

	List<View> viewList = new ArrayList<View>();
	viewList.add(new MappingJackson2JsonView());
	viewList.add(new MarshallingView(marshaller));

	ContentNegotiationManager manager = new ContentNegotiationManager(
			new HeaderContentNegotiationStrategy(), new FixedContentNegotiationStrategy(MediaType.TEXT_HTML));

	ContentNegotiatingViewResolver cnViewResolver = new ContentNegotiatingViewResolver();
	cnViewResolver.setDefaultViews(viewList);
	cnViewResolver.setContentNegotiationManager(manager);
	cnViewResolver.afterPropertiesSet();

	MockMvc mockMvc =
		standaloneSetup(new PersonController())
			.setViewResolvers(cnViewResolver, new InternalResourceViewResolver())
			.build();

	mockMvc.perform(get("/person/Corea"))
		.andExpect(status().isOk())
		.andExpect(model().size(1))
		.andExpect(model().attributeExists("person"))
		.andExpect(forwardedUrl("person/show"));

	mockMvc.perform(get("/person/Corea").accept(MediaType.APPLICATION_JSON))
		.andExpect(status().isOk())
		.andExpect(content().contentType(MediaType.APPLICATION_JSON))
		.andExpect(jsonPath("$.person.name").value("Corea"));

	mockMvc.perform(get("/person/Corea").accept(MediaType.APPLICATION_XML))
		.andExpect(status().isOk())
		.andExpect(content().contentType(MediaType.APPLICATION_XML))
		.andExpect(xpath("/person/name/text()").string(equalTo("Corea")));
}
 
Example #12
Source File: WebConfig.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Bean
public ContentNegotiationManager contentNegotiationManager(
    CustomPathExtensionContentNegotiationStrategy customPathExtensionContentNegotiationStrategy,
    ParameterContentNegotiationStrategy parameterContentNegotiationStrategy,
    HeaderContentNegotiationStrategy headerContentNegotiationStrategy,
    FixedContentNegotiationStrategy fixedContentNegotiationStrategy )
{
    return new ContentNegotiationManager( Arrays.asList( customPathExtensionContentNegotiationStrategy,
        parameterContentNegotiationStrategy, headerContentNegotiationStrategy, fixedContentNegotiationStrategy ) );
}
 
Example #13
Source File: AdviceTraitTesting.java    From problem-spring-web with MIT License 5 votes vote down vote up
default MockMvc mvc() {
    final ObjectMapper mapper = mapper();

    return MockMvcBuilders
            .standaloneSetup(new ExampleRestController())
            .setContentNegotiationManager(new ContentNegotiationManager(singletonList(
                    new FixedContentNegotiationStrategy(APPLICATION_JSON))))
            .setControllerAdvice(unit())
            .setMessageConverters(
                    new MappingJackson2HttpMessageConverter(mapper),
                    new MappingJackson2XmlHttpMessageConverter())
            .build();
}
 
Example #14
Source File: ViewResolutionTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Test
public void testContentNegotiation() throws Exception {
	Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
	marshaller.setClassesToBeBound(Person.class);

	List<View> viewList = new ArrayList<>();
	viewList.add(new MappingJackson2JsonView());
	viewList.add(new MarshallingView(marshaller));

	ContentNegotiationManager manager = new ContentNegotiationManager(
			new HeaderContentNegotiationStrategy(), new FixedContentNegotiationStrategy(MediaType.TEXT_HTML));

	ContentNegotiatingViewResolver cnViewResolver = new ContentNegotiatingViewResolver();
	cnViewResolver.setDefaultViews(viewList);
	cnViewResolver.setContentNegotiationManager(manager);
	cnViewResolver.afterPropertiesSet();

	MockMvc mockMvc =
		standaloneSetup(new PersonController())
			.setViewResolvers(cnViewResolver, new InternalResourceViewResolver())
			.build();

	mockMvc.perform(get("/person/Corea"))
		.andExpect(status().isOk())
		.andExpect(model().size(1))
		.andExpect(model().attributeExists("person"))
		.andExpect(forwardedUrl("person/show"));

	mockMvc.perform(get("/person/Corea").accept(MediaType.APPLICATION_JSON))
		.andExpect(status().isOk())
		.andExpect(content().contentType(MediaType.APPLICATION_JSON))
		.andExpect(jsonPath("$.person.name").value("Corea"));

	mockMvc.perform(get("/person/Corea").accept(MediaType.APPLICATION_XML))
		.andExpect(status().isOk())
		.andExpect(content().contentType(MediaType.APPLICATION_XML))
		.andExpect(xpath("/person/name/text()").string(equalTo("Corea")));
}
 
Example #15
Source File: ViewResolutionTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Test
public void testContentNegotiation() throws Exception {
	Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
	marshaller.setClassesToBeBound(Person.class);

	List<View> viewList = new ArrayList<>();
	viewList.add(new MappingJackson2JsonView());
	viewList.add(new MarshallingView(marshaller));

	ContentNegotiationManager manager = new ContentNegotiationManager(
			new HeaderContentNegotiationStrategy(), new FixedContentNegotiationStrategy(MediaType.TEXT_HTML));

	ContentNegotiatingViewResolver cnViewResolver = new ContentNegotiatingViewResolver();
	cnViewResolver.setDefaultViews(viewList);
	cnViewResolver.setContentNegotiationManager(manager);
	cnViewResolver.afterPropertiesSet();

	MockMvc mockMvc =
		standaloneSetup(new PersonController())
			.setViewResolvers(cnViewResolver, new InternalResourceViewResolver())
			.build();

	mockMvc.perform(get("/person/Corea"))
		.andExpect(status().isOk())
		.andExpect(model().size(1))
		.andExpect(model().attributeExists("person"))
		.andExpect(forwardedUrl("person/show"));

	mockMvc.perform(get("/person/Corea").accept(MediaType.APPLICATION_JSON))
		.andExpect(status().isOk())
		.andExpect(content().contentType(MediaType.APPLICATION_JSON))
		.andExpect(jsonPath("$.person.name").value("Corea"));

	mockMvc.perform(get("/person/Corea").accept(MediaType.APPLICATION_XML))
		.andExpect(status().isOk())
		.andExpect(content().contentType(MediaType.APPLICATION_XML))
		.andExpect(xpath("/person/name/text()").string(equalTo("Corea")));
}
 
Example #16
Source File: WebConfig.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Bean
public FixedContentNegotiationStrategy fixedContentNegotiationStrategy()
{
    return new FixedContentNegotiationStrategy( MediaType.APPLICATION_JSON );
}