diff --git a/core-common/src/main/java/org/glassfish/jersey/ApplicationSupplier.java b/core-common/src/main/java/org/glassfish/jersey/ApplicationSupplier.java new file mode 100644 index 0000000000..1eb9ca5c72 --- /dev/null +++ b/core-common/src/main/java/org/glassfish/jersey/ApplicationSupplier.java @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2024 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; + +import javax.ws.rs.core.Application; + +/** + * Implementation of this interface is capable of returning {@link Application}. + */ +public interface ApplicationSupplier { + /** + * Get Application. + * + * @return Application. + */ + Application getApplication(); + +} diff --git a/core-common/src/main/java/org/glassfish/jersey/CommonProperties.java b/core-common/src/main/java/org/glassfish/jersey/CommonProperties.java index f1a713e246..bab760bd6f 100644 --- a/core-common/src/main/java/org/glassfish/jersey/CommonProperties.java +++ b/core-common/src/main/java/org/glassfish/jersey/CommonProperties.java @@ -130,6 +130,19 @@ public final class CommonProperties { */ public static final String JSON_BINDING_FEATURE_DISABLE_SERVER = "jersey.config.server.disableJsonBinding"; + /** + * Disables configuration of Json Binding (JSR-367) feature for {@link javax.ws.rs.core.Application} subclasses whose + * package names are specified as a value. The value is comma-separated string defining prefixes of the application + * package names. + *
+ * By default, Json Binding is automatically enabled. + *
+ * The name of the configuration property is {@value}. + *
+ * @since 2.45 + */ + public static final String JSON_BINDING_FEATURE_DISABLE_PACKAGE = "jersey.config.disableJsonBinding.package"; + /** * If {@code true} then disable configuration of Json Processing (JSR-353) feature. *diff --git a/core-common/src/main/java/org/glassfish/jersey/internal/util/PropertiesHelper.java b/core-common/src/main/java/org/glassfish/jersey/internal/util/PropertiesHelper.java index cb1c87b864..82c08ef3b1 100644 --- a/core-common/src/main/java/org/glassfish/jersey/internal/util/PropertiesHelper.java +++ b/core-common/src/main/java/org/glassfish/jersey/internal/util/PropertiesHelper.java @@ -389,6 +389,27 @@ public static boolean isProperty(final Object value) { } } + /** + * Converts the property value to {@code boolean} and checks it is {@code true} or empty. + * Returns {@code true} if the value is {@code true} or empty but not {@code null}. + * + *
+ * The rationale behind this is that system property {@code -Dprop=true} is the same as {@code -Dprop}. + * The property {@code -Dprop=false} behaves as if the {@code -Dprop} is not set at all. + *
+ * + * @param value property value. + * @return {@code boolean} property value or {@code true} if the property value is not set or {@code false} if the property + * is otherwise not convertible. + */ + public static boolean isPropertyOrNotSet(final Object value) { + if (value instanceof Boolean) { + return Boolean.class.cast(value); + } else { + return value != null && ("".equals(value.toString()) || Boolean.parseBoolean(value.toString())); + } + } + /** * Faster replacement of {@code RuntimeType#name().toLowerCase(Locale.ROOT)} * @param runtimeType The runtime type to lower case diff --git a/core-common/src/test/java/org/glassfish/jersey/internal/util/PropertiesHelperTest.java b/core-common/src/test/java/org/glassfish/jersey/internal/util/PropertiesHelperTest.java index 68f6aac7d4..69acabf48b 100644 --- a/core-common/src/test/java/org/glassfish/jersey/internal/util/PropertiesHelperTest.java +++ b/core-common/src/test/java/org/glassfish/jersey/internal/util/PropertiesHelperTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, 2022 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2013, 2024 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 @@ -199,4 +199,15 @@ public void testconvertValue() { } + @Test + public void isPropertyOrNotSetTest() { + assertEquals(false, PropertiesHelper.isPropertyOrNotSet((Boolean) null)); + assertEquals(true, PropertiesHelper.isPropertyOrNotSet(Boolean.TRUE)); + assertEquals(false, PropertiesHelper.isPropertyOrNotSet(Boolean.FALSE)); + assertEquals(false, PropertiesHelper.isPropertyOrNotSet((String) null)); + assertEquals(true, PropertiesHelper.isPropertyOrNotSet("")); + assertEquals(false, PropertiesHelper.isPropertyOrNotSet("treu")); // false for non-boolean values + assertEquals(true, PropertiesHelper.isPropertyOrNotSet("TRUE")); + assertEquals(false, PropertiesHelper.isPropertyOrNotSet("false")); + } } diff --git a/core-server/src/main/java/org/glassfish/jersey/server/ResourceConfig.java b/core-server/src/main/java/org/glassfish/jersey/server/ResourceConfig.java index 1b13d056e3..359363e4b4 100644 --- a/core-server/src/main/java/org/glassfish/jersey/server/ResourceConfig.java +++ b/core-server/src/main/java/org/glassfish/jersey/server/ResourceConfig.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2021 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2024 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 @@ -37,6 +37,7 @@ import javax.ws.rs.core.Configuration; import javax.ws.rs.core.Feature; +import org.glassfish.jersey.ApplicationSupplier; import org.glassfish.jersey.internal.Errors; import org.glassfish.jersey.internal.config.ExternalPropertiesConfigurationFactory; import org.glassfish.jersey.internal.inject.Binder; @@ -67,7 +68,7 @@ * @author Michal Gajdos * @author Marek Potociar */ -public class ResourceConfig extends Application implements Configurable
@@ -50,17 +60,85 @@
*/
public class JsonBindingFeature implements Feature {
+ private static final Logger LOGGER = Logger.getLogger(JsonBindingFeature.class.getName());
private static final String JSON_FEATURE = JsonBindingFeature.class.getSimpleName();
@Override
public boolean configure(final FeatureContext context) {
final Configuration config = context.getConfiguration();
- if (CommonProperties.getValue(config.getProperties(), config.getRuntimeType(),
- CommonProperties.JSON_BINDING_FEATURE_DISABLE, Boolean.FALSE, Boolean.class)) {
+ // ---- Allow to disable for compatibility with Pre JAX-RS 2.1 Jersey.
+
+ /* Either system properties */
+ final String bindingDisabledBySystemProperty = AccessController.doPrivileged(
+ PropertiesHelper.getSystemProperty(CommonProperties.JSON_BINDING_FEATURE_DISABLE));
+
+ final String bindingDisabledBySystemPropertyClient = AccessController.doPrivileged(
+ PropertiesHelper.getSystemProperty(CommonProperties.JSON_BINDING_FEATURE_DISABLE_CLIENT));
+
+ final String bindingDisabledBySystemPropertyServer = AccessController.doPrivileged(
+ PropertiesHelper.getSystemProperty(CommonProperties.JSON_BINDING_FEATURE_DISABLE_SERVER));
+
+ final RuntimeType runtimeType = config.getRuntimeType();
+
+ boolean bindingDisabledBySystem = PropertiesHelper.isPropertyOrNotSet(bindingDisabledBySystemProperty)
+ || (runtimeType == RuntimeType.CLIENT
+ && PropertiesHelper.isPropertyOrNotSet(bindingDisabledBySystemPropertyClient))
+ || (runtimeType == RuntimeType.SERVER
+ && PropertiesHelper.isPropertyOrNotSet(bindingDisabledBySystemPropertyServer));
+
+ /* Or config property */
+ final Boolean bindingDisabled = CommonProperties.getValue(config.getProperties(), runtimeType,
+ CommonProperties.JSON_BINDING_FEATURE_DISABLE, Boolean.class);
+
+ /* Config property takes precedence */
+ if ((bindingDisabledBySystem && !Boolean.FALSE.equals(bindingDisabled)) || Boolean.TRUE.equals(bindingDisabled)) {
return false;
}
+ final Set