org.springframework.web.accept.PathExtensionContentNegotiationStrategy Java Examples

The following examples show how to use org.springframework.web.accept.PathExtensionContentNegotiationStrategy. 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: RequestMappingHandlerMappingTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void useRegisteredSuffixPatternMatch() {
	assertTrue(this.handlerMapping.useSuffixPatternMatch());
	assertFalse(this.handlerMapping.useRegisteredSuffixPatternMatch());

	Map<String, MediaType> fileExtensions = Collections.singletonMap("json", MediaType.APPLICATION_JSON);
	PathExtensionContentNegotiationStrategy strategy = new PathExtensionContentNegotiationStrategy(fileExtensions);
	ContentNegotiationManager manager = new ContentNegotiationManager(strategy);

	this.handlerMapping.setContentNegotiationManager(manager);
	this.handlerMapping.setUseRegisteredSuffixPatternMatch(true);
	this.handlerMapping.afterPropertiesSet();

	assertTrue(this.handlerMapping.useSuffixPatternMatch());
	assertTrue(this.handlerMapping.useRegisteredSuffixPatternMatch());
	assertEquals(Arrays.asList("json"), this.handlerMapping.getFileExtensions());
}
 
Example #2
Source File: RequestMappingHandlerMappingTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void useRegisteredSuffixPatternMatchInitialization() {
	Map<String, MediaType> fileExtensions = Collections.singletonMap("json", MediaType.APPLICATION_JSON);
	PathExtensionContentNegotiationStrategy strategy = new PathExtensionContentNegotiationStrategy(fileExtensions);
	ContentNegotiationManager manager = new ContentNegotiationManager(strategy);

	final Set<String> extensions = new HashSet<>();

	RequestMappingHandlerMapping hm = new RequestMappingHandlerMapping() {
		@Override
		protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
			extensions.addAll(getFileExtensions());
			return super.getMappingForMethod(method, handlerType);
		}
	};

	wac.registerSingleton("testController", ComposedAnnotationController.class);
	wac.refresh();

	hm.setContentNegotiationManager(manager);
	hm.setUseRegisteredSuffixPatternMatch(true);
	hm.setApplicationContext(wac);
	hm.afterPropertiesSet();

	assertEquals(Collections.singleton("json"), extensions);
}
 
Example #3
Source File: RequestMappingHandlerMappingTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void useRegisteredSuffixPatternMatch() {
	assertTrue(this.handlerMapping.useSuffixPatternMatch());
	assertFalse(this.handlerMapping.useRegisteredSuffixPatternMatch());

	Map<String, MediaType> fileExtensions = Collections.singletonMap("json", MediaType.APPLICATION_JSON);
	PathExtensionContentNegotiationStrategy strategy = new PathExtensionContentNegotiationStrategy(fileExtensions);
	ContentNegotiationManager manager = new ContentNegotiationManager(strategy);

	this.handlerMapping.setContentNegotiationManager(manager);
	this.handlerMapping.setUseRegisteredSuffixPatternMatch(true);
	this.handlerMapping.afterPropertiesSet();

	assertTrue(this.handlerMapping.useSuffixPatternMatch());
	assertTrue(this.handlerMapping.useRegisteredSuffixPatternMatch());
	assertEquals(Arrays.asList("json"), this.handlerMapping.getFileExtensions());
}
 
Example #4
Source File: RequestMappingHandlerMappingTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void useRegisteredSuffixPatternMatchInitialization() {
	Map<String, MediaType> fileExtensions = Collections.singletonMap("json", MediaType.APPLICATION_JSON);
	PathExtensionContentNegotiationStrategy strategy = new PathExtensionContentNegotiationStrategy(fileExtensions);
	ContentNegotiationManager manager = new ContentNegotiationManager(strategy);

	final Set<String> extensions = new HashSet<>();

	RequestMappingHandlerMapping hm = new RequestMappingHandlerMapping() {
		@Override
		protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
			extensions.addAll(getFileExtensions());
			return super.getMappingForMethod(method, handlerType);
		}
	};

	wac.registerSingleton("testController", ComposedAnnotationController.class);
	wac.refresh();

	hm.setContentNegotiationManager(manager);
	hm.setUseRegisteredSuffixPatternMatch(true);
	hm.setApplicationContext(wac);
	hm.afterPropertiesSet();

	assertEquals(Collections.singleton("json"), extensions);
}
 
Example #5
Source File: RequestMappingHandlerMappingTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void useRegisteredSuffixPatternMatch() {
	assertTrue(this.handlerMapping.useSuffixPatternMatch());
	assertFalse(this.handlerMapping.useRegisteredSuffixPatternMatch());

	Map<String, MediaType> fileExtensions = Collections.singletonMap("json", MediaType.APPLICATION_JSON);
	PathExtensionContentNegotiationStrategy strategy = new PathExtensionContentNegotiationStrategy(fileExtensions);
	ContentNegotiationManager manager = new ContentNegotiationManager(strategy);

	this.handlerMapping.setContentNegotiationManager(manager);
	this.handlerMapping.setUseRegisteredSuffixPatternMatch(true);
	this.handlerMapping.afterPropertiesSet();

	assertTrue(this.handlerMapping.useSuffixPatternMatch());
	assertTrue(this.handlerMapping.useRegisteredSuffixPatternMatch());
	assertEquals(Arrays.asList("json"), this.handlerMapping.getFileExtensions());
}
 
