15 changed files with 494 additions and 519 deletions
@ -1,79 +0,0 @@
@@ -1,79 +0,0 @@
|
||||
/* |
||||
* Copyright 2020 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.springframework.security.oauth2.core.converter; |
||||
|
||||
import org.springframework.core.convert.TypeDescriptor; |
||||
import org.springframework.core.convert.converter.ConditionalGenericConverter; |
||||
import org.springframework.core.convert.converter.GenericConverter; |
||||
import org.springframework.util.ClassUtils; |
||||
|
||||
import java.util.Collection; |
||||
import java.util.Collections; |
||||
import java.util.LinkedHashSet; |
||||
import java.util.Set; |
||||
|
||||
/** |
||||
* TODO |
||||
* This class is temporary and will be removed after upgrading to Spring Security 5.5.0 GA. |
||||
* |
||||
* @author Daniel Garnier-Moiroux |
||||
* @since 0.1.0 |
||||
* @see <a target="_blank" href="https://github.com/spring-projects/spring-security/pull/9146">Issue gh-9146</a> |
||||
*/ |
||||
final public class ObjectToSetStringConverter2 implements ConditionalGenericConverter { |
||||
|
||||
@Override |
||||
public Set<GenericConverter.ConvertiblePair> getConvertibleTypes() { |
||||
Set<GenericConverter.ConvertiblePair> convertibleTypes = new LinkedHashSet<>(); |
||||
convertibleTypes.add(new GenericConverter.ConvertiblePair(Object.class, Set.class)); |
||||
return convertibleTypes; |
||||
} |
||||
|
||||
@Override |
||||
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) { |
||||
if (targetType.getElementTypeDescriptor() == null |
||||
|| targetType.getElementTypeDescriptor().getType().equals(String.class) || sourceType == null |
||||
|| ClassUtils.isAssignable(sourceType.getType(), targetType.getElementTypeDescriptor().getType())) { |
||||
return true; |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
@Override |
||||
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { |
||||
if (source == null) { |
||||
return null; |
||||
} |
||||
if (source instanceof Set) { |
||||
Set<?> sourceList = (Set<?>) source; |
||||
for (Object entry: sourceList) { |
||||
if (entry instanceof String) { |
||||
return source; |
||||
} |
||||
} |
||||
} |
||||
if (source instanceof Collection) { |
||||
Collection<String> results = new LinkedHashSet<>(); |
||||
for (Object object : ((Collection<?>) source)) { |
||||
if (object != null) { |
||||
results.add(object.toString()); |
||||
} |
||||
} |
||||
return results; |
||||
} |
||||
return Collections.singleton(source.toString()); |
||||
} |
||||
} |
||||
@ -0,0 +1,67 @@
@@ -0,0 +1,67 @@
|
||||
/* |
||||
* Copyright 2002-2018 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.springframework.security.oauth2.core.oidc.http.converter; |
||||
|
||||
import org.springframework.http.converter.GenericHttpMessageConverter; |
||||
import org.springframework.http.converter.HttpMessageConverter; |
||||
import org.springframework.http.converter.json.GsonHttpMessageConverter; |
||||
import org.springframework.http.converter.json.JsonbHttpMessageConverter; |
||||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; |
||||
import org.springframework.util.ClassUtils; |
||||
|
||||
/** |
||||
* TODO |
||||
* This class is a straight copy from Spring Security. |
||||
* It should be consolidated when merging this codebase into Spring Security. |
||||
* |
||||
* Utility methods for {@link HttpMessageConverter}'s. |
||||
* |
||||
* @author Joe Grandja |
||||
* @since 5.1 |
||||
*/ |
||||
final class HttpMessageConverters { |
||||
|
||||
private static final boolean jackson2Present; |
||||
|
||||
private static final boolean gsonPresent; |
||||
|
||||
private static final boolean jsonbPresent; |
||||
|
||||
static { |
||||
ClassLoader classLoader = HttpMessageConverters.class.getClassLoader(); |
||||
jackson2Present = ClassUtils.isPresent("com.fasterxml.jackson.databind.ObjectMapper", classLoader) |
||||
&& ClassUtils.isPresent("com.fasterxml.jackson.core.JsonGenerator", classLoader); |
||||
gsonPresent = ClassUtils.isPresent("com.google.gson.Gson", classLoader); |
||||
jsonbPresent = ClassUtils.isPresent("javax.json.bind.Jsonb", classLoader); |
||||
} |
||||
|
||||
private HttpMessageConverters() { |
||||
} |
||||
|
||||
static GenericHttpMessageConverter<Object> getJsonMessageConverter() { |
||||
if (jackson2Present) { |
||||
return new MappingJackson2HttpMessageConverter(); |
||||
} |
||||
if (gsonPresent) { |
||||
return new GsonHttpMessageConverter(); |
||||
} |
||||
if (jsonbPresent) { |
||||
return new JsonbHttpMessageConverter(); |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
} |
||||
71
oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/core/http/converter/OidcProviderConfigurationHttpMessageConverter.java → oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/core/oidc/http/converter/OidcProviderConfigurationHttpMessageConverter.java
71
oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/core/http/converter/OidcProviderConfigurationHttpMessageConverter.java → oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/core/oidc/http/converter/OidcProviderConfigurationHttpMessageConverter.java
@ -1,76 +0,0 @@
@@ -1,76 +0,0 @@
|
||||
/* |
||||
* Copyright 2020 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package org.springframework.security.oauth2.core.converter; |
||||
|
||||
import org.junit.Test; |
||||
|
||||
import java.util.Arrays; |
||||
import java.util.Collections; |
||||
import java.util.HashSet; |
||||
import java.util.Set; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
/** |
||||
* TODO |
||||
* This class is temporary and will be removed after upgrading to Spring Security 5.5.0 GA. |
||||
* These tests will probably be folded into tests for {@link ClaimConversionService}. |
||||
* |
||||
* Tests for {@link ObjectToSetStringConverter2}. |
||||
* |
||||
* @author Daniel Garnier-Moiroux |
||||
*/ |
||||
public class ObjectToSetStringConverter2Test { |
||||
@Test |
||||
@SuppressWarnings("unchecked") |
||||
public void convertFromNullThenReturnNull() { |
||||
ObjectToSetStringConverter2 converter = new ObjectToSetStringConverter2(); |
||||
Set<String> result = (Set<String>) converter.convert(null, null, null); |
||||
assertThat(result).isNull(); |
||||
} |
||||
|
||||
@Test |
||||
@SuppressWarnings("unchecked") |
||||
public void convertFromStringThenReturnSet() { |
||||
ObjectToSetStringConverter2 converter = new ObjectToSetStringConverter2(); |
||||
Set<String> result = (Set<String>) converter.convert("Hello", null, null); |
||||
assertThat(result).containsExactly("Hello"); |
||||
} |
||||
|
||||
@Test |
||||
@SuppressWarnings("unchecked") |
||||
public void convertFromSetThenReturnSet() { |
||||
ObjectToSetStringConverter2 converter = new ObjectToSetStringConverter2(); |
||||
Set<String> result = (Set<String>) converter.convert(new HashSet<>(Arrays.asList("Hello", "world")), null, null); |
||||
assertThat(result).containsExactlyInAnyOrder("Hello", "world"); |
||||
} |
||||
|
||||
@Test |
||||
@SuppressWarnings("unchecked") |
||||
public void convertFromCollectionThenReturnSet() { |
||||
ObjectToSetStringConverter2 converter = new ObjectToSetStringConverter2(); |
||||
Set<String> result = (Set<String>) converter.convert(Arrays.asList("Hello", "world"), null, null); |
||||
assertThat(result).containsExactlyInAnyOrder("Hello", "world"); |
||||
} |
||||
|
||||
@Test |
||||
@SuppressWarnings("unchecked") |
||||
public void convertFromEmptyCollectionThenReturnEmptySet() { |
||||
ObjectToSetStringConverter2 converter = new ObjectToSetStringConverter2(); |
||||
Set<String> result = (Set<String>) converter.convert(Collections.emptyList(), null, null); |
||||
assertThat(result).isEmpty(); |
||||
} |
||||
} |
||||
34
oauth2-authorization-server/src/test/java/org/springframework/security/oauth2/core/http/converter/OidcProviderConfigurationHttpMessageConverterTests.java → oauth2-authorization-server/src/test/java/org/springframework/security/oauth2/core/oidc/http/converter/OidcProviderConfigurationHttpMessageConverterTests.java
34
oauth2-authorization-server/src/test/java/org/springframework/security/oauth2/core/http/converter/OidcProviderConfigurationHttpMessageConverterTests.java → oauth2-authorization-server/src/test/java/org/springframework/security/oauth2/core/oidc/http/converter/OidcProviderConfigurationHttpMessageConverterTests.java
Loading…
Reference in new issue