-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0c83db9
commit e7abd8f
Showing
10 changed files
with
460 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
mangooio-core/src/main/java/io/mangoo/routing/handlers/CorsHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package io.mangoo.routing.handlers; | ||
|
||
import java.util.Objects; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import com.google.inject.Inject; | ||
|
||
import io.mangoo.core.Application; | ||
import io.mangoo.core.Config; | ||
import io.mangoo.enums.Required; | ||
import io.undertow.server.HttpHandler; | ||
import io.undertow.server.HttpServerExchange; | ||
import io.undertow.util.HeaderValues; | ||
import io.undertow.util.HttpString; | ||
|
||
/** | ||
* | ||
* @author svenkubiak | ||
* | ||
*/ | ||
public class CorsHandler implements HttpHandler { | ||
private static final String ACCESS_CONTROL_ALLOW_ORIGIN = "Access-Control-Allow-Origin"; | ||
private static final String ACCESS_CONTROL_ALLOW_CREDENTIALS = "Access-Control-Allow-Credentials"; | ||
private static final String ACCESS_CONTROL_ALLOW_HEADERS = "Access-Control-Allow-Headers"; | ||
private static final String ACCESS_CONTROL_ALLOW_METHODS = "Access-Control-Allow-Methods"; | ||
private static final String ACCESS_CONTROL_EXPOSE_HEADERS = "Access-Control-Expose-Headers"; | ||
private static final String ACCESS_CONTROL_MAX_AGE = "Access-Control-Max-Age"; | ||
private Config config; | ||
|
||
@Inject | ||
public CorsHandler(Config config) { | ||
this.config = Objects.requireNonNull(config, Required.CONFIG.toString()); | ||
} | ||
|
||
@Override | ||
public void handleRequest(HttpServerExchange exchange) throws Exception { | ||
if (this.config.isCorsEnable() && config.getCorsUrlPattern().matcher(exchange.getRequestURL()).matches()) { | ||
applyHeader(exchange); | ||
} | ||
nextHandler(exchange); | ||
} | ||
|
||
private void applyHeader(HttpServerExchange exchange) { | ||
String origin = getOrigin(exchange); | ||
if (StringUtils.isNotBlank(origin) && this.config.getCorsAllowOrigin().matcher(origin).matches()) { | ||
if (doesNotHaveHeader(ACCESS_CONTROL_ALLOW_ORIGIN, exchange)) { | ||
System.out.println("aDDING HEADER"); | ||
addHeader(exchange, ACCESS_CONTROL_ALLOW_ORIGIN, origin); | ||
} | ||
|
||
if (doesNotHaveHeader(ACCESS_CONTROL_ALLOW_HEADERS, exchange)) { | ||
addHeader(exchange, ACCESS_CONTROL_ALLOW_HEADERS, this.config.getCorsHeadersAllowHeaders()); | ||
} | ||
|
||
if (doesNotHaveHeader(ACCESS_CONTROL_ALLOW_CREDENTIALS, exchange)) { | ||
addHeader(exchange, ACCESS_CONTROL_ALLOW_CREDENTIALS, this.config.getCorsHeadersAllowCredentials()); | ||
} | ||
|
||
if (doesNotHaveHeader(ACCESS_CONTROL_ALLOW_METHODS, exchange)) { | ||
addHeader(exchange, ACCESS_CONTROL_ALLOW_METHODS, this.config.getCorsHeadersAllowMethods()); | ||
} | ||
|
||
if (doesNotHaveHeader(ACCESS_CONTROL_EXPOSE_HEADERS, exchange)) { | ||
addHeader(exchange, ACCESS_CONTROL_EXPOSE_HEADERS, this.config.getCorsHeadersExposeHeaders()); | ||
} | ||
|
||
if (doesNotHaveHeader(ACCESS_CONTROL_MAX_AGE, exchange)) { | ||
addHeader(exchange, ACCESS_CONTROL_MAX_AGE, this.config.getCorsHeadersMaxAge()); | ||
} | ||
} | ||
} | ||
|
||
private String getOrigin(HttpServerExchange exchange) { | ||
HeaderValues headers = ((HttpServerExchange) exchange).getRequestHeaders().get("Origin"); | ||
return headers == null ? null : headers.peekFirst(); | ||
} | ||
|
||
private boolean doesNotHaveHeader(String name, HttpServerExchange exchange) { | ||
return exchange.getResponseHeaders().get(name) == null; | ||
} | ||
|
||
private void addHeader(HttpServerExchange exchange, String name, String value) { | ||
exchange.getResponseHeaders().add(HttpString.tryFromString(name), value); | ||
} | ||
|
||
/** | ||
* Handles the next request in the handler chain | ||
* | ||
* @param exchange The HttpServerExchange | ||
* @throws Exception Thrown when an exception occurs | ||
*/ | ||
protected void nextHandler(HttpServerExchange exchange) throws Exception { | ||
Application.getInstance(ResponseHandler.class).handleRequest(exchange); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.