-
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.
#1439 - Support for HAL FORMS min, max, minLength and maxLength attri…
…butes. Introduce support for the min, max, minLength and maxLength fields for HAL FORMS property descriptors. Extended PropertyMetadata to capture those based on JSR-303 annotations. Pulled up InputPayloadMetadata.createProperties(…) into AffordanceModel directly. Deprecated InputPayloadMetadata.applyTo(…).
- 3.0.0-M1
- 2.5.0-M1
- 2.4.1
- 2.4.0
- 2.4.0-RC2
- 2.4.0-RC1
- 2.4.0-M1
- 2.3.4
- 2.3.3
- 2.3.2
- 2.3.1
- 2.3.0
- 2.3.0-RC1
- 2.3.0-M1
- 2.2.5
- 2.2.4
- 2.2.3
- 2.2.2
- 2.2.1
- 2.2.0
- 2.2.0-RC1
- 2.2.0-M2
- 2.2.0-M1
- 2.1.5
- 2.1.4
- 2.1.3
- 2.1.2
- 2.1.1
- 2.1.0
- 2.1.0-RC1
- 2.1.0-M1
- 2.0.7
- 2.0.6
- 2.0.5
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 2.0.0-RC2
- 2.0.0-RC1
- 2.0.0-M6
- 2.0.0-M5
- 2.0.0-M4
- 2.0.0-M3
- 2.0.0-M2
- 2.0.0-M1
- 1.5.6
- 1.5.5
- 1.5.4
- 1.5.3
- 1.5.2
- 1.5.1
- 1.5.0
- 1.5.0-RC1
- 1.5.0-M2
- 1.5.0-M1
- 1.4.5
- 1.4.4
- 1.4.3
- 1.4.2
- 1.4.1
- 1.4.0
- 1.4.0-RC1
- 1.4.0-M3
- 1.4.0-M2
- 1.4.0-M1
- 1.3.7
- 1.3.6
- 1.3.5
- 1.3.4
- 1.3.3
- 1.3.2
- 1.3.1
- 1.3.0
- 1.3.0-RC1
- 1.3.0-M3
- 1.3.0-M2
Showing
9 changed files
with
511 additions
and
212 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
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
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
174 changes: 174 additions & 0 deletions
174
src/main/java/org/springframework/hateoas/mediatype/hal/forms/HalFormsPropertyFactory.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,174 @@ | ||
/* | ||
* Copyright 2021 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.hal.forms; | ||
|
||
import static org.springframework.http.HttpMethod.*; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.EnumSet; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.function.Function; | ||
|
||
import org.springframework.context.MessageSourceResolvable; | ||
import org.springframework.hateoas.AffordanceModel.InputPayloadMetadata; | ||
import org.springframework.hateoas.AffordanceModel.PropertyMetadata; | ||
import org.springframework.hateoas.mediatype.MessageResolver; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.lang.NonNull; | ||
import org.springframework.lang.Nullable; | ||
import org.springframework.util.Assert; | ||
import org.springframework.util.StringUtils; | ||
|
||
/** | ||
* Factory to create {@link HalFormsProperty} instances. | ||
* | ||
* @author Oliver Drotbohm | ||
* @since 1.3 | ||
* @soundtrack The Chicks - March March (Gaslighter) | ||
*/ | ||
public class HalFormsPropertyFactory { | ||
|
||
private static final Set<HttpMethod> ENTITY_ALTERING_METHODS = EnumSet.of(POST, PUT, PATCH); | ||
|
||
private final HalFormsConfiguration configuration; | ||
private final MessageResolver resolver; | ||
|
||
/** | ||
* Creates a new {@link HalFormsPropertyFactory} for the given {@link HalFormsConfiguration} and | ||
* {@link MessageResolver}. | ||
* | ||
* @param configuration must not be {@literal null}. | ||
* @param resolver must not be {@literal null}. | ||
*/ | ||
public HalFormsPropertyFactory(HalFormsConfiguration configuration, MessageResolver resolver) { | ||
|
||
Assert.notNull(configuration, "HalFormsConfiguration must not be null!"); | ||
Assert.notNull(resolver, "MessageResolver must not be null!"); | ||
|
||
this.configuration = configuration; | ||
this.resolver = resolver; | ||
} | ||
|
||
/** | ||
* Creates {@link HalFormsProperty} from the given {@link HalFormsAffordanceModel}. | ||
* | ||
* @param model must not be {@literal null}. | ||
* @return | ||
*/ | ||
public List<HalFormsProperty> createProperties(HalFormsAffordanceModel model) { | ||
|
||
Assert.notNull(model, "HalFormsModel must not be null!"); | ||
|
||
if (!ENTITY_ALTERING_METHODS.contains(model.getHttpMethod())) { | ||
return Collections.emptyList(); | ||
} | ||
|
||
return model.createProperties((payload, metadata) -> { | ||
|
||
HalFormsProperty property = new HalFormsProperty() | ||
.withName(metadata.getName()) | ||
.withRequired(metadata.isRequired()) // | ||
.withReadOnly(metadata.isReadOnly()) | ||
.withMin(metadata.getMin()) | ||
.withMax(metadata.getMax()) | ||
.withMinLength(metadata.getMinLength()) | ||
.withMaxLength(metadata.getMaxLength()) | ||
.withRegex(lookupRegex(metadata)); | ||
|
||
Function<String, I18nedPropertyMetadata> factory = I18nedPropertyMetadata.factory(payload, property); | ||
|
||
return Optional.of(property) | ||
.map(it -> i18n(it, factory.apply("_placeholder"), it::withPlaceholder)) | ||
.map(it -> i18n(it, factory.apply("_prompt"), it::withPrompt)) | ||
.map(it -> model.hasHttpMethod(HttpMethod.PATCH) ? it.withRequired(false) : it) | ||
.orElse(property); | ||
}); | ||
} | ||
|
||
private Optional<String> lookupRegex(PropertyMetadata metadata) { | ||
|
||
Optional<String> pattern = metadata.getPattern(); | ||
|
||
if (pattern.isPresent()) { | ||
return pattern; | ||
} | ||
|
||
return configuration.getTypePatternFor(metadata.getType()); | ||
} | ||
|
||
private HalFormsProperty i18n(HalFormsProperty property, MessageSourceResolvable metadata, | ||
Function<String, HalFormsProperty> application) { | ||
|
||
String resolved = resolver.resolve(metadata); | ||
|
||
return !StringUtils.hasText(resolved) | ||
? property | ||
: application.apply(resolved); | ||
} | ||
|
||
private static class I18nedPropertyMetadata implements MessageSourceResolvable { | ||
|
||
private final String template; | ||
private final InputPayloadMetadata metadata; | ||
private final HalFormsProperty property; | ||
|
||
private I18nedPropertyMetadata(String template, InputPayloadMetadata metadata, HalFormsProperty property) { | ||
|
||
this.template = template; | ||
this.metadata = metadata; | ||
this.property = property; | ||
} | ||
|
||
public static Function<String, I18nedPropertyMetadata> factory(InputPayloadMetadata metadata, | ||
HalFormsProperty property) { | ||
return suffix -> new I18nedPropertyMetadata("%s.".concat(suffix), metadata, property); | ||
} | ||
|
||
/* | ||
* (non-Javadoc) | ||
* @see org.springframework.context.MessageSourceResolvable#getDefaultMessage() | ||
*/ | ||
@Nullable | ||
@Override | ||
public String getDefaultMessage() { | ||
return ""; | ||
} | ||
|
||
/* | ||
* (non-Javadoc) | ||
* @see org.springframework.context.MessageSourceResolvable#getCodes() | ||
*/ | ||
@NonNull | ||
@Override | ||
public String[] getCodes() { | ||
|
||
String globalCode = String.format(template, property.getName()); | ||
|
||
List<String> codes = new ArrayList<>(); | ||
|
||
metadata.getI18nCodes().stream() // | ||
.map(it -> String.format("%s.%s", it, globalCode)) // | ||
.forEach(codes::add); | ||
|
||
codes.add(globalCode); | ||
|
||
return codes.toArray(new String[0]); | ||
} | ||
} | ||
} |
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
36 changes: 36 additions & 0 deletions
36
src/main/java/org/springframework/hateoas/support/ClassUtils.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,36 @@ | ||
/* | ||
* Copyright 2021 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.support; | ||
|
||
import org.springframework.lang.Nullable; | ||
|
||
/** | ||
* @author Oliver Drotbohm | ||
*/ | ||
public class ClassUtils { | ||
|
||
@Nullable | ||
@SuppressWarnings("unchecked") | ||
public static <T> Class<T> loadIfPresent(String type) { | ||
|
||
try { | ||
return (Class<T>) org.springframework.util.ClassUtils.forName(type, | ||
org.springframework.hateoas.support.ClassUtils.class.getClassLoader()); | ||
} catch (ClassNotFoundException | LinkageError e) { | ||
return null; | ||
} | ||
} | ||
} |
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