diff --git a/app/Global.java b/app/Global.java index 9b346e0b5..de8ad5e01 100644 --- a/app/Global.java +++ b/app/Global.java @@ -14,17 +14,22 @@ * the License. */ +import com.google.common.base.Strings; +import com.google.common.net.HostAndPort; import com.linkedin.drelephant.DrElephant; +import com.linkedin.drelephant.ElephantContext; import com.sun.security.sasl.util.AbstractSaslImpl; - -import play.Application; -import play.GlobalSettings; -import play.Logger; - import java.io.IOException; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.util.logging.Level; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import play.Application; +import play.GlobalSettings; +import play.Logger; +import play.api.mvc.Handler; +import play.mvc.Http; /** @@ -34,11 +39,24 @@ public class Global extends GlobalSettings { DrElephant _drElephant; + private final String INVALID_PORT = "-1"; + private final String HTTPS_PROTOCOL = "https://"; + private final String HTTP_PORT_CONFIG_NAME = "http_port"; + private final String HTTPS_PORT_CONFIG_NAME = "https_port"; + + private String HTTP_PORT; + private String HTTPS_PORT; + public void onStart(Application app) { Logger.info("Starting Application..."); fixJavaKerberos(); + HTTP_PORT = ElephantContext.instance().getElephnatConf() + .getProperty(HTTP_PORT_CONFIG_NAME); + HTTPS_PORT = ElephantContext.instance().getElephnatConf() + .getProperty(HTTPS_PORT_CONFIG_NAME); + try { _drElephant = new DrElephant(); _drElephant.start(); @@ -80,6 +98,29 @@ private static void fixJavaKerberos() { } + /** + * This method is overridden to re-direct all the requests to HTTP endpoint of Dr.Elephant to + * the respective HTTPS enabled endpoint(If such endpoints exists) + * @param requestHeader Request Header + * @return The Handler to appropriately handle the given request + */ + @Override + public Handler onRouteRequest(Http.RequestHeader requestHeader) { + if (!Strings.isNullOrEmpty(HTTP_PORT) && !Strings.isNullOrEmpty(HTTPS_PORT)) { + String port = INVALID_PORT; + if (!Strings.isNullOrEmpty(requestHeader.host())) { + HostAndPort hostAndPort = HostAndPort.fromString(requestHeader.host()) + .withDefaultPort(Integer.parseInt(HTTP_PORT)); + port = String.valueOf(hostAndPort.getPort()); + } + if (!port.equals(INVALID_PORT) && port.equals(HTTP_PORT)) { + return controllers.Default.redirect(HTTPS_PROTOCOL + requestHeader.host().replace(port, HTTPS_PORT) + + requestHeader.uri()); + } + } + return super.onRouteRequest(requestHeader); + } + static void setFinalStatic(Field field, Object newValue) throws Exception { field.setAccessible(true); Field modifiersField = Field.class.getDeclaredField("modifiers");