diff --git a/containers/netty-http/src/main/java/org/glassfish/jersey/netty/httpserver/JerseyHttp2ServerHandler.java b/containers/netty-http/src/main/java/org/glassfish/jersey/netty/httpserver/JerseyHttp2ServerHandler.java index 924fb9a306..3212cd7a64 100644 --- a/containers/netty-http/src/main/java/org/glassfish/jersey/netty/httpserver/JerseyHttp2ServerHandler.java +++ b/containers/netty-http/src/main/java/org/glassfish/jersey/netty/httpserver/JerseyHttp2ServerHandler.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016, 2020 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2016, 2022 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 @@ -134,6 +134,11 @@ private ContainerRequest createContainerRequest(ChannelHandlerContext ctx, Http2 private final Map properties = new HashMap<>(); + @Override + public boolean hasProperty(final String name) { + return properties.containsKey(name); + } + @Override public Object getProperty(String name) { return properties.get(name); diff --git a/containers/netty-http/src/main/java/org/glassfish/jersey/netty/httpserver/JerseyServerHandler.java b/containers/netty-http/src/main/java/org/glassfish/jersey/netty/httpserver/JerseyServerHandler.java index dbe8dc784c..00758b43c1 100644 --- a/containers/netty-http/src/main/java/org/glassfish/jersey/netty/httpserver/JerseyServerHandler.java +++ b/containers/netty-http/src/main/java/org/glassfish/jersey/netty/httpserver/JerseyServerHandler.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016, 2021 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2016, 2022 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 @@ -169,6 +169,11 @@ private ContainerRequest createContainerRequest(ChannelHandlerContext ctx, HttpR private final Map properties = new HashMap<>(); + @Override + public boolean hasProperty(final String name) { + return properties.containsKey(name); + } + @Override public Object getProperty(String name) { return properties.get(name); diff --git a/core-client/src/main/java/org/glassfish/jersey/client/ClientConfig.java b/core-client/src/main/java/org/glassfish/jersey/client/ClientConfig.java index 43737f1ddf..fb9863258c 100644 --- a/core-client/src/main/java/org/glassfish/jersey/client/ClientConfig.java +++ b/core-client/src/main/java/org/glassfish/jersey/client/ClientConfig.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2021 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2022 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 @@ -329,6 +329,11 @@ public Map getProperties() { return commonConfig.getConfiguration().getProperties(); } + @Override + public boolean hasProperty(final String name) { + return commonConfig.getConfiguration().hasProperty(name); + } + @Override public Object getProperty(final String name) { return commonConfig.getConfiguration().getProperty(name); diff --git a/core-client/src/main/java/org/glassfish/jersey/client/ClientRequest.java b/core-client/src/main/java/org/glassfish/jersey/client/ClientRequest.java index b0e69abbe2..45cb994844 100644 --- a/core-client/src/main/java/org/glassfish/jersey/client/ClientRequest.java +++ b/core-client/src/main/java/org/glassfish/jersey/client/ClientRequest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2021 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2022 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 @@ -138,6 +138,11 @@ public T resolveProperty(final String name, final T defaultValue) { return propertiesResolver.get().resolveProperty(name, defaultValue); } + @Override + public boolean hasProperty(final String name) { + return propertiesDelegate.hasProperty(name); + } + @Override public Object getProperty(final String name) { return propertiesDelegate.getProperty(name); diff --git a/core-client/src/test/java/org/glassfish/jersey/client/ClientConfigTest.java b/core-client/src/test/java/org/glassfish/jersey/client/ClientConfigTest.java index 8f301f21f0..d2179f67e2 100644 --- a/core-client/src/test/java/org/glassfish/jersey/client/ClientConfigTest.java +++ b/core-client/src/test/java/org/glassfish/jersey/client/ClientConfigTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2020 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2022 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 @@ -103,6 +103,13 @@ public void testGetProperties() { } } + @Test + public void testHasProperty() { + ClientConfig instance = new ClientConfig().property("name", "value"); + assertTrue(instance.hasProperty("name")); + assertFalse(instance.hasProperty("other")); + } + @Test public void testGetProperty() { ClientConfig instance = new ClientConfig().property("name", "value"); diff --git a/core-client/src/test/java/org/glassfish/jersey/client/ClientRequestTest.java b/core-client/src/test/java/org/glassfish/jersey/client/ClientRequestTest.java index b432d9142f..960443879f 100644 --- a/core-client/src/test/java/org/glassfish/jersey/client/ClientRequestTest.java +++ b/core-client/src/test/java/org/glassfish/jersey/client/ClientRequestTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, 2021 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2013, 2022 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 @@ -92,6 +92,9 @@ public void testResolveProperty() { assertFalse(request.getConfiguration().getPropertyNames().contains("name")); assertFalse(request.getPropertyNames().contains("name")); + assertFalse(request.getConfiguration().hasProperty("name")); + assertFalse(request.hasProperty("name")); + assertNull(request.getConfiguration().getProperty("name")); assertNull(request.getProperty("name")); @@ -109,6 +112,9 @@ public void testResolveProperty() { assertTrue(request.getConfiguration().getPropertyNames().contains("name")); assertFalse(request.getPropertyNames().contains("name")); + assertTrue(request.getConfiguration().hasProperty("name")); + assertFalse(request.hasProperty("name")); + assertEquals("value-global", request.getConfiguration().getProperty("name")); assertNull(request.getProperty("name")); @@ -127,6 +133,9 @@ public void testResolveProperty() { assertFalse(request.getConfiguration().getPropertyNames().contains("name")); assertTrue(request.getPropertyNames().contains("name")); + assertFalse(request.getConfiguration().hasProperty("name")); + assertTrue(request.hasProperty("name")); + assertNull(request.getConfiguration().getProperty("name")); assertEquals("value-request", request.getProperty("name")); @@ -145,6 +154,9 @@ public void testResolveProperty() { assertTrue(request.getConfiguration().getPropertyNames().contains("name")); assertTrue(request.getPropertyNames().contains("name")); + assertTrue(request.getConfiguration().hasProperty("name")); + assertTrue(request.hasProperty("name")); + assertEquals("value-global", request.getConfiguration().getProperty("name")); assertEquals("value-request", request.getProperty("name")); diff --git a/core-common/src/main/java/org/glassfish/jersey/internal/MapPropertiesDelegate.java b/core-common/src/main/java/org/glassfish/jersey/internal/MapPropertiesDelegate.java index aed1cfe912..3cce5bdc45 100644 --- a/core-common/src/main/java/org/glassfish/jersey/internal/MapPropertiesDelegate.java +++ b/core-common/src/main/java/org/glassfish/jersey/internal/MapPropertiesDelegate.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2019 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2022 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 @@ -63,6 +63,11 @@ public MapPropertiesDelegate(PropertiesDelegate that) { } } + @Override + public boolean hasProperty(final String name) { + return store.containsKey(name); + } + @Override public Object getProperty(String name) { return store.get(name); diff --git a/core-common/src/main/java/org/glassfish/jersey/internal/PropertiesDelegate.java b/core-common/src/main/java/org/glassfish/jersey/internal/PropertiesDelegate.java index 70833b9462..3413de042c 100644 --- a/core-common/src/main/java/org/glassfish/jersey/internal/PropertiesDelegate.java +++ b/core-common/src/main/java/org/glassfish/jersey/internal/PropertiesDelegate.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2019 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2022 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 @@ -43,6 +43,22 @@ public interface PropertiesDelegate { */ public Object getProperty(String name); + /** + * Returns {@code true} if the property with the given name registered in the current request/response + * exchange context, or {@code false} if there is no property by that name. + *

+ * Use the {@link #getProperty} method with a property name to get the value of + * a property. + *

+ * + * @return {@code true} if a property matching the given name exists, or + * {@code false} otherwise. + * @see #getProperty + * @since 3.1.0 + */ + public default boolean hasProperty(String name) { + return getProperty(name) != null; + } /** * Returns an immutable {@link java.util.Collection collection} containing the property diff --git a/core-common/src/main/java/org/glassfish/jersey/message/internal/InterceptorExecutor.java b/core-common/src/main/java/org/glassfish/jersey/message/internal/InterceptorExecutor.java index 3040bf6ac0..9acc012316 100644 --- a/core-common/src/main/java/org/glassfish/jersey/message/internal/InterceptorExecutor.java +++ b/core-common/src/main/java/org/glassfish/jersey/message/internal/InterceptorExecutor.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2020 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2022 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 @@ -88,6 +88,11 @@ public InterceptorExecutor(final Class rawType, final Type type, final Annota this.tracingLogger = TracingLogger.getInstance(propertiesDelegate); } + @Override + public boolean hasProperty(final String name) { + return propertiesDelegate.hasProperty(name); + } + @Override public Object getProperty(final String name) { return propertiesDelegate.getProperty(name); diff --git a/core-common/src/main/java/org/glassfish/jersey/message/internal/TracingAwarePropertiesDelegate.java b/core-common/src/main/java/org/glassfish/jersey/message/internal/TracingAwarePropertiesDelegate.java index 97afd686e3..a75e554241 100644 --- a/core-common/src/main/java/org/glassfish/jersey/message/internal/TracingAwarePropertiesDelegate.java +++ b/core-common/src/main/java/org/glassfish/jersey/message/internal/TracingAwarePropertiesDelegate.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013, 2019 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2013, 2022 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 @@ -58,6 +58,14 @@ public void setProperty(String name, Object object) { propertiesDelegate.setProperty(name, object); } + @Override + public boolean hasProperty(final String name) { + if (tracingLogger != null && TracingLogger.PROPERTY_NAME.equals(name)) { + return true; + } + return propertiesDelegate.hasProperty(name); + } + @Override public Object getProperty(String name) { if (tracingLogger != null && TracingLogger.PROPERTY_NAME.equals(name)) { diff --git a/core-server/src/main/java/org/glassfish/jersey/server/ContainerRequest.java b/core-server/src/main/java/org/glassfish/jersey/server/ContainerRequest.java index 61688b5c2f..c7a8615d3c 100644 --- a/core-server/src/main/java/org/glassfish/jersey/server/ContainerRequest.java +++ b/core-server/src/main/java/org/glassfish/jersey/server/ContainerRequest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2021 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2022 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 @@ -300,6 +300,11 @@ public T resolveProperty(final String name, final T defaultValue) { return propertiesResolver.get().resolveProperty(name, defaultValue); } + @Override + public boolean hasProperty(final String name) { + return propertiesDelegate.hasProperty(name); + } + @Override public Object getProperty(final String name) { return propertiesDelegate.getProperty(name); 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 291b76fb5d..ae39a6848f 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, 2022 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 @@ -755,6 +755,11 @@ public final Map getProperties() { return state.getProperties(); } + @Override + public final boolean hasProperty(final String name) { + return state.hasProperty(name); + } + @Override public final Object getProperty(final String name) { return state.getProperty(name); diff --git a/core-server/src/test/java/org/glassfish/jersey/server/internal/RuntimeDelegateImplTest.java b/core-server/src/test/java/org/glassfish/jersey/server/internal/RuntimeDelegateImplTest.java index 5a49cbb473..3b42c6dfd3 100644 --- a/core-server/src/test/java/org/glassfish/jersey/server/internal/RuntimeDelegateImplTest.java +++ b/core-server/src/test/java/org/glassfish/jersey/server/internal/RuntimeDelegateImplTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2020 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2022 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 @@ -16,11 +16,19 @@ package org.glassfish.jersey.server.internal; +import jakarta.ws.rs.SeBootstrap; import jakarta.ws.rs.core.Application; import jakarta.ws.rs.ext.RuntimeDelegate; import org.junit.Test; + +import java.security.NoSuchAlgorithmException; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.notNullValue; import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; /** @@ -43,8 +51,64 @@ public void testCreateEndpoint() { } } + /** + * Checks that the right RuntimeDelegateImpl is loaded by JAX-RS. + */ @Test public void testRuntimeDelegateInstance() { assertSame(RuntimeDelegateImpl.class, RuntimeDelegate.getInstance().getClass()); } + + @Test + public final void shouldCreateConfigurationBuilder() { + // given + final RuntimeDelegate runtimeDelegate = new RuntimeDelegateImpl(); + // when + final SeBootstrap.Configuration.Builder configurationBuilder = runtimeDelegate.createConfigurationBuilder(); + // then + assertThat(configurationBuilder, is(notNullValue())); + } + + @Test + public final void shouldBuildDefaultConfiguration() throws NoSuchAlgorithmException { + // given + final SeBootstrap.Configuration.Builder configurationBuilder = new RuntimeDelegateImpl().createConfigurationBuilder(); + // when + final SeBootstrap.Configuration configuration = configurationBuilder.build(); + + // then + assertThat(configuration, is(notNullValue())); + assertTrue(configuration.hasProperty(SeBootstrap.Configuration.PROTOCOL)); + assertTrue(configuration.hasProperty(SeBootstrap.Configuration.HOST)); + assertTrue(configuration.hasProperty(SeBootstrap.Configuration.PORT)); + assertTrue(configuration.hasProperty(SeBootstrap.Configuration.ROOT_PATH)); + assertTrue(configuration.hasProperty(SeBootstrap.Configuration.SSL_CLIENT_AUTHENTICATION)); + assertTrue(configuration.hasProperty(SeBootstrap.Configuration.SSL_CONTEXT)); + assertThat(configuration.property(SeBootstrap.Configuration.PROTOCOL), is("HTTP")); + assertThat(configuration.property(SeBootstrap.Configuration.HOST), is("localhost")); + assertThat(configuration.property(SeBootstrap.Configuration.PORT), is(SeBootstrap.Configuration.DEFAULT_PORT)); + assertThat(configuration.property(SeBootstrap.Configuration.ROOT_PATH), is("/")); + assertThat(configuration.property(SeBootstrap.Configuration.SSL_CLIENT_AUTHENTICATION), + is(SeBootstrap.Configuration.SSLClientAuthentication.NONE)); +// assertThat(configuration.property(SeBootstrap.Configuration.SSL_CONTEXT), is(theInstance(SSLContext.getDefault()))); + assertThat(configuration.protocol(), is("HTTP")); + assertThat(configuration.host(), is("localhost")); + assertThat(configuration.port(), is(SeBootstrap.Configuration.DEFAULT_PORT)); + assertThat(configuration.rootPath(), is("/")); + assertThat(configuration.sslClientAuthentication(), is(SeBootstrap.Configuration.SSLClientAuthentication.NONE)); +// assertThat(configuration.sslContext(), is(theInstance(SSLContext.getDefault()))); + } + + @Test + public final void shouldBuildConfigurationContainingCustomProperties() { + // given + final SeBootstrap.Configuration.Builder configurationBuilder = new RuntimeDelegateImpl().createConfigurationBuilder(); + // when + final SeBootstrap.Configuration configuration = configurationBuilder.property("property", "value").build(); + + // then + assertThat(configuration, is(notNullValue())); + assertTrue(configuration.hasProperty("property")); + assertThat(configuration.property("property"), is("value")); + } }