17
17
18
18
import java .lang .reflect .AnnotatedElement ;
19
19
import java .lang .reflect .Method ;
20
+ import java .util .BitSet ;
20
21
import java .util .List ;
21
22
import java .util .Map ;
22
23
import java .util .TimeZone ;
55
56
import org .springframework .web .servlet .mvc .method .annotation .RequestMappingHandlerMapping ;
56
57
57
58
import com .auth0 .jwt .interfaces .JWTVerifier ;
58
- import com .b2international .commons .exceptions .NotImplementedException ;
59
59
import com .b2international .commons .options .Metadata ;
60
60
import com .b2international .commons .options .MetadataHolder ;
61
61
import com .b2international .commons .options .MetadataHolderMixin ;
79
79
import com .fasterxml .jackson .datatype .jsr310 .JavaTimeModule ;
80
80
import com .google .common .base .Charsets ;
81
81
import com .google .common .base .Strings ;
82
+ import com .google .common .cache .CacheBuilder ;
83
+ import com .google .common .cache .CacheLoader ;
84
+ import com .google .common .cache .LoadingCache ;
82
85
import com .google .common .collect .ImmutableMap ;
83
- import com .google .common .collect .MapMaker ;
84
86
import com .google .inject .Provider ;
85
87
86
88
import io .micrometer .core .instrument .MeterRegistry ;
98
100
public class SnowOwlApiConfig extends WebMvcConfigurationSupport {
99
101
100
102
private static final String INCLUDE_NULL = "includeNull" ;
103
+ private static final int INCLUDE_NULL_IDX = 0 ;
101
104
private static final String PRETTY = "pretty" ;
105
+ private static final int PRETTY_IDX = 1 ;
102
106
103
107
static {
104
108
SpringDocUtils .getConfig ().addResponseWrapperToIgnore (Promise .class );
@@ -107,14 +111,22 @@ public class SnowOwlApiConfig extends WebMvcConfigurationSupport {
107
111
@ Autowired
108
112
private org .springframework .context .ApplicationContext ctx ;
109
113
110
- private Map <String , ObjectMapper > objectMappers ;
114
+ private final LoadingCache <BitSet , ObjectMapper > objectMappers = CacheBuilder .newBuilder ().build (new CacheLoader <BitSet , ObjectMapper >() {
115
+ @ Override
116
+ public ObjectMapper load (BitSet configuration ) throws Exception {
117
+ ObjectMapper mapper = createObjectMapper ();
118
+ mapper .setSerializationInclusion (configuration .get (INCLUDE_NULL_IDX ) ? Include .ALWAYS : Include .NON_NULL );
119
+ mapper .configure (SerializationFeature .INDENT_OUTPUT , configuration .get (PRETTY_IDX ));
120
+ return mapper ;
121
+ }
122
+ });
111
123
112
124
@ Bean
113
125
public OpenAPI openAPI () {
114
126
return new OpenAPI ();
115
127
}
116
128
117
- public ObjectMapper createObjectMapper () {
129
+ public static ObjectMapper createObjectMapper () {
118
130
final ObjectMapper objectMapper = new ObjectMapper ();
119
131
objectMapper .registerModule (new JavaTimeModule ());
120
132
objectMapper .setSerializationInclusion (Include .NON_NULL );
@@ -130,32 +142,21 @@ public ObjectMapper createObjectMapper() {
130
142
@ Bean
131
143
@ Scope (scopeName = WebApplicationContext .SCOPE_REQUEST , proxyMode = ScopedProxyMode .TARGET_CLASS )
132
144
public ObjectMapper objectMapper (@ Autowired HttpServletRequest request ) {
133
- if (objectMappers == null ) {
134
- objectMappers = new MapMaker ().makeMap ();
135
- objectMappers .put ("includeNonNullObjectMapper" , createObjectMapper ());
136
- objectMappers .put ("includeNonNullObjectMapperPretty" , createObjectMapper ()
137
- .enable (SerializationFeature .INDENT_OUTPUT ));
138
- objectMappers .put ("includeAllObjectMapper" , createObjectMapper ()
139
- .setSerializationInclusion (Include .ALWAYS ));
140
- objectMappers .put ("includeAllObjectMapperPretty" , createObjectMapper ()
141
- .setSerializationInclusion (Include .ALWAYS )
142
- .enable (SerializationFeature .INDENT_OUTPUT ));
143
- }
145
+ return objectMappers .getUnchecked (toConfig (
146
+ extractBooleanQueryParameterValue (request , INCLUDE_NULL ),
147
+ extractBooleanQueryParameterValue (request , PRETTY )
148
+ ));
149
+ }
144
150
145
- boolean includeNulls = extractBooleanQueryParameterValue (request , INCLUDE_NULL );
146
- boolean pretty = extractBooleanQueryParameterValue (request , PRETTY );
147
-
148
- if (includeNulls && pretty ) {
149
- return objectMappers .get ("includeAllObjectMapperPretty" );
150
- } else if (includeNulls && !pretty ) {
151
- return objectMappers .get ("includeAllObjectMapper" );
152
- } else if (!includeNulls && pretty ) {
153
- return objectMappers .get ("includeNonNullObjectMapperPretty" );
154
- } else if (!includeNulls && !pretty ) {
155
- return objectMappers .get ("includeNonNullObjectMapper" );
156
- } else {
157
- throw new NotImplementedException ("Unexpected case that should not happen" );
151
+ private BitSet toConfig (boolean ...serializationFeatureValuesInOrder ) {
152
+ if (serializationFeatureValuesInOrder == null ) {
153
+ return new BitSet (0 );
154
+ }
155
+ BitSet config = new BitSet (serializationFeatureValuesInOrder .length );
156
+ for (int i = 0 ; i < serializationFeatureValuesInOrder .length ; i ++) {
157
+ config .set (i , serializationFeatureValuesInOrder [i ]);
158
158
}
159
+ return config ;
159
160
}
160
161
161
162
private boolean extractBooleanQueryParameterValue (HttpServletRequest request , String queryParameterKey ) {
0 commit comments