Example #6
Source File: RequestMappingHandlerMappingTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void useRegisteredSuffixPatternMatchInitialization() {
	Map<String, MediaType> fileExtensions = Collections.singletonMap("json", MediaType.APPLICATION_JSON);
	PathExtensionContentNegotiationStrategy strategy = new PathExtensionContentNegotiationStrategy(fileExtensions);
	ContentNegotiationManager manager = new ContentNegotiationManager(strategy);

	final Set<String> extensions = new HashSet<String>();

	RequestMappingHandlerMapping hm = new RequestMappingHandlerMapping() {
		@Override
		protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
			extensions.addAll(getFileExtensions());
			return super.getMappingForMethod(method, handlerType);
		}
	};

	wac.registerSingleton("testController", ComposedAnnotationController.class);
	wac.refresh();

	hm.setContentNegotiationManager(manager);
	hm.setUseRegisteredSuffixPatternMatch(true);
	hm.setApplicationContext(wac);
	hm.afterPropertiesSet();

	assertEquals(Collections.singleton("json"), extensions);
}
 
Example #7
Source File: ResourceHttpRequestHandler.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Initialize the content negotiation strategy depending on the {@code ContentNegotiationManager}
 * setup and the availability of a {@code ServletContext}.
 * @see ServletPathExtensionContentNegotiationStrategy
 * @see PathExtensionContentNegotiationStrategy
 */
protected PathExtensionContentNegotiationStrategy initContentNegotiationStrategy() {
	Map<String, MediaType> mediaTypes = null;
	if (getContentNegotiationManager() != null) {
		PathExtensionContentNegotiationStrategy strategy =
				getContentNegotiationManager().getStrategy(PathExtensionContentNegotiationStrategy.class);
		if (strategy != null) {
			mediaTypes = new HashMap<>(strategy.getMediaTypes());
		}
	}
	return (getServletContext() != null ?
			new ServletPathExtensionContentNegotiationStrategy(getServletContext(), mediaTypes) :
			new PathExtensionContentNegotiationStrategy(mediaTypes));
}
 
Example #8
Source File: ContentNegotiatingViewResolverTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void resolveViewNameFilenameDefaultView() throws Exception {
	request.setRequestURI("/test.json");

	Map<String, MediaType> mapping = Collections.singletonMap("json", MediaType.APPLICATION_JSON);
	PathExtensionContentNegotiationStrategy pathStrategy = new PathExtensionContentNegotiationStrategy(mapping);
	viewResolver.setContentNegotiationManager(new ContentNegotiationManager(pathStrategy));

	ViewResolver viewResolverMock1 = mock(ViewResolver.class);
	ViewResolver viewResolverMock2 = mock(ViewResolver.class);
	viewResolver.setViewResolvers(Arrays.asList(viewResolverMock1, viewResolverMock2));

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

	List<View> defaultViews = new ArrayList<>();
	defaultViews.add(viewMock3);
	viewResolver.setDefaultViews(defaultViews);

	viewResolver.afterPropertiesSet();

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

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

	View result = viewResolver.resolveViewName(viewName, locale);
	assertSame("Invalid view", viewMock3, result);
}
 
Example #9
Source File: ResourceHttpRequestHandler.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Initialize the content negotiation strategy depending on the {@code ContentNegotiationManager}
 * setup and the availability of a {@code ServletContext}.
 * @see ServletPathExtensionContentNegotiationStrategy
 * @see PathExtensionContentNegotiationStrategy
 */
protected PathExtensionContentNegotiationStrategy initContentNegotiationStrategy() {
	Map<String, MediaType> mediaTypes = null;
	if (getContentNegotiationManager() != null) {
		PathExtensionContentNegotiationStrategy strategy =
				getContentNegotiationManager().getStrategy(PathExtensionContentNegotiationStrategy.class);
		if (strategy != null) {
			mediaTypes = new HashMap<>(strategy.getMediaTypes());
		}
	}
	return (getServletContext() != null ?
			new ServletPathExtensionContentNegotiationStrategy(getServletContext(), mediaTypes) :
			new PathExtensionContentNegotiationStrategy(mediaTypes));
}
 
