Skip to content

Commit

Permalink
Add option --redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
xnbox committed Sep 11, 2021
1 parent 55b6ee8 commit 99483e8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 39 deletions.
67 changes: 34 additions & 33 deletions src/org/tommy/common/utils/CommonUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -293,48 +293,49 @@ public static void prepareTomcatConf(Path confPath, Path keystorePath, Integer p
DocumentBuilder builder = builderFactory.newDocumentBuilder();
serverXmlDocument = builder.parse(is);

if (port != null) {
Node autoDeployNode = (Node) XPathFactory.newInstance().newXPath().compile("/Server/Service/Engine/Host/@autoDeploy").evaluate(serverXmlDocument, XPathConstants.NODE);
autoDeployNode.setTextContent(Boolean.toString(false));

Node connectorNode = (Node) XPathFactory.newInstance().newXPath().compile("/Server/Service/Connector").evaluate(serverXmlDocument, XPathConstants.NODE);
if (port == null)
connectorNode.getParentNode().removeChild(connectorNode);
else {
Node portNode = (Node) XPathFactory.newInstance().newXPath().compile("/Server/Service/Connector/@port").evaluate(serverXmlDocument, XPathConstants.NODE);
portNode.setTextContent(Integer.toString(port)); // update node with real TCP port number
}

if (sslPort != null) {
/* Add TLS(SSL) support */
if (port != null && sslPort != null) {
Node redirectPortNode = (Node) XPathFactory.newInstance().newXPath().compile("/Server/Service/Connector/@redirectPort").evaluate(serverXmlDocument, XPathConstants.NODE);
redirectPortNode.setTextContent(Integer.toString(sslPort)); // update node with real TCP port number
redirectPortNode.setTextContent(Integer.toString(sslPort)); // update node with real SSL TCP port number
}

Node autoDeployNode = (Node) XPathFactory.newInstance().newXPath().compile("/Server/Service/Engine/Host/@autoDeploy").evaluate(serverXmlDocument, XPathConstants.NODE);
autoDeployNode.setTextContent(Boolean.toString(false));

/* Add TLS(SSL) support */

Node serviceNode = (Node) XPathFactory.newInstance().newXPath().compile("/Server/Service").evaluate(serverXmlDocument, XPathConstants.NODE);
Element tlsConnectorNode = serverXmlDocument.createElement("Connector");

if (sslPort == null)
sslPort = 8443;

tlsConnectorNode.setAttribute("port", Integer.toString(sslPort));
tlsConnectorNode.setAttribute("protocol", "org.apache.coyote.http11.Http11NioProtocol");
tlsConnectorNode.setAttribute("SSLEnabled", "true");
//tlsConnectorNode.setAttribute("maxThreads", "150");
serviceNode.appendChild(tlsConnectorNode);

Element upgradeProtocolEl = serverXmlDocument.createElement("UpgradeProtocol");
upgradeProtocolEl.setAttribute("className", "org.apache.coyote.http2.Http2Protocol");
tlsConnectorNode.appendChild(upgradeProtocolEl);

Element sslHostConfigEl = serverXmlDocument.createElement("SSLHostConfig");
tlsConnectorNode.appendChild(sslHostConfigEl);

Element certificateEl = serverXmlDocument.createElement("Certificate");
certificateEl.setAttribute("certificateKeystoreFile", "conf/keystore/localhost-rsa.jks");
certificateEl.setAttribute("certificateKeystorePassword", "changeit");
certificateEl.setAttribute("type", "RSA");
sslHostConfigEl.appendChild(certificateEl);
if (sslPort != null) {
Node serviceNode = (Node) XPathFactory.newInstance().newXPath().compile("/Server/Service").evaluate(serverXmlDocument, XPathConstants.NODE);
Element tlsConnectorNode = serverXmlDocument.createElement("Connector");

tlsConnectorNode.setAttribute("port", Integer.toString(sslPort));
tlsConnectorNode.setAttribute("protocol", "org.apache.coyote.http11.Http11NioProtocol");
tlsConnectorNode.setAttribute("SSLEnabled", "true");
serviceNode.appendChild(tlsConnectorNode);

Element upgradeProtocolEl = serverXmlDocument.createElement("UpgradeProtocol");
upgradeProtocolEl.setAttribute("className", "org.apache.coyote.http2.Http2Protocol");
tlsConnectorNode.appendChild(upgradeProtocolEl);

Element sslHostConfigEl = serverXmlDocument.createElement("SSLHostConfig");
tlsConnectorNode.appendChild(sslHostConfigEl);

Element certificateEl = serverXmlDocument.createElement("Certificate");
certificateEl.setAttribute("certificateKeystoreFile", "conf/keystore/localhost-rsa.jks");
certificateEl.setAttribute("certificateKeystorePassword", "changeit");
certificateEl.setAttribute("type", "RSA");
sslHostConfigEl.appendChild(certificateEl);
}
}
}

/* update web.xml document */
Document webXmlDocument = null;
try (InputStream is = cl.getResourceAsStream("META-INF/tomcat/conf/web.xml")) {
if (is != null) {
Expand Down Expand Up @@ -372,7 +373,7 @@ public static void prepareTomcatConf(Path confPath, Path keystorePath, Integer p

copyConfDocumentXml(confPath, "server.xml", serverXmlDocument);
copyConfDocumentXml(confPath, "web.xml", webXmlDocument);
//copyConfResource(confPath, "web.xml");

copyConfResource(confPath, "tomcat-users.xsd");
copyConfResource(confPath, "tomcat-users.xml");
copyConfResource(confPath, "logging.properties");
Expand Down
21 changes: 15 additions & 6 deletions src/org/tommy/main/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ The manifest file can have any name, but is commonly named manifest.json and ser
*/
public class Main {

private static final int DEFAULT_PORT_8080 = 8080;
private static final int DEFAULT_SSL_PORT_8433 = 8433;

private static Class clazz = Main.class;
private static ClassLoader cl = clazz.getClassLoader();
private static Logger logger = LoggerUtils.createLogger(clazz);
Expand All @@ -67,7 +70,7 @@ public class Main {
private static final String ARGS_HELP_OPTION = "--help";
private static final String ARGS_PORT_OPTION = "--port";
private static final String ARGS_PORT_SSL_OPTION = "--port-ssl";
private static final String ARGS_NO_REDIRECT_OPTION = "--no-redirect";
private static final String ARGS_REDIRECT_OPTION = "--redirect";
private static final String ARGS_CONTEXT_PATH_OPTION = "--context-path";

public static void main(String[] args) throws Throwable {
Expand All @@ -85,7 +88,7 @@ public static void main(String[] args) throws Throwable {
Integer sslPort = null;
String contextPath = "/";
boolean help = false;
boolean noRedirect = false;
boolean redirect = false;

for (int i = 1; i < args.length; i++) {
if (args[i].equals(ARGS_APP_OPTION)) {
Expand Down Expand Up @@ -117,12 +120,18 @@ public static void main(String[] args) throws Throwable {
} catch (Throwable e) {
// ignore exception
}
} else if (args[i].equals(ARGS_NO_REDIRECT_OPTION))
noRedirect = true;
} else if (args[i].equals(ARGS_REDIRECT_OPTION))
redirect = true;
else if (args[i].equals(ARGS_HELP_OPTION))
help = true;
}

if (sslPort == null) {
if (port == null)
port = DEFAULT_PORT_8080;
if (redirect)
sslPort = DEFAULT_SSL_PORT_8433;
}
/**
* Custom command line args
*/
Expand Down Expand Up @@ -150,7 +159,7 @@ else if (args[i].equals(ARGS_HELP_OPTION))
sb.append(" --app <file|dir|URL> run app from ZIP or WAR archive, directory or URL\n");
sb.append(" --port <number> HTTP TCP port number, default: 8080 \n");
sb.append(" --port-ssl <number> HTTPS TCP port number, default: 8443 \n");
sb.append(" --no-redirect disable redirect HTTP to HTTPS \n");
sb.append(" --redirect redirect HTTP to HTTPS \n");
sb.append(" --context-path <string> context path, default: / \n");
sb.append(" --password <string> provide password for encrypted ZIP or WAR archive\n");
System.out.println(sb);
Expand Down Expand Up @@ -213,7 +222,7 @@ else if (args[i].equals(ARGS_HELP_OPTION))
*/
contextPath = CommonUtils.getContextPath(contextPath);

CommonUtils.prepareTomcatConf(confPath, keystorePath, port, sslPort, !noRedirect);
CommonUtils.prepareTomcatConf(confPath, keystorePath, port, sslPort, redirect);

Tomcat tomcat = CommonUtils.prepareTomcat(logger, catalinaHome, app, argz);
org.apache.catalina.Context ctx = tomcat.addWebapp(contextPath, warPath.toString());
Expand Down

0 comments on commit 99483e8

Please sign in to comment.