1
1
/*
2
- * Copyright 2002-2015 the original author or authors.
2
+ * Copyright 2002-2016 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
6
6
* You may obtain a copy of the License at
7
7
*
8
- * http://www.apache.org/licenses/LICENSE-2.0
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
9
*
10
10
* Unless required by applicable law or agreed to in writing, software
11
11
* distributed under the License is distributed on an "AS IS" BASIS,
21
21
import java .util .List ;
22
22
23
23
import org .springframework .http .HttpMethod ;
24
+ import org .springframework .util .CollectionUtils ;
24
25
import org .springframework .util .ObjectUtils ;
25
26
import org .springframework .util .StringUtils ;
26
27
@@ -86,10 +87,10 @@ public CorsConfiguration combine(CorsConfiguration other) {
86
87
return this ;
87
88
}
88
89
CorsConfiguration config = new CorsConfiguration (this );
89
- config .setAllowedOrigins (combine (this . getAllowedOrigins (), other .getAllowedOrigins ()));
90
- config .setAllowedMethods (combine (this . getAllowedMethods (), other .getAllowedMethods ()));
91
- config .setAllowedHeaders (combine (this . getAllowedHeaders (), other .getAllowedHeaders ()));
92
- config .setExposedHeaders (combine (this . getExposedHeaders (), other .getExposedHeaders ()));
90
+ config .setAllowedOrigins (combine (getAllowedOrigins (), other .getAllowedOrigins ()));
91
+ config .setAllowedMethods (combine (getAllowedMethods (), other .getAllowedMethods ()));
92
+ config .setAllowedHeaders (combine (getAllowedHeaders (), other .getAllowedHeaders ()));
93
+ config .setExposedHeaders (combine (getExposedHeaders (), other .getExposedHeaders ()));
93
94
Boolean allowCredentials = other .getAllowCredentials ();
94
95
if (allowCredentials != null ) {
95
96
config .setAllowCredentials (allowCredentials );
@@ -137,7 +138,7 @@ public List<String> getAllowedOrigins() {
137
138
*/
138
139
public void addAllowedOrigin (String origin ) {
139
140
if (this .allowedOrigins == null ) {
140
- this .allowedOrigins = new ArrayList <String >();
141
+ this .allowedOrigins = new ArrayList <String >(4 );
141
142
}
142
143
this .allowedOrigins .add (origin );
143
144
}
@@ -179,7 +180,7 @@ public void addAllowedMethod(HttpMethod method) {
179
180
public void addAllowedMethod (String method ) {
180
181
if (StringUtils .hasText (method )) {
181
182
if (this .allowedMethods == null ) {
182
- this .allowedMethods = new ArrayList <String >();
183
+ this .allowedMethods = new ArrayList <String >(4 );
183
184
}
184
185
this .allowedMethods .add (method );
185
186
}
@@ -213,7 +214,7 @@ public List<String> getAllowedHeaders() {
213
214
*/
214
215
public void addAllowedHeader (String allowedHeader ) {
215
216
if (this .allowedHeaders == null ) {
216
- this .allowedHeaders = new ArrayList <String >();
217
+ this .allowedHeaders = new ArrayList <String >(4 );
217
218
}
218
219
this .allowedHeaders .add (allowedHeader );
219
220
}
@@ -230,7 +231,7 @@ public void setExposedHeaders(List<String> exposedHeaders) {
230
231
if (exposedHeaders != null && exposedHeaders .contains (ALL )) {
231
232
throw new IllegalArgumentException ("'*' is not a valid exposed header value" );
232
233
}
233
- this .exposedHeaders = (exposedHeaders == null ? null : new ArrayList <String >(exposedHeaders ));
234
+ this .exposedHeaders = (exposedHeaders != null ? new ArrayList <String >(exposedHeaders ) : null );
234
235
}
235
236
236
237
/**
@@ -251,7 +252,7 @@ public void addExposedHeader(String exposedHeader) {
251
252
throw new IllegalArgumentException ("'*' is not a valid exposed header value" );
252
253
}
253
254
if (this .exposedHeaders == null ) {
254
- this .exposedHeaders = new ArrayList <String >();
255
+ this .exposedHeaders = new ArrayList <String >(4 );
255
256
}
256
257
this .exposedHeaders .add (exposedHeader );
257
258
}
@@ -333,14 +334,18 @@ public List<HttpMethod> checkHttpMethod(HttpMethod requestMethod) {
333
334
if (requestMethod == null ) {
334
335
return null ;
335
336
}
336
- List <String > allowedMethods =
337
- (this .allowedMethods != null ? this .allowedMethods : new ArrayList <String >());
338
- if (allowedMethods .contains (ALL )) {
339
- return Collections .singletonList (requestMethod );
337
+
338
+ List <String > allowedMethods = this .allowedMethods ;
339
+ if (!CollectionUtils .isEmpty (allowedMethods )) {
340
+ if (allowedMethods .contains (ALL )) {
341
+ return Collections .singletonList (requestMethod );
342
+ }
340
343
}
341
- if (allowedMethods .isEmpty ()) {
344
+ else {
345
+ allowedMethods = new ArrayList <String >(1 );
342
346
allowedMethods .add (HttpMethod .GET .name ());
343
347
}
348
+
344
349
List <HttpMethod > result = new ArrayList <HttpMethod >(allowedMethods .size ());
345
350
boolean allowed = false ;
346
351
for (String method : allowedMethods ) {
0 commit comments