Java源码示例:org.springframework.web.accept.ContentNegotiationStrategy

示例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));
}
 
示例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));
}
 
示例3
@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;
}
 
示例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);
  }
}
 
示例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);

  }

}
 
示例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));
}
 
示例7
@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);
                }
            }));
}
 
示例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));
}
 
示例9
private static PathExtensionContentNegotiationStrategy initPathStrategy(ContentNegotiationManager manager) {
	for (ContentNegotiationStrategy strategy : manager.getStrategies()) {
		if (strategy instanceof PathExtensionContentNegotiationStrategy) {
			return (PathExtensionContentNegotiationStrategy) strategy;
		}
	}
	return new PathExtensionContentNegotiationStrategy();
}
 
示例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;
}
 
示例11
@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);
}
 
示例12
FallbackContentNegotiationStrategy(final ContentNegotiationStrategy delegate) {
    this.delegate = delegate;
}
 
示例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));

}
 
示例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;
}
 
示例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;
}
 
示例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;
}
 
示例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;
}