Java Code Examples for org.springframework.web.accept.ContentNegotiationManagerFactoryBean#getObject()

The following examples show how to use org.springframework.web.accept.ContentNegotiationManagerFactoryBean#getObject() . 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: ResourceHttpRequestHandlerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test  // SPR-13658
public void getResourceWithRegisteredMediaType() throws Exception {
	ContentNegotiationManagerFactoryBean factory = new ContentNegotiationManagerFactoryBean();
	factory.addMediaType("bar", new MediaType("foo", "bar"));
	factory.afterPropertiesSet();
	ContentNegotiationManager manager = factory.getObject();

	List<Resource> paths = Collections.singletonList(new ClassPathResource("test/", getClass()));
	ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler();
	handler.setServletContext(new MockServletContext());
	handler.setLocations(paths);
	handler.setContentNegotiationManager(manager);
	handler.afterPropertiesSet();

	this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.bar");
	handler.handleRequest(this.request, this.response);

	assertEquals("foo/bar", this.response.getContentType());
	assertEquals("h1 { color:red; }", this.response.getContentAsString());
}
 
Example 2
Source File: ResourceHttpRequestHandlerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test  // SPR-14577
public void getMediaTypeWithFavorPathExtensionOff() throws Exception {
	ContentNegotiationManagerFactoryBean factory = new ContentNegotiationManagerFactoryBean();
	factory.setFavorPathExtension(false);
	factory.afterPropertiesSet();
	ContentNegotiationManager manager = factory.getObject();

	List<Resource> paths = Collections.singletonList(new ClassPathResource("test/", getClass()));
	ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler();
	handler.setServletContext(new MockServletContext());
	handler.setLocations(paths);
	handler.setContentNegotiationManager(manager);
	handler.afterPropertiesSet();

	this.request.addHeader("Accept", "application/json,text/plain,*/*");
	this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.html");
	handler.handleRequest(this.request, this.response);

	assertEquals("text/html", this.response.getContentType());
}
 
Example 3
Source File: ResourceHttpRequestHandlerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test  // SPR-13658
public void getResourceWithRegisteredMediaType() throws Exception {
	ContentNegotiationManagerFactoryBean factory = new ContentNegotiationManagerFactoryBean();
	factory.addMediaType("bar", new MediaType("foo", "bar"));
	factory.afterPropertiesSet();
	ContentNegotiationManager manager = factory.getObject();

	List<Resource> paths = Collections.singletonList(new ClassPathResource("test/", getClass()));
	ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler();
	handler.setServletContext(new MockServletContext());
	handler.setLocations(paths);
	handler.setContentNegotiationManager(manager);
	handler.afterPropertiesSet();

	this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.bar");
	handler.handleRequest(this.request, this.response);

	assertEquals("foo/bar", this.response.getContentType());
	assertEquals("h1 { color:red; }", this.response.getContentAsString());
}
 
Example 4
Source File: ResourceHttpRequestHandlerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test  // SPR-14577
public void getMediaTypeWithFavorPathExtensionOff() throws Exception {
	ContentNegotiationManagerFactoryBean factory = new ContentNegotiationManagerFactoryBean();
	factory.setFavorPathExtension(false);
	factory.afterPropertiesSet();
	ContentNegotiationManager manager = factory.getObject();

	List<Resource> paths = Collections.singletonList(new ClassPathResource("test/", getClass()));
	ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler();
	handler.setServletContext(new MockServletContext());
	handler.setLocations(paths);
	handler.setContentNegotiationManager(manager);
	handler.afterPropertiesSet();

	this.request.addHeader("Accept", "application/json,text/plain,*/*");
	this.request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, "foo.html");
	handler.handleRequest(this.request, this.response);

	assertEquals("text/html", this.response.getContentType());
}
 
Example 5
Source File: RequestResponseBodyMethodProcessorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void addContentDispositionHeader() throws Exception {
	ContentNegotiationManagerFactoryBean factory = new ContentNegotiationManagerFactoryBean();
	factory.addMediaType("pdf", new MediaType("application", "pdf"));
	factory.afterPropertiesSet();

	RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(
			Collections.singletonList(new StringHttpMessageConverter()),
			factory.getObject());

	assertContentDisposition(processor, false, "/hello.json", "whitelisted extension");
	assertContentDisposition(processor, false, "/hello.pdf", "registered extension");
	assertContentDisposition(processor, true, "/hello.dataless", "unknown extension");

	// path parameters
	assertContentDisposition(processor, false, "/hello.json;a=b", "path param shouldn't cause issue");
	assertContentDisposition(processor, true, "/hello.json;a=b;setup.dataless", "unknown ext in path params");
	assertContentDisposition(processor, true, "/hello.dataless;a=b;setup.json", "unknown ext in filename");
	assertContentDisposition(processor, false, "/hello.json;a=b;setup.json", "whitelisted extensions");

	// encoded dot
	assertContentDisposition(processor, true, "/hello%2Edataless;a=b;setup.json", "encoded dot in filename");
	assertContentDisposition(processor, true, "/hello.json;a=b;setup%2Edataless", "encoded dot in path params");
	assertContentDisposition(processor, true, "/hello.dataless%3Bsetup.bat", "encoded dot in path params");

	this.servletRequest.setAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE, "/hello.bat");
	assertContentDisposition(processor, true, "/bonjour", "forwarded URL");
	this.servletRequest.removeAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE);
}
 
