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

The following examples show how to use org.springframework.web.accept.ContentNegotiationStrategy. 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
public void defaultSettings() throws Exception {
	ContentNegotiationManager manager = this.configurer.buildContentNegotiationManager();

	this.servletRequest.setRequestURI("/flower.gif");

	assertEquals("Should be able to resolve file extensions by default",
			MediaType.IMAGE_GIF, manager.resolveMediaTypes(this.webRequest).get(0));

	this.servletRequest.setRequestURI("/flower?format=gif");
	this.servletRequest.addParameter("format", "gif");

	assertEquals("Should not resolve request parameters by default",
			ContentNegotiationStrategy.MEDIA_TYPE_ALL_LIST, manager.resolveMediaTypes(this.webRequest));

	this.servletRequest.setRequestURI("/flower");
	this.servletRequest.addHeader("Accept", MediaType.IMAGE_GIF_VALUE);

	assertEquals("Should resolve Accept header by default",
			MediaType.IMAGE_GIF, manager.resolveMediaTypes(this.webRequest).get(0));
}
 
Example 2
@Test
public void defaultSettings() throws Exception {
	ContentNegotiationManager manager = this.configurer.buildContentNegotiationManager();

	this.servletRequest.setRequestURI("/flower.gif");

	assertEquals("Should be able to resolve file extensions by default",
			MediaType.IMAGE_GIF, manager.resolveMediaTypes(this.webRequest).get(0));

	this.servletRequest.setRequestURI("/flower?format=gif");
	this.servletRequest.addParameter("format", "gif");

	assertEquals("Should not resolve request parameters by default",
			ContentNegotiationStrategy.MEDIA_TYPE_ALL_LIST, manager.resolveMediaTypes(this.webRequest));

	this.servletRequest.setRequestURI("/flower");
	this.servletRequest.addHeader("Accept", MediaType.IMAGE_GIF_VALUE);

	assertEquals("Should resolve Accept header by default",
			MediaType.IMAGE_GIF, manager.resolveMediaTypes(this.webRequest).get(0));
}
 
Example 3
Source Project: pizzeria   Source File: SecurityConfig.java    License: MIT License 6 votes vote down vote up
@Bean
public LogoutSuccessHandler logoutSuccessHandler() {
    ContentNegotiationStrategy contentNegotiationStrategy = new HeaderContentNegotiationStrategy();

    MediaTypeRequestMatcher jsonMediaTypeRequestMatcher = new MediaTypeRequestMatcher(contentNegotiationStrategy, MediaType.APPLICATION_JSON);
    jsonMediaTypeRequestMatcher.setUseEquals(true);

    LinkedHashMap<RequestMatcher, LogoutSuccessHandler> matcherToHandler = new LinkedHashMap<>();
    matcherToHandler.put(jsonMediaTypeRequestMatcher, new HttpStatusReturningLogoutSuccessHandler());

    DelegatingLogoutSuccessHandler delegatingLogoutSuccessHandler = new DelegatingLogoutSuccessHandler(matcherToHandler);

    SimpleUrlLogoutSuccessHandler simpleUrlLogoutSuccessHandler = new SimpleUrlLogoutSuccessHandler();
    simpleUrlLogoutSuccessHandler.setUseReferer(true);
    simpleUrlLogoutSuccessHandler.setDefaultTargetUrl("/");

    delegatingLogoutSuccessHandler.setDefaultLogoutSuccessHandler(simpleUrlLogoutSuccessHandler);

    return delegatingLogoutSuccessHandler;
}
 
Example 4
@Override
public void commence(HttpServletRequest request, HttpServletResponse response,
    AuthenticationException authException) throws IOException, ServletException {

  ContentNegotiationStrategy negotiationStrategy = new HeaderContentNegotiationStrategy();
  MediaTypeRequestMatcher matcher =
      new MediaTypeRequestMatcher(negotiationStrategy, MediaType.TEXT_HTML);
  matcher.setUseEquals(false);

  if (matcher.matches(request)) {
    DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
    redirectStrategy.setContextRelative(false);
    redirectStrategy.sendRedirect(request, response, LOGIN_FORM_URL);
  } else {
    response.sendError(HttpServletResponse.SC_FORBIDDEN);
  }
}
 
Example 5
@Override
public void handle(HttpServletRequest request, HttpServletResponse response,
    AccessDeniedException accessDeniedException) throws IOException, ServletException {

  ContentNegotiationStrategy negotiationStrategy = new HeaderContentNegotiationStrategy();
  MediaTypeRequestMatcher matcher =
      new MediaTypeRequestMatcher(negotiationStrategy, MediaType.TEXT_HTML);
  matcher.setUseEquals(false);

  if (matcher.matches(request)) {
    DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
    redirectStrategy.setContextRelative(false);
    redirectStrategy.sendRedirect(request, response, "/errores/403");
  } else {
    response.sendError(HttpServletResponse.SC_FORBIDDEN);

  }

}
 
Example 6
@Test
public void ignoreAcceptHeader() throws Exception {
	this.configurer.ignoreAcceptHeader(true);
	ContentNegotiationManager manager = this.configurer.buildContentNegotiationManager();

	this.servletRequest.setRequestURI("/flower");
	this.servletRequest.addHeader("Accept", MediaType.IMAGE_GIF_VALUE);

	assertEquals(ContentNegotiationStrategy.MEDIA_TYPE_ALL_LIST, manager.resolveMediaTypes(this.webRequest));
}
 
