I have a java web application with embedded tomcat version 6.0.37 and I am using http as well as https. I have a configuration page where I have options:
- SSL enable
- SSL redirect
- SSL Port
- HTTP Port
- Restart
SSL is enabled by default. I want to redirect every http request to https. So, on configuration page, I check SSL redirect option, checks Restart option and submits my form. On server side, I am doing following:
if( webServerProperties.getSslRedirect() ) {
boolean constraintExists = false;
for( SecurityConstraint constraint : uiContext.findConstraints() ) {
if( constraint.getDisplayName().equals(SSL_REDIRECT_CONSTRAINT_NAME) ) {
constraintExists = true;
break;
}
}
if( !constraintExists ) {
SecurityConstraint constraint = new SecurityConstraint();
constraint.setDisplayName(SSL_REDIRECT_CONSTRAINT_NAME);
constraint.setAuthConstraint(false);
SecurityCollection collection = new SecurityCollection();
collection.addPattern("/*");
constraint.setUserConstraint("CONFIDENTIAL");
constraint.addCollection(collection);
uiContext.addConstraint(constraint);
}
}
else {
for( SecurityConstraint constraint : uiContext.findConstraints() ) {
if( constraint.getDisplayName().equals(SSL_REDIRECT_CONSTRAINT_NAME) ) {
uiContext.removeConstraint(constraint);
}
}
}
uiContainer.removeConnector(uiHTTPConnector);
uiHTTPConnector.pause();
uiHTTPConnector.destroy();
uiHTTPConnector = null;
uiHTTPConnector = createHTTPConnector(
newPort,
webServerProperties.getSslRedirect() ? webServerProperties.getSslPort() : -1,
newMaxThreads
);
uiContainer.addConnector(uiHTTPConnector);
uiHTTPConnector.start();
protected Connector createHTTPConnector(int port, int sslRedirectPort, int maxThreads) throws Exception {
Connector connector = new LifecycleEventConnector();
org.apache.tomcat.util.IntrospectionUtils.setProperty(connector, "port", "" + port);
connector.setAttribute("maxThreads", maxThreads);
connector.setAttribute("keepAliveTimeout", MAX_IDLE_TIME);
connector.setAttribute("connectionTimeout", MAX_IDLE_TIME);
connector.setMaxParameterCount(-1);
if( sslRedirectPort > 0 ) {
org.apache.tomcat.util.IntrospectionUtils.setProperty(connector, "redirectPort", "" + sslRedirectPort);
}
return connector;
}
private static final class LifecycleEventConnector extends Connector {
public LifecycleEventConnector() throws Exception {
super();
}
@Override
public void initialize() throws LifecycleException {
lifecycle.fireLifecycleEvent(INIT_EVENT, null);
super.initialize();
}
}
I want to just restart the connector instead of restarting the whole container and letting my whole application down. Changing other options(HTTP port, SSL port, SSL enabled), connector is restarted and works fine, but enabling SSL redirect isn't working. SSL redirect works after I restart my application. I have searched a lot, but I am unable to resolve this issue. Will appreciate any help.