-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide a RESTEasy Classic ThresholdConverter
- Loading branch information
1 parent
5b8fa78
commit eede590
Showing
2 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
...n/runtime/src/main/java/io/quarkus/resteasy/common/runtime/config/ThresholdConverter.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,70 @@ | ||
package io.quarkus.resteasy.common.runtime.config; | ||
|
||
import static io.quarkus.runtime.configuration.ConverterSupport.DEFAULT_QUARKUS_CONVERTER_PRIORITY; | ||
|
||
import java.io.Serializable; | ||
import java.time.Duration; | ||
import java.util.Locale; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import jakarta.annotation.Priority; | ||
|
||
import org.eclipse.microprofile.config.spi.Converter; | ||
import org.jboss.resteasy.spi.config.SizeUnit; | ||
import org.jboss.resteasy.spi.config.Threshold; | ||
|
||
/** | ||
* A converter for a {@link Threshold} interface. | ||
*/ | ||
@Priority(DEFAULT_QUARKUS_CONVERTER_PRIORITY) | ||
public class ThresholdConverter implements Converter<Threshold>, Serializable { | ||
|
||
public static final Threshold NONE = Threshold.of(-1L, SizeUnit.BYTE); | ||
public static final Threshold DEFAULT = Threshold.of(512L, SizeUnit.KILOBYTE); | ||
private static final Pattern PATTERN = Pattern.compile("(?<size>-?(?!0)\\d+)\\s*(?<unit>(?:ZB|EB|TB|PB|GB|MB|KB|B)\\b)?"); | ||
|
||
public ThresholdConverter() { | ||
} | ||
|
||
/** | ||
* The converter accepts a value which start with a number by implicitly appending `PT` to it. | ||
* If the value consists only of a number, it implicitly treats the value as seconds. | ||
* Otherwise, tries to convert the value assuming that it is in the accepted ISO-8601 duration format. | ||
* | ||
* @param value duration as String | ||
* @return {@link Duration} | ||
*/ | ||
@Override | ||
public Threshold convert(String value) { | ||
value = value.trim(); | ||
if (value.isEmpty()) { | ||
return null; | ||
} | ||
|
||
// The value should be something like 1 MB or 1MB | ||
final Matcher matcher = PATTERN.matcher(value.toUpperCase(Locale.ROOT)); | ||
if (!matcher.find()) { | ||
return DEFAULT; | ||
} | ||
final String stringSize = matcher.group("size"); | ||
final String stringUnit = matcher.group("unit"); | ||
final long size; | ||
if (stringSize == null || stringSize.isBlank()) { | ||
return DEFAULT; | ||
} else { | ||
size = Long.parseLong(stringSize); | ||
} | ||
if (size < 0L) { | ||
return NONE; | ||
} | ||
SizeUnit unit = null; | ||
for (SizeUnit u : SizeUnit.values()) { | ||
if (u.abbreviation().equals(stringUnit)) { | ||
unit = u; | ||
break; | ||
} | ||
} | ||
return Threshold.of(size, unit == null ? SizeUnit.BYTE : unit); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...untime/src/main/resources/META-INF/services/org.eclipse.microprofile.config.spi.Converter
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 @@ | ||
io.quarkus.resteasy.common.runtime.config.ThresholdConverter |