Example 7
Source Project: kbear   Source File: RestConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
    configurer.defaultContentType(MediaType.APPLICATION_JSON)
            .strategies(Arrays.asList(new ContentNegotiationStrategy() {
                @Override
                public List<MediaType> resolveMediaTypes(NativeWebRequest webRequest)
                        throws HttpMediaTypeNotAcceptableException {
                    MediaType mediaType = MediaType.APPLICATION_JSON;
                    String accept = webRequest.getHeader(HttpHeaders.ACCEPT);
                    if (accept == null)
                        return Arrays.asList(mediaType);

                    switch (accept) {
                        case APPLICATION_PROTOBUF_VALUE:
                            mediaType = ProtobufHttpMessageConverter.PROTOBUF;
                            break;
                        case MediaType.APPLICATION_JSON_VALUE:
                            mediaType = MediaType.APPLICATION_JSON;
                            break;
                        default:
                            mediaType = MediaType.APPLICATION_JSON;
                            break;
                    }

                    return Arrays.asList(mediaType);
                }
            }));
}
 
Example 8
@Test
public void ignoreAcceptHeader() throws Exception {
	this.configurer.ignoreAcceptHeader(true);
	ContentNegotiationManager manager = this.configurer.buildContentNegotiationManager();

	this.servletRequest.setRequestURI("/flower");
	this.servletRequest.addHeader("Accept", MediaType.IMAGE_GIF_VALUE);

	assertEquals(ContentNegotiationStrategy.MEDIA_TYPE_ALL_LIST, manager.resolveMediaTypes(this.webRequest));
}
 
Example 9
private static PathExtensionContentNegotiationStrategy initPathStrategy(ContentNegotiationManager manager) {
	for (ContentNegotiationStrategy strategy : manager.getStrategies()) {
		if (strategy instanceof PathExtensionContentNegotiationStrategy) {
			return (PathExtensionContentNegotiationStrategy) strategy;
		}
	}
	return new PathExtensionContentNegotiationStrategy();
}
 
Example 10
private boolean contains(Class<? extends ContentNegotiationStrategy> strategyClass,
                         List<ContentNegotiationStrategy> strategies) {

    boolean contained = false;

    for (ContentNegotiationStrategy strategy : strategies) {

        contained = strategyClass.isInstance(strategy);

        if (contained) {
            break;
        }

    }

    return contained;
}
 
Example 11
Source Project: problem-spring-web   Source File: AdviceTrait.java    License: MIT License 4 votes vote down vote up
@SneakyThrows(HttpMediaTypeNotAcceptableException.class)
default Optional<MediaType> negotiate(final NativeWebRequest request) {
    final ContentNegotiationStrategy negotiator = ContentNegotiation.DEFAULT;
    final List<MediaType> mediaTypes = negotiator.resolveMediaTypes(request);
    return AdviceTraits.getProblemMediaType(mediaTypes);
}
 
Example 12
FallbackContentNegotiationStrategy(final ContentNegotiationStrategy delegate) {
    this.delegate = delegate;
}
 
Example 13
@Test
public void testContentNegotiationManagerConfigurationOnDisabled() {

    Assert.assertFalse(BeanUtils.isBeanPresent(applicationContext, ContentNegotiationManagerConfiguration.class));

    ContentNegotiationManager contentNegotiationManager =
            contentNegotiatingViewResolver.getContentNegotiationManager();

    List<ContentNegotiationStrategy> strategies = contentNegotiationManager.getStrategies();

    Assert.assertEquals(1, strategies.size());
    Assert.assertTrue(contains(HeaderContentNegotiationStrategy.class, strategies));

}
 
Example 14
/**
 * Set a custom {@link ContentNegotiationStrategy} to use to determine
 * the content type to use when no content type is requested.
 * <p>By default this is not set.
 * @since 4.1.2
 * @see #defaultContentType
 */
public ContentNegotiationConfigurer defaultContentTypeStrategy(ContentNegotiationStrategy defaultStrategy) {
	this.factory.setDefaultContentTypeStrategy(defaultStrategy);
	return this;
}
 
Example 15
/**
 * Set a custom {@link ContentNegotiationStrategy} to use to determine
 * the content type to use when no content type is requested.
 * <p>By default this is not set.
 * @since 4.1.2
 * @see #defaultContentType
 */
public ContentNegotiationConfigurer defaultContentTypeStrategy(ContentNegotiationStrategy defaultStrategy) {
	this.factory.setDefaultContentTypeStrategy(defaultStrategy);
	return this;
}
 
Example 16
/**
 * Set a custom {@link ContentNegotiationStrategy} to use to determine
 * the content type to use when no content type is requested.
 * <p>By default this is not set.
 * @see #defaultContentType
 * @since 4.1.2
 */
public ContentNegotiationConfigurer defaultContentTypeStrategy(ContentNegotiationStrategy defaultStrategy) {
	this.factory.setDefaultContentTypeStrategy(defaultStrategy);
	return this;
}
 
Example 17
/**
 * Set a custom {@link ContentNegotiationStrategy} to use to determine
 * the content type to use when no content type is requested.
 * <p>By default this is not set.
 * @see #defaultContentType
 * @since 4.1.2
 */
public ContentNegotiationConfigurer defaultContentTypeStrategy(ContentNegotiationStrategy defaultStrategy) {
	this.factory.setDefaultContentTypeStrategy(defaultStrategy);
	return this;
}