/* * Copyright @ 2013 CurisTEC, S.A.S. All Rights Reserved. */ package net.curisit.securis.ioc; import java.io.File; import java.net.URI; import java.text.MessageFormat; import java.util.Arrays; import java.util.List; import java.util.Properties; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Named; import jakarta.ws.rs.core.UriBuilder; import jakarta.ws.rs.core.UriBuilderException; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import com.google.inject.AbstractModule; import com.google.inject.Provides; /** * SecurisModule *
* Guice module that provides application-level infrastructural dependencies * (base URI, app directories, DB files list, support email/hash, etc.). *
* Configuration: * - Reads server port from /securis-server.properties (key: "port"). * - Defaults to port 9997 when not present or on read errors. * - Constructs base URI as http://0.0.0.0:{port}/ with UriBuilder. * - Creates working directories under ${user.home}/.SeCuris on demand. * * Security note: * - getPassword/getFilePassword are simple helpers; secrets should be * managed via a secure vault/env vars in production. * * @author JRA * Last reviewed by JRA on Oct 7, 2025. */ public class SecurisModule extends AbstractModule { private static final int DEFAULT_PORT = 9997; private static final String PROPERTIES_FILE_NAME = "/securis-server.properties"; private static final Logger LOG = LogManager.getLogger(SecurisModule.class); /** configure
Currently no explicit bindings; providers below supply instances. */ @Override protected void configure() { } /** * getPassword
* Composite password (example use with encrypted H2 URL). * * @return concatenated password string */ public String getPassword() { return getFilePassword() + " " + "53curi5"; } /** * getFilePassword
* Standalone file password (for H2 CIPHER). * * @return file password string */ public String getFilePassword() { return "cur151T"; } /** * getUrl
* H2 JDBC URL with AES cipher pointing to {appDir}/db/securis. * * @param appDir application working directory * @return JDBC URL (H2) */ public String getUrl(File appDir) { return String.format("jdbc:h2:%s/db/securis;CIPHER=AES", appDir.getAbsolutePath()); } /** * getBaseURI
* Provide the base URI for the HTTP server using configured or default port. * * @return base URI (http://0.0.0.0:{port}/) */ @Named("base-uri") @Provides @ApplicationScoped public URI getBaseURI() { try { String url = MessageFormat.format("http://{0}/", "0.0.0.0"); LOG.debug("Server url{}", url); return UriBuilder.fromUri(url).port(getPort()).build(); } catch (IllegalArgumentException | UriBuilderException e) { return UriBuilder.fromUri("http://localhost/").port(getPort()).build(); } } /** * getPort
* Read port from properties file or return default. * * @return HTTP port */ private int getPort() { Integer port; Properties prop = new Properties(); try { prop.load(getClass().getResourceAsStream(PROPERTIES_FILE_NAME)); port = Integer.valueOf(prop.getProperty("port")); return (port == null ? DEFAULT_PORT : port); } catch (Exception ex) { return DEFAULT_PORT; } } /** * getAppDbFiles
* List of SQL files to initialize the application DB.
*
* @return list of classpath resource paths
*/
protected List
* Provide a temp directory inside the app working dir (.TEMP).
* Creates it if missing and marks for deletion on exit.
*
* @return temp directory or null if creation failed
*/
@Named("temporary-dir")
@Provides
@ApplicationScoped
public File getTemporaryDir() {
String tmp = getAppDir().getAbsolutePath() + File.separator + ".TEMP";
File ftmp = new File(tmp);
if (!ftmp.exists()) {
if (!ftmp.mkdirs()) {
return null;
}
LOG.debug("Created temporary directory for app in: {}", ftmp.getAbsolutePath());
ftmp.deleteOnExit();
}
return ftmp;
}
/**
* getAppDir
* Provide the app working directory under ${user.home}/.SeCuris (creates if missing).
*
* @return working directory or null if creation failed
*/
@Named("app-dir")
@Provides
@ApplicationScoped
public File getAppDir() {
String appDir = System.getProperty("user.home", System.getProperty("user.dir"));
if (appDir == null) appDir = ".";
appDir += File.separator + ".SeCuris";
File fAppDir = new File(appDir);
if (!fAppDir.exists()) {
if (!fAppDir.mkdirs()) {
return null;
}
LOG.debug("Created app working directory app in: {}", fAppDir.getAbsolutePath());
}
return fAppDir;
}
/**
* getSupportEmail
* Provide support email address.
*
* @return email
*/
@Named("support-email")
@Provides
@ApplicationScoped
public String getSupportEmail() {
return "support@curisit.net";
}
/**
* getHashLogo
* Provide a static content hash for the logo (cache-busting or integrity).
*
* @return hex SHA-256
*/
@Named("hash-logo")
@Provides
@ApplicationScoped
public String getHashLogo() {
return "1b42616809d4cd8ccf109e3c30d0ab25067f160b30b7354a08ddd563de0096ba";
}
/**
* getDbFiles
* Provide DB initialization files list (delegates to {@link #getAppDbFiles()}).
*
* @return list of SQL resource paths
*/
@Named("db-files")
@Provides
@ApplicationScoped
public List