org.springframework.web.accept.ContentNegotiationManagerFactoryBean Java Examples

The following examples show how to use org.springframework.web.accept.ContentNegotiationManagerFactoryBean. 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: ServletAnnotationControllerHandlerMethodTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void responseBodyAsHtml() throws Exception {
	initServlet(wac -> {
		ContentNegotiationManagerFactoryBean factoryBean = new ContentNegotiationManagerFactoryBean();
		factoryBean.afterPropertiesSet();
		RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
		adapterDef.getPropertyValues().add("contentNegotiationManager", factoryBean.getObject());
		wac.registerBeanDefinition("handlerAdapter", adapterDef);
	}, TextRestController.class);

	byte[] content = "alert('boo')".getBytes(StandardCharsets.ISO_8859_1);
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/a1.html");
	request.setContent(content);
	MockHttpServletResponse response = new MockHttpServletResponse();

	getServlet().service(request, response);

	assertEquals(200, response.getStatus());
	assertEquals("text/html;charset=ISO-8859-1", response.getContentType());
	assertEquals("inline;filename=f.txt", response.getHeader("Content-Disposition"));
	assertArrayEquals(content, response.getContentAsByteArray());
}
 
Example #2
Source File: ServletAnnotationControllerHandlerMethodTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void responseBodyAsTextWithCssExtension() throws Exception {
	initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {
		@Override
		public void initialize(GenericWebApplicationContext wac) {
			ContentNegotiationManagerFactoryBean factoryBean = new ContentNegotiationManagerFactoryBean();
			factoryBean.afterPropertiesSet();
			RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
			adapterDef.getPropertyValues().add("contentNegotiationManager", factoryBean.getObject());
			wac.registerBeanDefinition("handlerAdapter", adapterDef);
		}
	}, TextRestController.class);

	byte[] content = "body".getBytes(Charset.forName("ISO-8859-1"));
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/a4.css");
	request.setContent(content);
	MockHttpServletResponse response = new MockHttpServletResponse();

	getServlet().service(request, response);

	assertEquals(200, response.getStatus());
	assertEquals("text/css", response.getContentType());
	assertNull(response.getHeader("Content-Disposition"));
	assertArrayEquals(content, response.getContentAsByteArray());
}
 
Example #3
Source File: ServletAnnotationControllerHandlerMethodTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void responseBodyAsHtmlWithProducesCondition() throws Exception {
	initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {
		@Override
		public void initialize(GenericWebApplicationContext wac) {
			ContentNegotiationManagerFactoryBean factoryBean = new ContentNegotiationManagerFactoryBean();
			factoryBean.afterPropertiesSet();
			RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
			adapterDef.getPropertyValues().add("contentNegotiationManager", factoryBean.getObject());
			wac.registerBeanDefinition("handlerAdapter", adapterDef);
		}
	}, TextRestController.class);

	byte[] content = "alert('boo')".getBytes(Charset.forName("ISO-8859-1"));
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/a3.html");
	request.setContent(content);
	MockHttpServletResponse response = new MockHttpServletResponse();

	getServlet().service(request, response);

	assertEquals(200, response.getStatus());
	assertEquals("text/html", response.getContentType());
	assertNull(response.getHeader("Content-Disposition"));
	assertArrayEquals(content, response.getContentAsByteArray());
}
 
Example #4
Source File: ServletAnnotationControllerHandlerMethodTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void responseBodyAsHtmlWithSuffixPresent() throws Exception {
	initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {
		@Override
		public void initialize(GenericWebApplicationContext wac) {
			ContentNegotiationManagerFactoryBean factoryBean = new ContentNegotiationManagerFactoryBean();
			factoryBean.afterPropertiesSet();
			RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
			adapterDef.getPropertyValues().add("contentNegotiationManager", factoryBean.getObject());
			wac.registerBeanDefinition("handlerAdapter", adapterDef);
		}
	}, TextRestController.class);

	byte[] content = "alert('boo')".getBytes(Charset.forName("ISO-8859-1"));
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/a2.html");
	request.setContent(content);
	MockHttpServletResponse response = new MockHttpServletResponse();

	getServlet().service(request, response);

	assertEquals(200, response.getStatus());
	assertEquals("text/html", response.getContentType());
	assertNull(response.getHeader("Content-Disposition"));
	assertArrayEquals(content, response.getContentAsByteArray());
}
 