Example 6
Source File: ReactiveTypeHandlerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Before
public void setup() throws Exception {
	ContentNegotiationManagerFactoryBean factoryBean = new ContentNegotiationManagerFactoryBean();
	factoryBean.afterPropertiesSet();
	ContentNegotiationManager manager = factoryBean.getObject();
	ReactiveAdapterRegistry adapterRegistry = ReactiveAdapterRegistry.getSharedInstance();
	this.handler = new ReactiveTypeHandler(adapterRegistry, new SyncTaskExecutor(), manager);
	resetRequest();
}
 
Example 7
Source File: RequestResponseBodyMethodProcessorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void addContentDispositionHeader() throws Exception {
	ContentNegotiationManagerFactoryBean factory = new ContentNegotiationManagerFactoryBean();
	factory.addMediaType("pdf", new MediaType("application", "pdf"));
	factory.afterPropertiesSet();

	RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(
			Collections.singletonList(new StringHttpMessageConverter()),
			factory.getObject());

	assertContentDisposition(processor, false, "/hello.json", "whitelisted extension");
	assertContentDisposition(processor, false, "/hello.pdf", "registered extension");
	assertContentDisposition(processor, true, "/hello.dataless", "unknown extension");

	// path parameters
	assertContentDisposition(processor, false, "/hello.json;a=b", "path param shouldn't cause issue");
	assertContentDisposition(processor, true, "/hello.json;a=b;setup.dataless", "unknown ext in path params");
	assertContentDisposition(processor, true, "/hello.dataless;a=b;setup.json", "unknown ext in filename");
	assertContentDisposition(processor, false, "/hello.json;a=b;setup.json", "whitelisted extensions");

	// encoded dot
	assertContentDisposition(processor, true, "/hello%2Edataless;a=b;setup.json", "encoded dot in filename");
	assertContentDisposition(processor, true, "/hello.json;a=b;setup%2Edataless", "encoded dot in path params");
	assertContentDisposition(processor, true, "/hello.dataless%3Bsetup.bat", "encoded dot in path params");

	this.servletRequest.setAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE, "/hello.bat");
	assertContentDisposition(processor, true, "/bonjour", "forwarded URL");
	this.servletRequest.removeAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE);
}
 
Example 8
Source File: ReactiveTypeHandlerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Before
public void setup() throws Exception {
	ContentNegotiationManagerFactoryBean factoryBean = new ContentNegotiationManagerFactoryBean();
	factoryBean.afterPropertiesSet();
	ContentNegotiationManager manager = factoryBean.getObject();
	ReactiveAdapterRegistry adapterRegistry = ReactiveAdapterRegistry.getSharedInstance();
	this.handler = new ReactiveTypeHandler(adapterRegistry, new SyncTaskExecutor(), manager);
	resetRequest();
}
 
Example 9
Source File: RequestResponseBodyMethodProcessorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void addContentDispositionHeader() throws Exception {

	ContentNegotiationManagerFactoryBean factory = new ContentNegotiationManagerFactoryBean();
	factory.addMediaType("pdf", new MediaType("application", "pdf"));
	factory.afterPropertiesSet();

	RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(
			Collections.singletonList(new StringHttpMessageConverter()),
			factory.getObject());

	assertContentDisposition(processor, false, "/hello.json", "whitelisted extension");
	assertContentDisposition(processor, false, "/hello.pdf", "registered extension");
	assertContentDisposition(processor, true, "/hello.dataless", "uknown extension");

	// path parameters
	assertContentDisposition(processor, false, "/hello.json;a=b", "path param shouldn't cause issue");
	assertContentDisposition(processor, true, "/hello.json;a=b;setup.dataless", "uknown ext in path params");
	assertContentDisposition(processor, true, "/hello.dataless;a=b;setup.json", "uknown ext in filename");
	assertContentDisposition(processor, false, "/hello.json;a=b;setup.json", "whitelisted extensions");

	// encoded dot
	assertContentDisposition(processor, true, "/hello%2Edataless;a=b;setup.json", "encoded dot in filename");
	assertContentDisposition(processor, true, "/hello.json;a=b;setup%2Edataless", "encoded dot in path params");
	assertContentDisposition(processor, true, "/hello.dataless%3Bsetup.bat", "encoded dot in path params");

	this.servletRequest.setAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE, "/hello.bat");
	assertContentDisposition(processor, true, "/bonjour", "forwarded URL");
	this.servletRequest.removeAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE);
}