org.springframework.web.accept.ContentNegotiationStrategy Java Examples

The following examples show how to use org.springframework.web.accept.ContentNegotiationStrategy. 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: ContentNegotiationConfigurerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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
Source File: ContentNegotiationConfigurerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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 File: SpringletsSecurityWebAccessDeniedHandlerImpl.java    From springlets with Apache License 2.0 6 votes vote down vote up
@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 #4
Source File: SpringletsSecurityWebAuthenticationEntryPoint.java    From springlets with Apache License 2.0 6 votes vote down vote up
@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
Source File: SecurityConfig.java    From pizzeria with 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 #6
Source File: ContentNegotiationConfigurerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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 File: RestConfig.java    From kbear with 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
Source File: ContentNegotiationConfigurerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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
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 #10
Source File: FallbackContentNegotiationStrategy.java    From problem-spring-web with MIT License 4 votes vote down vote up
FallbackContentNegotiationStrategy(final ContentNegotiationStrategy delegate) {
    this.delegate = delegate;
}
 
Example #11
Source File: AdviceTrait.java    From problem-spring-web with 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
Source File: ContentNegotiationManagerConfigurationDisabledTest.java    From spring-boot-web-support with GNU General Public License v3.0 4 votes vote down vote up
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 #13
Source File: ContentNegotiationManagerConfigurationDisabledTest.java    From spring-boot-web-support with GNU General Public License v3.0 3 votes vote down vote up
@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
Source File: ContentNegotiationConfigurer.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * 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 #15
Source File: ContentNegotiationConfigurer.java    From spring4-understanding with Apache License 2.0 2 votes vote down vote up
/**
 * 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 #16
Source File: ContentNegotiationConfigurer.java    From java-technology-stack with MIT License 2 votes vote down vote up
/**
 * 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 #17
Source File: ContentNegotiationConfigurer.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * 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;
}