Example #5
Source File: ServletAnnotationControllerHandlerMethodTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void responseBodyAsHtml() throws Exception {
	initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {
		@Override
		public void initialize(GenericWebApplicationContext wac) {
			ContentNegotiationManagerFactoryBean factoryBean = new ContentNegotiationManagerFactoryBean();
			factoryBean.afterPropertiesSet();
			RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
			adapterDef.getPropertyValues().add("contentNegotiationManager", factoryBean.getObject());
			wac.registerBeanDefinition("handlerAdapter", adapterDef);
		}
	}, TextRestController.class);

	byte[] content = "alert('boo')".getBytes(Charset.forName("ISO-8859-1"));
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/a1.html");
	request.setContent(content);
	MockHttpServletResponse response = new MockHttpServletResponse();

	getServlet().service(request, response);

	assertEquals(200, response.getStatus());
	assertEquals("text/html", response.getContentType());
	assertEquals("inline;filename=f.txt", response.getHeader("Content-Disposition"));
	assertArrayEquals(content, response.getContentAsByteArray());
}
 
Example #6
Source File: ConfigurableContentNegotiationManagerWebMvcConfigurerTest.java    From spring-webmvc-support with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testConfigureContentNegotiationOnDefaultValues() {

    WebMvcConfigurer webMvcConfigurer =
            new ConfigurableContentNegotiationManagerWebMvcConfigurer(new HashMap<String, String>());

    ContentNegotiationConfigurer configurer = new ContentNegotiationConfigurer(new MockServletContext());

    webMvcConfigurer.configureContentNegotiation(configurer);

    ContentNegotiationManagerFactoryBean factoryBean =
            FieldUtils.getFieldValue(configurer, "factory", ContentNegotiationManagerFactoryBean.class);

    Assert.assertTrue(FieldUtils.getFieldValue(factoryBean, "favorPathExtension", boolean.class));
    Assert.assertFalse(FieldUtils.getFieldValue(factoryBean, "favorParameter", boolean.class));
    Assert.assertFalse(FieldUtils.getFieldValue(factoryBean, "ignoreAcceptHeader", boolean.class));
    Assert.assertNull(FieldUtils.getFieldValue(factoryBean, "useJaf", Boolean.class));
    Assert.assertEquals("format", FieldUtils.getFieldValue(factoryBean, "parameterName", String.class));
    Assert.assertTrue(FieldUtils.getFieldValue(factoryBean, "mediaTypes", Map.class).isEmpty());
    Assert.assertNull(FieldUtils.getFieldValue(factoryBean, "defaultContentType", MediaType.class));

}
 
Example #7
Source File: ServletAnnotationControllerHandlerMethodTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void responseBodyAsTextWithCssExtension() throws Exception {
	initServlet(wac -> {
		ContentNegotiationManagerFactoryBean factoryBean = new ContentNegotiationManagerFactoryBean();
		factoryBean.afterPropertiesSet();
		RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
		adapterDef.getPropertyValues().add("contentNegotiationManager", factoryBean.getObject());
		wac.registerBeanDefinition("handlerAdapter", adapterDef);
	}, TextRestController.class);

	byte[] content = "body".getBytes(StandardCharsets.ISO_8859_1);
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/a4.css");
	request.setContent(content);
	MockHttpServletResponse response = new MockHttpServletResponse();

	getServlet().service(request, response);

	assertEquals(200, response.getStatus());
	assertEquals("text/css;charset=ISO-8859-1", response.getContentType());
	assertNull(response.getHeader("Content-Disposition"));
	assertArrayEquals(content, response.getContentAsByteArray());
}
 
Example #8
Source File: ServletAnnotationControllerHandlerMethodTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void responseBodyAsHtmlWithProducesCondition() throws Exception {
	initServlet(wac -> {
		ContentNegotiationManagerFactoryBean factoryBean = new ContentNegotiationManagerFactoryBean();
		factoryBean.afterPropertiesSet();
		RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
		adapterDef.getPropertyValues().add("contentNegotiationManager", factoryBean.getObject());
		wac.registerBeanDefinition("handlerAdapter", adapterDef);
	}, TextRestController.class);

	byte[] content = "alert('boo')".getBytes(StandardCharsets.ISO_8859_1);
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/a3.html");
	request.setContent(content);
	MockHttpServletResponse response = new MockHttpServletResponse();

	getServlet().service(request, response);

	assertEquals(200, response.getStatus());
	assertEquals("text/html;charset=ISO-8859-1", response.getContentType());
	assertNull(response.getHeader("Content-Disposition"));
	assertArrayEquals(content, response.getContentAsByteArray());
}
 