Example #10
Source File: ContentNegotiatingViewResolverTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void resolveViewNameFilenameDefaultView() throws Exception {
	request.setRequestURI("/test.json");

	Map<String, MediaType> mapping = Collections.singletonMap("json", MediaType.APPLICATION_JSON);
	PathExtensionContentNegotiationStrategy pathStrategy = new PathExtensionContentNegotiationStrategy(mapping);
	viewResolver.setContentNegotiationManager(new ContentNegotiationManager(pathStrategy));

	ViewResolver viewResolverMock1 = mock(ViewResolver.class);
	ViewResolver viewResolverMock2 = mock(ViewResolver.class);
	viewResolver.setViewResolvers(Arrays.asList(viewResolverMock1, viewResolverMock2));

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

	List<View> defaultViews = new ArrayList<>();
	defaultViews.add(viewMock3);
	viewResolver.setDefaultViews(defaultViews);

	viewResolver.afterPropertiesSet();

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

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

	View result = viewResolver.resolveViewName(viewName, locale);
	assertSame("Invalid view", viewMock3, result);
}
 
Example #11
Source File: ResourceHttpRequestHandler.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Initialize the content negotiation strategy depending on the {@code ContentNegotiationManager}
 * setup and the availability of a {@code ServletContext}.
 * @see ServletPathExtensionContentNegotiationStrategy
 * @see PathExtensionContentNegotiationStrategy
 */
protected PathExtensionContentNegotiationStrategy initContentNegotiationStrategy() {
	Map<String, MediaType> mediaTypes = null;
	if (getContentNegotiationManager() != null) {
		PathExtensionContentNegotiationStrategy strategy =
				getContentNegotiationManager().getStrategy(PathExtensionContentNegotiationStrategy.class);
		if (strategy != null) {
			mediaTypes = new HashMap<String, MediaType>(strategy.getMediaTypes());
		}
	}
	return (getServletContext() != null ?
			new ServletPathExtensionContentNegotiationStrategy(getServletContext(), mediaTypes) :
			new PathExtensionContentNegotiationStrategy(mediaTypes));
}
 
Example #12
Source File: AbstractMessageConverterMethodProcessor.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private static PathExtensionContentNegotiationStrategy initPathStrategy(ContentNegotiationManager manager) {
	for (ContentNegotiationStrategy strategy : manager.getStrategies()) {
		if (strategy instanceof PathExtensionContentNegotiationStrategy) {
			return (PathExtensionContentNegotiationStrategy) strategy;
		}
	}
	return new PathExtensionContentNegotiationStrategy();
}
 
Example #13
Source File: ContentNegotiatingViewResolverTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void resolveViewNameFilenameDefaultView() throws Exception {
	request.setRequestURI("/test.json");

	Map<String, MediaType> mapping = Collections.singletonMap("json", MediaType.APPLICATION_JSON);
	PathExtensionContentNegotiationStrategy pathStrategy = new PathExtensionContentNegotiationStrategy(mapping);
	viewResolver.setContentNegotiationManager(new ContentNegotiationManager(pathStrategy));

	ViewResolver viewResolverMock1 = mock(ViewResolver.class);
	ViewResolver viewResolverMock2 = mock(ViewResolver.class);
	viewResolver.setViewResolvers(Arrays.asList(viewResolverMock1, viewResolverMock2));

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

	List<View> defaultViews = new ArrayList<View>();
	defaultViews.add(viewMock3);
	viewResolver.setDefaultViews(defaultViews);

	viewResolver.afterPropertiesSet();

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

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

	View result = viewResolver.resolveViewName(viewName, locale);
	assertSame("Invalid view", viewMock3, result);
}
 
Example #14
Source File: AbstractMessageConverterMethodProcessor.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private static PathExtensionContentNegotiationStrategy initPathStrategy(ContentNegotiationManager manager) {
	Class<PathExtensionContentNegotiationStrategy> clazz = PathExtensionContentNegotiationStrategy.class;
	PathExtensionContentNegotiationStrategy strategy = manager.getStrategy(clazz);
	return (strategy != null ? strategy : new PathExtensionContentNegotiationStrategy());
}
 
Example #15
Source File: AbstractMessageConverterMethodProcessor.java    From java-technology-stack with MIT License 4 votes vote down vote up
private static PathExtensionContentNegotiationStrategy initPathStrategy(ContentNegotiationManager manager) {
	Class<PathExtensionContentNegotiationStrategy> clazz = PathExtensionContentNegotiationStrategy.class;
	PathExtensionContentNegotiationStrategy strategy = manager.getStrategy(clazz);
	return (strategy != null ? strategy : new PathExtensionContentNegotiationStrategy());
}
 
Example #16
Source File: AbstractMessageConverterMethodProcessor.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private static PathExtensionContentNegotiationStrategy initPathStrategy(ContentNegotiationManager manager) {
	Class<PathExtensionContentNegotiationStrategy> clazz = PathExtensionContentNegotiationStrategy.class;
	PathExtensionContentNegotiationStrategy strategy = manager.getStrategy(clazz);
	return (strategy != null ? strategy : new PathExtensionContentNegotiationStrategy());
}