Skip to content

Add supportedMediaTypes to the ProducesRequestCondition #301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 3.0.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -19,11 +19,13 @@
import lombok.RequiredArgsConstructor;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
@@ -41,6 +43,7 @@
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.MimeType;
import org.springframework.util.StringUtils;
import org.springframework.util.StringValueResolver;
import org.springframework.web.bind.annotation.CrossOrigin;
@@ -60,6 +63,7 @@
* @author Jon Brisbin
* @author Oliver Gierke
* @author Mark Paluch
* @author Petar Tahchiev
*/
public class RepositoryRestHandlerMapping extends BasePathAwareHandlerMapping {

@@ -69,6 +73,7 @@ public class RepositoryRestHandlerMapping extends BasePathAwareHandlerMapping {

private RepositoryCorsConfigurationAccessor corsConfigurationAccessor;
private JpaHelper jpaHelper;
private Collection<MediaType> supportedMediaTypes;

/**
* Creates a new {@link RepositoryRestHandlerMapping} for the given {@link ResourceMappings} and
@@ -203,6 +208,9 @@ protected ProducesRequestCondition customize(ProducesRequestCondition condition)
HashSet<String> mediaTypes = new LinkedHashSet<String>();
mediaTypes.add(configuration.getDefaultMediaType().toString());
mediaTypes.add(MediaType.APPLICATION_JSON_VALUE);
if (supportedMediaTypes != null) {
mediaTypes.addAll(supportedMediaTypes.stream().map(MimeType::toString).collect(Collectors.toList()));
}

return new ProducesRequestCondition(mediaTypes.toArray(new String[mediaTypes.size()]));
}
@@ -235,6 +243,10 @@ private static String getRepositoryBasePath(String repositoryLookupPath) {
return secondSlashIndex == -1 ? repositoryLookupPath : repositoryLookupPath.substring(0, secondSlashIndex);
}

public void setSupportedMediaTypes(Collection<MediaType> supportedMediaTypes) {
this.supportedMediaTypes = supportedMediaTypes;
}

/**
* No-op {@link StringValueResolver} that returns the given {@link String} value as is.
*
Original file line number Diff line number Diff line change
@@ -24,6 +24,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.BeanCreationException;
@@ -173,6 +174,7 @@
* @author Jon Brisbin
* @author Greg Turnquist
* @author Mark Paluch
* @author Petar Tahchiev
*/
@Configuration
@EnableHypermediaSupport(type = HypermediaType.HAL)
@@ -626,6 +628,7 @@ public DelegatingHandlerMapping restHandlerMapping() {
repositoryMapping.setJpaHelper(jpaHelper());
repositoryMapping.setApplicationContext(applicationContext);
repositoryMapping.setCorsConfigurations(corsConfigurations);
repositoryMapping.setSupportedMediaTypes(getSupportedMediaTypes());
repositoryMapping.afterPropertiesSet();

BasePathAwareHandlerMapping basePathMapping = new BasePathAwareHandlerMapping(repositoryRestConfiguration());
@@ -640,6 +643,16 @@ public DelegatingHandlerMapping restHandlerMapping() {
return new DelegatingHandlerMapping(mappings);
}

private Collection<MediaType> getSupportedMediaTypes() {

List<MediaType> result = new ArrayList<>();
for(HttpMessageConverter<?> httpMessageConverter : defaultMessageConverters()) {
result.addAll(httpMessageConverter.getSupportedMediaTypes());
}

return result;
}

@Bean
public RepositoryResourceMappings resourceMappings() {
return new RepositoryResourceMappings(repositories(), persistentEntities(), repositoryRestConfiguration());