77
88import com .typesafe .config .Config ;
99
10+ import javax .annotation .Nonnull ;
1011import java .time .Duration ;
1112import java .util .Arrays ;
1213import java .util .Collections ;
@@ -55,7 +56,7 @@ private static class Matcher<T> implements Predicate<T> {
5556
5657 private boolean wild ;
5758
58- public Matcher (final List <String > values , final Predicate <T > predicate ) {
59+ Matcher (final List <String > values , final Predicate <T > predicate ) {
5960 this .values = values ;
6061 this .predicate = predicate ;
6162 this .wild = values .contains ("*" );
@@ -71,6 +72,9 @@ public boolean test(final T value) {
7172 }
7273 }
7374
75+ /** Default max-age in minutes. */
76+ private static final int _30 = 30 ;
77+
7478 private Matcher <String > origin ;
7579
7680 private boolean credentials ;
@@ -100,7 +104,7 @@ public Cors() {
100104 setUseCredentials (true );
101105 setMethods ("GET" , "POST" );
102106 setHeaders ("X-Requested-With" , "Content-Type" , "Accept" , "Origin" );
103- setMaxAge (Duration .ofMinutes (30 ));
107+ setMaxAge (Duration .ofMinutes (_30 ));
104108 }
105109
106110 /**
@@ -112,6 +116,12 @@ public boolean getUseCredentials() {
112116 return this .credentials ;
113117 }
114118
119+ /**
120+ * If true, set the <code>Access-Control-Allow-Credentials</code> header.
121+ *
122+ * @param credentials Credentials.
123+ * @return This cors.
124+ */
115125 public Cors setUseCredentials (boolean credentials ) {
116126 this .credentials = credentials ;
117127 return this ;
@@ -310,7 +320,23 @@ public Cors setMaxAge(final Duration preflightMaxAge) {
310320 return this ;
311321 }
312322
313- public static Cors from (Config conf ) {
323+ /**
324+ * Get cors options from application configuration file.
325+ *
326+ * <pre>{@code
327+ * cors {
328+ * origin: *
329+ * methods: [GET, POST]
330+ * headers: [Custom-Header]
331+ * maxAge: 30m
332+ * exposesHeaders: [Header]
333+ * }
334+ * }</pre>
335+ *
336+ * @param conf Configuration.
337+ * @return Cors options.
338+ */
339+ public static @ Nonnull Cors from (@ Nonnull Config conf ) {
314340 Config cors = conf .hasPath ("cors" ) ? conf .getConfig ("cors" ) : conf ;
315341 Cors options = new Cors ();
316342 if (cors .hasPath ("origin" )) {
0 commit comments