Joaquín Reñé
2025-10-07 146a0fb8b0e90f9196e569152f649baf60d6cc8f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
* 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
* <p>
* Guice module that provides application-level infrastructural dependencies
* (base URI, app directories, DB files list, support email/hash, etc.).
* <p>
* 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<p>Currently no explicit bindings; providers below supply instances. */
    @Override
    protected void configure() { }
    /**
    * getPassword<p>
    * Composite password (example use with encrypted H2 URL).
    *
    * @return concatenated password string
    */
    public String getPassword() {
        return getFilePassword() + " " + "53curi5";
    }
    /**
    * getFilePassword<p>
    * Standalone file password (for H2 CIPHER).
    *
    * @return file password string
    */
    public String getFilePassword() {
        return "cur151T";
    }
    /**
    * getUrl<p>
    * 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<p>
    * 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<p>
    * 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<p>
    * List of SQL files to initialize the application DB.
    *
    * @return list of classpath resource paths
    */
    protected List<String> getAppDbFiles() {
        return Arrays.asList("/db/schema.sql");
    }
    /**
    * getTemporaryDir<p>
    * 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<p>
    * 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<p>
    * Provide support email address.
    *
    * @return email
    */
    @Named("support-email")
    @Provides
    @ApplicationScoped
    public String getSupportEmail() {
        return "support@curisit.net";
    }
    /**
    * getHashLogo<p>
    * 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<p>
    * Provide DB initialization files list (delegates to {@link #getAppDbFiles()}).
    *
    * @return list of SQL resource paths
    */
    @Named("db-files")
    @Provides
    @ApplicationScoped
    public List<String> getDbFiles() {
        return getAppDbFiles();
    }
}