Refactoring of config objs
This commit is contained in:
parent
4c4d424ff7
commit
af1d6546eb
8 changed files with 256 additions and 330 deletions
|
|
@ -1,141 +1,97 @@
|
|||
package wa.server.plugin.apache;
|
||||
|
||||
import wa.server.WAConstants;
|
||||
import wa.server.WAContext;
|
||||
import wa.server.page.WAConfigPage;
|
||||
import wa.server.plugin.WAConfigEntry;
|
||||
import wa.server.util.ConfigFileUtil;
|
||||
import zutil.db.bean.DBBean;
|
||||
import wa.server.plugin.WAConfiguration;
|
||||
import zutil.db.bean.DBBean.DBTable;
|
||||
import zutil.io.IOUtil;
|
||||
import zutil.ui.Configurator;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintStream;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
public class ApacheConfigVirtualHost extends WAConfigPage {
|
||||
@DBTable(WAConstants.DB_TABLE_PREFIX + "_apache_vhost")
|
||||
public class ApacheConfigVirtualHost extends WAConfiguration {
|
||||
private static final String CONFIG_NAME = "vhost";
|
||||
private static final String NAVIGATION_NAME = "Apache Virtual Host";
|
||||
|
||||
private static final String APACHE_MAIN_CONFIG_FILE = "/apache2/apache2.conf";
|
||||
private static final String APACHE_SITE_AVAILABLE_PATH = "/apache2/site-available/";
|
||||
private static final String STATIC_PRE_CONF = "wa/server/plugin/apache/apache_default.config";
|
||||
|
||||
private List<ApacheVirtualHostEntry> vhosts = new ArrayList<>();
|
||||
private static final String SITE_AVAILABLE_DIR = "/apache2/site-available/";
|
||||
private static final String SITE_ENABLED_DIR = "/apache2/site-enabled/";
|
||||
|
||||
|
||||
public ApacheConfigVirtualHost() throws SQLException {
|
||||
super(ApacheService.NAVIGATION_NAME, CONFIG_NAME, NAVIGATION_NAME);
|
||||
vhosts = DBBean.load(WAContext.getDB(), ApacheVirtualHostEntry.class);
|
||||
@Configurator.Configurable("Domain")
|
||||
protected String domain;
|
||||
protected transient String domain_old;
|
||||
@Configurator.Configurable("DocRoot")
|
||||
protected String path;
|
||||
@Configurator.Configurable("SSL")
|
||||
protected boolean ssl;
|
||||
|
||||
|
||||
|
||||
public String getDomain() {
|
||||
return domain;
|
||||
}
|
||||
public void setDomain(String domain) {
|
||||
this.domain = domain;
|
||||
}
|
||||
public String getPath() {
|
||||
return path;
|
||||
}
|
||||
public void setPath(String path) {
|
||||
this.path = path;
|
||||
}
|
||||
public boolean isSSL() {
|
||||
return ssl;
|
||||
}
|
||||
public void setSSL(boolean ssl) {
|
||||
this.ssl = ssl;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public WAConfigEntry createConfig(){
|
||||
return new ApacheVirtualHostEntry();
|
||||
}
|
||||
public void deleteConfig(int id){
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void configure() throws Exception {
|
||||
// Update main configuration file
|
||||
ConfigFileUtil.writeBetweenBoundary(
|
||||
WAConstants.getConfigFile(APACHE_MAIN_CONFIG_FILE),
|
||||
"#",
|
||||
IOUtil.readContentAsString(new FileInputStream(STATIC_PRE_CONF), true));
|
||||
// Write Vhost configuration
|
||||
for(ApacheVirtualHostEntry vhost : vhosts){
|
||||
vhost.configure();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<? extends WAConfigEntry> getConfigData() {
|
||||
return vhosts;
|
||||
public void saveConfiguration() throws IOException {
|
||||
if(domain != domain_old){ // Configuration file has changed name
|
||||
WAConstants.getConfigFile(SITE_AVAILABLE_DIR+ domain_old).delete();
|
||||
domain_old = domain;
|
||||
}
|
||||
PrintStream out = new PrintStream(
|
||||
WAConstants.getConfigFile(SITE_AVAILABLE_DIR+ domain));
|
||||
|
||||
saveConfiguration(out);
|
||||
out.close();
|
||||
}
|
||||
|
||||
private void saveConfiguration(PrintStream out){
|
||||
if(ssl){
|
||||
out.println("<VirtualHost *:80>");
|
||||
out.println(" ServerName "+ domain +":80");
|
||||
out.println(" RewriteEngine On");
|
||||
out.println(" RewriteCond %{SERVER_PORT} !^443$");
|
||||
out.println(" RewriteRule ^(.*)$ https://server$1 [L,R]");
|
||||
out.println("</VirtualHost>");
|
||||
out.println("<VirtualHost *:443>");
|
||||
out.println(" ServerName "+ domain +":443");
|
||||
out.println(" DocumentRoot "+ path);
|
||||
out.println();
|
||||
out.println(" SSLEngine on");
|
||||
out.println(" SSLCertificateFile "+ WAConstants.getConfigFile(WAConstants.WA_SSL_CERT));
|
||||
out.println(" SSLCertificateKeyFile "+ WAConstants.getConfigFile(WAConstants.WA_SSL_KEY));
|
||||
out.println("</VirtualHost>");
|
||||
out.println("");
|
||||
}
|
||||
else {
|
||||
out.println("<VirtualHost *:80>");
|
||||
out.println(" ServerName "+ domain +":80");
|
||||
out.println(" DocumentRoot "+ path);
|
||||
out.println("</VirtualHost>");
|
||||
out.println("");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<? extends WAConfigEntry> getConfigClass() {
|
||||
return ApacheVirtualHostEntry.class;
|
||||
}
|
||||
public void deleteConfiguration() throws Exception {
|
||||
|
||||
|
||||
@DBTable(WAConstants.DB_TABLE_PREFIX + "_apache_vhost")
|
||||
public static class ApacheVirtualHostEntry extends WAConfigEntry {
|
||||
@Configurator.Configurable("Domain")
|
||||
protected String domain;
|
||||
protected transient String domain_old;
|
||||
@Configurator.Configurable("DocRoot")
|
||||
protected String path;
|
||||
@Configurator.Configurable("SSL")
|
||||
protected boolean ssl;
|
||||
|
||||
|
||||
|
||||
public String getDomain() {
|
||||
return domain;
|
||||
}
|
||||
public void setDomain(String domain) {
|
||||
this.domain = domain;
|
||||
}
|
||||
public String getPath() {
|
||||
return path;
|
||||
}
|
||||
public void setPath(String path) {
|
||||
this.path = path;
|
||||
}
|
||||
public boolean isSSL() {
|
||||
return ssl;
|
||||
}
|
||||
public void setSSL(boolean ssl) {
|
||||
this.ssl = ssl;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public final void configure() throws IOException {
|
||||
if(domain != domain_old){ // Configuration file has changed name
|
||||
WAConstants.getConfigFile(APACHE_SITE_AVAILABLE_PATH+ domain_old).delete();
|
||||
domain_old = domain;
|
||||
}
|
||||
PrintStream out = new PrintStream(
|
||||
WAConstants.getConfigFile(APACHE_SITE_AVAILABLE_PATH+ domain));
|
||||
|
||||
writeConfiguration(out);
|
||||
out.close();
|
||||
}
|
||||
protected void writeConfiguration(PrintStream out){
|
||||
if(ssl){
|
||||
out.println("<VirtualHost *:80>");
|
||||
out.println(" ServerName "+ domain +":80");
|
||||
out.println(" RewriteEngine On");
|
||||
out.println(" RewriteCond %{SERVER_PORT} !^443$");
|
||||
out.println(" RewriteRule ^(.*)$ https://server$1 [L,R]");
|
||||
out.println("</VirtualHost>");
|
||||
out.println("<VirtualHost *:443>");
|
||||
out.println(" ServerName "+ domain +":443");
|
||||
out.println(" DocumentRoot "+ path);
|
||||
out.println();
|
||||
out.println(" SSLEngine on");
|
||||
out.println(" SSLCertificateFile "+ WAConstants.getConfigFile(WAConstants.WA_SSL_CERT));
|
||||
out.println(" SSLCertificateKeyFile "+ WAConstants.getConfigFile(WAConstants.WA_SSL_KEY));
|
||||
out.println("</VirtualHost>");
|
||||
out.println("");
|
||||
}
|
||||
else {
|
||||
out.println("<VirtualHost *:80>");
|
||||
out.println(" ServerName "+ domain +":80");
|
||||
out.println(" DocumentRoot "+ path);
|
||||
out.println("</VirtualHost>");
|
||||
out.println("");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue