Java Code Examples for org.springframework.web.accept.ContentNegotiationManagerFactoryBean

The following examples show how to use org.springframework.web.accept.ContentNegotiationManagerFactoryBean. These examples are extracted from open source projects. 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
@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
@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
@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 4
@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 5
@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 6
@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 7
@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 8
@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 9
@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 10
@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 11
@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 12
@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 13
@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 14
@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 15
@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 16
@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 17
@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 18
@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 Project: spring-analysis-note   Source File: ReactiveTypeHandlerTests.java    License: 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
@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 21
Source Project: java-technology-stack   Source File: ReactiveTypeHandlerTests.java    License: 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
@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 23
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
@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());

}