-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #1228: change header values for live channel timeout fallback f…
…rom duration to a strategy enum. Signed-off-by: Yufei Cai <yufei.cai@bosch.io>
- Loading branch information
Showing
7 changed files
with
175 additions
and
16 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
82 changes: 82 additions & 0 deletions
82
base/model/src/main/java/org/eclipse/ditto/base/model/headers/EnumValueValidator.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,82 @@ | ||
/* | ||
* Copyright (c) 2021 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.eclipse.ditto.base.model.headers; | ||
|
||
import java.text.MessageFormat; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.Locale; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
import javax.annotation.concurrent.Immutable; | ||
|
||
import org.eclipse.ditto.base.model.exceptions.DittoHeaderInvalidException; | ||
|
||
/** | ||
* This validator checks if a normalized CharSequence denote an enum value. | ||
* | ||
* @since 2.3.0 | ||
*/ | ||
@Immutable | ||
final class EnumValueValidator extends AbstractHeaderValueValidator { | ||
|
||
private final Set<String> enumValueSet; | ||
private final String errorDescription; | ||
|
||
private EnumValueValidator(final Enum<?>[] enumValues) { | ||
super(String.class::equals); | ||
enumValueSet = groupByNormalizedName(enumValues); | ||
errorDescription = formatErrorDescription(enumValueSet); | ||
} | ||
|
||
/** | ||
* Returns an instance of {@code DittoChannelValueValidator}. | ||
* | ||
* @return the instance. | ||
*/ | ||
static EnumValueValidator getInstance(final Enum<?>[] enumValues) { | ||
return new EnumValueValidator(enumValues); | ||
} | ||
|
||
@Override | ||
protected void validateValue(final HeaderDefinition definition, final CharSequence value) { | ||
final String normalizedValue = normalize(value); | ||
if (!enumValueSet.contains(normalizedValue)) { | ||
throw DittoHeaderInvalidException.newInvalidTypeBuilder(definition, value, | ||
DittoHeaderDefinition.ON_LIVE_CHANNEL_TIMEOUT.getKey()) | ||
.description(errorDescription) | ||
.build(); | ||
} | ||
} | ||
|
||
private static String normalize(final CharSequence charSequence) { | ||
return charSequence.toString().trim().toLowerCase(Locale.ENGLISH); | ||
} | ||
|
||
private static Set<String> groupByNormalizedName(final Enum<?>[] enumValues) { | ||
final Set<String> set = Arrays.stream(enumValues) | ||
.map(Enum::toString) | ||
.map(EnumValueValidator::normalize) | ||
.collect(Collectors.toSet()); | ||
return Collections.unmodifiableSet(set); | ||
} | ||
|
||
private static String formatErrorDescription(final Collection<String> normalizedNames) { | ||
final String valuesString = normalizedNames.stream() | ||
.map(name -> "<" + name + ">") | ||
.collect(Collectors.joining(", ")); | ||
return MessageFormat.format("The value must either be one of: {0}.", valuesString); | ||
} | ||
} |
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
51 changes: 51 additions & 0 deletions
51
.../model/src/main/java/org/eclipse/ditto/base/model/headers/LiveChannelTimeoutStrategy.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,51 @@ | ||
/* | ||
* Copyright (c) 2021 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.eclipse.ditto.base.model.headers; | ||
|
||
import java.util.Arrays; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Headers for commands and their responses which provide additional information needed for correlation and transfer. | ||
* | ||
* @since 2.3.0 | ||
*/ | ||
public enum LiveChannelTimeoutStrategy { | ||
|
||
FAIL("fail"), | ||
|
||
USE_TWIN("use-twin"); | ||
|
||
private final String headerValue; | ||
|
||
LiveChannelTimeoutStrategy(final String headerValue) { | ||
this.headerValue = headerValue; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return headerValue; | ||
} | ||
|
||
/** | ||
* Find a live channel timeout strategy by header value. | ||
* | ||
* @param headerValue the header value of the strategy. | ||
* @return the strategy with the given header value if any exists. | ||
*/ | ||
public static Optional<LiveChannelTimeoutStrategy> forHeaderValue(final String headerValue) { | ||
return Arrays.stream(values()) | ||
.filter(strategy -> strategy.toString().equals(headerValue)) | ||
.findAny(); | ||
} | ||
} |
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