|
1 | 1 | /* |
2 | | - * Copyright (c) 2012, 2023 Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2012, 2025 Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * Copyright (c) 2018 Payara Foundation and/or its affiliates. |
4 | 4 | * |
5 | 5 | * This program and the accompanying materials are made available under the |
|
27 | 27 | import java.util.Date; |
28 | 28 | import java.util.List; |
29 | 29 | import java.util.concurrent.ExecutionException; |
| 30 | +import java.util.concurrent.atomic.AtomicInteger; |
30 | 31 | import java.util.stream.Collectors; |
31 | 32 |
|
| 33 | +import javax.annotation.Priority; |
| 34 | +import javax.inject.Singleton; |
32 | 35 | import javax.ws.rs.DefaultValue; |
33 | 36 | import javax.ws.rs.GET; |
34 | 37 | import javax.ws.rs.HeaderParam; |
@@ -104,6 +107,15 @@ public void testBadEnumResource() throws ExecutionException, InterruptedExceptio |
104 | 107 | assertEquals(404, responseContext.getStatus()); |
105 | 108 | } |
106 | 109 |
|
| 110 | + @Test |
| 111 | + public void testCustomEnumResource() throws ExecutionException, InterruptedException { |
| 112 | + initiateWebApplication(BadEnumResource.class, EnumParamConverterProvider.class); |
| 113 | + final ContainerResponse responseContext = getResponseContext(UriBuilder.fromPath("/") |
| 114 | + .queryParam("d", "A").build().toString()); |
| 115 | + assertEquals(1, counter.get()); |
| 116 | + assertEquals(200, responseContext.getStatus()); |
| 117 | + } |
| 118 | + |
107 | 119 | public static class URIStringReaderProvider implements ParamConverterProvider { |
108 | 120 |
|
109 | 121 | @Override |
@@ -188,6 +200,40 @@ public String toString(final List<String> value) throws IllegalArgumentException |
188 | 200 | } |
189 | 201 | } |
190 | 202 |
|
| 203 | + static final AtomicInteger counter = new AtomicInteger(0); |
| 204 | + @Singleton |
| 205 | + @Priority(1) |
| 206 | + public static class EnumParamConverterProvider implements ParamConverterProvider { |
| 207 | + |
| 208 | + @Override |
| 209 | + public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) { |
| 210 | + if (Enum.class.isAssignableFrom(rawType)) { |
| 211 | + return new ParamConverter<T>() { |
| 212 | + @Override |
| 213 | + public T fromString(final String value) { |
| 214 | + counter.addAndGet(1); |
| 215 | + if (value == null) { |
| 216 | + return null; |
| 217 | + } |
| 218 | + Class<? extends Enum> enumClass = null; |
| 219 | + try { |
| 220 | + enumClass = (Class<Enum>) Class.forName(genericType.getTypeName()); |
| 221 | + } catch (ClassNotFoundException e) { |
| 222 | + throw new RuntimeException(e); |
| 223 | + } |
| 224 | + return (T) Enum.valueOf(enumClass, value.toUpperCase()); |
| 225 | + } |
| 226 | + |
| 227 | + @Override |
| 228 | + public String toString(final T value) { |
| 229 | + return String.valueOf(value); |
| 230 | + } |
| 231 | + }; |
| 232 | + } |
| 233 | + return null; |
| 234 | + } |
| 235 | + } |
| 236 | + |
191 | 237 | @Path("/") |
192 | 238 | public static class ListOfStringResource { |
193 | 239 |
|
|
0 commit comments