Skip to content

Commit a181733

Browse files
committed
Encapsulate GenericHttpMessageConverterAdapter
This will allow its removal in gh-18073
1 parent 51e8f8f commit a181733

File tree

11 files changed

+497
-7
lines changed

11 files changed

+497
-7
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright 2004-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.config.web.server;
18+
19+
import java.io.IOException;
20+
import java.lang.reflect.Type;
21+
import java.util.List;
22+
23+
import org.jspecify.annotations.Nullable;
24+
25+
import org.springframework.core.ResolvableType;
26+
import org.springframework.http.HttpInputMessage;
27+
import org.springframework.http.HttpOutputMessage;
28+
import org.springframework.http.MediaType;
29+
import org.springframework.http.converter.GenericHttpMessageConverter;
30+
import org.springframework.http.converter.HttpMessageNotReadableException;
31+
import org.springframework.http.converter.HttpMessageNotWritableException;
32+
import org.springframework.http.converter.SmartHttpMessageConverter;
33+
34+
/**
35+
* {@link GenericHttpMessageConverter} implementation that delegates to a
36+
* {@link SmartHttpMessageConverter}.
37+
*
38+
* @param <T> the converted object type
39+
* @author Sebastien Deleuze
40+
* @since 7.0
41+
*/
42+
final class GenericHttpMessageConverterAdapter<T> implements GenericHttpMessageConverter<T> {
43+
44+
private final SmartHttpMessageConverter<T> smartConverter;
45+
46+
GenericHttpMessageConverterAdapter(SmartHttpMessageConverter<T> smartConverter) {
47+
this.smartConverter = smartConverter;
48+
}
49+
50+
@Override
51+
public boolean canRead(Type type, @Nullable Class<?> contextClass, @Nullable MediaType mediaType) {
52+
return this.smartConverter.canRead(ResolvableType.forType(type), mediaType);
53+
}
54+
55+
@Override
56+
public T read(Type type, @Nullable Class<?> contextClass, HttpInputMessage inputMessage)
57+
throws IOException, HttpMessageNotReadableException {
58+
return this.smartConverter.read(ResolvableType.forType(type), inputMessage, null);
59+
}
60+
61+
@Override
62+
public boolean canWrite(@Nullable Type type, Class<?> clazz, @Nullable MediaType mediaType) {
63+
return this.smartConverter.canWrite(ResolvableType.forType(type), clazz, mediaType);
64+
}
65+
66+
@Override
67+
public void write(T t, @Nullable Type type, @Nullable MediaType contentType, HttpOutputMessage outputMessage)
68+
throws IOException, HttpMessageNotWritableException {
69+
this.smartConverter.write(t, ResolvableType.forType(type), contentType, outputMessage, null);
70+
}
71+
72+
@Override
73+
public boolean canRead(Class<?> clazz, @Nullable MediaType mediaType) {
74+
return this.smartConverter.canRead(ResolvableType.forClass(clazz), mediaType);
75+
}
76+
77+
@Override
78+
public boolean canWrite(Class<?> clazz, @Nullable MediaType mediaType) {
79+
return this.smartConverter.canWrite(clazz, mediaType);
80+
}
81+
82+
@Override
83+
public List<MediaType> getSupportedMediaTypes() {
84+
return this.smartConverter.getSupportedMediaTypes();
85+
}
86+
87+
@Override
88+
public T read(Class<? extends T> clazz, HttpInputMessage inputMessage)
89+
throws IOException, HttpMessageNotReadableException {
90+
return this.smartConverter.read(clazz, inputMessage);
91+
}
92+
93+
@Override
94+
public void write(T t, @Nullable MediaType contentType, HttpOutputMessage outputMessage)
95+
throws IOException, HttpMessageNotWritableException {
96+
this.smartConverter.write(t, contentType, outputMessage);
97+
}
98+
99+
}

config/src/main/java/org/springframework/security/config/web/server/HttpMessageConverters.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.springframework.http.converter.json.JacksonJsonHttpMessageConverter;
2323
import org.springframework.http.converter.json.JsonbHttpMessageConverter;
2424
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
25-
import org.springframework.security.oauth2.core.http.converter.GenericHttpMessageConverterAdapter;
2625
import org.springframework.util.ClassUtils;
2726

2827
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright 2004-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.oauth2.server.authorization.http.converter;
18+
19+
import java.io.IOException;
20+
import java.lang.reflect.Type;
21+
import java.util.List;
22+
23+
import org.jspecify.annotations.Nullable;
24+
25+
import org.springframework.core.ResolvableType;
26+
import org.springframework.http.HttpInputMessage;
27+
import org.springframework.http.HttpOutputMessage;
28+
import org.springframework.http.MediaType;
29+
import org.springframework.http.converter.GenericHttpMessageConverter;
30+
import org.springframework.http.converter.HttpMessageNotReadableException;
31+
import org.springframework.http.converter.HttpMessageNotWritableException;
32+
import org.springframework.http.converter.SmartHttpMessageConverter;
33+
34+
/**
35+
* {@link GenericHttpMessageConverter} implementation that delegates to a
36+
* {@link SmartHttpMessageConverter}.
37+
*
38+
* @param <T> the converted object type
39+
* @author Sebastien Deleuze
40+
* @since 7.0
41+
*/
42+
final class GenericHttpMessageConverterAdapter<T> implements GenericHttpMessageConverter<T> {
43+
44+
private final SmartHttpMessageConverter<T> smartConverter;
45+
46+
GenericHttpMessageConverterAdapter(SmartHttpMessageConverter<T> smartConverter) {
47+
this.smartConverter = smartConverter;
48+
}
49+
50+
@Override
51+
public boolean canRead(Type type, @Nullable Class<?> contextClass, @Nullable MediaType mediaType) {
52+
return this.smartConverter.canRead(ResolvableType.forType(type), mediaType);
53+
}
54+
55+
@Override
56+
public T read(Type type, @Nullable Class<?> contextClass, HttpInputMessage inputMessage)
57+
throws IOException, HttpMessageNotReadableException {
58+
return this.smartConverter.read(ResolvableType.forType(type), inputMessage, null);
59+
}
60+
61+
@Override
62+
public boolean canWrite(@Nullable Type type, Class<?> clazz, @Nullable MediaType mediaType) {
63+
return this.smartConverter.canWrite(ResolvableType.forType(type), clazz, mediaType);
64+
}
65+
66+
@Override
67+
public void write(T t, @Nullable Type type, @Nullable MediaType contentType, HttpOutputMessage outputMessage)
68+
throws IOException, HttpMessageNotWritableException {
69+
this.smartConverter.write(t, ResolvableType.forType(type), contentType, outputMessage, null);
70+
}
71+
72+
@Override
73+
public boolean canRead(Class<?> clazz, @Nullable MediaType mediaType) {
74+
return this.smartConverter.canRead(ResolvableType.forClass(clazz), mediaType);
75+
}
76+
77+
@Override
78+
public boolean canWrite(Class<?> clazz, @Nullable MediaType mediaType) {
79+
return this.smartConverter.canWrite(clazz, mediaType);
80+
}
81+
82+
@Override
83+
public List<MediaType> getSupportedMediaTypes() {
84+
return this.smartConverter.getSupportedMediaTypes();
85+
}
86+
87+
@Override
88+
public T read(Class<? extends T> clazz, HttpInputMessage inputMessage)
89+
throws IOException, HttpMessageNotReadableException {
90+
return this.smartConverter.read(clazz, inputMessage);
91+
}
92+
93+
@Override
94+
public void write(T t, @Nullable MediaType contentType, HttpOutputMessage outputMessage)
95+
throws IOException, HttpMessageNotWritableException {
96+
this.smartConverter.write(t, contentType, outputMessage);
97+
}
98+
99+
}

oauth2/oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/http/converter/HttpMessageConverters.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.springframework.http.converter.json.JacksonJsonHttpMessageConverter;
2323
import org.springframework.http.converter.json.JsonbHttpMessageConverter;
2424
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
25-
import org.springframework.security.web.http.GenericHttpMessageConverterAdapter;
2625
import org.springframework.util.ClassUtils;
2726

