Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DATAREST-948 - Introduced ExposureConfiguration to allow customizing …
…the exposure of HTTP methods of repositories. ExposureConfiguration exposes methods to register AggregateResourceHttpMethodsFilter and AssociationResourceHttpMethodsFilter (both applied by type or globally) to customize the supported HTTP methods by collection, item and association resources. It also provides shortcuts for common use cases like disabling PUT for item resources etc.
- Loading branch information
Showing
18 changed files
with
847 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
...-rest-core/src/main/java/org/springframework/data/rest/core/mapping/ComposableFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright 2018 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.data.rest.core.mapping; | ||
|
||
interface ComposableFilter<T, S> { | ||
|
||
S filter(T first, S second); | ||
|
||
default ComposableFilter<T, S> andThen(ComposableFilter<T, S> filter) { | ||
return (first, second) -> filter.filter(first, filter(first, second)); | ||
} | ||
} |
131 changes: 131 additions & 0 deletions
131
...ore/src/main/java/org/springframework/data/rest/core/mapping/ConfigurableHttpMethods.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/* | ||
* Copyright 2018 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.data.rest.core.mapping; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
import org.springframework.http.HttpMethod; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* {@link HttpMethods} that expose methods to create different {@link ConfigurableHttpMethods}. | ||
* | ||
* @author Oliver Gierke | ||
* @since 3.1 | ||
*/ | ||
@RequiredArgsConstructor(staticName = "of", access = AccessLevel.PACKAGE) | ||
public class ConfigurableHttpMethods implements HttpMethods { | ||
|
||
public static final ConfigurableHttpMethods NONE = ConfigurableHttpMethods.of(); | ||
public static final ConfigurableHttpMethods ALL = ConfigurableHttpMethods.of(HttpMethod.values()); | ||
|
||
private final Collection<HttpMethod> methods; | ||
|
||
/** | ||
* Creates a new {@link ConfigurableHttpMethods} of the given {@link HttpMethod}s. | ||
* | ||
* @param methods must not be {@literal null}. | ||
* @return | ||
*/ | ||
static ConfigurableHttpMethods of(HttpMethod... methods) { | ||
|
||
Assert.notNull(methods, "HttpMethods must not be null!"); | ||
|
||
return new ConfigurableHttpMethods(Arrays.stream(methods).collect(Collectors.toSet())); | ||
} | ||
|
||
/** | ||
* Creates a new {@link ConfigurableHttpMethods} of the given {@link HttpMethods}. | ||
* | ||
* @param methods must not be {@literal null}. | ||
* @return | ||
*/ | ||
static ConfigurableHttpMethods of(HttpMethods methods) { | ||
|
||
Assert.notNull(methods, "HttpMethods must not be null!"); | ||
|
||
if (ConfigurableHttpMethods.class.isInstance(methods)) { | ||
return ConfigurableHttpMethods.class.cast(methods); | ||
} | ||
|
||
return new ConfigurableHttpMethods(methods.stream().collect(Collectors.toSet())); | ||
} | ||
|
||
/** | ||
* Disables the given {@link HttpMethod}s. | ||
* | ||
* @param methods must not be {@literal null}. | ||
* @return | ||
*/ | ||
public ConfigurableHttpMethods disable(HttpMethod... methods) { | ||
|
||
Assert.notNull(methods, "HttpMethods must not be null!"); | ||
|
||
List<HttpMethod> toRemove = Arrays.asList(methods); | ||
|
||
return new ConfigurableHttpMethods(this.methods.stream() // | ||
.filter(it -> !toRemove.contains(it)) // | ||
.collect(Collectors.toSet())); | ||
} | ||
|
||
/** | ||
* Enables the given {@link HttpMethod}s. | ||
* | ||
* @param methods must not be {@literal null}. | ||
* @return | ||
*/ | ||
public HttpMethods enable(HttpMethod... methods) { | ||
|
||
Assert.notNull(methods, "HttpMethods must not be null!"); | ||
|
||
List<HttpMethod> toAdd = Arrays.asList(methods); | ||
|
||
if (this.methods.containsAll(toAdd)) { | ||
return this; | ||
} | ||
|
||
return ConfigurableHttpMethods.of(Stream.concat(this.methods.stream(), toAdd.stream()).collect(Collectors.toSet())); | ||
} | ||
|
||
/* | ||
* (non-Javadoc) | ||
* @see org.springframework.data.rest.core.mapping.HttpMethods#contains(org.springframework.http.HttpMethod) | ||
*/ | ||
@Override | ||
public boolean contains(HttpMethod method) { | ||
|
||
Assert.notNull(method, "HTTP method must not be null!"); | ||
|
||
return methods.contains(method); | ||
} | ||
|
||
/* | ||
* (non-Javadoc) | ||
* @see java.lang.Iterable#iterator() | ||
*/ | ||
@Override | ||
public Iterator<HttpMethod> iterator() { | ||
return methods.iterator(); | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
...ingframework/data/rest/core/mapping/ConfigurationApplyingSupportedHttpMethodsAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright 2018 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.data.rest.core.mapping; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
import org.springframework.data.mapping.PersistentProperty; | ||
|
||
/** | ||
* Adapter for a {@link SupportedHttpMethods} instance that applies settings made through {@link ExposureConfiguration} | ||
* to the calculated {@link ConfigurableHttpMethods} | ||
* | ||
* @author Oliver Gierke | ||
* @see ExposureConfiguration | ||
* @since 3.1 | ||
*/ | ||
@RequiredArgsConstructor | ||
public class ConfigurationApplyingSupportedHttpMethodsAdapter implements SupportedHttpMethods { | ||
|
||
private final ExposureConfiguration configuration; | ||
private final ResourceMetadata resourceMetadata; | ||
private final SupportedHttpMethods delegate; | ||
|
||
/* | ||
* (non-Javadoc) | ||
* @see org.springframework.data.rest.core.mapping.SupportedHttpMethods#getMethodsFor(org.springframework.data.rest.core.mapping.ResourceType) | ||
*/ | ||
@Override | ||
public HttpMethods getMethodsFor(ResourceType type) { | ||
|
||
HttpMethods methods = delegate.getMethodsFor(type); | ||
|
||
return configuration.filter(ConfigurableHttpMethods.of(methods), type, resourceMetadata); | ||
} | ||
|
||
/* | ||
* (non-Javadoc) | ||
* @see org.springframework.data.rest.core.mapping.SupportedHttpMethods#getMethodsFor(org.springframework.data.mapping.PersistentProperty) | ||
*/ | ||
@Override | ||
public HttpMethods getMethodsFor(PersistentProperty<?> property) { | ||
|
||
HttpMethods methodsFor = delegate.getMethodsFor(property); | ||
ResourceMapping mapping = resourceMetadata.getMappingFor(property); | ||
|
||
if (!PropertyAwareResourceMapping.class.isInstance(mapping)) { | ||
return methodsFor; | ||
} | ||
|
||
ConfigurableHttpMethods methods = ConfigurableHttpMethods.of(methodsFor); | ||
|
||
return configuration.filter(methods, PropertyAwareResourceMapping.class.cast(mapping)); | ||
} | ||
|
||
/* | ||
* (non-Javadoc) | ||
* @see org.springframework.data.rest.core.mapping.SupportedHttpMethods#allowsPutForCreation() | ||
*/ | ||
@Override | ||
public boolean allowsPutForCreation() { | ||
return configuration.allowsPutForCreation(resourceMetadata); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.