/**
* OverEncrypt project hosted by Università degli Studi di Bergamo
* -> for PrimeLife project {@link http://www.primelife.eu/}
*/
package unibg.overencrypt.server;
import java.io.File;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import unibg.overencrypt.core.Configuration;
import unibg.overencrypt.core.DBConnection;
import unibg.overencrypt.server.managers.SessionManager;
import com.bradmcevoy.http.AuthenticationHandler;
import com.bradmcevoy.http.AuthenticationService;
import com.bradmcevoy.http.ResourceFactory;
import com.bradmcevoy.http.ResourceFactoryFactory;
import com.bradmcevoy.http.http11.auth.BasicAuthHandler;
import com.bradmcevoy.http.webdav.DefaultWebDavResponseHandler;
import com.bradmcevoy.http.webdav.WebDavResponseHandler;
/**
* Initializes the connection server.
*
* @author Flavio Giovarruscio & Riccardo Tribbia
* @version 1.0
*/
public class Initializer implements ResourceFactoryFactory {
/** Logger for this class. */
private Logger log = LoggerFactory.getLogger(Initializer.class);
//TODO private static SessionFactory sessionFactory;
/** The authentication service. */
private static AuthenticationService authenticationService;
/** The resource factory. */
private static ResourcesManager resourceFactory;
/** The database connection. */
private static DBConnection dbConnection;
@SuppressWarnings("unused")
private static SessionManager sessionManager;
/*
* @see com.bradmcevoy.http.ResourceFactoryFactory#createResourceFactory()
*/
/* (non-Javadoc)
* @see com.bradmcevoy.http.ResourceFactoryFactory#createResourceFactory()
*/
@Override
public ResourceFactory createResourceFactory() {
return resourceFactory;
}
/*
* @see com.bradmcevoy.http.ResourceFactoryFactory#createResponseHandler()
*/
/* (non-Javadoc)
* @see com.bradmcevoy.http.ResourceFactoryFactory#createResponseHandler()
*/
@Override
public WebDavResponseHandler createResponseHandler() {
return new DefaultWebDavResponseHandler(authenticationService);
}
/* (non-Javadoc)
* @see com.bradmcevoy.http.ResourceFactoryFactory#init()
*/
@Override
public void init() {
log.debug("OverEncryption Server initialization");
try{
XMLConfiguration config = new XMLConfiguration("OverEncrypt.cfg.xml");
//Initialized session manager
sessionManager = new SessionManager();
//Server WebDAV configuration
if(config.getString("webdav-server.context") != null) ServerConfiguration.setCONTEXT(config.getString("webdav-server.context"));
if(config.getString("webdav-server.realm") != null) ServerConfiguration.setREALM(config.getString("webdav-server.realm"));
if(config.getString("webdav-server.filesystem-root") != null) ServerConfiguration.setWebDAVrootPath(config.getString("webdav-server.filesystem-root").replace("~", System.getProperty("user.home")));
if(config.getString("webdav-server.temp-folder") != null) ServerConfiguration.setTempFilePath(config.getString("webdav-server.temp-folder"));
if(config.getString("webdav-server.executables-folder") != null) ServerConfiguration.setEXECUTABLES_PATH(config.getString("webdav-server.executables-folder").replace("~", System.getProperty("user.home")));
if(config.getString("webdav-server.dynamicSEL") != null) ServerConfiguration.setDynamicSEL(config.getString("webdav-server.dynamicSEL"));
//OverEncription module configuration
if(config.getString("database.name") != null) Configuration.setDBNAME(config.getString("database.name"));
if(config.getString("database.user") != null) Configuration.setUSER(config.getString("database.user"));
if(config.getString("database.password") != null) Configuration.setPASSWORD(config.getString("database.password"));
if(config.getString("database.driver") != null) Configuration.setDRIVER(config.getString("database.driver"));
if(config.getString("database.url") != null) Configuration.setURL(config.getString("database.url"));
log.debug("REALM CONFIGURED: " + ServerConfiguration.getREALM());
log.debug("ROOT CONFIGURED: " + ServerConfiguration.getWebDAVrootPath());
log.debug("SERVER TEMP CONFIGURED: " + ServerConfiguration.getTempFilePath());
log.debug("SERVER EXECUTABLES CONFIGURED: " + ServerConfiguration.getEXECUTABLES_PATH());
log.debug("SERVER DYNAMIC SEL: " + ServerConfiguration.isDynamicSEL());
log.debug("DB NAME CONFIGURED: " + Configuration.getDBNAME());
log.debug("DB USER CONFIGURED: " + Configuration.getUSER());
log.debug("DB PASSWORD CONFIGURED: " + Configuration.getPASSWORD());
log.debug("DB DRIVER CONFIGURED: " + Configuration.getDRIVER());
log.debug("DB URL CONFIGURED: " + Configuration.getURL());
log.debug("ROOT CONFIGURED: " + ServerConfiguration.getWebDAVrootPath());
log.debug("SESSION MANAGER INITIALIZED");
}catch (ConfigurationException e) {
log.warn("Configuration Exception thrown during initial configuration - no xml config file?");
log.warn("Possible solution is put your configuration xml file in your root folder");
}
log.debug("WebDAV Server initialization");
if( authenticationService == null ) {
dbConnection = new DBConnection(); //PostgreSQL connection
ArrayList<AuthenticationHandler> authHandls = new ArrayList<AuthenticationHandler>();
authHandls.add(new BasicAuthHandler());
authenticationService = new AuthenticationService(authHandls);
//TODO sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
resourceFactory = new ResourcesManager();
dataInitializing();
}
}
/**
* Database connection initializing.
*/
private void dataInitializing(){
//Get connection
Connection connection = dbConnection.getConnection();
if(connection != null){
log.debug("<<<<Database connection correctly>>>>");
try {
DatabaseMetaData dbMetaData = connection.getMetaData();
log.debug(dbMetaData.getDatabaseProductName() + "-" + dbMetaData.getDatabaseProductVersion());
log.debug(dbMetaData.getDriverName() + "-" + dbMetaData.getDriverVersion());
log.debug(dbMetaData.getURL() + "-" + dbMetaData.getUserName());
} catch (SQLException e) {
log.warn(e.getMessage());
}
}else{
log.error("Database connection failed. Try search for wrong configurazion in OverEncription.config");
}
File rootWebDAVFolder = new File(ServerConfiguration.getWebDAVrootPath());
if(!rootWebDAVFolder.exists() || !rootWebDAVFolder.isDirectory())
rootWebDAVFolder.mkdir();
File tempWebDAVFolder = new File(ServerConfiguration.getTempFilePath());
if(!tempWebDAVFolder.exists() || !tempWebDAVFolder.isDirectory())
tempWebDAVFolder.mkdir();
}
}