rsanchez
2014-11-14 5beef4e86aa5355342e4e128752f40c51a493765
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
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 javax.inject.Named;
import javax.inject.Singleton;
import javax.sql.DataSource;
import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.core.UriBuilderException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.h2.jdbcx.JdbcDataSource;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
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);
    @Override
    protected void configure() {
    }
    public String getPassword() {
        return getFilePassword() + " " + "53curi5";
    }
    public String getFilePassword() {
        return "cur151T";
    }
    public String getUrl(File appDir) {
        return String.format("jdbc:h2:%s/db/securis;CIPHER=AES", appDir.getAbsolutePath());
    }
    @Named("base-uri")
    @Provides
    @Singleton
    public URI getBaseURI() {
        // TODO Read from configuration, where?
        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();
        }
    }
    private int getPort() {
        Integer port;
        Properties prop = new Properties();
        try {
            prop.load(getClass().getResourceAsStream(PROPERTIES_FILE_NAME));
            port = Integer.valueOf(prop.getProperty("port"));
            if (port == null) {
                return DEFAULT_PORT;
            } else {
                return port;
            }
        } catch (Exception ex) {
            return DEFAULT_PORT;
        }
    }
    protected List<String> getAppDbFiles() {
        return Arrays.asList("/db/schema.sql");
    }
    @Named("temporary-dir")
    @Provides
    @Singleton
    public File getTemporaryDir() {
        String tmp = getAppDir().getAbsolutePath();
        tmp += 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;
    }
    @Named("app-dir")
    @Provides
    @Singleton
    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;
    }
    @Named("support-email")
    @Provides
    @Singleton
    public String getSupportEmail() {
        return "integrity@curistec.com";
    }
    @Named("hash-logo")
    @Provides
    @Singleton
    public String getHashLogo() {
        return "1b42616809d4cd8ccf109e3c30d0ab25067f160b30b7354a08ddd563de0096ba";
    }
    @Provides
    @Singleton
    public DataSource getDataSource(@Named("app-dir") File appDir) {
        JdbcDataSource dataSource = new JdbcDataSource();
        dataSource.setURL(getUrl(appDir));
        dataSource.setUser("curis");
        dataSource.setPassword(getPassword());
        LOG.debug("JdbcDataSource: {}", dataSource);
        return dataSource;
    }
    @Named("db-files")
    @Provides
    @Singleton
    public List<String> getDbFiles() {
        return getAppDbFiles();
    }
}