From 162fe13c1a2dab5ede5ca16aacf6c086a7e4f3c5 Mon Sep 17 00:00:00 2001 From: Jorge Bescos Gascon Date: Mon, 18 Jan 2021 12:42:03 +0100 Subject: [PATCH] Add a ParamConverterProvider for java.util.Optional parameters Signed-off-by: Jorge Bescos Gascon --- .../internal/OptionalParamConverterTest.java | 106 ++++++++++++++++++ .../internal/inject/ParamConverters.java | 66 ++++++++++- 2 files changed, 167 insertions(+), 5 deletions(-) create mode 100644 connectors/jdk-connector/src/test/java/org/glassfish/jersey/jdk/connector/internal/OptionalParamConverterTest.java diff --git a/connectors/jdk-connector/src/test/java/org/glassfish/jersey/jdk/connector/internal/OptionalParamConverterTest.java b/connectors/jdk-connector/src/test/java/org/glassfish/jersey/jdk/connector/internal/OptionalParamConverterTest.java new file mode 100644 index 00000000000..06fd5d44aa1 --- /dev/null +++ b/connectors/jdk-connector/src/test/java/org/glassfish/jersey/jdk/connector/internal/OptionalParamConverterTest.java @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package org.glassfish.jersey.jdk.connector.internal; + +import java.util.List; +import java.util.Optional; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.Application; +import javax.ws.rs.core.Response; + +import org.glassfish.jersey.client.ClientConfig; +import org.glassfish.jersey.jdk.connector.JdkConnectorProvider; +import org.glassfish.jersey.server.ResourceConfig; +import org.glassfish.jersey.test.JerseyTest; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class OptionalParamConverterTest extends JerseyTest { + + private static final String PARAM_NAME = "paramName"; + + @Path("/OptionalResource") + public static class OptionalResource { + + @GET + @Path("/fromString") + public Response fromString(@QueryParam(PARAM_NAME) Optional data) { + return Response.ok(data.orElse("")).build(); + } + + @GET + @Path("/fromInteger") + public Response fromInteger(@QueryParam(PARAM_NAME) Optional data) { + return Response.ok(data.orElse(0)).build(); + } + + @GET + @Path("/fromList") + public Response fromList(@QueryParam(PARAM_NAME) List> data) { + StringBuilder builder = new StringBuilder(""); + for (Optional val : data) { + builder.append(val.orElse(0)); + } + return Response.ok(builder.toString()).build(); + } + } + + @Override + protected Application configure() { + return new ResourceConfig(OptionalResource.class); + } + + @Override + protected void configureClient(ClientConfig config) { + config.connectorProvider(new JdkConnectorProvider()); + } + + @Test + public void fromOptionalStr() { + Response empty = target("/OptionalResource/fromString").request().get(); + Response notEmpty = target("/OptionalResource/fromString").queryParam(PARAM_NAME, "anyValue").request().get(); + assertEquals(200, empty.getStatus()); + assertEquals("", empty.readEntity(String.class)); + assertEquals(200, notEmpty.getStatus()); + assertEquals("anyValue", notEmpty.readEntity(String.class)); + } + + @Test + public void fromOptionalInt() { + Response empty = target("/OptionalResource/fromInteger").request().get(); + Response notEmpty = target("/OptionalResource/fromInteger").queryParam(PARAM_NAME, 1).request().get(); + assertEquals(200, empty.getStatus()); + assertEquals(Integer.valueOf(0), empty.readEntity(Integer.class)); + assertEquals(200, notEmpty.getStatus()); + assertEquals(Integer.valueOf(1), notEmpty.readEntity(Integer.class)); + } + + @Test + public void fromOptionalList() { + Response empty = target("/OptionalResource/fromList").request().get(); + Response notEmpty = target("/OptionalResource/fromList").queryParam(PARAM_NAME, 1) + .queryParam(PARAM_NAME, 2).request().get(); + assertEquals(200, empty.getStatus()); + assertEquals("", empty.readEntity(String.class)); + assertEquals(200, notEmpty.getStatus()); + assertEquals("12", notEmpty.readEntity(String.class)); + } +} diff --git a/core-common/src/main/java/org/glassfish/jersey/internal/inject/ParamConverters.java b/core-common/src/main/java/org/glassfish/jersey/internal/inject/ParamConverters.java index 31b1a586ffb..797b0994611 100644 --- a/core-common/src/main/java/org/glassfish/jersey/internal/inject/ParamConverters.java +++ b/core-common/src/main/java/org/glassfish/jersey/internal/inject/ParamConverters.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2019 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2021 Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2018 Payara Foundation and/or its affiliates. * * This program and the accompanying materials are made available under the @@ -21,21 +21,23 @@ import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.security.AccessController; import java.text.ParseException; import java.util.Date; +import java.util.Optional; +import javax.inject.Inject; +import javax.inject.Singleton; import javax.ws.rs.ProcessingException; import javax.ws.rs.WebApplicationException; import javax.ws.rs.ext.ParamConverter; import javax.ws.rs.ext.ParamConverterProvider; -import javax.inject.Singleton; - +import org.glassfish.jersey.internal.LocalizationMessages; import org.glassfish.jersey.internal.util.ReflectionHelper; import org.glassfish.jersey.message.internal.HttpDateFormat; -import org.glassfish.jersey.internal.LocalizationMessages; /** * Container of several different {@link ParamConverterProvider param converter providers} @@ -247,6 +249,59 @@ public String toString(final T value) throws IllegalArgumentException { } } + /** + * Provider of {@link ParamConverter param converter} that produce the target Java type instance + * by invoking a static {@code fromString(String)} method on the target type. + */ + @Singleton + public static class OptionalProvider implements ParamConverterProvider { + + // Delegates to this provider when the type of Optional is extracted. + private final AggregatedProvider aggregated; + + @Inject + public OptionalProvider(AggregatedProvider aggregated) { + this.aggregated = aggregated; + } + + @Override + public ParamConverter getConverter(Class rawType, Type genericType, Annotation[] annotations) { + return (rawType != Optional.class) ? null : new ParamConverter() { + + @Override + public T fromString(String value) { + if (value == null) { + return (T) Optional.empty(); + } else { + ParameterizedType parametrized = (ParameterizedType) genericType; + Type type = parametrized.getActualTypeArguments()[0]; + T val = aggregated.getConverter((Class) type, type, annotations).fromString(value.toString()); + if (val != null) { + return (T) Optional.of(val); + } else { + /* + * In this case we don't send Optional.empty() because 'value' is not null. + * But we return null because the provider didn't find how to parse it. + */ + return null; + } + } + } + + @Override + public String toString(T value) throws IllegalArgumentException { + /* + * Unfortunately 'orElse' cannot be stored in an Optional. As only one value can + * be stored, it makes no sense that 'value' is Optional. It can just be the value. + * We don't fail here but we don't process it. + */ + return null; + } + }; + } + + } + /** * Aggregated {@link ParamConverterProvider param converter provider}. */ @@ -267,7 +322,8 @@ public AggregatedProvider() { new TypeValueOf(), new CharacterProvider(), new TypeFromString(), - new StringConstructor() + new StringConstructor(), + new OptionalProvider(this) }; }