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
16 changed files
with
634 additions
and
42 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)); | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
...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,94 @@ | ||
/* | ||
* 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; | ||
|
||
/** | ||
* {@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; | ||
|
||
static ConfigurableHttpMethods of(HttpMethod... methods) { | ||
return new ConfigurableHttpMethods(Arrays.stream(methods).collect(Collectors.toSet())); | ||
} | ||
|
||
static ConfigurableHttpMethods of(HttpMethods methods) { | ||
|
||
if (ConfigurableHttpMethods.class.isInstance(methods)) { | ||
return ConfigurableHttpMethods.class.cast(methods); | ||
} | ||
|
||
return new ConfigurableHttpMethods(methods.stream().collect(Collectors.toSet())); | ||
} | ||
|
||
public ConfigurableHttpMethods disable(HttpMethod... methods) { | ||
|
||
List<HttpMethod> toRemove = Arrays.asList(methods); | ||
|
||
return new ConfigurableHttpMethods(this.methods.stream() // | ||
.filter(it -> !toRemove.contains(it)) // | ||
.collect(Collectors.toSet())); | ||
} | ||
|
||
public HttpMethods enable(HttpMethod... methods) { | ||
|
||
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) { | ||
return methods.contains(method); | ||
} | ||
|
||
/* | ||
* (non-Javadoc) | ||
* @see java.lang.Iterable#iterator() | ||
*/ | ||
@Override | ||
public Iterator<HttpMethod> iterator() { | ||
return methods.iterator(); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
...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,67 @@ | ||
/* | ||
* 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)); | ||
} | ||
} |
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.