-
Notifications
You must be signed in to change notification settings - Fork 474
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#2035 - Introduce MediaTypeConfigurationCustomizer.
HAL and HAL Forms now support customization of the media type-specific configuration via MediaTypeConfigurationCustomizer instances registered in the application context.
- Loading branch information
Showing
5 changed files
with
211 additions
and
43 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
src/main/java/org/springframework/hateoas/mediatype/MediaTypeConfigurationCustomizer.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,34 @@ | ||
/* | ||
* Copyright 2023 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 | ||
* | ||
* https://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.hateoas.mediatype; | ||
|
||
/** | ||
* Callback interface to customize media type-specific configuration. Declare instances of the interface as bean methods | ||
* in Spring configuration. | ||
* | ||
* @author Oliver Drotbohm | ||
* @since 2.2 | ||
*/ | ||
public interface MediaTypeConfigurationCustomizer<T> { | ||
|
||
/** | ||
* Customize the given configuration instance. | ||
* | ||
* @param configuration will never be {@literal null}. | ||
* @return must not be {@literal null}. | ||
*/ | ||
T customize(T configuration); | ||
} |
77 changes: 77 additions & 0 deletions
77
src/main/java/org/springframework/hateoas/mediatype/MediaTypeConfigurationFactory.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,77 @@ | ||
/* | ||
* Copyright 2023 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 | ||
* | ||
* https://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.hateoas.mediatype; | ||
|
||
import java.util.function.Supplier; | ||
import java.util.stream.Stream; | ||
|
||
import org.springframework.beans.factory.ObjectProvider; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* Factory to provide instances of media type-specific configuration processed by | ||
* {@link MediaTypeConfigurationCustomizer}s. | ||
* | ||
* @author Oliver Drotbohm | ||
* @since 2.2 | ||
*/ | ||
public class MediaTypeConfigurationFactory<T, S extends MediaTypeConfigurationCustomizer<T>> { | ||
|
||
private final Supplier<T> supplier; | ||
private final Supplier<Stream<S>> customizers; | ||
|
||
private T resolved; | ||
|
||
/** | ||
* Creates a new {@link MediaTypeConfigurationFactory} for the given supplier of the original instance and all | ||
* {@link MediaTypeConfigurationCustomizer}s. | ||
* | ||
* @param supplier must not be {@literal null}. | ||
* @param customizers must not be {@literal null}. | ||
*/ | ||
MediaTypeConfigurationFactory(Supplier<T> supplier, Supplier<Stream<S>> customizers) { | ||
|
||
Assert.notNull(supplier, "Supplier must not be null!"); | ||
Assert.notNull(customizers, "Customizers must not be null!"); | ||
|
||
this.supplier = supplier; | ||
this.customizers = customizers; | ||
} | ||
|
||
public MediaTypeConfigurationFactory(Supplier<T> supplier, ObjectProvider<S> customizers) { | ||
this(supplier, () -> customizers.orderedStream()); | ||
} | ||
|
||
/** | ||
* Returns the customized configuration instance. | ||
* | ||
* @return will never be {@literal null}. | ||
*/ | ||
public T getConfiguration() { | ||
|
||
if (resolved == null) { | ||
|
||
var source = supplier.get(); | ||
|
||
Assert.notNull(source, "Source instance must not be null!"); | ||
|
||
this.resolved = this.customizers.get() | ||
.reduce(source, (config, customizer) -> customizer.customize(config), (__, r) -> r); | ||
} | ||
|
||
return resolved; | ||
} | ||
} |
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
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
60 changes: 60 additions & 0 deletions
60
...st/java/org/springframework/hateoas/mediatype/MediaTypeConfigurationFactoryUnitTests.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,60 @@ | ||
/* | ||
* Copyright 2023 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 | ||
* | ||
* https://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.hateoas.mediatype; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
import static org.mockito.ArgumentMatchers.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
/** | ||
* @author Oliver Drotbohm | ||
*/ | ||
@ExtendWith(MockitoExtension.class) | ||
class MediaTypeConfigurationFactoryUnitTests { | ||
|
||
@Mock MediaTypeConfigurationCustomizer<Object> first, second; | ||
|
||
@Test // GH-2035 | ||
void invokesCustomizers() { | ||
|
||
var source = new Object(); | ||
var afterFirst = new Object(); | ||
var afterSecond = new Object(); | ||
|
||
doReturn(afterFirst).when(first).customize(source); | ||
doReturn(afterSecond).when(second).customize(afterFirst); | ||
|
||
var factory = new MediaTypeConfigurationFactory<>(() -> source, () -> Stream.of(first, second)); | ||
|
||
assertThat(factory.getConfiguration()).isSameAs(afterSecond); | ||
|
||
verify(first, times(1)).customize(source); | ||
verify(second, times(1)).customize(afterFirst); | ||
|
||
assertThat(factory.getConfiguration()).isSameAs(afterSecond); | ||
|
||
// Does not re-process source instance | ||
verify(first, times(1)).customize(any()); | ||
verify(second, times(1)).customize(any()); | ||
} | ||
} |