2827
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright 2004-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.oauth2.server.authorization.oidc.http.converter;
18+
19+
import java.io.IOException;
20+
import java.lang.reflect.Type;
21+
import java.util.List;
22+
23+
import org.jspecify.annotations.Nullable;
24+
25+
import org.springframework.core.ResolvableType;
26+
import org.springframework.http.HttpInputMessage;
27+
import org.springframework.http.HttpOutputMessage;
28+
import org.springframework.http.MediaType;
29+
import org.springframework.http.converter.GenericHttpMessageConverter;
30+
import org.springframework.http.converter.HttpMessageNotReadableException;
31+
import org.springframework.http.converter.HttpMessageNotWritableException;
32+
import org.springframework.http.converter.SmartHttpMessageConverter;
33+
34+
/**
35+
* {@link GenericHttpMessageConverter} implementation that delegates to a
36+
* {@link SmartHttpMessageConverter}.
37+
*
38+
* @param <T> the converted object type
39+
* @author Sebastien Deleuze
40+
* @since 7.0
41+
*/
42+
final class GenericHttpMessageConverterAdapter<T> implements GenericHttpMessageConverter<T> {
43+
44+
private final SmartHttpMessageConverter<T> smartConverter;
45+
46+
GenericHttpMessageConverterAdapter(SmartHttpMessageConverter<T> smartConverter) {
47+
this.smartConverter = smartConverter;
48+
}
49+
50+
@Override
51+
public boolean canRead(Type type, @Nullable Class<?> contextClass, @Nullable MediaType mediaType) {
52+
return this.smartConverter.canRead(ResolvableType.forType(type), mediaType);
53+
}
54+
55+
@Override
56+
public T read(Type type, @Nullable Class<?> contextClass, HttpInputMessage inputMessage)
57+
throws IOException, HttpMessageNotReadableException {
58+
return this.smartConverter.read(ResolvableType.forType(type), inputMessage, null);
59+
}
60+
61+
@Override
62+
public boolean canWrite(@Nullable Type type, Class<?> clazz, @Nullable MediaType mediaType) {
63+
return this.smartConverter.canWrite(ResolvableType.forType(type), clazz, mediaType);
64+
}
65+
66+
@Override
67+
public void write(T t, @Nullable Type type, @Nullable MediaType contentType, HttpOutputMessage outputMessage)
68+
throws IOException, HttpMessageNotWritableException {
69+
this.smartConverter.write(t, ResolvableType.forType(type), contentType, outputMessage, null);
70+
}
71+
72+
@Override
73+
public boolean canRead(Class<?> clazz, @Nullable MediaType mediaType) {
74+
return this.smartConverter.canRead(ResolvableType.forClass(clazz), mediaType);
75+
}
76+
77+
@Override
78+
public boolean canWrite(Class<?> clazz, @Nullable MediaType mediaType) {
79+
return this.smartConverter.canWrite(clazz, mediaType);
80+
}
81+
82+
@Override
83+
public List<MediaType> getSupportedMediaTypes() {
84+
return this.smartConverter.getSupportedMediaTypes();
85+
}
86+
87+
@Override
88+
public T read(Class<? extends T> clazz, HttpInputMessage inputMessage)
89+
throws IOException, HttpMessageNotReadableException {
90+
return this.smartConverter.read(clazz, inputMessage);
91+
}
92+
93+
@Override
94+
public void write(T t, @Nullable MediaType contentType, HttpOutputMessage outputMessage)
95+
throws IOException, HttpMessageNotWritableException {
96+
this.smartConverter.write(t, contentType, outputMessage);
97+
}
98+
99+
}

oauth2/oauth2-authorization-server/src/main/java/org/springframework/security/oauth2/server/authorization/oidc/http/converter/HttpMessageConverters.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.springframework.http.converter.json.JacksonJsonHttpMessageConverter;
2323
import org.springframework.http.converter.json.JsonbHttpMessageConverter;
2424
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
25-
import org.springframework.security.web.http.GenericHttpMessageConverterAdapter;
2625
import org.springframework.util.ClassUtils;
2726

2827
/**

0 commit comments

Comments
 (0)