Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding the provision of redirecting HTTP requests to HTTPS #675

Open
wants to merge 2 commits into
base: tuning_20190221
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 46 additions & 5 deletions app/Global.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;


/**
Expand All @@ -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();
Expand Down Expand Up @@ -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");
Expand Down