diff --git a/src/wa/server/plugin/apache/ApacheConfig.java b/src/wa/server/plugin/apache/ApacheConfig.java deleted file mode 100644 index 6e6d4f0..0000000 --- a/src/wa/server/plugin/apache/ApacheConfig.java +++ /dev/null @@ -1,93 +0,0 @@ -package wa.server.plugin.apache; - -import java.io.File; -import java.io.IOException; -import java.io.PrintStream; -import java.sql.SQLException; -import java.util.LinkedList; -import java.util.List; - -import wa.server.WAConstants; -import wa.server.plugin.WAServiceConfig; -import wa.server.util.ConfigFileUtil; -import zutil.db.DBConnection; -import zutil.io.file.FileUtil; - -public class ApacheConfig{ - private static final String APACHE_CONF_FILE = "wa_apache_vhost.conf"; - private static final String APACHE_MAIN_CONFIG_FILE = "/etc/apache2/apache2.conf"; - private static final String STATIC_PRE_CONF = "wa/server/plugin/apache/apache_default.config"; - - // Configuration data - List vhosts; - - - - - public void save() throws IOException { - File file = WAConstants.getConfigFile(APACHE_CONF_FILE); - // Update main configuration file - ConfigFileUtil.writeBetweenBoundary( - new File(APACHE_MAIN_CONFIG_FILE), - "#", - "Include "+file.getAbsolutePath()); - - // Write Vhost configuration - PrintStream out = new PrintStream(file); - - out.println(FileUtil.getContent(new File(STATIC_PRE_CONF))); - out.println("######################################"); - out.println("# vhost.php"); - for(ApacheConfigVirtualHost.VirtualHostData vhost : vhosts){ - if(vhost.isSSL()) - writeSSLVhost(out, vhost); - else - writeVhost(out, vhost); - } - - out.close(); - } - - private void writeVhost(PrintStream out, ApacheConfigVirtualHost.VirtualHostData conf) throws IOException{ - out.println(""); - out.println(" ServerName "+conf.getDomain()+":80"); - out.println(" DocumentRoot "+conf.getPath()); - out.println(""); - out.println(""); - } - - private void writeSSLVhost(PrintStream out, ApacheConfigVirtualHost.VirtualHostData conf) throws IOException{ - out.println(""); - out.println(" ServerName "+conf.getDomain()+":80"); - out.println(" RewriteEngine On"); - out.println(" RewriteCond %{SERVER_PORT} !^443$"); - out.println(" RewriteRule ^(.*)$ https://server$1 [L,R]"); - out.println(""); - out.println(""); - out.println(" ServerName "+conf.getDomain()+":443"); - out.println(" DocumentRoot "+conf.getPath()); - 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(""); - out.println(""); - } - - private void writeTomcatVhost(PrintStream out, ApacheConfigVirtualHost.VirtualHostData conf) throws IOException{ - out.println(""); - out.println(" ServerName "+conf.getDomain()+":80"); - out.println(" "); - out.println(" RewriteEngine On"); - out.println(" RewriteRule ^/$ /"+conf.getPath()+" [R]"); - out.println(" ProxyPreserveHost on"); - out.println(" "); - out.println(" Order deny,allow"); - out.println(" Allow from all"); - out.println(" "); - out.println(" ProxyPass / ajp://localhost:8009/"); - out.println(" ProxyPassReverse / http://localhost:8080/"); - out.println(""); - out.println(""); - } -} diff --git a/src/wa/server/plugin/apache/ApacheConfigVirtualHost.java b/src/wa/server/plugin/apache/ApacheConfigVirtualHost.java index b824bd9..2553b3d 100644 --- a/src/wa/server/plugin/apache/ApacheConfigVirtualHost.java +++ b/src/wa/server/plugin/apache/ApacheConfigVirtualHost.java @@ -2,18 +2,23 @@ package wa.server.plugin.apache; import wa.server.WAConstants; import wa.server.plugin.WAServiceConfig; +import wa.server.util.ConfigFileUtil; import zutil.db.DBConnection; import zutil.db.bean.DBBean; import zutil.db.bean.DBBean.DBTable; +import zutil.io.IOUtil; +import java.io.*; import java.util.ArrayList; import java.util.List; public class ApacheConfigVirtualHost implements WAServiceConfig{ - public static final String CONFIG_NAME = "Apache Virtual Host"; + private static final String CONFIG_NAME = "Apache Virtual Host"; + private static final String APACHE_MAIN_CONFIG_FILE = "apache2/apache2.conf"; + private static final String STATIC_PRE_CONF = "wa/server/plugin/apache/apache_default.config"; - private List vhosts = new ArrayList<>(); + private List vhosts = new ArrayList<>(); @Override @@ -24,23 +29,37 @@ public class ApacheConfigVirtualHost implements WAServiceConfig{ @Override public void read() throws Exception { DBConnection db = WAConstants.getDB(); - vhosts = DBBean.load(db, VirtualHostData.class); + vhosts = DBBean.load(db, ApacheVirtualHostData.class); } @Override public void save() throws Exception { - + DBConnection db = WAConstants.getDB(); + for(ApacheVirtualHostData vhost : vhosts){ + vhost.save(db); + } } @Override public void configure() throws Exception { + // Update main configuration file + ConfigFileUtil.writeBetweenBoundary( + WAConstants.getConfigFile(APACHE_MAIN_CONFIG_FILE), + "#", + IOUtil.getContentString( + new FileInputStream(STATIC_PRE_CONF))); + // Write Vhost configuration + for(ApacheVirtualHostData vhost : vhosts){ + vhost.configure(); + } } @DBTable(WAConstants.DB_TABLE_PREFIX + "_apache_vhost") - public static class VirtualHostData extends DBBean{ + public static class ApacheVirtualHostData extends DBBean{ protected String domain; + protected String domain_old; protected String path; protected boolean ssl; @@ -64,5 +83,44 @@ public class ApacheConfigVirtualHost implements WAServiceConfig{ 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("apache2/enabled/"+ domain_old).delete(); + } + PrintStream out = new PrintStream( + WAConstants.getConfigFile("apache2/enabled/"+ domain)); + + writeConfiguration(out); + out.close(); + } + public void writeConfiguration(PrintStream out){ + if(ssl){ + out.println(""); + 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(""); + out.println(""); + 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(""); + out.println(""); + } + else { + out.println(""); + out.println(" ServerName " + domain + ":80"); + out.println(" DocumentRoot " + path); + out.println(""); + out.println(""); + } + } } } diff --git a/src/wa/server/plugin/tomcat/TomcatConfigApplication.java b/src/wa/server/plugin/tomcat/TomcatConfigApplication.java new file mode 100644 index 0000000..58264f7 --- /dev/null +++ b/src/wa/server/plugin/tomcat/TomcatConfigApplication.java @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2015 Ziver + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package wa.server.plugin.tomcat; + +import java.io.PrintStream; + +import wa.server.plugin.apache.ApacheConfigVirtualHost.ApacheVirtualHostData; + + +public class TomcatConfigApplication { + + + + public static class TomcatApplicationData extends ApacheVirtualHostData { + @Override + public void writeConfiguration(PrintStream out) { + out.println(""); + out.println(" ServerName " + domain + ":80"); + out.println(" "); + out.println(" RewriteEngine On"); + out.println(" RewriteRule ^/$ /" + path + " [R]"); + out.println(" ProxyPreserveHost on"); + out.println(" "); + out.println(" Order deny,allow"); + out.println(" Allow from all"); + out.println(" "); + out.println(" ProxyPass / ajp://localhost:8009/"); + out.println(" ProxyPassReverse / http://localhost:8080/"); + out.println(""); + out.println(""); + } + } +} diff --git a/src/wa/server/plugin/tomcat/plugin.json b/src/wa/server/plugin/tomcat/plugin.json new file mode 100644 index 0000000..7cb64cf --- /dev/null +++ b/src/wa/server/plugin/tomcat/plugin.json @@ -0,0 +1,8 @@ +{ + "version": "1.0", + "name": "Apache Tomcat", + "interfaces": { + "wa.server.plugin.WAService": "wa.server.plugin.apache.TomcatService", + "wa.server.plugin.WAServiceStatus": "wa.server.plugin.tomcat.TomcatStatus" + } +} \ No newline at end of file