Example #9
Source File: ServletAnnotationControllerHandlerMethodTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void responseBodyAsHtmlWithSuffixPresent() throws Exception {
	initServlet(wac -> {
		ContentNegotiationManagerFactoryBean factoryBean = new ContentNegotiationManagerFactoryBean();
		factoryBean.afterPropertiesSet();
		RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
		adapterDef.getPropertyValues().add("contentNegotiationManager", factoryBean.getObject());
		wac.registerBeanDefinition("handlerAdapter", adapterDef);
	}, TextRestController.class);

	byte[] content = "alert('boo')".getBytes(StandardCharsets.ISO_8859_1);
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/a2.html");
	request.setContent(content);
	MockHttpServletResponse response = new MockHttpServletResponse();

	getServlet().service(request, response);

	assertEquals(200, response.getStatus());
	assertEquals("text/html;charset=ISO-8859-1", response.getContentType());
	assertNull(response.getHeader("Content-Disposition"));
	assertArrayEquals(content, response.getContentAsByteArray());
}
 
Example #10
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 #11
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 #12
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 #13
Source File: ServletAnnotationControllerHandlerMethodTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void responseBodyAsTextWithCssExtension() throws Exception {
	initServlet(wac -> {
		ContentNegotiationManagerFactoryBean factoryBean = new ContentNegotiationManagerFactoryBean();
		factoryBean.afterPropertiesSet();
		RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
		adapterDef.getPropertyValues().add("contentNegotiationManager", factoryBean.getObject());
		wac.registerBeanDefinition("handlerAdapter", adapterDef);
	}, TextRestController.class);

	byte[] content = "body".getBytes(StandardCharsets.ISO_8859_1);
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/a4.css");
	request.setContent(content);
	MockHttpServletResponse response = new MockHttpServletResponse();

	getServlet().service(request, response);

	assertEquals(200, response.getStatus());
	assertEquals("text/css;charset=ISO-8859-1", response.getContentType());
	assertNull(response.getHeader("Content-Disposition"));
	assertArrayEquals(content, response.getContentAsByteArray());
}
 
Example #14
Source File: ServletAnnotationControllerHandlerMethodTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void responseBodyAsHtmlWithProducesCondition() throws Exception {
	initServlet(wac -> {
		ContentNegotiationManagerFactoryBean factoryBean = new ContentNegotiationManagerFactoryBean();
		factoryBean.afterPropertiesSet();
		RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
		adapterDef.getPropertyValues().add("contentNegotiationManager", factoryBean.getObject());
		wac.registerBeanDefinition("handlerAdapter", adapterDef);
	}, TextRestController.class);

	byte[] content = "alert('boo')".getBytes(StandardCharsets.ISO_8859_1);
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/a3.html");
	request.setContent(content);
	MockHttpServletResponse response = new MockHttpServletResponse();

	getServlet().service(request, response);

	assertEquals(200, response.getStatus());
	assertEquals("text/html;charset=ISO-8859-1", response.getContentType());
	assertNull(response.getHeader("Content-Disposition"));
	assertArrayEquals(content, response.getContentAsByteArray());
}
 
Example #15
Source File: ServletAnnotationControllerHandlerMethodTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void responseBodyAsHtmlWithSuffixPresent() throws Exception {
	initServlet(wac -> {
		ContentNegotiationManagerFactoryBean factoryBean = new ContentNegotiationManagerFactoryBean();
		factoryBean.afterPropertiesSet();
		RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
		adapterDef.getPropertyValues().add("contentNegotiationManager", factoryBean.getObject());
		wac.registerBeanDefinition("handlerAdapter", adapterDef);
	}, TextRestController.class);

	byte[] content = "alert('boo')".getBytes(StandardCharsets.ISO_8859_1);
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/a2.html");
	request.setContent(content);
	MockHttpServletResponse response = new MockHttpServletResponse();

	getServlet().service(request, response);

	assertEquals(200, response.getStatus());
	assertEquals("text/html;charset=ISO-8859-1", response.getContentType());
	assertNull(response.getHeader("Content-Disposition"));
	assertArrayEquals(content, response.getContentAsByteArray());
}
 
Example #16
Source File: ServletAnnotationControllerHandlerMethodTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void responseBodyAsHtml() throws Exception {
	initServlet(wac -> {
		ContentNegotiationManagerFactoryBean factoryBean = new ContentNegotiationManagerFactoryBean();
		factoryBean.afterPropertiesSet();
		RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
		adapterDef.getPropertyValues().add("contentNegotiationManager", factoryBean.getObject());
		wac.registerBeanDefinition("handlerAdapter", adapterDef);
	}, TextRestController.class);

	byte[] content = "alert('boo')".getBytes(StandardCharsets.ISO_8859_1);
	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/a1.html");
	request.setContent(content);
	MockHttpServletResponse response = new MockHttpServletResponse();

	getServlet().service(request, response);

	assertEquals(200, response.getStatus());
	assertEquals("text/html;charset=ISO-8859-1", response.getContentType());
	assertEquals("inline;filename=f.txt", response.getHeader("Content-Disposition"));
	assertArrayEquals(content, response.getContentAsByteArray());
}
 
Example #17
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 #18
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 #19
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 #20
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);
}
 
Example #21
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 #22
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 #23
Source File: ConfigurableContentNegotiationManagerWebMvcConfigurer.java    From spring-webmvc-support with GNU General Public License v3.0 4 votes vote down vote up
protected void configureContentNegotiationManagerFactoryBean(ContentNegotiationManagerFactoryBean factoryBean) {

        DataBinder dataBinder = new DataBinder(factoryBean);

        dataBinder.setDisallowedFields("contentNegotiationManager", "servletContext");

        dataBinder.setAutoGrowNestedPaths(true);

        dataBinder.registerCustomEditor(MediaType.class, "defaultContentType", new MediaTypePropertyEditor());

        MutablePropertyValues propertyValues = new MutablePropertyValues();

        propertyValues.addPropertyValues(this.propertyValues);

        dataBinder.bind(propertyValues);

    }
 
Example #24
Source File: ConfigurableContentNegotiationManagerWebMvcConfigurerTest.java    From spring-webmvc-support with GNU General Public License v3.0 4 votes vote down vote up
@Test
public void testConfigureContentNegotiation() {

    Map<String, String> properties = new HashMap<String, String>();

    properties.put("favorPathExtension", "false");
    properties.put("favorParameter", "true");
    properties.put("ignoreAcceptHeader", "true");
    properties.put("useJaf", "true");
    properties.put("parameterName", "test-format");
    properties.put("mediaTypes.html", TEXT_HTML_VALUE);
    properties.put("mediaTypes.xml", APPLICATION_XML_VALUE);
    properties.put("mediaTypes.json", APPLICATION_JSON_VALUE);
    properties.put("mediaTypes.gif", IMAGE_GIF_VALUE);
    properties.put("mediaTypes.jpeg", IMAGE_JPEG_VALUE);
    properties.put("defaultContentType", TEXT_HTML_VALUE);


    WebMvcConfigurer webMvcConfigurer = new ConfigurableContentNegotiationManagerWebMvcConfigurer(properties);

    ContentNegotiationConfigurer configurer = new ContentNegotiationConfigurer(new MockServletContext());

    ContentNegotiationManagerFactoryBean factoryBean =
            FieldUtils.getFieldValue(configurer, "factory", ContentNegotiationManagerFactoryBean.class);

    webMvcConfigurer.configureContentNegotiation(configurer);


    Assert.assertFalse(FieldUtils.getFieldValue(factoryBean, "favorPathExtension", boolean.class));
    Assert.assertTrue(FieldUtils.getFieldValue(factoryBean, "favorParameter", boolean.class));
    Assert.assertTrue(FieldUtils.getFieldValue(factoryBean, "ignoreAcceptHeader", boolean.class));
    Assert.assertTrue(FieldUtils.getFieldValue(factoryBean, "useJaf", Boolean.class));
    Assert.assertEquals("test-format", FieldUtils.getFieldValue(factoryBean, "parameterName", String.class));

    Map<String, MediaType> mediaTypesMap = FieldUtils.getFieldValue(factoryBean, "mediaTypes", Map.class);

    Assert.assertEquals("html", mediaTypesMap.get("html").getSubtype());
    Assert.assertEquals("xml", mediaTypesMap.get("xml").getSubtype());
    Assert.assertEquals("json", mediaTypesMap.get("json").getSubtype());
    Assert.assertEquals("gif", mediaTypesMap.get("gif").getSubtype());
    Assert.assertEquals("jpeg", mediaTypesMap.get("jpeg").getSubtype());

}