11/*
2- * Copyright 2002-2015 the original author or authors.
2+ * Copyright 2002-2016 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
66 * You may obtain a copy of the License at
77 *
8- * http://www.apache.org/licenses/LICENSE-2.0
8+ * http://www.apache.org/licenses/LICENSE-2.0
99 *
1010 * Unless required by applicable law or agreed to in writing, software
1111 * distributed under the License is distributed on an "AS IS" BASIS,
2121import java .util .List ;
2222
2323import org .springframework .http .HttpMethod ;
24+ import org .springframework .util .CollectionUtils ;
2425import org .springframework .util .ObjectUtils ;
2526import org .springframework .util .StringUtils ;
2627
@@ -86,10 +87,10 @@ public CorsConfiguration combine(CorsConfiguration other) {
8687 return this ;
8788 }
8889 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 ()));
9394 Boolean allowCredentials = other .getAllowCredentials ();
9495 if (allowCredentials != null ) {
9596 config .setAllowCredentials (allowCredentials );
@@ -137,7 +138,7 @@ public List<String> getAllowedOrigins() {
137138 */
138139 public void addAllowedOrigin (String origin ) {
139140 if (this .allowedOrigins == null ) {
140- this .allowedOrigins = new ArrayList <String >();
141+ this .allowedOrigins = new ArrayList <String >(4 );
141142 }
142143 this .allowedOrigins .add (origin );
143144 }
@@ -179,7 +180,7 @@ public void addAllowedMethod(HttpMethod method) {
179180 public void addAllowedMethod (String method ) {
180181 if (StringUtils .hasText (method )) {
181182 if (this .allowedMethods == null ) {
182- this .allowedMethods = new ArrayList <String >();
183+ this .allowedMethods = new ArrayList <String >(4 );
183184 }
184185 this .allowedMethods .add (method );
185186 }
@@ -213,7 +214,7 @@ public List<String> getAllowedHeaders() {
213214 */
214215 public void addAllowedHeader (String allowedHeader ) {
215216 if (this .allowedHeaders == null ) {
216- this .allowedHeaders = new ArrayList <String >();
217+ this .allowedHeaders = new ArrayList <String >(4 );
217218 }
218219 this .allowedHeaders .add (allowedHeader );
219220 }
@@ -230,7 +231,7 @@ public void setExposedHeaders(List<String> exposedHeaders) {
230231 if (exposedHeaders != null && exposedHeaders .contains (ALL )) {
231232 throw new IllegalArgumentException ("'*' is not a valid exposed header value" );
232233 }
233- this .exposedHeaders = (exposedHeaders == null ? null : new ArrayList <String >(exposedHeaders ));
234+ this .exposedHeaders = (exposedHeaders != null ? new ArrayList <String >(exposedHeaders ) : null );
234235 }
235236
236237 /**
@@ -251,7 +252,7 @@ public void addExposedHeader(String exposedHeader) {
251252 throw new IllegalArgumentException ("'*' is not a valid exposed header value" );
252253 }
253254 if (this .exposedHeaders == null ) {
254- this .exposedHeaders = new ArrayList <String >();
255+ this .exposedHeaders = new ArrayList <String >(4 );
255256 }
256257 this .exposedHeaders .add (exposedHeader );
257258 }
@@ -333,14 +334,18 @@ public List<HttpMethod> checkHttpMethod(HttpMethod requestMethod) {
333334 if (requestMethod == null ) {
334335 return null ;
335336 }
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+ }
340343 }
341- if (allowedMethods .isEmpty ()) {
344+ else {
345+ allowedMethods = new ArrayList <String >(1 );
342346 allowedMethods .add (HttpMethod .GET .name ());
343347 }
348+
344349 List <HttpMethod > result = new ArrayList <HttpMethod >(allowedMethods .size ());
345350 boolean allowed = false ;
346351 for (String method : allowedMethods ) {
0 commit comments