Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for hasProperty method #4956

Merged
merged 3 commits into from
Jan 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -134,6 +134,11 @@ private ContainerRequest createContainerRequest(ChannelHandlerContext ctx, Http2

private final Map<String, Object> properties = new HashMap<>();

@Override
public boolean hasProperty(final String name) {
return properties.containsKey(name);
}

@Override
public Object getProperty(String name) {
return properties.get(name);
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -169,6 +169,11 @@ private ContainerRequest createContainerRequest(ChannelHandlerContext ctx, HttpR

private final Map<String, Object> properties = new HashMap<>();

@Override
public boolean hasProperty(final String name) {
return properties.containsKey(name);
}

@Override
public Object getProperty(String name) {
return properties.get(name);
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -329,6 +329,11 @@ public Map<String, Object> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -138,6 +138,11 @@ public <T> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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"));

Expand All @@ -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"));

Expand All @@ -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"));

Expand All @@ -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"));

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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.
* <p>
* Use the {@link #getProperty} method with a property name to get the value of
* a property.
* </p>
*
* @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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -300,6 +300,11 @@ public <T> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -755,6 +755,11 @@ public final Map<String, Object> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;

/**
Expand All @@ -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"));
}
}